debian-mirror-gitlab/spec/features/profiles/personal_access_tokens_spec.rb

115 lines
3.8 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2016-06-22 15:30:34 +05:30
require 'spec_helper'
2018-03-17 18:26:18 +05:30
describe 'Profile > Personal Access Tokens', :js do
2016-06-22 15:30:34 +05:30
let(:user) { create(:user) }
def active_personal_access_tokens
2017-08-17 22:00:37 +05:30
find(".table.active-tokens")
2016-06-22 15:30:34 +05:30
end
2017-09-10 17:25:29 +05:30
def no_personal_access_tokens_message
find(".settings-message")
2016-06-22 15:30:34 +05:30
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)
2017-09-10 17:25:29 +05:30
2016-06-22 15:30:34 +05:30
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
2017-09-10 17:25:29 +05:30
sign_in(user)
2016-06-22 15:30:34 +05:30
end
describe "token creation" do
2017-08-17 22:00:37 +05:30
it "allows creation of a personal access token" do
name = 'My PAT'
2016-06-22 15:30:34 +05:30
visit profile_personal_access_tokens_path
2017-08-17 22:00:37 +05:30
fill_in "Name", with: name
2016-06-22 15:30:34 +05:30
# Set date to 1st of next month
2018-03-17 18:26:18 +05:30
find_field("Expires at").click
2017-08-17 22:00:37 +05:30
find(".pika-next").click
2016-06-22 15:30:34 +05:30
click_on "1"
2017-08-17 22:00:37 +05:30
# Scopes
check "api"
check "read_user"
click_on "Create personal access token"
2018-12-05 23:21:45 +05:30
2017-08-17 22:00:37 +05:30
expect(active_personal_access_tokens).to have_text(name)
expect(active_personal_access_tokens).to have_text('In')
expect(active_personal_access_tokens).to have_text('api')
expect(active_personal_access_tokens).to have_text('read_user')
2018-12-05 23:21:45 +05:30
expect(created_personal_access_token).not_to be_empty
2016-06-22 15:30:34 +05:30
end
context "when creation fails" do
it "displays an error message" do
disallow_personal_access_token_saves!
visit profile_personal_access_tokens_path
2017-08-17 22:00:37 +05:30
fill_in "Name", with: 'My PAT'
2016-06-22 15:30:34 +05:30
2017-08-17 22:00:37 +05:30
expect { click_on "Create personal access token" }.not_to change { PersonalAccessToken.count }
2016-06-22 15:30:34 +05:30
expect(page).to have_content("Name cannot be nil")
2018-12-05 23:21:45 +05:30
expect(page).not_to have_selector("#created-personal-access-token")
2016-06-22 15:30:34 +05:30
end
end
end
2017-08-17 22:00:37 +05:30
describe 'active tokens' do
let!(:impersonation_token) { create(:personal_access_token, :impersonation, user: user) }
let!(:personal_access_token) { create(:personal_access_token, user: user) }
it 'only shows personal access tokens' do
visit profile_personal_access_tokens_path
expect(active_personal_access_tokens).to have_text(personal_access_token.name)
expect(active_personal_access_tokens).not_to have_text(impersonation_token.name)
end
end
2016-06-22 15:30:34 +05:30
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
2018-03-17 18:26:18 +05:30
accept_confirm { click_on "Revoke" }
2016-06-22 15:30:34 +05:30
2017-09-10 17:25:29 +05:30
expect(page).to have_selector(".settings-message")
2020-05-24 23:13:21 +05:30
expect(no_personal_access_tokens_message).to have_text("This user has no active personal access tokens.")
2016-06-22 15:30:34 +05:30
end
2017-09-10 17:25:29 +05:30
it "removes expired tokens from 'active' section" do
2016-06-22 15:30:34 +05:30
personal_access_token.update(expires_at: 5.days.ago)
visit profile_personal_access_tokens_path
2017-09-10 17:25:29 +05:30
expect(page).to have_selector(".settings-message")
2020-05-24 23:13:21 +05:30
expect(no_personal_access_tokens_message).to have_text("This user has no active personal access tokens.")
2016-06-22 15:30:34 +05:30
end
context "when revocation fails" do
it "displays an error message" do
visit profile_personal_access_tokens_path
2017-09-10 17:25:29 +05:30
allow_any_instance_of(PersonalAccessToken).to receive(:update!).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)
2016-06-22 15:30:34 +05:30
2018-03-17 18:26:18 +05:30
accept_confirm { click_on "Revoke" }
2016-06-22 15:30:34 +05:30
expect(active_personal_access_tokens).to have_text(personal_access_token.name)
expect(page).to have_content("Could not revoke")
end
end
end
end