invites_spec.rb 7.51 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5 6 7 8 9
require 'spec_helper'

describe 'Invites' do
  let(:user) { create(:user) }
  let(:owner) { create(:user, name: 'John Doe') }
  let(:group) { create(:group, name: 'Owned') }
  let(:project) { create(:project, :repository, namespace: group) }
10
  let(:group_invite) { group.group_members.invite.last }
11 12

  before do
Alex Buijs's avatar
Alex Buijs committed
13
    stub_feature_flags(invisible_captcha: false)
14
    project.add_maintainer(owner)
15 16
    group.add_user(owner, Gitlab::Access::OWNER)
    group.add_developer('user@example.com', owner)
17 18 19
    group_invite.generate_invite_token!
  end

Alex Buijs's avatar
Alex Buijs committed
20
  def confirm_email(new_user)
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    new_user_token = User.find_by_email(new_user.email).confirmation_token

    visit user_confirmation_path(confirmation_token: new_user_token)
  end

  def fill_in_sign_up_form(new_user)
    fill_in 'new_user_name',                with: new_user.name
    fill_in 'new_user_username',            with: new_user.username
    fill_in 'new_user_email',               with: new_user.email
    fill_in 'new_user_email_confirmation',  with: new_user.email
    fill_in 'new_user_password',            with: new_user.password
    click_button "Register"
  end

  def fill_in_sign_in_form(user)
    fill_in 'user_login', with: user.email
    fill_in 'user_password', with: user.password
    check 'user_remember_me'
    click_button 'Sign in'
40 41 42 43
  end

  context 'when signed out' do
    before do
44
      visit invite_path(group_invite.raw_invite_token)
45 46 47 48 49 50 51 52
    end

    it 'renders sign in page with sign in notice' do
      expect(current_path).to eq(new_user_session_path)
      expect(page).to have_content('To accept this invitation, sign in')
    end

    it 'sign in and redirects to invitation page' do
53
      fill_in_sign_in_form(user)
54

55
      expect(current_path).to eq(invite_path(group_invite.raw_invite_token))
56 57 58 59 60 61 62 63 64 65 66 67 68 69
      expect(page).to have_content(
        'You have been invited by John Doe to join group Owned as Developer.'
      )
      expect(page).to have_link('Accept invitation')
      expect(page).to have_link('Decline')
    end
  end

  context 'when signed in as an exists member' do
    before do
      sign_in(owner)
    end

    it 'shows message user already a member' do
70
      visit invite_path(group_invite.raw_invite_token)
71 72 73 74 75 76 77
      expect(page).to have_content('However, you are already a member of this group.')
    end
  end

  describe 'accepting the invitation' do
    before do
      sign_in(user)
78
      visit invite_path(group_invite.raw_invite_token)
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    end

    it 'grants access and redirects to group page' do
      page.click_link 'Accept invitation'
      expect(current_path).to eq(group_path(group))
      expect(page).to have_content(
        'You have been granted Developer access to group Owned.'
      )
    end
  end

  describe 'declining the application' do
    context 'when signed in' do
      before do
        sign_in(user)
94
        visit invite_path(group_invite.raw_invite_token)
95 96 97 98 99 100 101 102 103 104 105 106 107
      end

      it 'declines application and redirects to dashboard' do
        page.click_link 'Decline'
        expect(current_path).to eq(dashboard_projects_path)
        expect(page).to have_content(
          'You have declined the invitation to join group Owned.'
        )
      end
    end

    context 'when signed out' do
      before do
108
        visit decline_invite_path(group_invite.raw_invite_token)
