diff --git a/Gemfile b/Gemfile index 92b05781a..23117db8e 100644 --- a/Gemfile +++ b/Gemfile @@ -22,7 +22,6 @@ gem 'net-ldap', '~> 0.16.0' # Seamless JWT authentication for Rails API gem 'jwt' -gem 'knock', github: 'nsarno/knock' # ActiveAdmin gem 'activeadmin' diff --git a/Gemfile.lock b/Gemfile.lock index ab10fc481..a8f0cdbe8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -64,15 +64,6 @@ GIT jquery-ui-rails (7.0.0) railties (>= 3.2.16) -GIT - remote: https://github.com/nsarno/knock.git - revision: 37e403a7c6d44f585b56a086245e41566a8d6fe1 - specs: - knock (2.2.0) - bcrypt (~> 3.1) - jwt (~> 2.2.1) - rails (>= 5) - GIT remote: https://github.com/senid231/excelinator.git revision: 25afcc544a6f287a2ace9018adbe6fc96ea29cb2 @@ -900,7 +891,6 @@ DEPENDENCIES jrpc! jsonapi-resources (~> 0.9.12) jwt - knock! listen matrix (~> 0.4.2) mini_racer diff --git a/app/controllers/api/rest/admin/auth_controller.rb b/app/controllers/api/rest/admin/auth_controller.rb index c7d935242..ef9d369d1 100644 --- a/app/controllers/api/rest/admin/auth_controller.rb +++ b/app/controllers/api/rest/admin/auth_controller.rb @@ -1,40 +1,50 @@ # frozen_string_literal: true -class Api::Rest::Admin::AuthController < Knock::AuthTokenController +class Api::Rest::Admin::AuthController < ApplicationController + skip_before_action :verify_authenticity_token + include Memoizable include WithPayloads + rescue_from Authentication::AdminAuth::AuthenticationError, with: :handle_authentication_error + rescue_from Authentication::AdminAuth::IpAddressNotAllowedError, with: :handle_ip_not_allowed + define_memoizable :debug_mode, apply: -> { System::ApiLogConfig.exists?(controller: self.class.name) } + before_action :authenticate + + def create + render json: { jwt: @auth_token }, status: 201 + end + def meta nil end private - def entity_name - 'AdminUser' + def authenticate + result = Authentication::AdminAuth.authenticate!( + auth_params[:username], + auth_params[:password], + remote_ip: request.remote_ip + ) + @auth_token = result.token end def auth_params - params.require(:auth).permit :username, :password + params.require(:auth).permit(:username, :password) end - def not_found + def handle_authentication_error error = JSONAPI::Exceptions::AuthenticationFailed.new render status: 401, json: { errors: error.errors.map(&:to_hash) } end - def ip_not_allowed + def handle_ip_not_allowed error = JSONAPI::Exceptions::AuthenticationFailed.new( detail: 'Your IP address is not allowed.' ) render status: 401, json: { errors: error.errors.map(&:to_hash) } end - - def authenticate - super - - ip_not_allowed unless entity.ip_allowed?(request.remote_ip) - end end diff --git a/app/controllers/concerns/admin_api_authorizable.rb b/app/controllers/concerns/admin_api_authorizable.rb index 47e06fee7..6d71b7338 100644 --- a/app/controllers/concerns/admin_api_authorizable.rb +++ b/app/controllers/concerns/admin_api_authorizable.rb @@ -4,10 +4,10 @@ module AdminApiAuthorizable extend ActiveSupport::Concern included do - include Knock::Authenticable - undef :current_admin_user + rescue_from Authorization::AdminAuth::AuthorizationError, with: :handle_authorization_error - before_action :authenticate_admin_user! + before_action :authorize + attr_reader :current_admin_user end def context @@ -27,9 +27,13 @@ def capture_user private - def authenticate_admin_user! - if !authenticate_for(::AdminUser) || !current_admin_user.ip_allowed?(request.remote_ip) - unauthorized_entity('admin_user') - end + def authorize + auth_token = params[:token] || request.headers['Authorization']&.split&.last + result = Authorization::AdminAuth.authorize!(auth_token, remote_ip: request.remote_ip) + @current_admin_user = result.entity + end + + def handle_authorization_error + head 401 end end diff --git a/app/lib/authentication/admin_auth.rb b/app/lib/authentication/admin_auth.rb new file mode 100644 index 000000000..0cb24f3f5 --- /dev/null +++ b/app/lib/authentication/admin_auth.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +module Authentication + class AdminAuth + EXPIRATION_INTERVAL = YetiConfig.api.token_lifetime.presence&.seconds&.freeze + AUDIENCE = 'admin' + AuthenticationError = Class.new(StandardError) + IpAddressNotAllowedError = Class.new(AuthenticationError) + Result = Data.define(:token) + + class << self + # @param username [String] + # @param password [String] + # @param remote_ip [String] + # @return [Authentication::AdminAuth::Result] when success + # @raise [Authentication::AdminAuth::AuthenticationError] when failed caused by invalid username or password + # @raise [Authentication::AdminAuth::IpAddressNotAllowedError] when failed caused by IP address not allowed + def authenticate!(username, password, remote_ip:) + instance = new(username, password, remote_ip: remote_ip) + instance.authenticate! + end + + # @param admin_user [AdminUser] + # @param expires_at [Time,nil] overrides default expiration + # @return [Authentication::CustomerV1Auth::Result] + def build_auth_data(admin_user, expires_at: nil) + expires_at ||= EXPIRATION_INTERVAL&.from_now + payload = { sub: admin_user.id, aud: AUDIENCE } + payload[:exp] = expires_at.to_i unless expires_at.nil? + token = JwtToken.encode(payload) + Result.new(token) + end + end + + # @param username [String] + # @param password [String] + # @param remote_ip [String] + def initialize(username, password, remote_ip:) + @username = username + @password = password + @remote_ip = remote_ip + end + + # @return [Authentication::AdminAuth::Result] when success + # @raise [Authentication::AdminAuth::AuthenticationError] when failed caused by invalid username or password + # @raise [Authentication::AdminAuth::IpAddressNotAllowedError] when failed caused by IP address not allowed + def authenticate! + admin_user = AdminUser.find_by(username: @username) + raise AuthenticationError if admin_user.nil? + raise AuthenticationError unless admin_user.authenticate(@password) + raise IpAddressNotAllowedError unless admin_user.ip_allowed?(@remote_ip) + + self.class.build_auth_data(admin_user) + end + end +end diff --git a/app/lib/authorization/admin_auth.rb b/app/lib/authorization/admin_auth.rb new file mode 100644 index 000000000..71a0eef19 --- /dev/null +++ b/app/lib/authorization/admin_auth.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +module Authorization + class AdminAuth + Result = Struct.new(:entity) + AuthorizationError = Class.new(StandardError) + + class << self + # @param token [String] + # @param remote_ip [String] + # @return [Authorization::AdminAuth::Result] when success + # @raise [Authorization::AdminAuth::AuthenticationError] when failed + def authorize!(token, remote_ip:) + new(token, remote_ip:).authorize! + end + end + + # @param token [String] + # @param remote_ip [String] + def initialize(token, remote_ip:) + @token = token + @remote_ip = remote_ip + end + + # @return [Authorization::AdminAuth::Result] when success + # @raise [Authorization::AdminAuth::AuthenticationError] when failed + def authorize! + verify_expiration = Authentication::AdminAuth::EXPIRATION_INTERVAL.present? + audience = Authentication::AdminAuth::AUDIENCE + payload = JwtToken.decode(@token, verify_expiration: verify_expiration, aud: audience) + raise AuthorizationError if payload.nil? + + admin_user = AdminUser.find_by(id: payload[:sub]) + raise AuthorizationError if admin_user.nil? + raise AuthorizationError unless admin_user.ip_allowed?(@remote_ip) + + Result.new(admin_user) + end + end +end diff --git a/config/initializers/knock.rb b/config/initializers/knock.rb deleted file mode 100644 index 0600a26f9..000000000 --- a/config/initializers/knock.rb +++ /dev/null @@ -1,59 +0,0 @@ -# frozen_string_literal: true - -require 'rails/generators' -Knock.setup do |config| - ## Expiration claim - ## ---------------- - ## - ## How long before a token is expired. If nil is provided, token will - ## last forever. - ## - config.token_lifetime = YetiConfig.api.token_lifetime.presence&.seconds - - ## Audience claim - ## -------------- - ## - ## Configure the audience claim to identify the recipients that the token - ## is intended for. - ## - ## Default: - # config.token_audience = nil - - ## If using Auth0, uncomment the line below - # config.token_audience = -> { Rails.application.secrets.auth0_client_id } - - ## Signature algorithm - ## ------------------- - ## - ## Configure the algorithm used to encode the token - ## - ## Default: - # config.token_signature_algorithm = 'HS256' - - ## Signature key - ## ------------- - ## - ## Configure the key used to sign tokens. - ## - ## Default: - # config.token_secret_signature_key = -> { Rails.application.secrets.secret_key_base } - - ## If using Auth0, uncomment the line below - # config.token_secret_signature_key = -> { JWT.base64url_decode Rails.application.secrets.auth0_client_secret } - - ## Public key - ## ---------- - ## - ## Configure the public key used to decode tokens, if required. - ## - ## Default: - # config.token_public_key = nil - - ## Exception Class - ## --------------- - ## - ## Configure the exception to be used when user cannot be found. - ## - ## Default: - # config.not_found_exception_class_name = 'ActiveRecord::RecordNotFound' -end diff --git a/spec/acceptance/rest/admin/api/accounting_profiles_spec.rb b/spec/acceptance/rest/admin/api/accounting_profiles_spec.rb index 4fe915310..b03b06ce8 100644 --- a/spec/acceptance/rest/admin/api/accounting_profiles_spec.rb +++ b/spec/acceptance/rest/admin/api/accounting_profiles_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Accounting profiles' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'accounting-profiles' } required_params = %i[name server port secret timeout attempts] diff --git a/spec/acceptance/rest/admin/api/accounts_spec.rb b/spec/acceptance/rest/admin/api/accounts_spec.rb index 4051d9067..f3d4526d6 100644 --- a/spec/acceptance/rest/admin/api/accounts_spec.rb +++ b/spec/acceptance/rest/admin/api/accounts_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Accounts' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'accounts' } required_params = %i[name min-balance max-balance] diff --git a/spec/acceptance/rest/admin/api/active_calls_spec.rb b/spec/acceptance/rest/admin/api/active_calls_spec.rb index 071558f19..c3057109e 100644 --- a/spec/acceptance/rest/admin/api/active_calls_spec.rb +++ b/spec/acceptance/rest/admin/api/active_calls_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'ActiveCalls' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'active-calls' } include_context :active_calls_stub_helpers do diff --git a/spec/acceptance/rest/admin/api/api_accesses_spec.rb b/spec/acceptance/rest/admin/api/api_accesses_spec.rb index e3930dff2..fef4f526a 100644 --- a/spec/acceptance/rest/admin/api/api_accesses_spec.rb +++ b/spec/acceptance/rest/admin/api/api_accesses_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'ApiAccesses' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'api-accesses' } required_params = %i[customer-id login password] diff --git a/spec/acceptance/rest/admin/api/auth_profiles_spec.rb b/spec/acceptance/rest/admin/api/auth_profiles_spec.rb index 1b1c1f81c..8a2729311 100644 --- a/spec/acceptance/rest/admin/api/auth_profiles_spec.rb +++ b/spec/acceptance/rest/admin/api/auth_profiles_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Auth profiles' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'auth-profiles' } required_params = %i[name server port secret timeout attempts] diff --git a/spec/acceptance/rest/admin/api/cdr_exports_spec.rb b/spec/acceptance/rest/admin/api/cdr_exports_spec.rb index d81c744cc..1b83fdb22 100644 --- a/spec/acceptance/rest/admin/api/cdr_exports_spec.rb +++ b/spec/acceptance/rest/admin/api/cdr_exports_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Cdr Exports' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'cdr-exports' } let(:country) { System::Country.take! } diff --git a/spec/acceptance/rest/admin/api/cdrs_spec.rb b/spec/acceptance/rest/admin/api/cdrs_spec.rb index 9db89052f..4c4716d9b 100644 --- a/spec/acceptance/rest/admin/api/cdrs_spec.rb +++ b/spec/acceptance/rest/admin/api/cdrs_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Cdrs' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'cdrs' } get '/api/rest/admin/cdrs' do diff --git a/spec/acceptance/rest/admin/api/codec_groups_spec.rb b/spec/acceptance/rest/admin/api/codec_groups_spec.rb index 7ab3be509..a4617bf78 100644 --- a/spec/acceptance/rest/admin/api/codec_groups_spec.rb +++ b/spec/acceptance/rest/admin/api/codec_groups_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Codec groups' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'codec-groups' } get '/api/rest/admin/codec-groups' do diff --git a/spec/acceptance/rest/admin/api/contacts_spec.rb b/spec/acceptance/rest/admin/api/contacts_spec.rb index bce9aec60..3fcd523ec 100644 --- a/spec/acceptance/rest/admin/api/contacts_spec.rb +++ b/spec/acceptance/rest/admin/api/contacts_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Contacts' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'contacts' } required_params = %i[email] diff --git a/spec/acceptance/rest/admin/api/contractors_spec.rb b/spec/acceptance/rest/admin/api/contractors_spec.rb index c01a86bf1..875804804 100644 --- a/spec/acceptance/rest/admin/api/contractors_spec.rb +++ b/spec/acceptance/rest/admin/api/contractors_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Contractors' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'contractors' } required_params = %i[name vendor customer] diff --git a/spec/acceptance/rest/admin/api/customers_auths_spec.rb b/spec/acceptance/rest/admin/api/customers_auths_spec.rb index 539e7deef..b13abafd6 100644 --- a/spec/acceptance/rest/admin/api/customers_auths_spec.rb +++ b/spec/acceptance/rest/admin/api/customers_auths_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Customer Auths' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'customers-auths' } required_params = %i[name ip] diff --git a/spec/acceptance/rest/admin/api/destination_next_rates_spec.rb b/spec/acceptance/rest/admin/api/destination_next_rates_spec.rb index 68379d3d8..cad64a7c1 100644 --- a/spec/acceptance/rest/admin/api/destination_next_rates_spec.rb +++ b/spec/acceptance/rest/admin/api/destination_next_rates_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Destination next rates' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'destination-next-rates' } let(:destination) { create(:destination) } let(:destination_id) { destination.id } diff --git a/spec/acceptance/rest/admin/api/destinations_spec.rb b/spec/acceptance/rest/admin/api/destinations_spec.rb index 2e661ada4..eed38c724 100644 --- a/spec/acceptance/rest/admin/api/destinations_spec.rb +++ b/spec/acceptance/rest/admin/api/destinations_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Destinations' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'destinations' } required_params = %i[ diff --git a/spec/acceptance/rest/admin/api/dialpeer_next_rates_spec.rb b/spec/acceptance/rest/admin/api/dialpeer_next_rates_spec.rb index ef2dc8594..08bf5a4bf 100644 --- a/spec/acceptance/rest/admin/api/dialpeer_next_rates_spec.rb +++ b/spec/acceptance/rest/admin/api/dialpeer_next_rates_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Dialpeer next rates' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'dialpeer-next-rates' } let(:dialpeer) { create(:dialpeer) } let(:dialpeer_id) { dialpeer.id } diff --git a/spec/acceptance/rest/admin/api/dialpeers_spec.rb b/spec/acceptance/rest/admin/api/dialpeers_spec.rb index 89fd94fff..1b134441c 100644 --- a/spec/acceptance/rest/admin/api/dialpeers_spec.rb +++ b/spec/acceptance/rest/admin/api/dialpeers_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Dialpeers' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'dialpeers' } required_params = %i[ diff --git a/spec/acceptance/rest/admin/api/disconnect_policies_spec.rb b/spec/acceptance/rest/admin/api/disconnect_policies_spec.rb index 6a8caabf3..85679cc37 100644 --- a/spec/acceptance/rest/admin/api/disconnect_policies_spec.rb +++ b/spec/acceptance/rest/admin/api/disconnect_policies_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Disconnect policies' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'disconnect-policies' } get '/api/rest/admin/disconnect-policies' do diff --git a/spec/acceptance/rest/admin/api/diversion_policies_spec.rb b/spec/acceptance/rest/admin/api/diversion_policies_spec.rb index 8ec5b43cd..386d312a1 100644 --- a/spec/acceptance/rest/admin/api/diversion_policies_spec.rb +++ b/spec/acceptance/rest/admin/api/diversion_policies_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Diversion policies' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'diversion-policies' } get '/api/rest/admin/diversion-policies' do diff --git a/spec/acceptance/rest/admin/api/dtmf_receive_modes_spec.rb b/spec/acceptance/rest/admin/api/dtmf_receive_modes_spec.rb index 0c70351b9..dbed3ca72 100644 --- a/spec/acceptance/rest/admin/api/dtmf_receive_modes_spec.rb +++ b/spec/acceptance/rest/admin/api/dtmf_receive_modes_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Dtmf receive modes' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'dtmf-receive-modes' } get '/api/rest/admin/dtmf-receive-modes' do diff --git a/spec/acceptance/rest/admin/api/dtmf_send_modes_spec.rb b/spec/acceptance/rest/admin/api/dtmf_send_modes_spec.rb index 2d8bf5f6f..c926ba755 100644 --- a/spec/acceptance/rest/admin/api/dtmf_send_modes_spec.rb +++ b/spec/acceptance/rest/admin/api/dtmf_send_modes_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Dtmf send modes' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'dtmf-send-modes' } get '/api/rest/admin/dtmf-send-modes' do diff --git a/spec/acceptance/rest/admin/api/filter_types_spec.rb b/spec/acceptance/rest/admin/api/filter_types_spec.rb index b2aa2d816..e485a7f84 100644 --- a/spec/acceptance/rest/admin/api/filter_types_spec.rb +++ b/spec/acceptance/rest/admin/api/filter_types_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Filter types' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'filter-types' } get '/api/rest/admin/filter-types' do diff --git a/spec/acceptance/rest/admin/api/gateway_groups_spec.rb b/spec/acceptance/rest/admin/api/gateway_groups_spec.rb index 1cfc198bd..59f27e6d1 100644 --- a/spec/acceptance/rest/admin/api/gateway_groups_spec.rb +++ b/spec/acceptance/rest/admin/api/gateway_groups_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Gateway groups' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'gateway-groups' } get '/api/rest/admin/gateway-groups' do diff --git a/spec/acceptance/rest/admin/api/gateway_inband_dtmf_filtering_modes_spec.rb b/spec/acceptance/rest/admin/api/gateway_inband_dtmf_filtering_modes_spec.rb index ccfba42bb..d80dd18e9 100644 --- a/spec/acceptance/rest/admin/api/gateway_inband_dtmf_filtering_modes_spec.rb +++ b/spec/acceptance/rest/admin/api/gateway_inband_dtmf_filtering_modes_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Gateway inband dtmf filtering mode' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'gateway-inband-dtmf-filtering-modes' } get '/api/rest/admin/gateway-inband-dtmf-filtering-modes' do diff --git a/spec/acceptance/rest/admin/api/gateway_media_encryption_modes_spec.rb b/spec/acceptance/rest/admin/api/gateway_media_encryption_modes_spec.rb index 57c0c5702..c3a414ccc 100644 --- a/spec/acceptance/rest/admin/api/gateway_media_encryption_modes_spec.rb +++ b/spec/acceptance/rest/admin/api/gateway_media_encryption_modes_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Gateway Media encryption modes' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'gateway-media-encryption-modes' } get '/api/rest/admin/gateway-media-encryption-modes' do diff --git a/spec/acceptance/rest/admin/api/gateway_network_protocol_priorities_spec.rb b/spec/acceptance/rest/admin/api/gateway_network_protocol_priorities_spec.rb index caed96ae1..ab5afec67 100644 --- a/spec/acceptance/rest/admin/api/gateway_network_protocol_priorities_spec.rb +++ b/spec/acceptance/rest/admin/api/gateway_network_protocol_priorities_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Gateway Network protocol priorities' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'gateway-network-protocol-priorities' } get '/api/rest/admin/gateway-network-protocol-priorities' do diff --git a/spec/acceptance/rest/admin/api/gateway_rel100_modes_spec.rb b/spec/acceptance/rest/admin/api/gateway_rel100_modes_spec.rb index 325377ff9..1e4affbcc 100644 --- a/spec/acceptance/rest/admin/api/gateway_rel100_modes_spec.rb +++ b/spec/acceptance/rest/admin/api/gateway_rel100_modes_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Gateway rel100 modes' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'gateway-rel100-modes' } get '/api/rest/admin/gateway-rel100-modes' do diff --git a/spec/acceptance/rest/admin/api/gateways_spec.rb b/spec/acceptance/rest/admin/api/gateways_spec.rb index e21481428..12096a87f 100644 --- a/spec/acceptance/rest/admin/api/gateways_spec.rb +++ b/spec/acceptance/rest/admin/api/gateways_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Gateways' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'gateways' } resource_data = jsonapi_extract_fields_data(Api::Rest::Admin::GatewayResource) diff --git a/spec/acceptance/rest/admin/api/incoming_registrations_spec.rb b/spec/acceptance/rest/admin/api/incoming_registrations_spec.rb index 13b08c7b0..ea715f831 100644 --- a/spec/acceptance/rest/admin/api/incoming_registrations_spec.rb +++ b/spec/acceptance/rest/admin/api/incoming_registrations_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'IncomingRegistrations' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'incoming-registrations' } before { FactoryBot.create(:node) } diff --git a/spec/acceptance/rest/admin/api/invoice_originated_destinations_spec.rb b/spec/acceptance/rest/admin/api/invoice_originated_destinations_spec.rb index 906a19f80..3d545a16d 100644 --- a/spec/acceptance/rest/admin/api/invoice_originated_destinations_spec.rb +++ b/spec/acceptance/rest/admin/api/invoice_originated_destinations_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Invoice Originated Destinations' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'invoice-originated-destinations' } get '/api/rest/admin/invoice-originated-destinations' do diff --git a/spec/acceptance/rest/admin/api/invoice_originated_networks_spec.rb b/spec/acceptance/rest/admin/api/invoice_originated_networks_spec.rb index e42d0f06a..5acccaf31 100644 --- a/spec/acceptance/rest/admin/api/invoice_originated_networks_spec.rb +++ b/spec/acceptance/rest/admin/api/invoice_originated_networks_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Invoice Originated Networks' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'invoice-originated-networks' } get '/api/rest/admin/invoice-originated-networks' do diff --git a/spec/acceptance/rest/admin/api/invoice_service_data_spec.rb b/spec/acceptance/rest/admin/api/invoice_service_data_spec.rb index fe15b5348..9a652ba39 100644 --- a/spec/acceptance/rest/admin/api/invoice_service_data_spec.rb +++ b/spec/acceptance/rest/admin/api/invoice_service_data_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Invoice Service Data' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'invoice-service-data' } get '/api/rest/admin/invoice-service-data' do diff --git a/spec/acceptance/rest/admin/api/invoice_template_spec.rb b/spec/acceptance/rest/admin/api/invoice_template_spec.rb index b4820ca0c..4c7c4100a 100644 --- a/spec/acceptance/rest/admin/api/invoice_template_spec.rb +++ b/spec/acceptance/rest/admin/api/invoice_template_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Invoice template' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'invoice-templates' } get '/api/rest/admin/invoice-template' do diff --git a/spec/acceptance/rest/admin/api/invoice_terminated_destinations_spec.rb b/spec/acceptance/rest/admin/api/invoice_terminated_destinations_spec.rb index 11bd2df68..10966d3f0 100644 --- a/spec/acceptance/rest/admin/api/invoice_terminated_destinations_spec.rb +++ b/spec/acceptance/rest/admin/api/invoice_terminated_destinations_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Invoice Terminated Destinations' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'invoice-terminated-destinations' } get '/api/rest/admin/invoice-terminated-destinations' do diff --git a/spec/acceptance/rest/admin/api/invoice_terminated_networks_spec.rb b/spec/acceptance/rest/admin/api/invoice_terminated_networks_spec.rb index 2116dcc4a..7545b1b21 100644 --- a/spec/acceptance/rest/admin/api/invoice_terminated_networks_spec.rb +++ b/spec/acceptance/rest/admin/api/invoice_terminated_networks_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Invoice Terminated Networks' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'invoice-terminated-networks' } get '/api/rest/admin/invoice-terminated-networks' do diff --git a/spec/acceptance/rest/admin/api/invoices_spec.rb b/spec/acceptance/rest/admin/api/invoices_spec.rb index 691b45697..e0b27b40f 100644 --- a/spec/acceptance/rest/admin/api/invoices_spec.rb +++ b/spec/acceptance/rest/admin/api/invoices_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Invoices' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'invoices' } get '/api/rest/admin/invoices' do diff --git a/spec/acceptance/rest/admin/api/network_types_spec.rb b/spec/acceptance/rest/admin/api/network_types_spec.rb index 5a347de92..06bfc540c 100644 --- a/spec/acceptance/rest/admin/api/network_types_spec.rb +++ b/spec/acceptance/rest/admin/api/network_types_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'NetworkTypes' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'network-types' } required_params = %i[name] diff --git a/spec/acceptance/rest/admin/api/networks_spec.rb b/spec/acceptance/rest/admin/api/networks_spec.rb index d52659f31..7453bc988 100644 --- a/spec/acceptance/rest/admin/api/networks_spec.rb +++ b/spec/acceptance/rest/admin/api/networks_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Networks' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'networks' } required_params = %i[name] diff --git a/spec/acceptance/rest/admin/api/nodes_spec.rb b/spec/acceptance/rest/admin/api/nodes_spec.rb index 3355df865..237f65c40 100644 --- a/spec/acceptance/rest/admin/api/nodes_spec.rb +++ b/spec/acceptance/rest/admin/api/nodes_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Nodes' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'nodes' } get '/api/rest/admin/nodes' do diff --git a/spec/acceptance/rest/admin/api/payments_spec.rb b/spec/acceptance/rest/admin/api/payments_spec.rb index 8fc729982..9700c2353 100644 --- a/spec/acceptance/rest/admin/api/payments_spec.rb +++ b/spec/acceptance/rest/admin/api/payments_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Payments' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'payments' } get '/api/rest/admin/payments' do diff --git a/spec/acceptance/rest/admin/api/pops_spec.rb b/spec/acceptance/rest/admin/api/pops_spec.rb index 72eabda30..005605187 100644 --- a/spec/acceptance/rest/admin/api/pops_spec.rb +++ b/spec/acceptance/rest/admin/api/pops_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Pops' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'pops' } get '/api/rest/admin/pops' do diff --git a/spec/acceptance/rest/admin/api/rateplans_spec.rb b/spec/acceptance/rest/admin/api/rateplans_spec.rb index c72337f4e..b3b5375b1 100644 --- a/spec/acceptance/rest/admin/api/rateplans_spec.rb +++ b/spec/acceptance/rest/admin/api/rateplans_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Rateplans' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'rateplans' } get '/api/rest/admin/rateplans' do diff --git a/spec/acceptance/rest/admin/api/routeset_discriminators_spec.rb b/spec/acceptance/rest/admin/api/routeset_discriminators_spec.rb index 27ddffbfa..9dde1b41a 100644 --- a/spec/acceptance/rest/admin/api/routeset_discriminators_spec.rb +++ b/spec/acceptance/rest/admin/api/routeset_discriminators_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Routeset discriminators' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'routeset-discriminators' } get '/api/rest/admin/routeset-discriminators' do diff --git a/spec/acceptance/rest/admin/api/routing_groups_spec.rb b/spec/acceptance/rest/admin/api/routing_groups_spec.rb index 5d1ab46f2..67627f9fa 100644 --- a/spec/acceptance/rest/admin/api/routing_groups_spec.rb +++ b/spec/acceptance/rest/admin/api/routing_groups_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Routing groups' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'routing-groups' } get '/api/rest/admin/routing-groups' do diff --git a/spec/acceptance/rest/admin/api/routing_plans_spec.rb b/spec/acceptance/rest/admin/api/routing_plans_spec.rb index 1370d5027..74ac8709c 100644 --- a/spec/acceptance/rest/admin/api/routing_plans_spec.rb +++ b/spec/acceptance/rest/admin/api/routing_plans_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Routing plans' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'routing-plans' } get '/api/rest/admin/routing-plans' do diff --git a/spec/acceptance/rest/admin/api/sdp_c_locations_spec.rb b/spec/acceptance/rest/admin/api/sdp_c_locations_spec.rb index 3ca0a9d55..3dab76d11 100644 --- a/spec/acceptance/rest/admin/api/sdp_c_locations_spec.rb +++ b/spec/acceptance/rest/admin/api/sdp_c_locations_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Sdp c locations' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'sdp-c-locations' } get '/api/rest/admin/sdp-c-locations' do diff --git a/spec/acceptance/rest/admin/api/sensor_levels_spec.rb b/spec/acceptance/rest/admin/api/sensor_levels_spec.rb index 41e2113b7..be205fcae 100644 --- a/spec/acceptance/rest/admin/api/sensor_levels_spec.rb +++ b/spec/acceptance/rest/admin/api/sensor_levels_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Sensor levels' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'sensor-levels' } get '/api/rest/admin/sensor-levels' do diff --git a/spec/acceptance/rest/admin/api/sensors_spec.rb b/spec/acceptance/rest/admin/api/sensors_spec.rb index 3c52c1750..ada4a7093 100644 --- a/spec/acceptance/rest/admin/api/sensors_spec.rb +++ b/spec/acceptance/rest/admin/api/sensors_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Sensors' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'sensors' } required_params = %i[name mode-id target-ip source-ip] diff --git a/spec/acceptance/rest/admin/api/service_types_spec.rb b/spec/acceptance/rest/admin/api/service_types_spec.rb index dc1255e4e..8b88b2c47 100644 --- a/spec/acceptance/rest/admin/api/service_types_spec.rb +++ b/spec/acceptance/rest/admin/api/service_types_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Service Types' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'service-types' } get '/api/rest/admin/service-types' do diff --git a/spec/acceptance/rest/admin/api/services_spec.rb b/spec/acceptance/rest/admin/api/services_spec.rb index 4c30d2273..bc0392c91 100644 --- a/spec/acceptance/rest/admin/api/services_spec.rb +++ b/spec/acceptance/rest/admin/api/services_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Services' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'services' } get '/api/rest/admin/services' do diff --git a/spec/acceptance/rest/admin/api/session_refresh_methods_spec.rb b/spec/acceptance/rest/admin/api/session_refresh_methods_spec.rb index fc90e8ebb..948f99687 100644 --- a/spec/acceptance/rest/admin/api/session_refresh_methods_spec.rb +++ b/spec/acceptance/rest/admin/api/session_refresh_methods_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Session refresh methods' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'session-refresh-methods' } get '/api/rest/admin/session-refresh-methods' do diff --git a/spec/acceptance/rest/admin/api/sip_options_probers_spec.rb b/spec/acceptance/rest/admin/api/sip_options_probers_spec.rb index 6f626a958..63ee5f06e 100644 --- a/spec/acceptance/rest/admin/api/sip_options_probers_spec.rb +++ b/spec/acceptance/rest/admin/api/sip_options_probers_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Sip options prober' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'sip-options-probers' } required_params = %i[name ruri-username ruri-domain] diff --git a/spec/acceptance/rest/admin/api/smtp_connections_spec.rb b/spec/acceptance/rest/admin/api/smtp_connections_spec.rb index 3efa3c999..1d936e598 100644 --- a/spec/acceptance/rest/admin/api/smtp_connections_spec.rb +++ b/spec/acceptance/rest/admin/api/smtp_connections_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Smtp connections' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'smtp-connections' } required_params = %i[name host port from-address] diff --git a/spec/acceptance/rest/admin/api/timezones_spec.rb b/spec/acceptance/rest/admin/api/timezones_spec.rb index 21d5eee6c..a49549f7f 100644 --- a/spec/acceptance/rest/admin/api/timezones_spec.rb +++ b/spec/acceptance/rest/admin/api/timezones_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Timezones' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'timezones' } get '/api/rest/admin/timezones' do diff --git a/spec/acceptance/rest/admin/api/transport_protocols_spec.rb b/spec/acceptance/rest/admin/api/transport_protocols_spec.rb index 82ef808a6..d80af801e 100644 --- a/spec/acceptance/rest/admin/api/transport_protocols_spec.rb +++ b/spec/acceptance/rest/admin/api/transport_protocols_spec.rb @@ -3,12 +3,7 @@ require 'rspec_api_documentation/dsl' RSpec.resource 'Transport protocol' do - header 'Accept', 'application/vnd.api+json' - header 'Content-Type', 'application/vnd.api+json' - header 'Authorization', :auth_token - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } + include_context :acceptance_admin_user let(:type) { 'transport-protocols' } get '/api/rest/admin/transport-protocols' do diff --git a/spec/controllers/api/rest/admin/accounts_controller_spec.rb b/spec/controllers/api/rest/admin/accounts_controller_spec.rb index 11e158353..073fd0897 100755 --- a/spec/controllers/api/rest/admin/accounts_controller_spec.rb +++ b/spec/controllers/api/rest/admin/accounts_controller_spec.rb @@ -1,14 +1,7 @@ # frozen_string_literal: true RSpec.describe Api::Rest::Admin::AccountsController, type: :controller do - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } - - before do - request.accept = 'application/vnd.api+json' - request.headers['Content-Type'] = 'application/vnd.api+json' - request.headers['Authorization'] = auth_token - end + include_context :jsonapi_admin_headers describe 'GET index' do let!(:accounts) { create_list :account, 2 } diff --git a/spec/controllers/api/rest/admin/api_accesses_controller_spec.rb b/spec/controllers/api/rest/admin/api_accesses_controller_spec.rb index 485d9c6f9..806823372 100644 --- a/spec/controllers/api/rest/admin/api_accesses_controller_spec.rb +++ b/spec/controllers/api/rest/admin/api_accesses_controller_spec.rb @@ -1,14 +1,7 @@ # frozen_string_literal: true RSpec.describe Api::Rest::Admin::ApiAccessesController, type: :controller do - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } - - before do - request.accept = 'application/vnd.api+json' - request.headers['Content-Type'] = 'application/vnd.api+json' - request.headers['Authorization'] = auth_token - end + include_context :jsonapi_admin_headers describe 'GET index' do let!(:api_access) { create_list :api_access, 2 } diff --git a/spec/controllers/api/rest/admin/balance_controller_spec.rb b/spec/controllers/api/rest/admin/balance_controller_spec.rb index 8a8b9a5c4..2ebc63fe2 100644 --- a/spec/controllers/api/rest/admin/balance_controller_spec.rb +++ b/spec/controllers/api/rest/admin/balance_controller_spec.rb @@ -1,14 +1,7 @@ # frozen_string_literal: true RSpec.describe Api::Rest::Admin::BalanceController, type: :controller do - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } - - before do - request.accept = 'application/vnd.api+json' - request.headers['Content-Type'] = 'application/vnd.api+json' - request.headers['Authorization'] = auth_token - end + include_context :jsonapi_admin_headers describe 'PUT update', versioning: true do let(:external_id) do diff --git a/spec/controllers/api/rest/admin/codec_groups_controller_spec.rb b/spec/controllers/api/rest/admin/codec_groups_controller_spec.rb index f0fa451ea..36cc2366c 100644 --- a/spec/controllers/api/rest/admin/codec_groups_controller_spec.rb +++ b/spec/controllers/api/rest/admin/codec_groups_controller_spec.rb @@ -1,14 +1,7 @@ # frozen_string_literal: true RSpec.describe Api::Rest::Admin::CodecGroupsController, type: :controller do - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } - - before do - request.accept = 'application/vnd.api+json' - request.headers['Content-Type'] = 'application/vnd.api+json' - request.headers['Authorization'] = auth_token - end + include_context :jsonapi_admin_headers describe 'GET index with ransack filters' do subject do diff --git a/spec/controllers/api/rest/admin/contacts_controller_spec.rb b/spec/controllers/api/rest/admin/contacts_controller_spec.rb index 01bf14c92..b8e32ef1e 100644 --- a/spec/controllers/api/rest/admin/contacts_controller_spec.rb +++ b/spec/controllers/api/rest/admin/contacts_controller_spec.rb @@ -1,14 +1,7 @@ # frozen_string_literal: true RSpec.describe Api::Rest::Admin::ContactsController, type: :controller do - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } - - before do - request.accept = 'application/vnd.api+json' - request.headers['Content-Type'] = 'application/vnd.api+json' - request.headers['Authorization'] = auth_token - end + include_context :jsonapi_admin_headers describe 'GET index with ransack filters' do subject do diff --git a/spec/controllers/api/rest/admin/contractors_controller_spec.rb b/spec/controllers/api/rest/admin/contractors_controller_spec.rb index c30f1fdd9..34ec7bcd8 100755 --- a/spec/controllers/api/rest/admin/contractors_controller_spec.rb +++ b/spec/controllers/api/rest/admin/contractors_controller_spec.rb @@ -1,14 +1,7 @@ # frozen_string_literal: true RSpec.describe Api::Rest::Admin::ContractorsController, type: :controller do - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } - - before do - request.accept = 'application/vnd.api+json' - request.headers['Content-Type'] = 'application/vnd.api+json' - request.headers['Authorization'] = auth_token - end + include_context :jsonapi_admin_headers describe 'GET index' do let!(:contractors) { create_list :contractor, 2, vendor: true } diff --git a/spec/controllers/api/rest/admin/destinations_controller_spec.rb b/spec/controllers/api/rest/admin/destinations_controller_spec.rb index e95af49f6..bb27b3be1 100755 --- a/spec/controllers/api/rest/admin/destinations_controller_spec.rb +++ b/spec/controllers/api/rest/admin/destinations_controller_spec.rb @@ -295,7 +295,7 @@ before { get :show, params: { id: destination.to_param, include: 'country' } } include_examples :responds_with_status, 200 - include_examples :returns_json_api_record, type: 'destinations', relationships: %i[country network] do + include_examples :returns_json_api_record, type: 'destinations', relationships: %i[country network destination-next-rates rate-group routing-tag-mode] do let(:json_api_record_id) { destination.id.to_s } let(:json_api_record_attributes) do hash_including( diff --git a/spec/controllers/api/rest/admin/gateway_groups_controller_spec.rb b/spec/controllers/api/rest/admin/gateway_groups_controller_spec.rb index d2843d3e4..fb1b4199c 100755 --- a/spec/controllers/api/rest/admin/gateway_groups_controller_spec.rb +++ b/spec/controllers/api/rest/admin/gateway_groups_controller_spec.rb @@ -2,15 +2,7 @@ RSpec.describe Api::Rest::Admin::GatewayGroupsController, type: :controller do let(:vendor) { create :contractor, vendor: true } - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } - - before do - request.accept = 'application/vnd.api+json' - request.headers['Content-Type'] = 'application/vnd.api+json' - request.headers['Authorization'] = auth_token - end + include_context :jsonapi_admin_headers describe 'GET index' do let!(:gateway_groups) { create_list :gateway_group, 2 } diff --git a/spec/controllers/api/rest/admin/payments_controller_spec.rb b/spec/controllers/api/rest/admin/payments_controller_spec.rb index abc6f58a2..5b3f315e0 100755 --- a/spec/controllers/api/rest/admin/payments_controller_spec.rb +++ b/spec/controllers/api/rest/admin/payments_controller_spec.rb @@ -1,16 +1,7 @@ # frozen_string_literal: true RSpec.describe Api::Rest::Admin::PaymentsController, type: :controller do - let(:account) { create(:account) } - - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } - - before do - request.accept = 'application/vnd.api+json' - request.headers['Content-Type'] = 'application/vnd.api+json' - request.headers['Authorization'] = auth_token - end + include_context :jsonapi_admin_headers describe 'GET index' do let!(:payments) { create_list :payment, 2 } diff --git a/spec/controllers/api/rest/admin/routing_plans_controller_spec.rb b/spec/controllers/api/rest/admin/routing_plans_controller_spec.rb index c732dc655..d241b0124 100755 --- a/spec/controllers/api/rest/admin/routing_plans_controller_spec.rb +++ b/spec/controllers/api/rest/admin/routing_plans_controller_spec.rb @@ -1,14 +1,7 @@ # frozen_string_literal: true RSpec.describe Api::Rest::Admin::RoutingPlansController, type: :controller do - let(:user) { create :admin_user } - let(:auth_token) { ::Knock::AuthToken.new(payload: { sub: user.id }).token } - - before do - request.accept = 'application/vnd.api+json' - request.headers['Content-Type'] = 'application/vnd.api+json' - request.headers['Authorization'] = auth_token - end + include_context :jsonapi_admin_headers describe 'GET index' do let!(:routing_plans) { create_list :routing_plan, 2 } diff --git a/spec/support/contexts/acceptance/acceptance_admin_user.rb b/spec/support/contexts/acceptance/acceptance_admin_user.rb index 85b873a0a..e026f9b30 100644 --- a/spec/support/contexts/acceptance/acceptance_admin_user.rb +++ b/spec/support/contexts/acceptance/acceptance_admin_user.rb @@ -5,9 +5,9 @@ header 'Content-Type', 'application/vnd.api+json' header 'Authorization', :auth_token - let(:user) { create :admin_user } + let(:admin_user) { create :admin_user } let(:auth_token) do - ::Knock::AuthToken.new(payload: { sub: user.id }).token + Authentication::AdminAuth.build_auth_data(admin_user).token end end diff --git a/spec/support/contexts/json_api/json_api_admin_helpers.rb b/spec/support/contexts/json_api/json_api_admin_helpers.rb index 06bf50a1c..34b15d2fb 100644 --- a/spec/support/contexts/json_api/json_api_admin_helpers.rb +++ b/spec/support/contexts/json_api/json_api_admin_helpers.rb @@ -2,7 +2,7 @@ RSpec.shared_context :json_api_admin_helpers do |type: nil| let(:json_api_resource_type) { type.to_s } - let(:json_api_auth_token) { ::Knock::AuthToken.new(payload: { sub: admin_user.id }).token } + let(:json_api_auth_token) { Authentication::AdminAuth.build_auth_data(admin_user).token } let(:json_api_request_path_prefix) { '/api/rest/admin' } let(:json_api_request_path) { "#{json_api_request_path_prefix}/#{json_api_resource_type}" } let(:json_api_request_headers) do diff --git a/spec/support/contexts/jsonapi_admin_headers.rb b/spec/support/contexts/jsonapi_admin_headers.rb index 5fcdb8383..32ee1f4e4 100644 --- a/spec/support/contexts/jsonapi_admin_headers.rb +++ b/spec/support/contexts/jsonapi_admin_headers.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true RSpec.shared_context :jsonapi_admin_headers do - let(:user) { create :admin_user } + let(:admin_user) { create :admin_user } let(:auth_token) do - ::Knock::AuthToken.new(payload: { sub: user.id }).token + Authentication::AdminAuth.build_auth_data(admin_user).token end before do diff --git a/spec/support/examples/json_api/json_api_admin_check_authorization.rb b/spec/support/examples/json_api/json_api_admin_check_authorization.rb index 57a3d6bd8..9e0cd5389 100644 --- a/spec/support/examples/json_api/json_api_admin_check_authorization.rb +++ b/spec/support/examples/json_api/json_api_admin_check_authorization.rb @@ -17,7 +17,7 @@ context 'with expired auth token' do let(:json_api_auth_token) do - ::Knock::AuthToken.new(payload: { sub: admin_user.id, exp: 1.minute.ago.to_i }).token + Authentication::AdminAuth.build_auth_data(admin_user, expires_at: 1.minute.ago).token end include_examples :responds_with_status, 401, without_body: true @@ -31,7 +31,7 @@ include_examples :responds_with_status, 401, without_body: true end - context 'when admin_user.allowed_ips does not match request.remote_ip' do + context 'when admin_user.allowed_ips matches request.remote_ip' do before do admin_user.update! allowed_ips: ['127.0.0.1'] when_valid_auth