BigW Consortium Gitlab

admin_users_impersonation_tokens_spec.rb 2.27 KB
Newer Older
1 2
require 'spec_helper'

3
describe 'Admin > Users > Impersonation Tokens', feature: true, js: true do
4 5 6
  let(:admin) { create(:admin) }
  let!(:user) { create(:user) }

7
  def active_impersonation_tokens
8
    find(".table.active-tokens")
9 10
  end

11
  def inactive_impersonation_tokens
12
    find(".table.inactive-tokens")
13 14
  end

15
  before { login_as(admin) }
16 17 18

  describe "token creation" do
    it "allows creation of a token" do
19
      name = 'Hello World'
20

21
      visit admin_user_impersonation_tokens_path(user_id: user.username)
22 23 24 25
      fill_in "Name", with: name

      # Set date to 1st of next month
      find_field("Expires at").trigger('focus')
26
      find(".pika-next").click
27 28 29 30 31 32
      click_on "1"

      # Scopes
      check "api"
      check "read_user"

33
      expect { click_on "Create impersonation token" }.to change { PersonalAccessTokensFinder.new(impersonation: true).execute.count }
34 35 36 37 38 39 40 41
      expect(active_impersonation_tokens).to have_text(name)
      expect(active_impersonation_tokens).to have_text('In')
      expect(active_impersonation_tokens).to have_text('api')
      expect(active_impersonation_tokens).to have_text('read_user')
    end
  end

  describe 'active tokens' do
42
    let!(:impersonation_token) { create(:personal_access_token, :impersonation, user: user) }
43 44 45 46 47 48 49
    let!(:personal_access_token) { create(:personal_access_token, user: user) }

    it 'only shows impersonation tokens' do
      visit admin_user_impersonation_tokens_path(user_id: user.username)

      expect(active_impersonation_tokens).to have_text(impersonation_token.name)
      expect(active_impersonation_tokens).not_to have_text(personal_access_token.name)
50 51 52 53
    end
  end

  describe "inactive tokens" do
54
    let!(:impersonation_token) { create(:personal_access_token, :impersonation, user: user) }
55 56 57

    it "allows revocation of an active impersonation token" do
      visit admin_user_impersonation_tokens_path(user_id: user.username)
58 59 60

      click_on "Revoke"

61
      expect(inactive_impersonation_tokens).to have_text(impersonation_token.name)
62 63 64
    end

    it "moves expired tokens to the 'inactive' section" do
65
      impersonation_token.update(expires_at: 5.days.ago)
66 67

      visit admin_user_impersonation_tokens_path(user_id: user.username)
68

69
      expect(inactive_impersonation_tokens).to have_text(impersonation_token.name)
70 71 72
    end
  end
end