2016-04-20 02:28:48 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2016-04-25 05:00:59 -04:00
|
|
|
describe 'Profile > Personal Access Tokens', feature: true, 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
|
|
|
|
|
|
|
|
def inactive_personal_access_tokens
|
2017-03-01 11:59:03 -05:00
|
|
|
find(".table.inactive-tokens")
|
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)
|
|
|
|
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
|
|
|
|
login_as(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "token creation" do
|
2017-03-01 11:59:03 -05:00
|
|
|
it "allows creation of a personal access token" do
|
2016-11-28 02:43:53 -05:00
|
|
|
name = FFaker::Product.brand
|
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
|
|
|
|
2016-11-28 02:43:53 -05:00
|
|
|
click_on "Create Personal Access Token"
|
|
|
|
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
|
|
|
|
fill_in "Name", with: FFaker::Product.brand
|
|
|
|
|
2016-06-09 04:38:49 -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"
|
|
|
|
|
2016-06-02 23:28:15 -04:00
|
|
|
expect(inactive_personal_access_tokens).to have_text(personal_access_token.name)
|
2016-04-20 02:28:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "moves expired tokens to the 'inactive' 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
|
|
|
|
|
2016-06-02 23:28:15 -04:00
|
|
|
expect(inactive_personal_access_tokens).to have_text(personal_access_token.name)
|
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
|
|
|
|
disallow_personal_access_token_saves!
|
|
|
|
visit profile_personal_access_tokens_path
|
|
|
|
|
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
|