require 'spec_helper'

describe 'Signing in' do
  context 'when visiting a protected page' do
    context 'with valid credentials' do
      context 'when user exists locally' do
        it 'should sign in the user successfully'
        it 'should redirect the user to the requested path'
        it 'should display name of the current user'
        it 'should display the email of the current user'
      end

      context 'when user does not exist locally' do
        it 'should redirect the user to the registration page'
        it 'should display an informative message'
        it 'should prefill the Email field'
      end
    end

    context 'with invalid credentials' do
      it 'should display an error'
    end
  end

  context 'when clicking a login link' do
    it 'should sign in the user successfully'
    it 'should redirect the user to the root path'
    it 'should display the name of the current user'
  end
end

require 'spec_helper'

describe 'Signing in' do
  context 'when visiting a protected page' do
    let(:protected_path) { edit_user_registration_path(user) }

    context 'with valid credentials' do
      before do
        stub_g5_omniauth(user)
        visit protected_path
      end

      context 'when user exists locally' do
        let(:user) { create(:user) }

        it 'should sign in the user successfully' do
          expect(page).to have_content('Signed in successfully.')
        end

        it 'should redirect the user to the requested path' do
          expect(current_path).to eq(protected_path)
        end
      end

      context 'when user does not exist locally' do
        let(:user) { build(:user) }

        it 'should display an informative message' do
          expect(page).to have_content('You must sign up before continuing.')
        end

        it 'should redirect the user to the registration page' do
          expect(current_path).to eq(new_user_registration_path)
        end

        it 'should prefill the Email field' do
          expect(find_field('Email').value).to eq(user.email)
        end
      end
    end

    context 'with invalid credentials' do
      before do
        stub_g5_invalid_credentials
        visit protected_path
      end

      let(:user) { create(:user) }

      it 'should display an error' do
        expect(page).to have_content('Invalid email or password.')
      end
    end
  end

  context 'when clicking a login link' do
    before do
      visit root_path
      stub_g5_omniauth(user)
      click_link 'Login'
    end

    let(:user) { create(:user) }

    it 'should sign in the user successfully' do
      expect(page).to have_content('Signed in successfully.')
    end

    it 'should redirect the user to the root path' do
      expect(current_path).to eq(root_path)
    end
  end
end

Implement failing feature specs around sign in
Implement failing feature specs around sign in
