<commit_before>require 'rails_helper'

feature 'Users' do

  context 'Regular authentication' do
    scenario 'Sign up' do
      visit '/'
      click_link 'Sign up'

      fill_in 'user_username',              with: 'Manuela Carmena'
      fill_in 'user_email',                 with: '<EMAIL>'
      fill_in 'user_password',              with: '<PASSWORD>'
      fill_in 'user_password_confirmation', with: '<PASSWORD>'
      fill_in 'user_captcha',               with: correct_captcha_text

      click_button 'Sign up'

      expect(page).to have_content "A message with a confirmation link has been sent to your email address. Please follow the link to activate your account."

      sent_token = /.*confirmation_token=(.*)".*/.match(ActionMailer::Base.deliveries.last.body.to_s)[1]
      visit user_confirmation_path(confirmation_token: sent_token)

      expect(page).to have_content "Your email address has been successfully confirmed"
    end

    scenario 'Errors on sign up' do
      visit '/'
      click_link 'Sign up'
      click_button 'Sign up'

      expect(page).to have_content error_message
    end

    scenario 'Sign in' do
      create(:user, email: '<EMAIL>', password: '<PASSWORD>')

      visit '/'
      click_link 'Log in'
      fill_in 'user_email',    with: '<EMAIL>'
      fill_in 'user_password', with: '<PASSWORD>'
      click_button 'Log in'

      expect(page).to have_content 'Signed in successfully.'
    end
  end

  context 'OAuth authentication' do
    context 'Twitter' do
      background do
        #request.env["devise.mapping"] = Devise.mappings[:user]
      end

      scenario 'Sign up, when email was provided by OAuth provider' do
        omniauth_twitter_hash = { 'provider' => 'twitter',
                                  'uid' => '12345',
                                  'info' => {
                                    'name' => 'manuela',
                                    'email' => '<EMAIL>',
                                    'nickname' => 'ManuelaRocks',
                                    'verified' => '1'
                                  },
                                  'extra' => { 'raw_info' =>
                                    { 'location' => 'Madrid',
                                      'name' => '<NAME>'
                                    }
                                  }
                                }

        OmniAuth.config.add_mock(:twitter, omniauth_twitter_hash)

        visit '/'
        click_link 'Sign up'

        expect do
          expect do
            expect do
              click_link 'Sign in with Twitter'
            end.not_to change { ActionMailer::Base.deliveries.size }
          end.to change { Identity.count }.by(1)
        end.to change { User.count }.by(1)

        expect(current_path).to eq(root_path)
        expect_to_be_signed_in

        user = User.last
        expect(user.username).to eq('ManuelaRocks')
        expect(user.email).to eq('<EMAIL>')
        expect(user.confirmed?).to eq(true)
      end

      scenario 'Sign up, when neither email nor nickname were provided by OAuth provider' do
        omniauth_twitter_hash = { 'provider' => 'twitter',
                                  'uid' => '12345',
                                  'info' => {
                                    'name' => 'manuela'
                                  },
                                  'extra' => { 'raw_info' =>
                                    { 'location' => 'Madrid',
                                      'name' => '<NAME>'
                                    }
                                  }
                                }

        OmniAuth.config.add_mock(:twitter, omniauth_twitter_hash)

        visit '/'
        click_link 'Sign up'

        expect do
          expect do
            expect do
              click_link 'Sign in with Twitter'
            end.not_to change { ActionMailer::Base.deliveries.size }
          end.to change { Identity.count }.by(1)
        end.to change { User.count }.by(1)

        expect(current_path).to eq(finish_signup_path)

        user = User.last
        expect(user.username).to eq('manuela-de-las-carmenas')
        expect(user.email).to eq("<EMAIL>")

        fill_in 'user_email', with: '<EMAIL>'
        click_button 'Sign up'

        sent_token = /.*confirmation_token=(.*)".*/.match(ActionMailer::Base.deliveries.last.body.to_s)[1]
        visit user_confirmation_path(confirmation_token: sent_token)

        expect(page).to have_content "Your email address has been successfully confirmed"

        expect(user.reload.email).to eq('<EMAIL>')
      end

      scenario 'Sign in, user was already signed up with OAuth' do
        user = create(:user, email: '<EMAIL>', password: '<PASSWORD>')
        identity = create(:identity, uid: '12345', provider: 'twitter', user: user)
        omniauth_twitter_hash = { 'provider' => 'twitter',
                                  'uid' => '12345',
                                  'info' => {
                                    'name' => 'manuela'
                                  }
                                }

        OmniAuth.config.add_mock(:twitter, omniauth_twitter_hash)

        visit '/'
        click_link 'Log in'

        expect do
          expect do
            click_link 'Sign in with Twitter'
          end.not_to change { Identity.count }
        end.not_to change { User.count }

        expect_to_be_signed_in
      end
    end
  end

  scenario 'Sign out' do
    user = create(:user)
    login_as(user)

    visit "/"
    click_link 'Logout'

    expect(page).to have_content 'Signed out successfully.'
  end

  scenario 'Reset password' do
    create(:user, email: '<EMAIL>')

    visit '/'
    click_link 'Log in'
    click_link 'Forgot your password?'

    fill_in 'user_email', with: '<EMAIL>'
    click_button 'Send me reset password instructions'

    expect(page).to have_content "You will receive an email with instructions on how to reset your password in a few minutes."

    sent_token = /.*reset_password_token=(.*)".*/.match(ActionMailer::Base.deliveries.last.body.to_s)[1]
    visit edit_user_password_path(reset_password_token: sent_token)

    fill_in 'user_password', with: '<PASSWORD>'
    fill_in 'user_password_confirmation', with: '<PASSWORD>'
    click_button 'Change my password'

    expect(page).to have_content "Your password has been changed successfully. You are now signed in."
  end
