2016-04-25 05:00:59 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe PersonalAccessToken do
|
2017-02-23 12:47:06 -05:00
|
|
|
describe '.build' do
|
|
|
|
let(:personal_access_token) { build(:personal_access_token) }
|
2017-03-01 11:59:03 -05:00
|
|
|
let(:invalid_personal_access_token) { build(:personal_access_token, :invalid) }
|
2017-02-23 12:47:06 -05:00
|
|
|
|
|
|
|
it 'is a valid personal access token' do
|
|
|
|
expect(personal_access_token).to be_valid
|
2016-04-25 05:00:59 -04:00
|
|
|
end
|
|
|
|
|
2017-02-23 12:47:06 -05:00
|
|
|
it 'ensures that the token is generated' do
|
|
|
|
invalid_personal_access_token.save!
|
|
|
|
|
|
|
|
expect(invalid_personal_access_token).to be_valid
|
|
|
|
expect(invalid_personal_access_token.token).not_to be_nil
|
2016-04-25 05:00:59 -04:00
|
|
|
end
|
|
|
|
end
|
2017-02-27 13:56:54 -05:00
|
|
|
|
2016-12-27 11:26:57 -05:00
|
|
|
describe ".active?" do
|
|
|
|
let(:active_personal_access_token) { build(:personal_access_token) }
|
2017-03-01 11:59:03 -05:00
|
|
|
let(:revoked_personal_access_token) { build(:personal_access_token, :revoked) }
|
|
|
|
let(:expired_personal_access_token) { build(:personal_access_token, :expired) }
|
2016-12-27 11:26:57 -05:00
|
|
|
|
|
|
|
it "returns false if the personal_access_token is revoked" do
|
|
|
|
expect(revoked_personal_access_token).not_to be_active
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false if the personal_access_token is expired" do
|
|
|
|
expect(expired_personal_access_token).not_to be_active
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true if the personal_access_token is not revoked and not expired" do
|
|
|
|
expect(active_personal_access_token).to be_active
|
|
|
|
end
|
|
|
|
end
|
2017-01-31 05:21:29 -05:00
|
|
|
|
2017-05-31 09:55:12 -04:00
|
|
|
describe 'revoke!' do
|
|
|
|
let(:active_personal_access_token) { create(:personal_access_token) }
|
|
|
|
|
|
|
|
it 'revokes the token' do
|
|
|
|
active_personal_access_token.revoke!
|
|
|
|
|
2017-08-04 09:17:20 -04:00
|
|
|
expect(active_personal_access_token).to be_revoked
|
2017-05-31 09:55:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-06 10:39:35 -05:00
|
|
|
context "validations" do
|
|
|
|
let(:personal_access_token) { build(:personal_access_token) }
|
|
|
|
|
|
|
|
it "requires at least one scope" do
|
|
|
|
personal_access_token.scopes = []
|
|
|
|
|
|
|
|
expect(personal_access_token).not_to be_valid
|
|
|
|
expect(personal_access_token.errors[:scopes].first).to eq "can't be blank"
|
|
|
|
end
|
|
|
|
|
2017-01-31 05:21:29 -05:00
|
|
|
it "allows creating a token with API scopes" do
|
|
|
|
personal_access_token.scopes = [:api, :read_user]
|
|
|
|
|
|
|
|
expect(personal_access_token).to be_valid
|
|
|
|
end
|
|
|
|
|
2017-08-04 09:17:20 -04:00
|
|
|
context 'when registry is disabled' do
|
|
|
|
before do
|
|
|
|
stub_container_registry_config(enabled: false)
|
|
|
|
end
|
2017-05-31 09:55:12 -04:00
|
|
|
|
2017-08-04 09:17:20 -04:00
|
|
|
it "rejects creating a token with read_registry scope" do
|
|
|
|
personal_access_token.scopes = [:read_registry]
|
|
|
|
|
|
|
|
expect(personal_access_token).not_to be_valid
|
|
|
|
expect(personal_access_token.errors[:scopes].first).to eq "can only contain available scopes"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows revoking a token with read_registry scope" do
|
|
|
|
personal_access_token.scopes = [:read_registry]
|
|
|
|
|
|
|
|
personal_access_token.revoke!
|
|
|
|
|
|
|
|
expect(personal_access_token).to be_revoked
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when registry is enabled' do
|
|
|
|
before do
|
|
|
|
stub_container_registry_config(enabled: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows creating a token with read_registry scope" do
|
|
|
|
personal_access_token.scopes = [:read_registry]
|
|
|
|
|
|
|
|
expect(personal_access_token).to be_valid
|
|
|
|
end
|
2017-05-31 09:55:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "rejects creating a token with unavailable scopes" do
|
2017-01-31 05:21:29 -05:00
|
|
|
personal_access_token.scopes = [:openid, :api]
|
|
|
|
|
|
|
|
expect(personal_access_token).not_to be_valid
|
2017-05-31 09:55:12 -04:00
|
|
|
expect(personal_access_token.errors[:scopes].first).to eq "can only contain available scopes"
|
2017-01-31 05:21:29 -05:00
|
|
|
end
|
|
|
|
end
|
2016-04-25 05:00:59 -04:00
|
|
|
end
|