2016-11-22 04:04:23 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe AccessTokenValidationService do
|
2016-12-05 12:25:53 -05:00
|
|
|
describe ".include_any_scope?" do
|
2017-06-20 04:27:45 -04:00
|
|
|
let(:request) { double("request") }
|
|
|
|
|
2016-11-22 04:04:23 -05:00
|
|
|
it "returns true if the required scope is present in the token's scopes" do
|
|
|
|
token = double("token", scopes: [:api, :read_user])
|
2017-06-30 03:32:25 -04:00
|
|
|
scopes = [:api]
|
2016-11-22 04:04:23 -05:00
|
|
|
|
2017-06-26 00:14:10 -04:00
|
|
|
expect(described_class.new(token, request: request).include_any_scope?(scopes)).to be(true)
|
2016-11-22 04:04:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true if more than one of the required scopes is present in the token's scopes" do
|
|
|
|
token = double("token", scopes: [:api, :read_user, :other_scope])
|
2017-06-30 03:32:25 -04:00
|
|
|
scopes = [:api, :other_scope]
|
2016-11-22 04:04:23 -05:00
|
|
|
|
2017-06-26 00:14:10 -04:00
|
|
|
expect(described_class.new(token, request: request).include_any_scope?(scopes)).to be(true)
|
2016-11-22 04:04:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true if the list of required scopes is an exact match for the token's scopes" do
|
|
|
|
token = double("token", scopes: [:api, :read_user, :other_scope])
|
2017-06-30 03:32:25 -04:00
|
|
|
scopes = [:api, :read_user, :other_scope]
|
2016-11-22 04:04:23 -05:00
|
|
|
|
2017-06-26 00:14:10 -04:00
|
|
|
expect(described_class.new(token, request: request).include_any_scope?(scopes)).to be(true)
|
2016-11-22 04:04:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true if the list of required scopes contains all of the token's scopes, in addition to others" do
|
|
|
|
token = double("token", scopes: [:api, :read_user])
|
2017-06-30 03:32:25 -04:00
|
|
|
scopes = [:api, :read_user, :other_scope]
|
2016-11-22 04:04:23 -05:00
|
|
|
|
2017-06-26 00:14:10 -04:00
|
|
|
expect(described_class.new(token, request: request).include_any_scope?(scopes)).to be(true)
|
2016-11-22 04:04:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true if the list of required scopes is blank' do
|
|
|
|
token = double("token", scopes: [])
|
2017-06-26 00:14:10 -04:00
|
|
|
scopes = []
|
2016-11-22 04:04:23 -05:00
|
|
|
|
2017-06-26 00:14:10 -04:00
|
|
|
expect(described_class.new(token, request: request).include_any_scope?(scopes)).to be(true)
|
2016-11-22 04:04:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false if there are no scopes in common between the required scopes and the token scopes" do
|
|
|
|
token = double("token", scopes: [:api, :read_user])
|
2017-06-30 03:32:25 -04:00
|
|
|
scopes = [:other_scope]
|
2016-11-22 04:04:23 -05:00
|
|
|
|
2017-06-26 00:14:10 -04:00
|
|
|
expect(described_class.new(token, request: request).include_any_scope?(scopes)).to be(false)
|
2017-06-20 04:27:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "conditions" do
|
2017-06-23 07:18:44 -04:00
|
|
|
it "ignores any scopes whose `if` condition returns false" do
|
|
|
|
token = double("token", scopes: [:api, :read_user])
|
2017-06-28 03:12:23 -04:00
|
|
|
scopes = [API::Scope.new(:api, if: ->(_) { false })]
|
2017-06-20 04:27:45 -04:00
|
|
|
|
2017-06-26 00:14:10 -04:00
|
|
|
expect(described_class.new(token, request: request).include_any_scope?(scopes)).to be(false)
|
2017-06-23 07:18:44 -04:00
|
|
|
end
|
2017-06-20 04:27:45 -04:00
|
|
|
|
2017-06-23 07:18:44 -04:00
|
|
|
it "does not ignore scopes whose `if` condition is not set" do
|
|
|
|
token = double("token", scopes: [:api, :read_user])
|
2017-06-30 03:32:25 -04:00
|
|
|
scopes = [API::Scope.new(:api, if: ->(_) { false }), :read_user]
|
2017-06-20 04:27:45 -04:00
|
|
|
|
2017-06-26 00:14:10 -04:00
|
|
|
expect(described_class.new(token, request: request).include_any_scope?(scopes)).to be(true)
|
2017-06-23 07:18:44 -04:00
|
|
|
end
|
2017-06-20 04:27:45 -04:00
|
|
|
|
2017-06-23 07:18:44 -04:00
|
|
|
it "does not ignore scopes whose `if` condition returns true" do
|
|
|
|
token = double("token", scopes: [:api, :read_user])
|
2017-06-28 03:12:23 -04:00
|
|
|
scopes = [API::Scope.new(:api, if: ->(_) { true }), API::Scope.new(:read_user, if: ->(_) { false })]
|
2017-06-20 04:27:45 -04:00
|
|
|
|
2017-06-26 00:14:10 -04:00
|
|
|
expect(described_class.new(token, request: request).include_any_scope?(scopes)).to be(true)
|
2017-06-20 04:27:45 -04:00
|
|
|
end
|
2016-11-22 04:04:23 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|