109 110 111 112 113 114 115 116 117 118
      end

      it 'declines application and redirects to sign in page' do
        expect(current_path).to eq(new_user_session_path)
        expect(page).to have_content(
          'You have declined the invitation to join group Owned.'
        )
      end
    end
  end
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

  describe 'invite an user using their email address' do
    let(:new_user) { build_stubbed(:user) }
    let(:invite_email) { new_user.email }
    let(:group_invite) { create(:group_member, :invited, group: group, invite_email: invite_email) }
    let!(:project_invite) { create(:project_member, :invited, project: project, invite_email: invite_email) }

    before do
      stub_application_setting(send_user_confirmation_email: send_email_confirmation)
      visit invite_path(group_invite.raw_invite_token)
    end

    context 'email confirmation disabled' do
      let(:send_email_confirmation) { false }

      it 'signs up and redirects to the dashboard page with all the projects/groups invitations automatically accepted' do
        fill_in_sign_up_form(new_user)

        expect(current_path).to eq(dashboard_projects_path)
        expect(page).to have_content(project.full_name)
        visit group_path(group)
        expect(page).to have_content(group.full_name)
      end

      context 'the user sign-up using a different email address' do
        let(:invite_email) { build_stubbed(:user).email }

        it 'signs up and redirects to the invitation page' do
          fill_in_sign_up_form(new_user)

          expect(current_path).to eq(invite_path(group_invite.raw_invite_token))
        end
      end
    end

    context 'email confirmation enabled' do
      let(:send_email_confirmation) { true }

Alex Buijs's avatar
Alex Buijs committed
157 158 159 160 161
      context 'when soft email confirmation is not enabled' do
        before do
          # stub_feature_flags(soft_email_confirmation: false)
          allow(User).to receive(:allow_unconfirmed_access_for).and_return 0
        end
162

Alex Buijs's avatar
Alex Buijs committed
163 164 165 166 167 168 169 170 171 172
        it 'signs up and redirects to root page with all the project/groups invitation automatically accepted' do
          fill_in_sign_up_form(new_user)
          confirm_email(new_user)
          fill_in_sign_in_form(new_user)

          expect(current_path).to eq(root_path)
          expect(page).to have_content(project.full_name)
          visit group_path(group)
          expect(page).to have_content(group.full_name)
        end
173 174
      end

Alex Buijs's avatar
Alex Buijs committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
      context 'when soft email confirmation is enabled' do
        before do
          allow(User).to receive(:allow_unconfirmed_access_for).and_return 2.days
        end

        it 'signs up and redirects to root page with all the project/groups invitation automatically accepted' do
          fill_in_sign_up_form(new_user)
          confirm_email(new_user)

          expect(current_path).to eq(root_path)
          expect(page).to have_content(project.full_name)
          visit group_path(group)
          expect(page).to have_content(group.full_name)
        end
      end

      it "doesn't accept invitations until the user confirms his email" do
192 193 194 195 196 197 198 199 200 201
        fill_in_sign_up_form(new_user)
        sign_in(owner)

        visit project_project_members_path(project)
        expect(page).to have_content 'Invited'
      end

      context 'the user sign-up using a different email address' do
        let(:invite_email) { build_stubbed(:user).email }

Alex Buijs's avatar
Alex Buijs committed
202 203 204 205 206
        context 'when soft email confirmation is not enabled' do
          before do
            stub_feature_flags(soft_email_confirmation: false)
            allow(User).to receive(:allow_unconfirmed_access_for).and_return 0
          end
207

Alex Buijs's avatar
Alex Buijs committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
          it 'signs up and redirects to the invitation page' do
            fill_in_sign_up_form(new_user)
            confirm_email(new_user)
            fill_in_sign_in_form(new_user)

            expect(current_path).to eq(invite_path(group_invite.raw_invite_token))
          end
        end

        context 'when soft email confirmation is enabled' do
          before do
            stub_feature_flags(soft_email_confirmation: true)
            allow(User).to receive(:allow_unconfirmed_access_for).and_return 2.days
          end

          it 'signs up and redirects to the invitation page' do
            fill_in_sign_up_form(new_user)

            expect(current_path).to eq(invite_path(group_invite.raw_invite_token))
          end
228 229 230 231
        end
      end
    end
  end
232
end