2016-04-20 02:28:48 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-06-29 00:13:10 -04:00
|
|
|
describe 'Profile > Personal Access Tokens', js: true do
|
2016-04-20 02:28:48 -04:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
2016-06-01 22:57:47 -04:00
|
|
|
def active_personal_access_tokens
|
2017-03-01 11:59:03 -05:00
|
|
|
find(".table.active-tokens")
|
2016-06-01 22:57:47 -04:00
|
|
|
end
|
|
|
|
|
2017-07-14 05:36:47 -04:00
|
|
|
def no_personal_access_tokens_message
|
|
|
|
find(".settings-message")
|
2016-06-01 22:57:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def created_personal_access_token
|
2016-06-09 23:19:05 -04:00
|
|
|
find("#created-personal-access-token").value
|
2016-06-01 22:57:47 -04:00
|
|
|
end
|
|
|
|
|
2016-06-03 00:23:49 -04:00
|
|
|
def disallow_personal_access_token_saves!
|
|
|
|
allow_any_instance_of(PersonalAccessToken).to receive(:save).and_return(false)
|
2017-06-06 07:18:01 -04:00
|
|
|
|
2016-06-03 00:23:49 -04:00
|
|
|
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
|
|
|
|
|
2016-04-20 02:28:48 -04:00
|
|
|
before do
|
2017-06-21 19:44:10 -04:00
|
|
|
sign_in(user)
|
2016-04-20 02:28:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "token creation" do
|
2017-03-01 11:59:03 -05:00
|
|
|
it "allows creation of a personal access token" do
|
2017-03-23 09:08:39 -04:00
|
|
|
name = 'My PAT'
|
2016-04-20 02:28:48 -04:00
|
|
|
|
2016-06-01 22:57:47 -04:00
|
|
|
visit profile_personal_access_tokens_path
|
2016-11-28 02:43:53 -05:00
|
|
|
fill_in "Name", with: name
|
2016-04-25 05:00:59 -04:00
|
|
|
|
|
|
|
# Set date to 1st of next month
|
2016-06-15 22:54:13 -04:00
|
|
|
find_field("Expires at").trigger('focus')
|
2017-01-06 09:43:21 -05:00
|
|
|
find(".pika-next").click
|
2016-04-25 05:00:59 -04:00
|
|
|
click_on "1"
|
|
|
|
|
2016-11-28 02:43:53 -05:00
|
|
|
# Scopes
|
|
|
|
check "api"
|
|
|
|
check "read_user"
|
2016-11-22 03:57:31 -05:00
|
|
|
|
2017-04-03 19:10:06 -04:00
|
|
|
click_on "Create personal access token"
|
2016-11-28 02:43:53 -05:00
|
|
|
expect(active_personal_access_tokens).to have_text(name)
|
2017-01-01 09:34:53 -05:00
|
|
|
expect(active_personal_access_tokens).to have_text('In')
|
2016-11-28 02:43:53 -05:00
|
|
|
expect(active_personal_access_tokens).to have_text('api')
|
|
|
|
expect(active_personal_access_tokens).to have_text('read_user')
|
2016-11-22 03:57:31 -05:00
|
|
|
end
|
|
|
|
|
2016-06-03 00:23:49 -04:00
|
|
|
context "when creation fails" do
|
|
|
|
it "displays an error message" do
|
|
|
|
disallow_personal_access_token_saves!
|
|
|
|
visit profile_personal_access_tokens_path
|
2017-03-23 09:08:39 -04:00
|
|
|
fill_in "Name", with: 'My PAT'
|
2016-06-03 00:23:49 -04:00
|
|
|
|
2017-04-03 19:10:06 -04:00
|
|
|
expect { click_on "Create personal access token" }.not_to change { PersonalAccessToken.count }
|
2016-06-03 00:23:49 -04:00
|
|
|
expect(page).to have_content("Name cannot be nil")
|
|
|
|
end
|
|
|
|
end
|
2016-04-20 02:28:48 -04:00
|
|
|
end
|
|
|
|
|
2017-02-23 12:47:06 -05:00
|
|
|
describe 'active tokens' do
|
2017-03-01 11:59:03 -05:00
|
|
|
let!(:impersonation_token) { create(:personal_access_token, :impersonation, user: user) }
|
2017-02-23 12:47:06 -05:00
|
|
|
let!(:personal_access_token) { create(:personal_access_token, user: user) }
|
|
|
|
|
2017-03-01 11:59:03 -05:00
|
|
|
it 'only shows personal access tokens' do
|
2017-02-23 12:47:06 -05:00
|
|
|
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-04-20 02:28:48 -04:00
|
|
|
describe "inactive tokens" do
|
2016-06-03 00:23:49 -04:00
|
|
|
let!(:personal_access_token) { create(:personal_access_token, user: user) }
|
|
|
|
|
2016-04-20 02:28:48 -04:00
|
|
|
it "allows revocation of an active token" do
|
|
|
|
visit profile_personal_access_tokens_path
|
|
|
|
click_on "Revoke"
|
|
|
|
|
2017-07-14 05:36:47 -04:00
|
|
|
expect(page).to have_selector(".settings-message")
|
|
|
|
expect(no_personal_access_tokens_message).to have_text("This user has no active Personal Access Tokens.")
|
2016-04-20 02:28:48 -04:00
|
|
|
end
|
|
|
|
|
2017-07-14 05:36:47 -04:00
|
|
|
it "removes expired tokens from 'active' section" do
|
2016-06-03 00:23:49 -04:00
|
|
|
personal_access_token.update(expires_at: 5.days.ago)
|
2016-04-20 02:28:48 -04:00
|
|
|
visit profile_personal_access_tokens_path
|
|
|
|
|
2017-07-14 05:36:47 -04:00
|
|
|
expect(page).to have_selector(".settings-message")
|
|
|
|
expect(no_personal_access_tokens_message).to have_text("This user has no active Personal Access Tokens.")
|
2016-04-20 02:28:48 -04:00
|
|
|
end
|
2016-06-03 00:23:49 -04:00
|
|
|
|
|
|
|
context "when revocation fails" do
|
|
|
|
it "displays an error message" do
|
|
|
|
visit profile_personal_access_tokens_path
|
2017-06-06 07:18:01 -04:00
|
|
|
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-03 00:23:49 -04:00
|
|
|
|
2016-11-28 02:43:53 -05:00
|
|
|
click_on "Revoke"
|
2016-06-03 00:23:49 -04:00
|
|
|
expect(active_personal_access_tokens).to have_text(personal_access_token.name)
|
|
|
|
expect(page).to have_content("Could not revoke")
|
|
|
|
end
|
|
|
|
end
|
2016-04-20 02:28:48 -04:00
|
|
|
end
|
|
|
|
end
|