From ecf5c6e1d4d67dff36afde7ec02b2b09dcb1a411 Mon Sep 17 00:00:00 2001 From: viehlieb Date: Tue, 29 Nov 2022 18:55:45 +0100 Subject: [PATCH] rswagged financial_transactions --- .../api/financial_transactions_spec.rb | 41 ++++++++----------- spec/support/api_helper.rb | 24 +++++++++++ spec/swagger_helper.rb | 15 +------ 3 files changed, 42 insertions(+), 38 deletions(-) diff --git a/spec/requests/api/financial_transactions_spec.rb b/spec/requests/api/financial_transactions_spec.rb index 6c27a9a8..84458e79 100644 --- a/spec/requests/api/financial_transactions_spec.rb +++ b/spec/requests/api/financial_transactions_spec.rb @@ -2,21 +2,26 @@ require 'swagger_helper' describe 'Financial Transaction', type: :request do include ApiHelper + let!(:finance_user) { create(:user, groups: [create(:workgroup, role_finance: true)]) } + let!(:api_scopes) { ['finance:read', 'finance:write'] } + let(:api_access_token) { create(:oauth2_access_token, resource_owner_id: finance_user.id, scopes: api_scopes&.join(' ')).token } + let(:financial_transaction) { create(:financial_transaction, user: user) } path '/financial_transactions' do get 'financial transactions' do tags 'Financial Transaction' produces 'application/json' - parameter name: "page[number]", in: :query, type: :integer, required: false - parameter name: "page[size]", in: :query, type: :integer, required: false - - let!(:financial_transaction) { create(:financial_transaction) } - let(:api_scopes) { ['finance:read', 'finance:write'] } + parameter name: "per_page", in: :query, type: :integer, required: false + parameter name: "page", in: :query, type: :integer, required: false response '200', 'success' do schema type: :object, properties: { meta: { - '$ref' => '#/components/schemas/pagination' + type: :object, + items: + { + '$ref': '#/components/schemas/Meta' + } }, financial_transaction: { type: :array, @@ -25,12 +30,11 @@ describe 'Financial Transaction', type: :request do } } } - - let(:page) { { number: 1, size: 20 } } + let(:page) { 1 } + let(:per_page) { 10 } run_test! end - - it_handles_invalid_token + it_handles_invalid_scope end end @@ -52,20 +56,7 @@ describe 'Financial Transaction', type: :request do let(:id) { FinancialTransaction.create(user: user).id } run_test! end - - response '401', 'not logged in' do - schema type: :object, properties: { - financial_transaction: { - type: :array, - items: { - '$ref': '#/components/schemas/FinancialTransaction' - } - } - } - let(:Authorization) { 'abc' } - let(:id) { FinancialTransaction.create(name: 'TestTransaction').id } - run_test! - end + it_handles_invalid_scope_with_id(:financial_transaction) response '404', 'financial transaction not found' do schema type: :object, properties: { @@ -79,6 +70,8 @@ describe 'Financial Transaction', type: :request do let(:id) { 'invalid' } run_test! end + # response 403 + it_handles_invalid_scope_with_id(:financial_transaction) end end end diff --git a/spec/support/api_helper.rb b/spec/support/api_helper.rb index 5c2246a3..5b97414a 100644 --- a/spec/support/api_helper.rb +++ b/spec/support/api_helper.rb @@ -18,6 +18,18 @@ module ApiHelper end end + 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 + def self.it_handles_invalid_scope context 'with invalid scope' do let(:api_scopes) { ['none'] } @@ -29,6 +41,18 @@ module ApiHelper end end + def self.it_handles_invalid_scope_with_id(class_sym) + context 'with invalid scope' do + let(:api_scopes) { ['none'] } + let(:id) { create(class_sym).id } + + response 403, 'missing scope' do + schema '$ref' => '#/components/schemas/Error403' + run_test! + end + end + end + def self.it_handles_invalid_token_and_scope(*args) it_handles_invalid_token(*args) it_handles_invalid_scope(*args) diff --git a/spec/swagger_helper.rb b/spec/swagger_helper.rb index 5f066aec..9f1d1f7e 100644 --- a/spec/swagger_helper.rb +++ b/spec/swagger_helper.rb @@ -24,16 +24,6 @@ RSpec.configure do |config| paths: {}, components: { schemas: { - pagination: { - type: :object, - properties: { - recordCount: { type: :integer }, - pageCount: { type: :integer }, - currentPage: { type: :integer }, - pageSize: { type: :integer } - }, - required: %w(recordCount pageCount currentPage pageSize) - }, ArticleCategory: { type: :object, properties: { @@ -51,12 +41,9 @@ RSpec.configure do |config| properties: { id: { type: :integer - }, - name: { - type: :string } }, - required: %w[id financial_transaction_type] + required: %w[amount note user_id] }, FinancialTransactionClass: { type: :object,