admin_users_spec.rb 5.9 KB
Newer Older
G
gitlabhq 已提交
1 2
require 'spec_helper'

3
describe "Admin::Users", feature: true  do
G
gitlabhq 已提交
4 5 6
  before { login_as :admin }

  describe "GET /admin/users" do
N
Nihad Abbasov 已提交
7
    before do
G
gitlabhq 已提交
8 9 10 11
      visit admin_users_path
    end

    it "should be ok" do
12
      expect(current_path).to eq(admin_users_path)
G
gitlabhq 已提交
13 14
    end

N
Nihad Abbasov 已提交
15
    it "should have users list" do
16 17
      expect(page).to have_content(@user.email)
      expect(page).to have_content(@user.name)
G
gitlabhq 已提交
18
    end
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

    describe 'Two-factor Authentication filters' do
      it 'counts users who have enabled 2FA' do
        create(:user, two_factor_enabled: true)

        visit admin_users_path

        page.within('.filter-two-factor-enabled small') do
          expect(page).to have_content('1')
        end
      end

      it 'filters by users who have enabled 2FA' do
        user = create(:user, two_factor_enabled: true)

        visit admin_users_path
        click_link '2FA Enabled'

        expect(page).to have_content(user.email)
      end

      it 'counts users who have not enabled 2FA' do
        create(:user, two_factor_enabled: false)

        visit admin_users_path

        page.within('.filter-two-factor-disabled small') do
          expect(page).to have_content('2') # Including admin
        end
      end

      it 'filters by users who have not enabled 2FA' do
        user = create(:user, two_factor_enabled: false)

        visit admin_users_path
        click_link '2FA Disabled'

        expect(page).to have_content(user.email)
      end
    end
G
gitlabhq 已提交
59 60
  end

N
Nihad Abbasov 已提交
61 62
  describe "GET /admin/users/new" do
    before do
G
gitlabhq 已提交
63
      visit new_admin_user_path
64
      fill_in "user_name", with: "Big Bang"
65
      fill_in "user_username", with: "bang"
66
      fill_in "user_email", with: "bigbang@mail.com"
G
gitlabhq 已提交
67 68
    end

N
Nihad Abbasov 已提交
69
    it "should create new user" do
70
      expect { click_button "Create user" }.to change {User.count}.by(1)
G
gitlabhq 已提交
71 72
    end

73 74
    it "should apply defaults to user" do
      click_button "Create user"
D
Dmitriy Zaporozhets 已提交
75
      user = User.find_by(username: 'bang')
J
Jeroen van Baarsen 已提交
76 77 78 79
      expect(user.projects_limit).
        to eq(Gitlab.config.gitlab.default_projects_limit)
      expect(user.can_create_group).
        to eq(Gitlab.config.gitlab.default_can_create_group)
80 81
    end

N
Nihad Abbasov 已提交
82
    it "should create user with valid data" do
83
      click_button "Create user"
D
Dmitriy Zaporozhets 已提交
84
      user = User.find_by(username: 'bang')
J
Jeroen van Baarsen 已提交
85 86
      expect(user.name).to eq('Big Bang')
      expect(user.email).to eq('bigbang@mail.com')
G
gitlabhq 已提交
87 88
    end

N
Nihad Abbasov 已提交
89
    it "should call send mail" do
90
      expect(Notify).to receive(:new_user_email)
91

92
      click_button "Create user"
G
gitlabhq 已提交
93 94
    end

N
Nihad Abbasov 已提交
95
    it "should send valid email to user with email & password" do
96
      click_button "Create user"
D
Dmitriy Zaporozhets 已提交
97
      user = User.find_by(username: 'bang')
98
      email = ActionMailer::Base.deliveries.last
J
Jeroen van Baarsen 已提交
99
      expect(email.subject).to have_content('Account was created')
100 101
      expect(email.text_part.body).to have_content(user.email)
      expect(email.text_part.body).to have_content('password')
