diff --git a/.gitignore b/.gitignore
index c4c0f864..e26f807d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -31,3 +31,5 @@
/config/master.key
/config/database.yml
+
+/vendor/bundle
diff --git a/.rspec b/.rspec
new file mode 100644
index 00000000..c99d2e73
--- /dev/null
+++ b/.rspec
@@ -0,0 +1 @@
+--require spec_helper
diff --git a/Gemfile b/Gemfile
index 46cae9ab..b94daf68 100644
--- a/Gemfile
+++ b/Gemfile
@@ -70,6 +70,7 @@ group :development do
# Speed up commands on slow machines / big apps [https://github.com/rails/spring]
# gem "spring"
+ gem 'rspec-rails', '~> 4.1.0'
end
group :test do
@@ -77,4 +78,9 @@ group :test do
gem 'capybara'
gem 'selenium-webdriver'
gem 'webdrivers'
+ gem 'rspec-rails', '~> 4.1.0'
+ gem 'shoulda-matchers', '~> 5.0'
+ gem 'shoulda-callback-matchers', '~> 1.1.1'
+ gem 'rails-controller-testing'
+ gem 'factory_bot_rails'
end
diff --git a/Gemfile.lock b/Gemfile.lock
index 9e32670c..ea62ba02 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -116,8 +116,14 @@ GEM
railties (>= 4.1.0)
responders
warden (~> 1.2.3)
+ diff-lcs (1.5.0)
erubi (1.12.0)
execjs (2.8.1)
+ factory_bot (6.2.1)
+ activesupport (>= 5.0.0)
+ factory_bot_rails (6.2.0)
+ factory_bot (~> 6.2.0)
+ railties (>= 5.0.0)
ffi (1.15.5-x64-mingw-ucrt)
globalid (1.1.0)
activesupport (>= 5.0)
@@ -203,6 +209,10 @@ GEM
activesupport (= 7.0.4.3)
bundler (>= 1.15.0)
railties (= 7.0.4.3)
+ rails-controller-testing (1.0.5)
+ actionpack (>= 5.0.1.rc1)
+ actionview (>= 5.0.1.rc1)
+ activesupport (>= 5.0.1.rc1)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
@@ -223,6 +233,23 @@ GEM
actionpack (>= 5.2)
railties (>= 5.2)
rexml (3.2.5)
+ rspec-core (3.12.2)
+ rspec-support (~> 3.12.0)
+ rspec-expectations (3.12.3)
+ diff-lcs (>= 1.2.0, < 2.0)
+ rspec-support (~> 3.12.0)
+ rspec-mocks (3.12.5)
+ diff-lcs (>= 1.2.0, < 2.0)
+ rspec-support (~> 3.12.0)
+ rspec-rails (4.1.2)
+ actionpack (>= 4.2)
+ activesupport (>= 4.2)
+ railties (>= 4.2)
+ rspec-core (~> 3.10)
+ rspec-expectations (~> 3.10)
+ rspec-mocks (~> 3.10)
+ rspec-support (~> 3.10)
+ rspec-support (3.12.1)
ruby-vips (2.1.4)
ffi (~> 1.12)
rubyzip (2.3.2)
@@ -238,6 +265,10 @@ GEM
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
+ shoulda-callback-matchers (1.1.4)
+ activesupport (>= 3)
+ shoulda-matchers (5.3.0)
+ activesupport (>= 5.2.0)
sprockets (4.2.0)
concurrent-ruby (~> 1.0)
rack (>= 2.2.4, < 4)
@@ -288,6 +319,7 @@ DEPENDENCIES
capybara
debug
devise (~> 4.9.2)
+ factory_bot_rails
image_processing (~> 1.2)
importmap-rails (~> 1.1.6)
jbuilder (~> 2.11.5)
@@ -296,9 +328,13 @@ DEPENDENCIES
pg (~> 1.1)
puma (~> 5.0)
rails (~> 7.0.4, >= 7.0.4.3)
+ rails-controller-testing
ransack!
+ rspec-rails (~> 4.1.0)
sassc-rails (~> 2.1.2)
selenium-webdriver
+ shoulda-callback-matchers (~> 1.1.1)
+ shoulda-matchers (~> 5.0)
sprockets-rails (~> 3.4.2)
stimulus-rails (~> 1.2.1)
stripe (~> 8.5.0)
diff --git a/app/controllers/admin/quizzes_controller.rb b/app/controllers/admin/quizzes_controller.rb
index ab0bc7a3..433ce6cb 100644
--- a/app/controllers/admin/quizzes_controller.rb
+++ b/app/controllers/admin/quizzes_controller.rb
@@ -52,7 +52,7 @@ def feature
private
def quiz_params
- params.require(:quiz).permit(:title, :description, :level, :time_limit_in_minutes, :featured_at, :quiz_banner, genre_ids: [], quiz_questions_attributes: [:id, :question_id, :active])
+ params.require(:quiz).permit(:title, :description, :level, :time_limit_in_minutes, :featured_at, :quiz_banner, :amount, genre_ids: [], quiz_questions_attributes: [:id, :question_id, :active])
end
def set_quiz
diff --git a/app/models/question.rb b/app/models/question.rb
index 5c689923..b961be31 100644
--- a/app/models/question.rb
+++ b/app/models/question.rb
@@ -38,7 +38,7 @@ def self.ransackable_associations(auth_object = nil)
private
def only_one_correct_option
- unless question_options.correct.count == 1
+ unless question_options.map(&:correct).count(true) == 1
errors.add :base, 'Choose exactly one correct option.'
end
end
diff --git a/app/models/question_option.rb b/app/models/question_option.rb
index faf0b693..ee2127d6 100644
--- a/app/models/question_option.rb
+++ b/app/models/question_option.rb
@@ -14,7 +14,7 @@ class QuestionOption < ApplicationRecord
private
def presence_of_either_data_or_image
- unless option_image.present? && data.present?
+ if option_image.blank? && data.blank?
errors.add :base, 'Both data and image can\'t be blank'
end
end
diff --git a/app/models/quiz.rb b/app/models/quiz.rb
index 6dd39f85..81ccc113 100644
--- a/app/models/quiz.rb
+++ b/app/models/quiz.rb
@@ -25,13 +25,14 @@ class Quiz < ApplicationRecord
validates_length_of :title_word_count, minimum: 5, message: 'should be at least 5', if: :title?
validates_length_of :description_word_count, minimum: 15, if: :description?
validates :description, allow_blank: true, format: {
- without: Quizathon::URL_REGEXP
+ without: Quizathon::URL_REGEXP,
+ message: 'invalid format for description'
}
validates :amount, numericality: { greater_than: 0 }
before_validation :set_time_limit_in_seconds, if: -> { time_limit_in_minutes.present? }
before_validation ActivableCallbacks, on: :update
- after_save_commit :schedule_mail_if_featured, if: :featured?
+ after_save_commit :schedule_mail_if_featured, if: :featured_at?
scope :active, -> { where(active: true) }
diff --git a/app/views/admin/quizzes/_edit_form.html.erb b/app/views/admin/quizzes/_edit_form.html.erb
index 96b927cc..a0bad3d5 100644
--- a/app/views/admin/quizzes/_edit_form.html.erb
+++ b/app/views/admin/quizzes/_edit_form.html.erb
@@ -34,6 +34,10 @@
<%= form.file_field :quiz_banner %>
+
+ <%= form.label :amount %>
+ <%= form.number_field :amount %>
+
<%= form.fields_for :quiz_questions do |quiz_questions_form| %>
<%= render 'quiz_question_selector', f: quiz_questions_form, object: quiz_questions_form.object %>
<% end %>
diff --git a/app/views/admin/quizzes/new.html.erb b/app/views/admin/quizzes/new.html.erb
index 79b2d7e4..a1822711 100644
--- a/app/views/admin/quizzes/new.html.erb
+++ b/app/views/admin/quizzes/new.html.erb
@@ -24,6 +24,10 @@
<%= form.file_field :quiz_banner %>
+
+ <%= form.label :amount %>
+ <%= form.number_field :amount %>
+
<%= form.submit class: 'btn btn-dark' %>
diff --git a/app/views/quizzes/_child_comment.html.erb b/app/views/quizzes/_child_comment.html.erb
index 5e09123a..dbec50d5 100644
--- a/app/views/quizzes/_child_comment.html.erb
+++ b/app/views/quizzes/_child_comment.html.erb
@@ -2,5 +2,5 @@
<%= form.text_field :data %>
<%= form.hidden_field :parent_comment_id, value: parent_comment_id %>
<%= form.submit 'Reply', class: 'btn btn-info' %>
- <%= link_to 'Remove Comment field', '#', class: 'remove_fields', data: {id: form.object.object_id}, class: 'btn btn-secondary' %>
+ <%= link_to 'Remove Comment field', '#', class: 'remove_fields btn btn-secondary', data: {id: form.object.object_id} %>
diff --git a/config/environments/test.rb b/config/environments/test.rb
index 6ea4d1e7..185ae1de 100644
--- a/config/environments/test.rb
+++ b/config/environments/test.rb
@@ -34,7 +34,7 @@
config.action_controller.allow_forgery_protection = false
# Store uploaded files on the local file system in a temporary directory.
- config.active_storage.service = :test
+ config.active_storage.service = :local
config.action_mailer.perform_caching = false
@@ -43,6 +43,8 @@
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test
+ config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
+
# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr
diff --git a/spec/factories/question.rb b/spec/factories/question.rb
new file mode 100644
index 00000000..f69b2220
--- /dev/null
+++ b/spec/factories/question.rb
@@ -0,0 +1,13 @@
+FactoryBot.define do
+ factory :question do
+ title { 'a new css question' }
+ score { 3 }
+
+ before(:create) do |question|
+ 3.times do
+ question.question_options << FactoryBot.build(:question_option, question: question)
+ end
+ question.question_options << FactoryBot.build(:question_option, question: question, correct: true)
+ end
+ end
+end
diff --git a/spec/factories/question_option.rb b/spec/factories/question_option.rb
new file mode 100644
index 00000000..843688d7
--- /dev/null
+++ b/spec/factories/question_option.rb
@@ -0,0 +1,7 @@
+FactoryBot.define do
+ factory :question_option do
+ data { 'abc' }
+ correct { false }
+ association :question
+ end
+end
diff --git a/spec/factories/quiz.rb b/spec/factories/quiz.rb
new file mode 100644
index 00000000..d53eeabb
--- /dev/null
+++ b/spec/factories/quiz.rb
@@ -0,0 +1,9 @@
+FactoryBot.define do
+ factory :quiz do
+ title { 'A random new quiz for testing' }
+ time_limit_in_minutes { 2 }
+ amount { 500 }
+ currency_code { 'INR' }
+ active { false }
+ end
+end
diff --git a/spec/factories/quiz_question.rb b/spec/factories/quiz_question.rb
new file mode 100644
index 00000000..48f1d46c
--- /dev/null
+++ b/spec/factories/quiz_question.rb
@@ -0,0 +1,6 @@
+FactoryBot.define do
+ factory :quiz_question do
+ association :quiz
+ association :question
+ end
+end
diff --git a/spec/factories/user.rb b/spec/factories/user.rb
new file mode 100644
index 00000000..e2821b24
--- /dev/null
+++ b/spec/factories/user.rb
@@ -0,0 +1,16 @@
+FactoryBot.define do
+ factory :user do
+ email { "abcd@gmail.com" }
+ password { "abcdef" }
+ password_confirmation { "abcdef" }
+ blocked { false }
+
+ trait :admin do
+ email { "admin@gmail.com" }
+ password { 'Adminn' }
+ password_confirmation { 'Adminn' }
+ role { 'admin' }
+ blocked { false }
+ end
+ end
+end
diff --git a/spec/models/quiz_spec.rb b/spec/models/quiz_spec.rb
new file mode 100644
index 00000000..4db1005d
--- /dev/null
+++ b/spec/models/quiz_spec.rb
@@ -0,0 +1,136 @@
+require 'rails_helper'
+
+RSpec.describe Quiz, type: :model do
+ subject {
+ described_class.new(title: 'A random new quiz for testing',
+ description: 'This random new quiz is for testing purpose only to check is associations and validations are properly checked.',
+ time_limit_in_seconds: 120,
+ amount: 500,
+ currency_code: 'INR')
+ }
+
+ describe "Validations" do
+ it 'is valid with valid attributes' do
+ expect(subject).to be_valid
+ end
+
+ it { should validate_presence_of(:title) }
+
+ it { should validate_numericality_of(:time_limit_in_seconds) }
+
+ it 'is not valid without 5 length title' do
+ subject.title = 'a b c d'
+ expect(subject).to_not be_valid
+ end
+
+ it 'is not valid without 15 length description' do
+ subject.description = 'a b c d e f g h i j k l m n'
+ expect(subject).to_not be_valid
+ end
+
+ it { should_not allow_value('https://foo.com').for(:description).with_message('invalid format for description') }
+
+ it { should validate_numericality_of(:amount) }
+ end
+
+ describe "Associations" do
+ it { should have_and_belong_to_many(:genres) }
+ it { should have_many(:quiz_questions).dependent(:destroy) }
+ it { should have_many(:questions).through(:quiz_questions) }
+ it { should have_many(:active_quiz_questions).conditions(active: true).class_name('QuizQuestion') }
+ it { should have_many(:active_questions).through(:active_quiz_questions).conditions(active: true).source(:question) }
+ it { should accept_nested_attributes_for(:quiz_questions) }
+ it { should have_one_attached(:quiz_banner) }
+ it { should have_many(:comments).dependent(:destroy) }
+ it { should have_many(:ratings).dependent(:destroy) }
+ it { should have_many(:quiz_runners) }
+ it { should have_many(:started_quiz_runners).conditions(status: 'started').class_name('QuizRunner') }
+ it { should have_many(:users).through(:quiz_runners) }
+ it { should have_many(:payments).dependent(:destroy) }
+ end
+
+ describe 'callbacks' do
+ it { is_expected.to callback(:set_time_limit_in_seconds).before(:validation) }
+ it { is_expected.to callback(ActivableCallbacks).before(:validation).on(:update) }
+ it { is_expected.to callback(:schedule_mail_if_featured).after(:commit) }
+ end
+
+ describe 'Enums' do
+ it {
+ should define_enum_for(:level).with_values(
+ beginner: 1, easy: 2, moderate: 3, difficult: 4, advance: 5).backed_by_column_of_type(:integer)
+ }
+ end
+
+ describe 'scopes' do
+ describe 'active' do
+ context 'when subject is not active' do
+ it 'doesnot return subject' do
+ expect(described_class.active).not_to include(subject)
+ end
+ end
+ context 'when a quiz is active' do
+ let(:quiz) { create(:quiz, active: true) }
+
+ it 'it returns the quiz' do
+ expect(described_class.active).to include(quiz)
+ end
+ end
+ end
+
+ describe 'featured' do
+ context 'when featured_at is null' do
+ it 'should not return subject' do
+ expect(described_class.featured).not_to include(subject)
+ end
+ end
+ context 'when quiz is featured' do
+ let!(:quiz) { create(:quiz, active: true, featured_at: Time.current) }
+ it 'should return quiz' do
+ expect(described_class.featured).to include(quiz)
+ end
+ end
+ context 'when quiz was featured a day before' do
+ let(:quiz) { create(:quiz, active: true, featured_at: 1.day.ago) }
+ it 'should return quiz' do
+ expect(described_class.featured).to include(quiz)
+ end
+ end
+ context 'when quiz was featured a day minus 1 second before' do
+ let(:quiz) { create(:quiz, active: true, featured_at: 1.day.ago - 1.second) }
+ it 'should not return quiz' do
+ expect(described_class.featured).not_to include(quiz)
+ end
+ end
+ context 'when quiz is featured for tomorrow' do
+ let(:quiz) { create(:quiz, active: true, featured_at: 1.day.after) }
+ it 'should not return quiz' do
+ expect(described_class.featured).not_to include(quiz)
+ end
+ end
+ end
+
+ describe 'instance methods' do
+ let(:quiz) { create(:quiz) }
+
+ describe 'to param' do
+ it 'should return slug' do
+ expect(quiz.to_param).to eq(quiz.slug)
+ end
+ end
+
+ describe 'average rating' do
+ it 'should return average rating' do
+ expect(quiz.average_rating).to eq(quiz.ratings.average(:value))
+ end
+ end
+
+ describe 'feature now' do
+ it 'should change value of featured_at column' do
+ quiz.feature_now
+ expect(quiz.featured_at).to be_between(Time.current - 1.minute, Time.current)
+ end
+ end
+ end
+ end
+end
diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb
new file mode 100644
index 00000000..c270ba95
--- /dev/null
+++ b/spec/rails_helper.rb
@@ -0,0 +1,74 @@
+# This file is copied to spec/ when you run 'rails generate rspec:install'
+require 'spec_helper'
+ENV['RAILS_ENV'] ||= 'test'
+
+require File.expand_path('../config/environment', __dir__)
+
+# Prevent database truncation if the environment is production
+abort("The Rails environment is running in production mode!") if Rails.env.production?
+require 'rspec/rails'
+# Add additional requires below this line. Rails is not loaded until this point!
+
+# Requires supporting ruby files with custom matchers and macros, etc, in
+# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
+# run as spec files by default. This means that files in spec/support that end
+# in _spec.rb will both be required and run as specs, causing the specs to be
+# run twice. It is recommended that you do not name files matching this glob to
+# end with _spec.rb. You can configure this pattern with the --pattern
+# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
+#
+# The following line is provided for convenience purposes. It has the downside
+# of increasing the boot-up time by auto-requiring all files in the support
+# directory. Alternatively, in the individual `*_spec.rb` files, manually
+# require only the support files necessary.
+#
+# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
+
+# Checks for pending migrations and applies them before tests are run.
+# If you are not using ActiveRecord, you can remove these lines.
+begin
+ ActiveRecord::Migration.maintain_test_schema!
+rescue ActiveRecord::PendingMigrationError => e
+ puts e.to_s.strip
+ exit 1
+end
+RSpec.configure do |config|
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
+ config.fixture_path = "#{::Rails.root}/spec/fixtures"
+
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
+ # examples within a transaction, remove the following line or assign false
+ # instead of true.
+ config.use_transactional_fixtures = true
+
+ # RSpec Rails can automatically mix in different behaviours to your tests
+ # based on their file location, for example enabling you to call `get` and
+ # `post` in specs under `spec/controllers`.
+ #
+ # You can disable this behaviour by removing the line below, and instead
+ # explicitly tag your specs with their type, e.g.:
+ #
+ # RSpec.describe UsersController, :type => :controller do
+ # # ...
+ # end
+ #
+ # The different available types are documented in the features, such as in
+ # https://relishapp.com/rspec/rspec-rails/docs
+ config.infer_spec_type_from_file_location!
+
+ # Filter lines from Rails gems in backtraces.
+ config.filter_rails_from_backtrace!
+ # arbitrary gems may also be filtered via:
+ # config.filter_gems_from_backtrace("gem name")
+
+ config.include Devise::Test::IntegrationHelpers, type: :request
+
+ config.include FactoryBot::Syntax::Methods
+
+ Shoulda::Matchers.configure do |config|
+ config.integrate do |with|
+ with.test_framework :rspec
+ with.library :rails
+ end
+ end
+end
diff --git a/spec/requests/admin/quizzes_controller_spec.rb b/spec/requests/admin/quizzes_controller_spec.rb
new file mode 100644
index 00000000..81bd9fde
--- /dev/null
+++ b/spec/requests/admin/quizzes_controller_spec.rb
@@ -0,0 +1,194 @@
+require 'rails_helper'
+
+RSpec.describe "Admin::QuizzesControllers", type: :request do
+ describe 'GET #index' do
+ context 'when user is not admin' do
+ let(:user) { create(:user) }
+ before do
+ get "/confirmation?confirmation_token=#{user.confirmation_token}"
+ get admin_quizzes_path
+ end
+ it 'cannot view all quizzes' do
+ expect(response).to redirect_to(root_path)
+ expect(flash[:alert]).to match(/You donot have privileges to access this section/)
+ end
+ end
+
+ context 'when user is admin' do
+ let(:admin) { create(:user, :admin) }
+ let!(:quizzes) { create_list(:quiz, 4) }
+ before do
+ sign_in admin
+ get admin_quizzes_path
+ end
+ it 'can view all quizzes' do
+ expect(assigns(:quizzes)).to include(quizzes.first)
+ expect(response).to render_template('admin/quizzes/index')
+ end
+ end
+ end
+
+ describe 'GET #new' do
+ context 'when user is not admin' do
+ let(:user) { create(:user) }
+ before do
+ get "/confirmation?confirmation_token=#{user.confirmation_token}"
+ get '/admin/quizzes/new'
+ end
+ it 'cannot create a new quiz' do
+ expect(response).to redirect_to(root_path)
+ expect(flash[:alert]).to match(/You donot have privileges to access this section/)
+ end
+ end
+
+ context 'when user is admin' do
+ let(:admin) { create(:user, :admin) }
+ before do
+ sign_in admin
+ get '/admin/quizzes/new'
+ end
+ it 'can create a new quiz' do
+ expect(response).to render_template('admin/quizzes/new')
+ end
+ end
+ end
+
+ describe 'POST #create' do
+ context 'when user is not admin' do
+ let(:user) { create(:user) }
+ before do
+ get "/confirmation?confirmation_token=#{user.confirmation_token}"
+ post '/admin/quizzes', params: { quiz: {title: 'abc'} }
+ end
+ it 'cannot create a new quiz' do
+ expect(response).to redirect_to(root_path)
+ expect(flash[:alert]).to match(/You donot have privileges to access this section/)
+ end
+ end
+
+ context 'when user is admin' do
+ let(:admin) { create(:user, :admin) }
+ let(:quiz) { build(:quiz) }
+ before do
+ sign_in admin
+ post '/admin/quizzes', params: { quiz: {title: quiz.title, time_limit_in_minutes: 2, amount: 500} }
+ end
+ it 'can create a new quiz' do
+ expect(response).to redirect_to(admin_quizzes_path)
+ expect(flash[:notice]).to match(/Quiz created successfully/)
+ end
+ end
+ end
+
+ describe 'GET #edit' do
+ context 'when user is not admin' do
+ let(:user) { create(:user) }
+ let(:quiz) { create(:quiz) }
+ before do
+ get "/confirmation?confirmation_token=#{user.confirmation_token}"
+ get edit_admin_quiz_path(quiz)
+ end
+ it 'cannot edit the quiz' do
+ expect(response).to redirect_to(root_path)
+ expect(flash[:alert]).to match(/You donot have privileges to access this section/)
+ end
+ end
+
+ context 'when user is admin' do
+ let(:admin) { create(:user, :admin) }
+ let(:quiz) { create(:quiz) }
+ before do
+ sign_in admin
+ get edit_admin_quiz_path(quiz)
+ end
+ it 'can edit the quiz' do
+ expect(response).to render_template('admin/quizzes/edit')
+ end
+ end
+ end
+
+ describe 'PATCH #update' do
+ context 'when user is not admin' do
+ let(:user) { create(:user) }
+ let(:quiz) { create(:quiz) }
+ before do
+ get "/confirmation?confirmation_token=#{user.confirmation_token}"
+ patch admin_quiz_path(quiz)
+ end
+ it 'cannot update the quiz' do
+ expect(response).to redirect_to(root_path)
+ expect(flash[:alert]).to match(/You donot have privileges to access this section/)
+ end
+ end
+
+ context 'when user is admin' do
+ let(:admin) { create(:user, :admin) }
+ let(:quiz) { create(:quiz) }
+ before do
+ sign_in admin
+ patch admin_quiz_path(quiz), params: { quiz: { title: quiz.title, time_limit_in_minutes: 2, amount: 500 } }
+ end
+ it 'can update the quiz' do
+ expect(response).to redirect_to(admin_quizzes_path)
+ expect(flash[:notice]).to match(/Quiz [\w|\s]* was successfully updated/)
+ end
+ end
+ end
+
+ describe 'DELETE #destroy' do
+ context 'when user is not admin' do
+ let(:user) { create(:user) }
+ let(:quiz) { create(:quiz) }
+ before do
+ get "/confirmation?confirmation_token=#{user.confirmation_token}"
+ delete admin_quiz_path(quiz)
+ end
+ it 'cannot delete the quiz' do
+ expect(response).to redirect_to(root_path)
+ expect(flash[:alert]).to match(/You donot have privileges to access this section/)
+ end
+ end
+
+ context 'when user is admin' do
+ let(:admin) { create(:user, :admin) }
+ let!(:quiz) { create(:quiz) }
+ before do
+ sign_in admin
+ delete admin_quiz_path(quiz)
+ end
+ it 'can delete the quiz' do
+ expect(response).to redirect_to(admin_quizzes_path)
+ expect(flash[:alert]).to match(/Quiz destroyed successfully/)
+ end
+ end
+ end
+
+ describe 'PATCH #feature' do
+ context 'when user is not admin' do
+ let(:user) { create(:user) }
+ let(:quiz) { create(:quiz) }
+ before do
+ get "/confirmation?confirmation_token=#{user.confirmation_token}"
+ patch feature_admin_quiz_path(quiz)
+ end
+ it 'cannot feature the quiz' do
+ expect(response).to redirect_to(root_path)
+ expect(flash[:alert]).to match(/You donot have privileges to access this section/)
+ end
+ end
+
+ context 'when user is admin' do
+ let(:admin) { create(:user, :admin) }
+ let!(:quiz) { create(:quiz) }
+ before do
+ sign_in admin
+ patch feature_admin_quiz_path(quiz)
+ end
+ it 'can feature the quiz' do
+ expect(Quiz.find_by_id(quiz.id).featured_at).to be_between(Time.current - 1.minute, Time.current)
+ expect(response).to redirect_to(admin_quizzes_path)
+ expect(flash[:notice]).to match(/Quiz featured successfully/)
+ end
+ end
+ end
+end
diff --git a/spec/requests/quizzes_controller_spec.rb b/spec/requests/quizzes_controller_spec.rb
new file mode 100644
index 00000000..8f5bc13a
--- /dev/null
+++ b/spec/requests/quizzes_controller_spec.rb
@@ -0,0 +1,200 @@
+require 'rails_helper'
+
+RSpec.describe "QuizzesControllers", type: :request do
+ describe "GET #index" do
+ before do
+ get quizzes_url
+ end
+
+ it 'return http success' do
+ expect(response).to have_http_status(:success)
+ end
+
+ it 'to render index' do
+ expect(response).to render_template(:index)
+ end
+ end
+
+ describe "GET #show" do
+ let(:quiz) { create(:quiz) }
+
+ before do
+ get quiz_url(quiz)
+ end
+
+ it 'return http success' do
+ expect(response).to have_http_status(:success)
+ end
+
+ it 'to render show' do
+ expect(response).to render_template(:show)
+ end
+ end
+
+ describe 'PUT #start' do
+ let(:participant) { create(:user) }
+ let(:quiz) { create(:quiz) }
+ let!(:question) { create(:question) }
+ let!(:quiz_question) { create(:quiz_question, quiz: quiz, question: question) }
+
+ before do
+ get "/confirmation?confirmation_token=#{participant.confirmation_token}"
+ end
+
+ before do
+ put start_quiz_path(quiz)
+ end
+
+ it do
+ quiz_runner = QuizRunner.find_by(quiz: quiz, user: participant)
+ expect(response).to have_http_status(:found)
+ expect(response).to redirect_to(question_path(question_slug: quiz_runner.questions_sorting_order.first))
+ end
+ end
+
+ describe 'PUT #complete' do
+ let(:participant) { create(:user) }
+ let(:quiz) { create(:quiz) }
+ let!(:question) { create(:question) }
+ let!(:quiz_question) { create(:quiz_question, quiz: quiz, question: question) }
+
+ before do
+ get "/confirmation?confirmation_token=#{participant.confirmation_token}"
+ end
+
+ before do
+ put complete_quiz_path(quiz)
+ end
+
+ it do
+ expect(response).to have_http_status(:found)
+ expect(response).to redirect_to(quiz_path(quiz))
+ end
+ end
+
+ describe 'GET #resume' do
+ let(:participant) { create(:user) }
+ let(:quiz) { create(:quiz) }
+ let!(:question) { create(:question) }
+ let!(:quiz_question) { create(:quiz_question, quiz: quiz, question: question) }
+
+ before do
+ get "/confirmation?confirmation_token=#{participant.confirmation_token}"
+ end
+
+ context 'when no quiz is started' do
+ before do
+ get resume_quiz_path(quiz)
+ end
+
+ it do
+ expect(response).to redirect_to(quiz_path(quiz))
+ expect(flash[:alert]).to match(/Quiz runner doesnot exist/)
+ end
+ end
+
+ context 'when a quiz is started' do
+ before do
+ put start_quiz_path(quiz)
+ end
+
+ before do
+ get resume_quiz_path(quiz)
+ end
+
+ it do
+ expect(response).to redirect_to(question_path(question_slug: assigns[:quiz_runner].questions_sorting_order.first))
+ end
+ end
+ end
+
+ describe 'GET #submit' do
+ let(:participant) { create(:user) }
+ let(:quiz) { create(:quiz) }
+ let!(:question) { create(:question) }
+ let!(:quiz_question) { create(:quiz_question, quiz: quiz, question: question) }
+
+ before do
+ get "/confirmation?confirmation_token=#{participant.confirmation_token}"
+ end
+
+ context 'when no quiz is started' do
+ before do
+ get submit_quiz_path(quiz)
+ end
+
+ it do
+ expect(response).to redirect_to(quiz_path(quiz))
+ expect(flash[:alert]).to match(/Quiz runner doesnot exist/)
+ end
+ end
+
+ context 'when a quiz is started' do
+ before do
+ put start_quiz_path(quiz)
+ end
+
+ before do
+ get submit_quiz_path(quiz)
+ end
+
+ it do
+ expect(response).to render_template(:submit)
+ end
+ end
+ end
+
+ describe 'POST #comment' do
+ let(:participant) { create(:user) }
+ let(:quiz) { create(:quiz) }
+
+ before do
+ get "/confirmation?confirmation_token=#{participant.confirmation_token}"
+ end
+
+ before do
+ post comment_quiz_path(quiz), params: { comment: { data: 'abc' } }
+ end
+
+ it do
+ expect(response).to have_http_status(:found)
+ expect(response).to redirect_to(quiz_path(quiz))
+ end
+ end
+
+ describe 'POST #rate' do
+ let(:participant) { create(:user) }
+ let(:quiz) { create(:quiz) }
+
+ before do
+ get "/confirmation?confirmation_token=#{participant.confirmation_token}"
+ end
+
+ before do
+ post rate_quiz_path(quiz)
+ end
+
+ it do
+ expect(response).to have_http_status(:found)
+ expect(response).to redirect_to(quiz_path(quiz))
+ end
+ end
+
+ describe 'PATCH #rate' do
+ let(:participant) { create(:user) }
+ let(:quiz) { create(:quiz) }
+
+ before do
+ get "/confirmation?confirmation_token=#{participant.confirmation_token}"
+ end
+
+ before do
+ patch rate_quiz_path(quiz)
+ end
+
+ it do
+ expect(response).to have_http_status(:found)
+ expect(response).to redirect_to(quiz_path(quiz))
+ end
+ end
+end
diff --git a/spec/requests/users/sessions_controller_spec.rb b/spec/requests/users/sessions_controller_spec.rb
new file mode 100644
index 00000000..40ad8a4c
--- /dev/null
+++ b/spec/requests/users/sessions_controller_spec.rb
@@ -0,0 +1,81 @@
+require 'rails_helper'
+
+RSpec.describe "Users::SessionsControllers", type: :request do
+
+ describe 'GET /sign_in', type: :request do
+ before do
+ get '/sign_in'
+ end
+
+ it 'opens sign in page' do
+ expect(response.body).to match(/Log in/)
+ expect(response).to render_template(:new)
+ end
+ end
+
+ describe "GET /confirmation", type: :request do
+ context 'when user is participant' do
+ let(:participant) { create(:user) }
+
+ before do
+ get "/confirmation?confirmation_token=#{participant.confirmation_token}"
+ end
+
+ it 'can view his profile' do
+ get '/profile'
+ expect(response).to have_http_status(:ok)
+ expect(response).to render_template(:show)
+ end
+
+ it 'cannot view all users' do
+ get '/admin/users'
+ expect(response).to redirect_to(root_path)
+ expect(flash[:alert]).to match(/You donot have privileges to access this section/)
+ end
+ end
+
+ context 'when user is admin' do
+ let(:admin) { create(:user, :admin) }
+
+ before do
+ sign_in admin
+ end
+
+ it 'can view all users' do
+ get '/admin/users'
+ expect(response).to have_http_status(:ok)
+ expect(response).to render_template(:index)
+ end
+ end
+ end
+
+ describe 'DELETE /sign_out', type: :request do
+ context 'when user is participant' do
+ let(:participant) { create(:user) }
+
+ before do
+ get "/confirmation?confirmation_token=#{participant.confirmation_token}"
+ end
+
+ it 'gets logged out' do
+ delete '/sign_out'
+ expect(response).to redirect_to(root_path)
+ expect(flash[:notice]).to match(/Signed out successfully/)
+ end
+ end
+
+ context 'when user is admin' do
+ let(:admin) { create(:user, :admin) }
+
+ before do
+ sign_in admin
+ end
+
+ it 'gets logged out' do
+ delete '/sign_out'
+ expect(response).to redirect_to(root_path)
+ expect(flash[:notice]).to match(/Signed out successfully/)
+ end
+ end
+ end
+end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
new file mode 100644
index 00000000..75cc2ced
--- /dev/null
+++ b/spec/spec_helper.rb
@@ -0,0 +1,100 @@
+# This file was generated by the `rails generate rspec:install` command. Conventionally, all
+# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
+# The generated `.rspec` file contains `--require spec_helper` which will cause
+# this file to always be loaded, without a need to explicitly require it in any
+# files.
+#
+# Given that it is always loaded, you are encouraged to keep this file as
+# light-weight as possible. Requiring heavyweight dependencies from this file
+# will add to the boot time of your test suite on EVERY test run, even for an
+# individual file that may not need all of that loaded. Instead, consider making
+# a separate helper file that requires the additional dependencies and performs
+# the additional setup, and require it from the spec files that actually need
+# it.
+#
+# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
+RSpec.configure do |config|
+ # rspec-expectations config goes here. You can use an alternate
+ # assertion/expectation library such as wrong or the stdlib/minitest
+ # assertions if you prefer.
+ config.expect_with :rspec do |expectations|
+ # This option will default to `true` in RSpec 4. It makes the `description`
+ # and `failure_message` of custom matchers include text for helper methods
+ # defined using `chain`, e.g.:
+ # be_bigger_than(2).and_smaller_than(4).description
+ # # => "be bigger than 2 and smaller than 4"
+ # ...rather than:
+ # # => "be bigger than 2"
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
+ end
+
+ # rspec-mocks config goes here. You can use an alternate test double
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
+ config.mock_with :rspec do |mocks|
+ # Prevents you from mocking or stubbing a method that does not exist on
+ # a real object. This is generally recommended, and will default to
+ # `true` in RSpec 4.
+ mocks.verify_partial_doubles = true
+ end
+
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
+ # have no way to turn it off -- the option exists only for backwards
+ # compatibility in RSpec 3). It causes shared context metadata to be
+ # inherited by the metadata hash of host groups and examples, rather than
+ # triggering implicit auto-inclusion in groups with matching metadata.
+ config.shared_context_metadata_behavior = :apply_to_host_groups
+
+# The settings below are suggested to provide a good initial experience
+# with RSpec, but feel free to customize to your heart's content.
+=begin
+ # This allows you to limit a spec run to individual examples or groups
+ # you care about by tagging them with `:focus` metadata. When nothing
+ # is tagged with `:focus`, all examples get run. RSpec also provides
+ # aliases for `it`, `describe`, and `context` that include `:focus`
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
+ config.filter_run_when_matching :focus
+
+ # Allows RSpec to persist some state between runs in order to support
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
+ # you configure your source control system to ignore this file.
+ config.example_status_persistence_file_path = "spec/examples.txt"
+
+ # Limits the available syntax to the non-monkey patched syntax that is
+ # recommended. For more details, see:
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
+ config.disable_monkey_patching!
+
+ # This setting enables warnings. It's recommended, but in some cases may
+ # be too noisy due to issues in dependencies.
+ config.warnings = true
+
+ # Many RSpec users commonly either run the entire suite or an individual
+ # file, and it's useful to allow more verbose output when running an
+ # individual spec file.
+ if config.files_to_run.one?
+ # Use the documentation formatter for detailed output,
+ # unless a formatter has already been configured
+ # (e.g. via a command-line flag).
+ config.default_formatter = "doc"
+ end
+
+ # Print the 10 slowest examples and example groups at the
+ # end of the spec run, to help surface which specs are running
+ # particularly slow.
+ config.profile_examples = 10
+
+ # Run specs in random order to surface order dependencies. If you find an
+ # order dependency and want to debug it, you can fix the order by providing
+ # the seed, which is printed after each run.
+ # --seed 1234
+ config.order = :random
+
+ # Seed global randomization in this process using the `--seed` CLI option.
+ # Setting this allows you to use `--seed` to deterministically reproduce
+ # test failures related to randomization by passing the same `--seed` value
+ # as the one that triggered the failure.
+ Kernel.srand config.seed
+=end
+end