From 05334e3bd0b9b47070140d2f3d827fbc93e777a5 Mon Sep 17 00:00:00 2001 From: nilsding Date: Sun, 18 Jan 2015 14:15:21 +0100 Subject: [PATCH] added some specs --- app/assets/javascripts/piwik.js.erb | 2 +- spec/factories/{users.rb => 10_users.rb} | 0 spec/factories/answers.rb | 6 ++ spec/factories/questions.rb | 6 ++ spec/features/users/banned_spec.rb | 43 ++++++++ spec/features/users/inbox_spec.rb | 124 +++++++++++++++++++++++ spec/models/answer_spec.rb | 19 ++++ spec/models/question_spec.rb | 35 +++++++ 8 files changed, 234 insertions(+), 1 deletion(-) rename spec/factories/{users.rb => 10_users.rb} (100%) create mode 100644 spec/factories/answers.rb create mode 100644 spec/factories/questions.rb create mode 100644 spec/features/users/banned_spec.rb create mode 100644 spec/features/users/inbox_spec.rb create mode 100644 spec/models/answer_spec.rb create mode 100644 spec/models/question_spec.rb diff --git a/app/assets/javascripts/piwik.js.erb b/app/assets/javascripts/piwik.js.erb index 5e716619..a3e62461 100644 --- a/app/assets/javascripts/piwik.js.erb +++ b/app/assets/javascripts/piwik.js.erb @@ -13,6 +13,6 @@ _paq.push(['setDocumentTitle', document.title]); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s); })(); -<% else %> +<% elsif !Rails.env.test? %> console.log("i track'd ur bich"); <% end %> \ No newline at end of file diff --git a/spec/factories/users.rb b/spec/factories/10_users.rb similarity index 100% rename from spec/factories/users.rb rename to spec/factories/10_users.rb diff --git a/spec/factories/answers.rb b/spec/factories/answers.rb new file mode 100644 index 00000000..6d32c335 --- /dev/null +++ b/spec/factories/answers.rb @@ -0,0 +1,6 @@ +FactoryGirl.define do + factory :answer do |u| + u.sequence(:content) { |n| "This is an answer. I'm number #{n}!" } + u.user FactoryGirl.create(:user) + end +end diff --git a/spec/factories/questions.rb b/spec/factories/questions.rb new file mode 100644 index 00000000..688854b1 --- /dev/null +++ b/spec/factories/questions.rb @@ -0,0 +1,6 @@ +FactoryGirl.define do + factory :question do |u| + u.sequence(:content) { |n| "#{QuestionGenerator.generate}#{n}" } + u.author_is_anonymous true + end +end diff --git a/spec/features/users/banned_spec.rb b/spec/features/users/banned_spec.rb new file mode 100644 index 00000000..6b4f4107 --- /dev/null +++ b/spec/features/users/banned_spec.rb @@ -0,0 +1,43 @@ +include Warden::Test::Helpers +Warden.test_mode! + +# Feature: Ban users +# As a user +# I want to get banned +# So I can't sign in anymore +feature "Ban users", :devise do + + after :each do + Warden.test_reset! + end + + # Scenario: User gets banned + # Given I am signed in + # When I visit another page + # And I am banned + # Then I see the sign in page + scenario "user gets banned", js: true do + me = FactoryGirl.create :user + + login_as me, scope: :user + visit root_path + expect(page).to have_text("Timeline") + page.driver.render Rails.root.join("tmp/#{Time.now.to_i}_1.png"), full: true + + me.banned = true + me.save + click_link "Inbox" + expect(current_path).to eq(new_user_session_path) + page.driver.render Rails.root.join("tmp/#{Time.now.to_i}_2.png"), full: true + end + + scenario 'user visits banned user profiles', js: true do + evil_user = FactoryGirl.create :user + evil_user.banned = true + evil_user.save + + visit show_user_profile_path(evil_user.screen_name) + expect(page).to have_text('Banned'.upcase) + page.driver.render Rails.root.join("tmp/#{Time.now.to_i}_3.png"), full: true + end +end diff --git a/spec/features/users/inbox_spec.rb b/spec/features/users/inbox_spec.rb new file mode 100644 index 00000000..2dd96409 --- /dev/null +++ b/spec/features/users/inbox_spec.rb @@ -0,0 +1,124 @@ +include Warden::Test::Helpers +Warden.test_mode! + +# Feature: Answer questions +# As a user +# I want to go to the inbox +# So I can answer and get new questions +feature "Inbox", :devise do + + after :each do + Warden.test_reset! + end + + # Scenario: User answers a question + # Given I am signed in + # When I visit the inbox + # And I have a question in my inbox + # Then I can answer my question + # And see the answer on my user profile + scenario "user answers a question", js: true do + me = FactoryGirl.create :user + question = FactoryGirl.create :question + Inbox.create question: question, user: me, new: true + + login_as me, scope: :user + visit root_path + expect(page).to have_text('1 new question') + page.driver.render Rails.root.join("tmp/#{Time.now.to_i}_1.png"), full: true + + click_link "Inbox" + expect(page).to have_text(question.content) + fill_in "ib-answer", with: Faker::Lorem.sentence + page.driver.render Rails.root.join("tmp/#{Time.now.to_i}_2.png"), full: true + + click_button "Answer" + wait_for_ajax + expect(page).not_to have_text(question.content) + page.driver.render Rails.root.join("tmp/#{Time.now.to_i}_3.png"), full: true + + visit show_user_profile_path(me.screen_name) + expect(page).to have_text(question.content) + page.driver.render Rails.root.join("tmp/#{Time.now.to_i}_4.png"), full: true + end + + # Scenario: User generates new question + # Given I am signed in + # When I visit the inbox + # And I click "Get new question" + # Then I get a new question + scenario 'user generates new question', js: true do + me = FactoryGirl.create :user + + login_as me, scope: :user + visit inbox_path + page.driver.render Rails.root.join("tmp/#{Time.now.to_i}_1.png"), full: true + + click_button "Get new question" + wait_for_ajax + expect(page).to have_text('Answer'.upcase) + page.driver.render Rails.root.join("tmp/#{Time.now.to_i}_2.png"), full: true + end + +=begin + # Scenario: User deletes a question + # Given I am signed in + # When I visit the inbox + # And I have a question in my inbox + # And I delete the question + # Then don't see it anymore in my inbox + scenario "user deletes a question", js: true do + me = FactoryGirl.create :user + question = FactoryGirl.create :question + Inbox.create question: question, user: me + + login_as me, scope: :user + visit inbox_path + expect(page).to have_text(question.content) + + click_button "Delete" + expect(page).to have_text('Really delete?') + page.driver.render Rails.root.join("tmp/#{Time.now.to_i}_1.png"), full: true + + # this apparently doesn't get triggered :( + page.find('.sweet-alert').click_button 'Delete' + wait_for_ajax + + visit root_path + visit inbox_path + expect(page).not_to have_text(question.content) + page.driver.render Rails.root.join("tmp/#{Time.now.to_i}_2.png"), full: true + end + + # Scenario: User deletes all questions + # Given I am signed in + # When I visit the inbox + # And I have a few questions in my inbox + # And I click on "Delete all questions" + # Then don't see them anymore in my inbox + scenario "user deletes all questions", js: true do + me = FactoryGirl.create :user + 5.times do + question = FactoryGirl.create :question + Inbox.create question: question, user: me + end + + login_as me, scope: :user + visit inbox_path + expect(page).to have_text('Answer'.upcase) + + click_button "Delete all questions" + expect(page).to have_text('Really delete 5 questions?') + page.driver.render Rails.root.join("tmp/#{Time.now.to_i}_1.png"), full: true + + # this apparently doesn't get triggered :( + page.find('.sweet-alert').click_button 'Delete' + wait_for_ajax + + visit root_path + visit inbox_path + page.driver.render Rails.root.join("tmp/#{Time.now.to_i}_2.png"), full: true + expect(page).not_to have_text('Answer'.upcase) + end +=end +end diff --git a/spec/models/answer_spec.rb b/spec/models/answer_spec.rb new file mode 100644 index 00000000..86714638 --- /dev/null +++ b/spec/models/answer_spec.rb @@ -0,0 +1,19 @@ +require 'rails_helper' + +RSpec.describe Answer, :type => :model do + before :each do + @answer = Answer.new( + content: 'This is an answer.', + user: FactoryGirl.create(:user), + question: FactoryGirl.create(:question) + ) + end + + subject { @answer } + + it { should respond_to(:content) } + + it '#content returns a string' do + expect(@answer.content).to match 'This is an answer.' + end +end diff --git a/spec/models/question_spec.rb b/spec/models/question_spec.rb new file mode 100644 index 00000000..9bfa8b3a --- /dev/null +++ b/spec/models/question_spec.rb @@ -0,0 +1,35 @@ +require 'rails_helper' + +RSpec.describe Question, :type => :model do + before :each do + @question = Question.new( + content: 'Is this a question?', + user: FactoryGirl.create(:user) + ) + end + + subject { @question } + + it { should respond_to(:content) } + + it '#content returns a string' do + expect(@question.content).to match 'Is this a question?' + end + + it 'does not save questions longer than 255 characters' do + @question.content = 'X' * 256 + expect{@question.save!}.to raise_error(ActiveRecord::RecordInvalid) + end + + it 'has many answers' do + 5.times { |i| Answer.create(content: "This is an answer. #{i}", user: FactoryGirl.create(:user), question: @question) } + expect(@question.answer_count).to match 5 + end + + it 'also deletes the answers when deleted' do + 5.times { |i| Answer.create(content: "This is an answer. #{i}", user: FactoryGirl.create(:user), question: @question) } + first_answer_id = @question.answers.first.id + @question.destroy + expect{Answer.find(first_answer_id)}.to raise_error(ActiveRecord::RecordNotFound) + end +end