2018-10-15 16:47:14 +02:00
|
|
|
module ApiHelper
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
let(:user) { create(:user) }
|
2019-02-05 20:53:02 +01:00
|
|
|
let(:api_scopes) { [] } # empty scopes for stricter testing (in reality this would be default_scopes)
|
|
|
|
let(:api_access_token) { create(:oauth2_access_token, resource_owner_id: user.id, scopes: api_scopes&.join(' ')).token }
|
2022-11-07 17:42:32 +01:00
|
|
|
let(:Authorization) { "Bearer #{api_access_token}" }
|
2019-02-05 20:53:02 +01:00
|
|
|
|
2022-11-21 13:17:14 +01:00
|
|
|
def self.it_handles_invalid_token
|
2019-02-05 20:53:02 +01:00
|
|
|
context 'with invalid access token' do
|
2022-11-07 17:42:32 +01:00
|
|
|
let(:Authorization) { 'abc' }
|
2022-02-20 16:15:22 +01:00
|
|
|
|
2022-11-07 17:42:32 +01:00
|
|
|
response 401, 'not logged-in' do
|
2022-11-21 12:49:53 +01:00
|
|
|
schema '$ref' => '#/components/schemas/Error401'
|
2022-11-07 17:42:32 +01:00
|
|
|
run_test!
|
|
|
|
end
|
2019-02-05 20:53:02 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-29 18:55:45 +01:00
|
|
|
def self.it_handles_invalid_token_with_id(class_sym)
|
|
|
|
context 'with invalid access token' do
|
|
|
|
let(:Authorization) { 'abc' }
|
|
|
|
let(:id) { create(class_sym).id }
|
|
|
|
|
|
|
|
response 401, 'not logged-in' do
|
|
|
|
schema '$ref' => '#/components/schemas/Error401'
|
|
|
|
run_test!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-12 11:30:09 +01:00
|
|
|
def self.it_handles_invalid_scope(description = "missing scope")
|
2019-02-05 20:53:02 +01:00
|
|
|
context 'with invalid scope' do
|
|
|
|
let(:api_scopes) { ['none'] }
|
2022-02-20 16:15:22 +01:00
|
|
|
|
2022-12-05 17:07:10 +01:00
|
|
|
response 403, description do
|
2022-11-21 12:49:53 +01:00
|
|
|
schema '$ref' => '#/components/schemas/Error403'
|
2022-11-07 17:42:32 +01:00
|
|
|
run_test!
|
|
|
|
end
|
2019-02-05 20:53:02 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-05 17:07:10 +01:00
|
|
|
def self.it_handles_invalid_scope_with_id(class_sym, description)
|
2022-11-29 18:55:45 +01:00
|
|
|
context 'with invalid scope' do
|
|
|
|
let(:api_scopes) { ['none'] }
|
|
|
|
let(:id) { create(class_sym).id }
|
2022-12-12 11:30:09 +01:00
|
|
|
|
2022-12-05 17:07:10 +01:00
|
|
|
response 403, description do
|
2022-11-29 18:55:45 +01:00
|
|
|
schema '$ref' => '#/components/schemas/Error403'
|
|
|
|
run_test!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-12 11:30:09 +01:00
|
|
|
def self.it_cannot_find_object(description = "not found")
|
|
|
|
let(:id) { 'invalid' }
|
|
|
|
|
|
|
|
|
|
|
|
response 404, description do
|
|
|
|
schema '$ref' => '#/components/schemas/Error404'
|
2022-12-05 17:07:10 +01:00
|
|
|
run_test!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-05 20:53:02 +01:00
|
|
|
def self.it_handles_invalid_token_and_scope(*args)
|
|
|
|
it_handles_invalid_token(*args)
|
|
|
|
it_handles_invalid_scope(*args)
|
|
|
|
end
|
2018-10-15 16:47:14 +02:00
|
|
|
end
|
|
|
|
end
|