Welcome to mirror list, hosted at ThFree Co, Russian Federation.

personal_access_tokens_spec.rb « profiles « features « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0ffeeff092158f929e1c473617bd62b9d2e8c186 (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
require 'spec_helper'

describe 'Profile > Personal Access Tokens', feature: true, js: true do
  let(:user) { create(:user) }

  def active_personal_access_tokens
    find(".table.active-personal-access-tokens")
  end

  def inactive_personal_access_tokens
    find(".table.inactive-personal-access-tokens")
  end

  def created_personal_access_token
    find("#created-personal-access-token").value
  end

  def disallow_personal_access_token_saves!
    allow_any_instance_of(PersonalAccessToken).to receive(:save).and_return(false)
    errors = ActiveModel::Errors.new(PersonalAccessToken.new).tap { |e| e.add(:name, "cannot be nil") }
    allow_any_instance_of(PersonalAccessToken).to receive(:errors).and_return(errors)
  end

  before do
    login_as(user)
  end

  describe "token creation" do
    it "allows creation of a token" do
      visit profile_personal_access_tokens_path
      fill_in "Name", with: FFaker::Product.brand

      expect {click_on "Create Personal Access Token"}.to change { PersonalAccessToken.count }.by(1)
      expect(created_personal_access_token).to eq(PersonalAccessToken.last.token)
      expect(active_personal_access_tokens).to have_text(PersonalAccessToken.last.name)
      expect(active_personal_access_tokens).to have_text("Never")
    end

    it "allows creation of a token with an expiry date" do
      visit profile_personal_access_tokens_path
      fill_in "Name", with: FFaker::Product.brand

      # Set date to 1st of next month
      find_field("Expires at").trigger('focus')
      find("a[title='Next']").click
      click_on "1"

      expect {click_on "Create Personal Access Token"}.to change { PersonalAccessToken.count }.by(1)
      expect(created_personal_access_token).to eq(PersonalAccessToken.last.token)
      expect(active_personal_access_tokens).to have_text(PersonalAccessToken.last.name)
      expect(active_personal_access_tokens).to have_text(Date.today.next_month.at_beginning_of_month.to_s(:medium))
    end

    context "scopes" do
      it "allows creation of a token with scopes" do
        visit profile_personal_access_tokens_path
        fill_in "Name", with: FFaker::Product.brand

        check "api"
        check "read_user"

        expect {click_on "Create Personal Access Token"}.to change { PersonalAccessToken.count }.by(1)
        expect(created_personal_access_token).to eq(PersonalAccessToken.last.token)
        expect(PersonalAccessToken.last.scopes).to match_array(['api', 'read_user'])
        expect(active_personal_access_tokens).to have_text('api')
        expect(active_personal_access_tokens).to have_text('read_user')
      end

      it "allows creation of a token with no scopes" do
        visit profile_personal_access_tokens_path
        fill_in "Name", with: FFaker::Product.brand

        expect {click_on "Create Personal Access Token"}.to change { PersonalAccessToken.count }.by(1)
        expect(created_personal_access_token).to eq(PersonalAccessToken.last.token)
        expect(PersonalAccessToken.last.scopes).to eq([])
        expect(active_personal_access_tokens).to have_text('no scopes')
      end
    end

    context "when creation fails" do
      it "displays an error message" do
        disallow_personal_access_token_saves!
        visit profile_personal_access_tokens_path
        fill_in "Name", with: FFaker::Product.brand

        expect { click_on "Create Personal Access Token" }.not_to change { PersonalAccessToken.count }
        expect(page).to have_content("Name cannot be nil")
      end
    end
  end

  describe "inactive tokens" do
    let!(:personal_access_token) { create(:personal_access_token, user: user) }

    it "allows revocation of an active token" do
      visit profile_personal_access_tokens_path
      click_on "Revoke"

      expect(inactive_personal_access_tokens).to have_text(personal_access_token.name)
    end

    it "moves expired tokens to the 'inactive' section" do
      personal_access_token.update(expires_at: 5.days.ago)
      visit profile_personal_access_tokens_path

      expect(inactive_personal_access_tokens).to have_text(personal_access_token.name)
    end

    context "when revocation fails" do
      it "displays an error message" do
        disallow_personal_access_token_saves!
        visit profile_personal_access_tokens_path

        expect { click_on "Revoke" }.not_to change { PersonalAccessToken.inactive.count }
        expect(active_personal_access_tokens).to have_text(personal_access_token.name)
        expect(page).to have_content("Could not revoke")
      end
    end
  end
end