The personal finance app for everyone (by everyone)
Ruby
Fix failing Trades request spec examples (#5304)
arnaurodondev31c91148f4ad
diff --git a/docs/api/openapi.yaml b/docs/api/openapi.yamlindex 5a8afcf08e..860664dad9 100644--- a/docs/api/openapi.yaml+++ b/docs/api/openapi.yaml@@ -2195,46 +2195,6 @@ components:properties:message:type: string- TransactionResponse:- type: object- required:- - id- - date- - amount- - currency- - name- - entryable_type- - account- properties:- id:- type: string- format: uuid- date:- type: string- format: date- amount:- type: string- currency:- type: string- name:- type: string- entryable_type:- type: string- account:- type: object- required:- - id- - name- - account_type- properties:- id:- type: string- format: uuid- name:- type: string- account_type:- type: string- nullable: trueImportConfiguration:type: objectproperties:@@ -10412,11 +10372,16 @@ paths:parameters: []responses:'201':- description: trade created+ description: 'created: a Trade for trade types, a Transaction for a deposit+ or withdrawal without transfer_account_id, or a TransferDecision for a+ deposit or withdrawal with transfer_account_id'content:application/json:schema:- "$ref": "#/components/schemas/TransactionResponse"+ oneOf:+ - "$ref": "#/components/schemas/Trade"+ - "$ref": "#/components/schemas/Transaction"+ - "$ref": "#/components/schemas/TransferDecision"'403':description: forbidden - api key missing read_write scopecontent:diff --git a/spec/requests/api/v1/trades_spec.rb b/spec/requests/api/v1/trades_spec.rbindex f51352dad7..975b5e012c 100644--- a/spec/requests/api/v1/trades_spec.rb+++ b/spec/requests/api/v1/trades_spec.rb@@ -3,6 +3,24 @@require 'swagger_helper'RSpec.describe 'API V1 Trades', type: :request do+ # POST /api/v1/trades answers 201 with one of three shapes. rswag keeps a+ # single 201 entry per operation (the last example's schema, deep-merged), so+ # every 201 example declares the same documented oneOf and then checks its own+ # response against the one schema it should match.+ created_response_schema = {+ oneOf: [+ { '$ref' => '#/components/schemas/Trade' },+ { '$ref' => '#/components/schemas/Transaction' },+ { '$ref' => '#/components/schemas/TransferDecision' }+ ]+ }++ def expect_created_shape(response, schema_name)+ metadata = RSpec.current_example.metadata.deep_dup+ metadata[:response][:schema] = { '$ref' => "#/components/schemas/#{schema_name}" }+ Rswag::Specs::ResponseValidator.new.validate!(metadata, response)+ end+let(:family) doFamily.create!(name: 'API Family',@@ -194,9 +212,11 @@ RSpec.describe 'API V1 Trades', type: :request doendresponse '201', 'trade created' do- schema '$ref' => '#/components/schemas/Trade'+ schema created_response_schema- run_test!+ run_test! do |response|+ expect_created_shape(response, 'Trade')+ endendresponse '403', 'forbidden - api key missing read_write scope' do@@ -328,7 +348,7 @@ RSpec.describe 'API V1 Trades', type: :request doendresponse '201', 'dividend created with security' do- schema '$ref' => '#/components/schemas/Trade'+ schema created_response_schemalet(:body) do{@@ -343,7 +363,9 @@ RSpec.describe 'API V1 Trades', type: :request do}end- run_test!+ run_test! do |response|+ expect_created_shape(response, 'Trade')+ endendresponse '422', 'dividend without security returns error' do@@ -397,7 +419,7 @@ RSpec.describe 'API V1 Trades', type: :request doendresponse '201', 'deposit created' do- schema '$ref' => '#/components/schemas/TransactionResponse'+ schema created_response_schemalet(:body) do{@@ -411,11 +433,13 @@ RSpec.describe 'API V1 Trades', type: :request do}end- run_test!+ run_test! do |response|+ expect_created_shape(response, 'Transaction')+ endendresponse '201', 'withdrawal created' do- schema '$ref' => '#/components/schemas/TransactionResponse'+ schema created_response_schemalet(:body) do{@@ -429,11 +453,44 @@ RSpec.describe 'API V1 Trades', type: :request do}end- run_test!+ run_test! do |response|+ expect_created_shape(response, 'Transaction')+ endend- response '201', 'trade created' do- schema '$ref' => '#/components/schemas/TransactionResponse'+ response '201', 'linked transfer created (deposit or withdrawal with transfer_account_id)' do+ schema created_response_schema++ let(:transfer_account) do+ Account.create!(+ family: family,+ name: 'Checking Account',+ balance: 10_000,+ currency: 'USD',+ accountable: Depository.create!+ )+ end++ let(:body) do+ {+ trade: {+ account_id: account.id,+ date: Date.current.to_s,+ type: 'withdrawal',+ amount: 100.00,+ currency: 'USD',+ transfer_account_id: transfer_account.id+ }+ }+ end++ run_test! do |response|+ expect_created_shape(response, 'TransferDecision')+ end+ end++ response '201', 'created: a Trade for trade types, a Transaction for a deposit or withdrawal without transfer_account_id, or a TransferDecision for a deposit or withdrawal with transfer_account_id' do+ schema created_response_schemalet(:body) do{@@ -447,7 +504,9 @@ RSpec.describe 'API V1 Trades', type: :request do}end- run_test!+ run_test! do |response|+ expect_created_shape(response, 'Trade')+ endendresponse '422', 'invalid trade request' do@@ -558,6 +617,7 @@ RSpec.describe 'API V1 Trades', type: :request doresponse '403', 'forbidden - api key missing read_write scope' doschema '$ref' => '#/components/schemas/ErrorResponse'+ let(:id) { trade.id }let(:read_only_api_key) dokey = ApiKey.generate_secure_keyApiKey.create!(@@ -607,6 +667,7 @@ RSpec.describe 'API V1 Trades', type: :request doresponse '403', 'forbidden - api key missing read_write scope' doschema '$ref' => '#/components/schemas/ErrorResponse'+ let(:id) { trade.id }let(:read_only_api_key) dokey = ApiKey.generate_secure_keyApiKey.create!(diff --git a/spec/swagger_helper.rb b/spec/swagger_helper.rbindex e94c16724b..236b70b9a3 100644--- a/spec/swagger_helper.rb+++ b/spec/swagger_helper.rb@@ -1178,27 +1178,6 @@ RSpec.configure do |config|message: { type: :string }}},- TransactionResponse: {- type: :object,- required: %w[id date amount currency name entryable_type account],- properties: {- id: { type: :string, format: :uuid },- date: { type: :string, format: :date },- amount: { type: :string },- currency: { type: :string },- name: { type: :string },- entryable_type: { type: :string },- account: {- type: :object,- required: %w[id name account_type],- properties: {- id: { type: :string, format: :uuid },- name: { type: :string },- account_type: { type: :string, nullable: true }- }- }- }- },ImportConfiguration: {type: :object,properties: {