summaryrefslogtreecommitdiff
path: root/spec/features/profiles/account_spec.rb
blob: 62d8a96c1b26cc4a54d6080967f72f699112c173 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Profile > Account', :js do
  let(:user) { create(:user, username: 'foo') }

  before do
    sign_in(user)
  end

  describe 'Social sign-in' do
    context 'when an identity does not exist' do
      before do
        allow(Devise).to receive_messages(omniauth_configs: { google_oauth2: {} })
      end

      it 'allows the user to connect' do
        visit profile_account_path

        expect(page).to have_link('Connect Google', href: '/users/auth/google_oauth2')
      end
    end

    context 'when an identity already exists' do
      before do
        allow(Devise).to receive_messages(omniauth_configs: { twitter: {}, saml: {} })

        create(:identity, user: user, provider: :twitter)
        create(:identity, user: user, provider: :saml)

        visit profile_account_path
      end

      it 'allows the user to disconnect when there is an existing identity' do
        expect(page).to have_link('Disconnect Twitter', href: '/-/profile/account/unlink?provider=twitter')
      end

      it 'shows active for a provider that is not allowed to unlink' do
        expect(page).to have_content('Saml Active')
      end
    end
  end

  describe 'Change username' do
    let(:new_username) { 'bar' }
    let(:new_user_path) { "/#{new_username}" }
    let(:old_user_path) { "/#{user.username}" }

    it 'the user is accessible via the new path' do
      update_username(new_username)
      visit new_user_path
      expect(current_path).to eq(new_user_path)
      expect(find('.user-info')).to have_content(new_username)
    end

    it 'the old user path redirects to the new path' do
      update_username(new_username)
      visit old_user_path
      expect(current_path).to eq(new_user_path)
      expect(find('.user-info')).to have_content(new_username)
    end

    context 'with a project' do
      let!(:project) { create(:project, namespace: user.namespace) }
      let(:new_project_path) { "/#{new_username}/#{project.path}" }
      let(:old_project_path) { "/#{user.username}/#{project.path}" }

      before(:context) do
        TestEnv.clean_test_path
      end

      after do
        TestEnv.clean_test_path
      end

      it 'the project is accessible via the new path' do
        update_username(new_username)
        visit new_project_path
        expect(current_path).to eq(new_project_path)
        expect(find('.breadcrumbs-sub-title')).to have_content('Details')
      end

      it 'the old project path redirects to the new path' do
        update_username(new_username)
        visit old_project_path
        expect(current_path).to eq(new_project_path)
        expect(find('.breadcrumbs-sub-title')).to have_content('Details')
      end
    end
  end

  describe 'Delete account' do
    before do
      create_list(:project, number_of_projects, namespace: user.namespace)
      visit profile_account_path
    end

    context 'when there are no personal projects' do
      let(:number_of_projects) { 0 }

      it 'does not show personal projects removal message' do
        expect(page).not_to have_content(/\d personal projects? will be removed and cannot be restored/)
      end
    end

    context 'when one personal project exists' do
      let(:number_of_projects) { 1 }

      it 'does show personal project removal message' do
        expect(page).to have_content('1 personal project will be removed and cannot be restored')
      end
    end

    context 'when more than one personal projects exists' do
      let(:number_of_projects) { 3 }

      it 'shows pluralized personal project removal message' do
        expect(page).to have_content('3 personal projects will be removed and cannot be restored')
      end
    end
  end
end

def update_username(new_username)
  allow(user.namespace).to receive(:move_dir)
  visit profile_account_path

  fill_in 'username-change-input', with: new_username

  page.find('[data-testid="username-change-confirmation-modal"]').click

  page.within('.modal') do
    find('.js-modal-action-primary').click
  end

  wait_for_requests
end