foodsoft/spec/support/api_helper.rb

46 lines
1.3 KiB
Ruby
Raw Normal View History

2018-10-15 16:47:14 +02:00
module ApiHelper
extend ActiveSupport::Concern
included do
let(:user) { create(:user) }
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}" }
2022-11-07 17:42:32 +01:00
# TODO: not needed anymore?
def self.it_handles_invalid_token()
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
run_test!
end
end
end
2022-11-07 17:42:32 +01:00
def self.it_handles_invalid_scope()
context 'with invalid scope' do
let(:api_scopes) { ['none'] }
2022-02-20 16:15:22 +01:00
2022-11-07 17:42:32 +01:00
response 403, 'missing scope' do
run_test!
end
end
end
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
# Add authentication to parameters for {Swagger::RspecHelpers#validate}
# @param params [Hash] Query parameters
# @return Query parameters with authentication header
# @see Swagger::RspecHelpers#validate
2022-11-07 17:42:32 +01:00
# def api_auth(params = {})
# { '_headers' => { 'Authorization' => api_authorization } }.deep_merge(params)
# end
# TODO: not needed anymore
2018-10-15 16:47:14 +02:00
end