admin_users_spec.rb 4.46 KB
Newer Older
gitlabhq's avatar
gitlabhq committed
1 2
require 'spec_helper'

3
describe "Admin::Users", feature: true  do
gitlabhq's avatar
gitlabhq committed
4 5 6
  before { login_as :admin }

  describe "GET /admin/users" do
Nihad Abbasov's avatar
Nihad Abbasov committed
7
    before do
gitlabhq's avatar
gitlabhq committed
8 9 10 11
      visit admin_users_path
    end

    it "should be ok" do
12
      expect(current_path).to eq(admin_users_path)
gitlabhq's avatar
gitlabhq committed
13 14
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
15
    it "should have users list" do
16 17
      expect(page).to have_content(@user.email)
      expect(page).to have_content(@user.name)
gitlabhq's avatar
gitlabhq committed
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
gitlabhq's avatar
gitlabhq committed
59 60
  end

Nihad Abbasov's avatar
Nihad Abbasov committed
61 62
  describe "GET /admin/users/new" do
    before do
gitlabhq's avatar
gitlabhq committed
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"
gitlabhq's avatar
gitlabhq committed
67 68
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
69
    it "should create new user" do
70
      expect { click_button "Create user" }.to change {User.count}.by(1)
gitlabhq's avatar
gitlabhq committed
71 72
    end

73 74
    it "should apply defaults to user" do
      click_button "Create user"
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
75
      user = User.find_by(username: 'bang')
Jeroen van Baarsen's avatar
Jeroen van Baarsen committed
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

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

Nihad Abbasov's avatar
Nihad Abbasov committed
89
    it "should call send mail" do
90
      expect(Notify).to receive(:new_user_email)
91

92
      click_button "Create user"
gitlabhq's avatar
gitlabhq committed
93 94
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
95
    it "should send valid email to user with email & password" do
96
      click_button "Create user"
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
97
      user = User.find_by(username: 'bang')
98
      email = ActionMailer::Base.deliveries.last
Jeroen van Baarsen's avatar
Jeroen van Baarsen committed
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')
Marin Jankovski's avatar
Marin Jankovski committed
102
    end
gitlabhq's avatar
gitlabhq committed
103 104
  end

Nihad Abbasov's avatar
Nihad Abbasov committed
105
  describe "GET /admin/users/:id" do
106
    it "should have user info" do
gitlabhq's avatar
gitlabhq committed
107
      visit admin_users_path
108
      click_link @user.name
gitlabhq's avatar
gitlabhq committed
109

110 111
      expect(page).to have_content(@user.email)
      expect(page).to have_content(@user.name)
gitlabhq's avatar
gitlabhq committed
112
    end
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

    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
gitlabhq's avatar
gitlabhq committed
135 136
  end

Nihad Abbasov's avatar
Nihad Abbasov committed
137 138
  describe "GET /admin/users/:id/edit" do
    before do
139
      @simple_user = create(:user)
gitlabhq's avatar
gitlabhq committed
140 141 142 143
      visit admin_users_path
      click_link "edit_user_#{@simple_user.id}"
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
144
    it "should have user edit page" do
Jeroen van Baarsen's avatar
Jeroen van Baarsen committed
145 146
      expect(page).to have_content('Name')
      expect(page).to have_content('Password')
gitlabhq's avatar
gitlabhq committed
147 148 149
    end

    describe "Update user" do
Nihad Abbasov's avatar
Nihad Abbasov committed
150
      before do
151 152
        fill_in "user_name", with: "Big Bang"
        fill_in "user_email", with: "bigbang@mail.com"
gitlabhq's avatar
gitlabhq committed
153
        check "user_admin"
154
        click_button "Save changes"
gitlabhq's avatar
gitlabhq committed
155 156
      end

Nihad Abbasov's avatar
Nihad Abbasov committed
157
      it "should show page with  new data" do
Jeroen van Baarsen's avatar
Jeroen van Baarsen committed
158 159
        expect(page).to have_content('bigbang@mail.com')
        expect(page).to have_content('Big Bang')
gitlabhq's avatar
gitlabhq committed
160 161
      end

Nihad Abbasov's avatar
Nihad Abbasov committed
162
      it "should change user entry" do
gitlabhq's avatar
gitlabhq committed
163
        @simple_user.reload
Jeroen van Baarsen's avatar
Jeroen van Baarsen committed
164
        expect(@simple_user.name).to eq('Big Bang')
165
        expect(@simple_user.is_admin?).to be_truthy
gitlabhq's avatar
gitlabhq committed
166 167 168 169
      end
    end
  end
end