end
<commit_msg>Updates tests for social sign up<commit_after>require 'rails_helper'

feature 'Users' do

  context 'Regular authentication' do
    scenario 'Sign up' do
      visit '/'
      click_link 'Sign up'

      fill_in 'user_username',              with: '<NAME>'
      fill_in 'user_email',                 with: '<EMAIL>'
      fill_in 'user_password',              with: '<PASSWORD>'
      fill_in 'user_password_confirmation', with: '<PASSWORD>'
      fill_in 'user_captcha',               with: correct_captcha_text

      click_button 'Sign up'

      expect(page).to have_content "A message with a confirmation link has been sent to your email address. Please follow the link to activate your account."

      sent_token = /.*confirmation_token=(.*)".*/.match(ActionMailer::Base.deliveries.last.body.to_s)[1]
      visit user_confirmation_path(confirmation_token: sent_token)

      expect(page).to have_content "Your email address has been successfully confirmed"
    end

    scenario 'Errors on sign up' do
      visit '/'
      click_link 'Sign up'
      click_button 'Sign up'

      expect(page).to have_content error_message
    end

    scenario 'Sign in' do
      create(:user, email: '<EMAIL>', password: '<PASSWORD>')

      visit '/'
      click_link 'Log in'
      fill_in 'user_email',    with: '<EMAIL>'
      fill_in 'user_password', with: '<PASSWORD>'
      click_button 'Log in'

      expect(page).to have_content 'Signed in successfully.'
    end
  end

  context 'OAuth authentication' do
    context 'Twitter' do
      background do
        #request.env["devise.mapping"] = Devise.mappings[:user]
      end

      scenario 'Sign up, when email was provided by OAuth provider' do
        omniauth_twitter_hash = { 'provider' => 'twitter',
                                  'uid' => '12345',
                                  'info' => {
                                    'name' => 'manuela',
                                    'email' => '<EMAIL>',
                                    'nickname' => 'ManuelaRocks',
                                    'verified' => '1'
                                  },
                                  'extra' => { 'raw_info' =>
                                    { 'location' => 'Madrid',
                                      'name' => '<NAME>'
                                    }
                                  }
                                }

        OmniAuth.config.add_mock(:twitter, omniauth_twitter_hash)

        visit '/'
        click_link 'Sign up'

        expect do
          expect do
            expect do
              click_link 'Sign up with Twitter'
            end.not_to change { ActionMailer::Base.deliveries.size }
          end.to change { Identity.count }.by(1)
        end.to change { User.count }.by(1)

        expect(current_path).to eq(root_path)
        expect_to_be_signed_in

        user = User.last
        expect(user.username).to eq('ManuelaRocks')
        expect(user.email).to eq('<EMAIL>')
        expect(user.confirmed?).to eq(true)
      end

      scenario 'Sign up, when neither email nor nickname were provided by OAuth provider' do
        omniauth_twitter_hash = { 'provider' => 'twitter',
                                  'uid' => '12345',
                                  'info' => {
                                    'name' => 'manuela'
                                  },
                                  'extra' => { 'raw_info' =>
                                    { 'location' => 'Madrid',
                                      'name' => '<NAME>'
                                    }
                                  }
                                }

        OmniAuth.config.add_mock(:twitter, omniauth_twitter_hash)

        visit '/'
        click_link 'Sign up'

        expect do
          expect do
            expect do
              click_link 'Sign up with Twitter'
            end.not_to change { ActionMailer::Base.deliveries.size }
          end.to change { Identity.count }.by(1)
        end.to change { User.count }.by(1)

        expect(current_path).to eq(finish_signup_path)

        user = User.last
        expect(user.username).to eq('manuela-de-las-carmenas')
        expect(user.email).to eq("om<EMAIL>")

        fill_in 'user_email', with: '<EMAIL>'
        click_button 'Sign up'

        sent_token = /.*confirmation_token=(.*)".*/.match(ActionMailer::Base.deliveries.last.body.to_s)[1]
        visit user_confirmation_path(confirmation_token: sent_token)

        expect(page).to have_content "Your email address has been successfully confirmed"

        expect(user.reload.email).to eq('<EMAIL>')
      end

      scenario 'Sign in, user was already signed up with OAuth' do
        user = create(:user, email: '<EMAIL>', password: '<PASSWORD>')
        identity = create(:identity, uid: '12345', provider: 'twitter', user: user)
        omniauth_twitter_hash = { 'provider' => 'twitter',
                                  'uid' => '12345',
                                  'info' => {
                                    'name' => 'manuela'
                                  }
                                }

        OmniAuth.config.add_mock(:twitter, omniauth_twitter_hash)

        visit '/'
        click_link 'Log in'

        expect do
          expect do
            click_link 'Sign in with Twitter'
          end.not_to change { Identity.count }
        end.not_to change { User.count }

        expect_to_be_signed_in
      end
    end
  end

  scenario 'Sign out' do
    user = create(:user)
    login_as(user)

    visit "/"
    click_link 'Logout'

    expect(page).to have_content 'Signed out successfully.'
  end

  scenario 'Reset password' do
    create(:user, email: '<EMAIL>')

    visit '/'
    click_link 'Log in'
    click_link 'Forgot your password?'

    fill_in 'user_email', with: '<EMAIL>'
    click_button 'Send me reset password instructions'

    expect(page).to have_content "You will receive an email with instructions on how to reset your password in a few minutes."

    sent_token = /.*reset_password_token=(.*)".*/.match(ActionMailer::Base.deliveries.last.body.to_s)[1]
    visit edit_user_password_path(reset_password_token: sent_token)

    fill_in 'user_password', with: '<PASSWORD>'
    fill_in 'user_password_confirmation', with: '<PASSWORD>'
    click_button 'Change my password'

    expect(page).to have_content "Your password has been changed successfully. You are now signed in."
  end
end

{'fraction_non_alphanumeric': 0.10446153846153847, 'fraction_numerical': 0.0043076923076923075, 'mean_word_length': 1.6376546966930412, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 90, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 44, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3192108', 'n_tokens_mistral': 3672, 'n_tokens_neox': 3500, 'n_words': 900}