<commit_before>require 'spec_helper'

describe 'authentication' do

  before :each do
    @user = FactoryGirl.create :user
  end

  context 'unauthenticated visitors' do

    it 'shows a login link in the main page' do
      visit '/'
      page.should have_css "a#sign_in[href*=\"#{new_user_session_path}\"]"
    end

    it 'shows a signup link in the main page' do
      visit '/'
      page.should have_css "a#sign_up[href*=\"#{new_user_registration_path}\"]"
    end

    it 'redirects user to feeds page after a successful login' do
      login_user_for_feature @user
      current_path.should eq feeds_path
    end

    it 'stays on the login page after a failed login attempt' do
      visit new_user_session_path
      fill_in 'Email', with: @user.email
      fill_in 'Password', with: '<PASSWORD>!!!'
      click_on 'Sign in'
      current_path.should eq new_user_session_path
    end

    it 'does not show navbar' do
      visit '/'
      page.should_not have_css 'div.navbar'
    end

  end

  context 'authenticated users' do

    before :each do
      login_user_for_feature @user
    end

    it 'redirects to feeds list after a successful login' do
      current_path.should eq feeds_path
    end

    it 'does not show the login link in the main page' do
      page.should_not have_css "a#sign_in[href*=\"#{new_user_session_path}\"]"
    end
<commit_msg>Added more acceptance tests for the authentication functionality.<commit_after>require 'spec_helper'

