Skip to content

armgit / sure

Public Imported from GitHub
0

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.yaml
index 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: true
ImportConfiguration:
type: object
properties:
@@ -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 scope
content:
diff --git a/spec/requests/api/v1/trades_spec.rb b/spec/requests/api/v1/trades_spec.rb
index 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) do
Family.create!(
name: 'API Family',
@@ -194,9 +212,11 @@ RSpec.describe 'API V1 Trades', type: :request do
end
response '201', 'trade created' do
- schema '$ref' => '#/components/schemas/Trade'
+ schema created_response_schema
- run_test!
+ run_test! do |response|
+ expect_created_shape(response, 'Trade')
+ end
end
response '403', 'forbidden - api key missing read_write scope' do
@@ -328,7 +348,7 @@ RSpec.describe 'API V1 Trades', type: :request do
end
response '201', 'dividend created with security' do
- schema '$ref' => '#/components/schemas/Trade'
+ schema created_response_schema
let(: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')
+ end
end
response '422', 'dividend without security returns error' do
@@ -397,7 +419,7 @@ RSpec.describe 'API V1 Trades', type: :request do
end
response '201', 'deposit created' do
- schema '$ref' => '#/components/schemas/TransactionResponse'
+ schema created_response_schema
let(: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')
+ end
end
response '201', 'withdrawal created' do
- schema '$ref' => '#/components/schemas/TransactionResponse'
+ schema created_response_schema
let(: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')
+ end
end
- 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_schema
let(: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')
+ end
end
response '422', 'invalid trade request' do
@@ -558,6 +617,7 @@ RSpec.describe 'API V1 Trades', type: :request do
response '403', 'forbidden - api key missing read_write scope' do
schema '$ref' => '#/components/schemas/ErrorResponse'
+ let(:id) { trade.id }
let(:read_only_api_key) do
key = ApiKey.generate_secure_key
ApiKey.create!(
@@ -607,6 +667,7 @@ RSpec.describe 'API V1 Trades', type: :request do
response '403', 'forbidden - api key missing read_write scope' do
schema '$ref' => '#/components/schemas/ErrorResponse'
+ let(:id) { trade.id }
let(:read_only_api_key) do
key = ApiKey.generate_secure_key
ApiKey.create!(
diff --git a/spec/swagger_helper.rb b/spec/swagger_helper.rb
index 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: {