summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorMarkus Koller <markus-koller@gmx.ch>2017-02-06 16:39:35 +0100
committerAlexis Reigel <mail@koffeinfrei.org>2017-03-07 15:00:29 +0100
commit8699c8338f21404aa08c9a141768201ed02b2c93 (patch)
tree168b3277c3c23a49268ec11dc38ed284ee610825 /spec
parenteefbc837301acc49a33617063faafa97adee307e (diff)
downloadgitlab-ce-8699c8338f21404aa08c9a141768201ed02b2c93.tar.gz
Require explicit scopes on personal access tokens
Gitlab::Auth and API::APIGuard already check for at least one valid scope on personal access tokens, so if the scopes are empty the token will always fail validation.
Diffstat (limited to 'spec')
-rw-r--r--spec/controllers/profiles/personal_access_tokens_spec.rb29
-rw-r--r--spec/models/personal_access_token_spec.rb14
2 files changed, 18 insertions, 25 deletions
diff --git a/spec/controllers/profiles/personal_access_tokens_spec.rb b/spec/controllers/profiles/personal_access_tokens_spec.rb
index 9d5f4c99f6d..19572ce53b7 100644
--- a/spec/controllers/profiles/personal_access_tokens_spec.rb
+++ b/spec/controllers/profiles/personal_access_tokens_spec.rb
@@ -2,6 +2,7 @@ require 'spec_helper'
describe Profiles::PersonalAccessTokensController do
let(:user) { create(:user) }
+ let(:token_attributes) { attributes_for(:personal_access_token) }
describe '#create' do
def created_token
@@ -10,40 +11,24 @@ describe Profiles::PersonalAccessTokensController do
before { sign_in(user) }
- it "allows creation of a token" do
- name = FFaker::Product.brand
+ it "allows creation of a token with scopes" do
+ scopes = %w[api read_user]
- post :create, personal_access_token: { name: name }
+ post :create, personal_access_token: token_attributes.merge(scopes: scopes)
expect(created_token).not_to be_nil
- expect(created_token.name).to eq(name)
- expect(created_token.expires_at).to be_nil
+ expect(created_token.name).to eq(token_attributes[:name])
+ expect(created_token.scopes).to eq(scopes)
expect(PersonalAccessToken.active).to include(created_token)
end
it "allows creation of a token with an expiry date" do
expires_at = 5.days.from_now
- post :create, personal_access_token: { name: FFaker::Product.brand, expires_at: expires_at }
+ post :create, personal_access_token: token_attributes.merge(expires_at: expires_at)
expect(created_token).not_to be_nil
expect(created_token.expires_at.to_i).to eq(expires_at.to_i)
end
-
- context "scopes" do
- it "allows creation of a token with scopes" do
- post :create, personal_access_token: { name: FFaker::Product.brand, scopes: %w(api read_user) }
-
- expect(created_token).not_to be_nil
- expect(created_token.scopes).to eq(%w(api read_user))
- end
-
- it "allows creation of a token with no scopes" do
- post :create, personal_access_token: { name: FFaker::Product.brand, scopes: [] }
-
- expect(created_token).not_to be_nil
- expect(created_token.scopes).to eq([])
- end
- end
end
end
diff --git a/spec/models/personal_access_token_spec.rb b/spec/models/personal_access_token_spec.rb
index 4cc9cf02e6d..50f61ec18fd 100644
--- a/spec/models/personal_access_token_spec.rb
+++ b/spec/models/personal_access_token_spec.rb
@@ -13,19 +13,27 @@ describe PersonalAccessToken, models: true do
end
end
- describe 'validate_scopes' do
+ 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
+
it "allows creating a token with API scopes" do
- personal_access_token = build(:personal_access_token)
personal_access_token.scopes = [:api, :read_user]
expect(personal_access_token).to be_valid
end
it "rejects creating a token with non-API scopes" do
- personal_access_token = build(:personal_access_token)
personal_access_token.scopes = [:openid, :api]
expect(personal_access_token).not_to be_valid
+ expect(personal_access_token.errors[:scopes].first).to eq "can only contain API scopes"
end
end
end