diff --git a/spec/features/account_spec.rb b/spec/features/account_spec.rb index 7179b6c6b..e622c83c1 100644 --- a/spec/features/account_spec.rb +++ b/spec/features/account_spec.rb @@ -21,6 +21,8 @@ feature 'Account' do fill_in 'account_first_name', with: 'Larry' fill_in 'account_last_name', with: 'Bird' + check 'account_email_on_debate_comment' + check 'account_email_on_comment_reply' click_button 'Save changes' expect(page).to have_content "Saved" @@ -29,5 +31,7 @@ feature 'Account' do expect(page).to have_selector("input[value='Larry']") expect(page).to have_selector("input[value='Bird']") + expect(page).to have_selector("input[id='account_email_on_debate_comment'][value='1']") + expect(page).to have_selector("input[id='account_email_on_comment_reply'][value='1']") end end \ No newline at end of file diff --git a/spec/features/emails_spec.rb b/spec/features/emails_spec.rb new file mode 100644 index 000000000..86c794fac --- /dev/null +++ b/spec/features/emails_spec.rb @@ -0,0 +1,63 @@ +require 'rails_helper' + +feature 'Emails' do + + background do + reset_mailer + end + + scenario "Signup Email" do + sign_up + + email = open_last_email + expect(email).to have_subject('Confirmation instructions') + expect(email).to deliver_to('manuela@madrid.es') + expect(email).to have_body_text(user_confirmation_path) + end + + scenario "Reset password" do + reset_password + + email = open_last_email + expect(email).to have_subject('Reset password instructions') + expect(email).to deliver_to('manuela@madrid.es') + expect(email).to have_body_text(edit_user_password_path) + end + + scenario "Debate comment", :js do + user = create(:user, email_on_debate_comment: true) + debate = create(:debate, author: user) + comment_on(debate) + + email = open_last_email + expect(email).to have_subject('Someone has commented on your debate') + expect(email).to deliver_to(debate.author) + expect(email).to have_body_text(debate_path(debate)) + end + + scenario "Comment reply", :js do + user = create(:user, email_on_comment_reply: true) + reply_to(user) + + email = open_last_email + expect(email).to have_subject('Someone has replied to your comment') + expect(email).to deliver_to(user) + expect(email).to have_body_text(debate_path(Comment.first.debate)) + end + + scenario 'Do not send email about debate comment unless set in preferences', :js do + user = create(:user, email_on_debate_comment: false) + debate = create(:debate, author: user) + comment_on(debate) + + expect { open_last_email }.to raise_error "No email has been sent!" + end + + scenario "Do not send email about comment reply unless set in preferences", :js do + user = create(:user, email_on_comment_reply: false) + reply_to(user) + + expect { open_last_email }.to raise_error "No email has been sent!" + end + +end \ No newline at end of file diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 0a7665127..93cde956e 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -34,6 +34,20 @@ describe User do expect(subject).to be_valid end + describe 'preferences' do + describe 'email_on_debate_comment' do + it 'should be false by default' do + expect(subject.email_on_debate_comment).to eq(false) + end + end + + describe 'email_on_comment_reply' do + it 'should be false by default' do + expect(subject.email_on_comment_reply).to eq(false) + end + end + end + describe 'use_nickname' do describe 'when true' do before { subject.use_nickname = true } diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5a6bb5fcc..640f9e96c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,7 @@ require 'factory_girl_rails' require 'database_cleaner' require "email_spec" +Dir["./spec/support/**/*.rb"].sort.each { |f| require f} RSpec.configure do |config| config.use_transactional_fixtures = false @@ -10,6 +11,7 @@ RSpec.configure do |config| config.include FactoryGirl::Syntax::Methods config.include(EmailSpec::Helpers) config.include(EmailSpec::Matchers) + config.include(CommonActions) config.before(:suite) do DatabaseCleaner.clean_with :truncation end diff --git a/spec/support/common_actions.rb b/spec/support/common_actions.rb new file mode 100644 index 000000000..d07f60261 --- /dev/null +++ b/spec/support/common_actions.rb @@ -0,0 +1,54 @@ +module CommonActions + + def sign_up + visit '/' + click_link 'Sign up' + + fill_in 'user_first_name', with: 'Manuela' + fill_in 'user_last_name', with: 'Carmena' + fill_in 'user_email', with: 'manuela@madrid.es' + fill_in 'user_password', with: 'judgementday' + fill_in 'user_password_confirmation', with: 'judgementday' + + click_button 'Sign up' + end + + def reset_password + create(:user, email: 'manuela@madrid.es') + + visit '/' + click_link 'Log in' + click_link 'Forgot your password?' + + fill_in 'user_email', with: 'manuela@madrid.es' + click_button 'Send me reset password instructions' + end + + def comment_on(debate) + user2 = create(:user) + + login_as(user2) + visit debate_path(debate) + + fill_in 'comment_body', with: 'Have you thought about...?' + click_button 'Publish comment' + expect(page).to have_content 'Have you thought about...?' + end + + def reply_to(user) + manuela = create(:user) + debate = create(:debate) + comment = create(:comment, commentable: debate, user: user) + + login_as(manuela) + visit debate_path(debate) + + click_link "Reply" + within "#js-comment-form-comment_#{comment.id}" do + fill_in 'comment_body', with: 'It will be done next week.' + click_button 'Publish reply' + end + expect(page).to have_content 'It will be done next week.' + end + +end \ No newline at end of file