M
Marin Jankovski 已提交
102
    end
G
gitlabhq 已提交
103 104
  end

N
Nihad Abbasov 已提交
105
  describe "GET /admin/users/:id" do
106
    it "should have user info" do
G
gitlabhq 已提交
107
      visit admin_users_path
108
      click_link @user.name
G
gitlabhq 已提交
109

110 111
      expect(page).to have_content(@user.email)
      expect(page).to have_content(@user.name)
G
gitlabhq 已提交
112
    end
113

114 115 116
    describe 'Impersonation' do
      let(:another_user) { create(:user) }
      before { visit admin_user_path(another_user) }
117

118 119 120 121
      context 'before impersonating' do
        it 'shows impersonate button for other users' do
          expect(page).to have_content('Impersonate')
        end
122

123 124
        it 'should not show impersonate button for admin itself' do
          visit admin_user_path(@user)
125

126
          expect(page).not_to have_content('Impersonate')
127 128 129
        end
      end

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 157
      context 'when impersonating' do
        before { click_link 'Impersonate' }

        it 'logs in as the user when impersonate is clicked' do
          page.within '.sidebar-user .username' do
            expect(page).to have_content(another_user.username)
          end
        end

        it 'sees impersonation log out icon' do
          icon = first('.fa.fa-user-secret')

          expect(icon).to_not eql nil
        end

        it 'can log out of impersonated user back to original user' do
          find(:css, 'li.impersonation a').click

          page.within '.sidebar-user .username' do
            expect(page).to have_content(@user.username)
          end
        end

        it 'is redirected back to the impersonated users page in the admin after stopping' do
          find(:css, 'li.impersonation a').click

          expect(current_path).to eql "/admin/users/#{another_user.username}"
        end
158 159 160
      end
    end

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    describe 'Two-factor Authentication status' do
      it 'shows when enabled' do
        @user.update_attribute(:two_factor_enabled, true)

        visit admin_user_path(@user)

        expect_two_factor_status('Enabled')
      end

      it 'shows when disabled' do
        visit admin_user_path(@user)

        expect_two_factor_status('Disabled')
      end

      def expect_two_factor_status(status)
        page.within('.two-factor-status') do
          expect(page).to have_content(status)
        end
      end
    end
G
gitlabhq 已提交
182 183
  end

N
Nihad Abbasov 已提交
184 185
  describe "GET /admin/users/:id/edit" do
    before do
186
      @simple_user = create(:user)
G
gitlabhq 已提交
187 188 189 190
      visit admin_users_path
      click_link "edit_user_#{@simple_user.id}"
    end

N
Nihad Abbasov 已提交
191
    it "should have user edit page" do
J
Jeroen van Baarsen 已提交
192 193
      expect(page).to have_content('Name')
      expect(page).to have_content('Password')
G
gitlabhq 已提交
194 195 196
    end

    describe "Update user" do
N
Nihad Abbasov 已提交
197
      before do
198 199
        fill_in "user_name", with: "Big Bang"
        fill_in "user_email", with: "bigbang@mail.com"
G
gitlabhq 已提交
200
        check "user_admin"
201
        click_button "Save changes"
G
gitlabhq 已提交
202 203
      end

N
Nihad Abbasov 已提交
204
      it "should show page with  new data" do
J
Jeroen van Baarsen 已提交
205 206
        expect(page).to have_content('bigbang@mail.com')
        expect(page).to have_content('Big Bang')
G
gitlabhq 已提交
207 208
      end

N
Nihad Abbasov 已提交
209
      it "should change user entry" do
G
gitlabhq 已提交
210
        @simple_user.reload
J
Jeroen van Baarsen 已提交
211
        expect(@simple_user.name).to eq('Big Bang')
212
        expect(@simple_user.is_admin?).to be_truthy
G
gitlabhq 已提交
213 214 215 216
      end
    end
  end
end