describe 'authentication' do

  before :each do
    @user = FactoryGirl.create :user
  end

  context 'unauthenticated visitors' do

    it 'shows a login link in the main page' do
      visit '/'
      page.should have_css "a#sign_in[href*=\"#{new_user_session_path}\"]"
    end

    it 'shows a signup link in the main page' do
      visit '/'
      page.should have_css "a#sign_up[href*=\"#{new_user_registration_path}\"]"
    end

    it 'redirects user to feeds page after a successful login' do
      login_user_for_feature @user
      current_path.should eq feeds_path
    end

    it 'stays on the login page after a failed login attempt' do
      visit new_user_session_path
      fill_in 'Email', with: @user.email
      fill_in 'Password', with: '<PASSWORD>!!!'
      click_on 'Sign in'
      current_path.should eq new_user_session_path
    end

    it 'does not show navbar' do
      visit '/'
      page.should_not have_css 'div.navbar'
    end

    context 'sign up' do

      before :each do
        visit new_user_registration_path
      end

      it 'signs up new user' do
        new_email = '<EMAIL>'
        new_password = '<PASSWORD>'
        user = FactoryGirl.build :user, email: new_email, password: <PASSWORD>
        fill_in 'Email', with: new_email
        fill_in 'Password', with: <PASSWORD>
        fill_in 'Confirm password', with: <PASSWORD>
        click_on 'Sign up'

        # test that a confirmation email is sent
        email = ActionMailer::Base.deliveries.pop
        email.present?.should be_true
        email.to.first.should eq user.email
        emailBody = Nokogiri::HTML email.body.to_s
        confirmation_link = emailBody.at_css "a[href*=\"#{confirmation_path}\"]"
        confirmation_link.present?.should be_true


        # Test that user cannot login before confirming the email address
        login_user_for_feature user
        page.should_not have_css 'div.navbar div.navbar-inner ul li a#sign_out'

        # Follow link received by email, user should get logged in
        visit confirmation_link[:href]
        page.should have_css 'div.navbar div.navbar-inner ul li a#sign_out'
        click_on 'Logout'

        # Test that user can login
        login_user_for_feature user
        page.should have_css 'div.navbar div.navbar-inner ul li a#sign_out'
      end

      it 'does not sign up user if email already registered' do
        new_password = '<PASSWORD>'
        user = FactoryGirl.build :user, email: @user.email, password: <PASSWORD>
        fill_in 'Email', with: @user.email
        fill_in 'Password', with: <PASSWORD>
        fill_in 'Confirm password', with: <PASSWORD>
        click_on 'Sign up'

        # test that a confirmation email is not sent
        email = ActionMailer::Base.deliveries.pop
        email.blank?.should be_true

        # Test that user cannot login
        login_user_for_feature user
        page.should_not have_css 'div.navbar div.navbar-inner ul li a#sign_out'
      end

      it 'does not sign up user if both password fields do not match' do
        new_email = '<EMAIL>'
        new_password = '<PASSWORD>'
        user = FactoryGirl.build :user, email: new_email, password: <PASSWORD>
        fill_in 'Email', with: @user.email
        fill_in 'Password', with: <PASSWORD>
        fill_in 'Confirm password', with: '<PASSWORD>'
        click_on 'Sign up'

        # test that a confirmation email is not sent
        email = ActionMailer::Base.deliveries.pop
        email.blank?.should be_true

        # Test that user cannot login
        login_user_for_feature user
        page.should_not have_css 'div.navbar div.navbar-inner ul li a#sign_out'
      end

    end

    context 'reset password' do

      before :each do
        visit new_user_password_path
      end

      it 'allows password reset' do
        fill_in 'Email', with: @user.email
        click_on 'Send password change email'

        # test that a confirmation email is sent
        email = ActionMailer::Base.deliveries.pop
        email.present?.should be_true
        email.to.first.should eq @user.email
        emailBody = Nokogiri::HTML email.body.to_s
        email_change_link = emailBody.at_css "a[href*=\"#{edit_user_password_path}\"]"
        email_change_link.present?.should be_true

        # follow link received by email
        visit email_change_link[:href]
        current_path.should eq edit_user_password_path

        # submit password change form
        new_password = '<PASSWORD>'
        fill_in 'New password', with: <PASSWORD>
        fill_in 'Confirm password', with: <PASSWORD>
        click_on 'Change your password'

        # after password change, user should be logged in
        current_path.should eq feeds_path
        page.should have_css 'div.navbar div.navbar-inner ul li a#sign_out'
        click_on 'Logout'

        # test that user cannot login with old password
        login_user_for_feature @user
        page.should_not have_css 'div.navbar div.navbar-inner ul li a#sign_out'

        # test that user can login with new password
        @user.password = <PASSWORD>
        login_user_for_feature @user
        page.should have_css 'div.navbar div.navbar-inner ul li a#sign_out'
      end

      it 'does not allow password change if both fields do not match' do
        fill_in 'Email', with: @user.email
        click_on 'Send password change email'

        # test that a confirmation email is sent
        email = ActionMailer::Base.deliveries.pop
        email.present?.should be_true
        email.to.first.should eq @user.email
        emailBody = Nokogiri::HTML email.body.to_s
        email_change_link = emailBody.at_css "a[href*=\"#{edit_user_password_path}\"]"
        email_change_link.present?.should be_true

        # follow link received by email
        visit email_change_link[:href]
        current_path.should eq edit_user_password_path

        # submit password change form
        new_password = '<PASSWORD>'
        fill_in 'New password', with: <PASSWORD>
        fill_in 'Confirm password', with: '<PASSWORD>'
        click_on 'Change your password'

        # after submit, user should NOT be logged in
        page.should_not have_css 'div.navbar div.navbar-inner ul li a#sign_out'

        # test that user can login with old password
        login_user_for_feature @user
        page.should have_css 'div.navbar div.navbar-inner ul li a#sign_out'
        click_on 'Logout'

        # test that user cannot login with new password
        @user.password = <PASSWORD>
        login_user_for_feature @user
        page.should_not have_css 'div.navbar div.navbar-inner ul li a#sign_out'
      end

      it 'does not send password change email to an unregistered address' do
        fill_in 'Email', with: '<EMAIL>'
        click_on 'Send password change email'

        # test that a confirmation email is not sent
        email = ActionMailer::Base.deliveries.pop
        email.blank?.should be_true
      end

    end

  end

  context 'authenticated users' do

    before :each do
      login_user_for_feature @user
    end

    it 'redirects to feeds list after a successful login' do
      current_path.should eq feeds_path
    end

    it 'does not show the login link in the main page' do
      page.should_not have_css "a#sign_in[href*=\"#{new_user_session_path}\"]"
    end

{'fraction_non_alphanumeric': 0.07810020403536613, 'fraction_numerical': 0.0, 'mean_word_length': 2.5895036615134255, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5433314', 'n_tokens_mistral': 2664, 'n_tokens_neox': 2566, 'n_words': 848}