Add RSwag coverage for /chat and /transactions API endpoints (#210)

* Add RSwag coverage for chat API

* Linter

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: Juan José Mata <jjmata@jjmata.com>

* Add transaction rswag

* FIX linter

---------

Signed-off-by: Juan José Mata <jjmata@jjmata.com>
Signed-off-by: Juan José Mata <juanjo.mata@gmail.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: sokie <sokysrm@gmail.com>
This commit is contained in:
Juan José Mata
2025-12-17 14:14:17 +01:00
committed by GitHub
parent 5f8a295479
commit 9d54719007
12 changed files with 2274 additions and 203 deletions

View File

@@ -0,0 +1,357 @@
# frozen_string_literal: true
require 'swagger_helper'
RSpec.describe 'API V1 Chats', type: :request do
let(:family) do
Family.create!(
name: 'API Family',
currency: 'USD',
locale: 'en',
date_format: '%m-%d-%Y'
)
end
let(:user) do
family.users.create!(
email: 'api-user@example.com',
password: 'password123',
password_confirmation: 'password123',
ai_enabled: true
)
end
let(:oauth_application) do
Doorkeeper::Application.create!(
name: 'API Docs',
redirect_uri: 'https://example.com/callback',
scopes: 'read read_write'
)
end
let(:access_token) do
Doorkeeper::AccessToken.create!(
application: oauth_application,
resource_owner_id: user.id,
scopes: 'read_write',
expires_in: 2.hours,
token: SecureRandom.hex(32)
)
end
let(:Authorization) { "Bearer #{access_token.token}" }
let!(:chat) do
user.chats.create!(title: 'Budget planning').tap do |record|
record.messages.create!(
type: 'UserMessage',
content: 'How should I budget for a vacation?',
ai_model: 'gpt-4'
)
assistant_message = record.messages.create!(
type: 'AssistantMessage',
content: "Let's review your spending patterns first.",
ai_model: 'gpt-4'
)
assistant_message.tool_calls.create!(
provider_id: 'openai',
type: 'ToolCall::Function',
function_name: 'get_accounts',
function_arguments: { 'scope' => 'spending' },
function_result: { 'total_spend' => 1500 }
)
record.messages.create!(
type: 'AssistantMessage',
content: 'Does this align with your savings goals?',
ai_model: 'gpt-4'
)
end
end
let!(:another_chat) do
user.chats.create!(title: 'Retirement planning').tap do |record|
record.messages.create!(
type: 'UserMessage',
content: 'How much should I contribute to my IRA?',
ai_model: 'gpt-4'
)
end
end
path '/api/v1/chats' do
get 'List chats' do
tags 'Chats'
security [ { bearerAuth: [] } ]
produces 'application/json'
parameter name: :Authorization, in: :header, required: true, schema: { type: :string },
description: 'Bearer token with read scope'
response '200', 'chats listed' do
schema '$ref' => '#/components/schemas/ChatCollection'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('chats')).to be_present
expect(payload.fetch('pagination')).to include('page', 'per_page', 'total_count', 'total_pages')
end
end
response '403', 'AI features disabled' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:user) do
family.users.create!(
email: 'no-ai@example.com',
password: 'password123',
password_confirmation: 'password123',
ai_enabled: false
)
end
run_test!
end
end
post 'Create chat' do
tags 'Chats'
security [ { bearerAuth: [] } ]
consumes 'application/json'
produces 'application/json'
parameter name: :Authorization, in: :header, required: true, schema: { type: :string },
description: 'Bearer token with write scope'
parameter name: :chat_params, in: :body, required: true, schema: {
type: :object,
properties: {
title: { type: :string, example: 'Monthly budget review' },
message: { type: :string, description: 'Initial message in the chat' },
model: { type: :string, description: 'Optional OpenAI model identifier' }
},
required: %w[title message]
}
let(:chat_params) do
{
title: 'Travel planning',
message: 'Can you help me plan a summer trip?',
model: 'gpt-4-turbo'
}
end
response '201', 'chat created' do
schema '$ref' => '#/components/schemas/ChatDetail'
run_test! do |response|
payload = JSON.parse(response.body)
chat_record = Chat.find(payload.fetch('id'))
expect(chat_record.messages.first.content).to eq('Can you help me plan a summer trip?')
end
end
response '422', 'validation error' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:chat_params) { { title: '' } }
run_test!
end
end
end
path '/api/v1/chats/{id}' do
parameter name: :Authorization, in: :header, required: true, schema: { type: :string },
description: 'Bearer token with read scope'
parameter name: :id, in: :path, type: :string, required: true, description: 'Chat ID'
get 'Retrieve a chat' do
tags 'Chats'
security [ { bearerAuth: [] } ]
produces 'application/json'
let(:id) { chat.id }
response '200', 'chat retrieved' do
schema '$ref' => '#/components/schemas/ChatDetail'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('messages').size).to be >= 1
end
end
response '404', 'chat not found' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:id) { SecureRandom.uuid }
run_test!
end
end
patch 'Update a chat' do
tags 'Chats'
security [ { bearerAuth: [] } ]
consumes 'application/json'
produces 'application/json'
let(:id) { chat.id }
parameter name: :chat_params, in: :body, required: true, schema: {
type: :object,
properties: {
title: { type: :string, example: 'Updated chat title' }
}
}
let(:chat_params) { { title: 'Updated budget plan' } }
response '200', 'chat updated' do
schema '$ref' => '#/components/schemas/ChatDetail'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('title')).to eq('Updated budget plan')
end
end
response '404', 'chat not found' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:id) { SecureRandom.uuid }
run_test!
end
response '422', 'validation error' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:chat_params) { { title: '' } }
run_test!
end
end
delete 'Delete a chat' do
tags 'Chats'
security [ { bearerAuth: [] } ]
produces 'application/json'
let(:id) { another_chat.id }
response '204', 'chat deleted' do
run_test!
end
response '404', 'chat not found' do
let(:id) { SecureRandom.uuid }
run_test!
end
end
end
path '/api/v1/chats/{chat_id}/messages' do
parameter name: :Authorization, in: :header, required: true, schema: { type: :string },
description: 'Bearer token with write scope'
parameter name: :chat_id, in: :path, type: :string, required: true, description: 'Chat ID'
post 'Create a message' do
tags 'Chat Messages'
security [ { bearerAuth: [] } ]
consumes 'application/json'
produces 'application/json'
let(:chat_id) { chat.id }
parameter name: :message_params, in: :body, required: true, schema: {
type: :object,
properties: {
content: { type: :string },
model: { type: :string }
},
required: %w[content]
}
let(:message_params) do
{
content: 'Please summarise the last conversation.',
model: 'gpt-4'
}
end
response '201', 'message created' do
schema '$ref' => '#/components/schemas/MessageResponse'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('ai_response_status')).to eq('pending')
end
end
response '404', 'chat not found' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:chat_id) { SecureRandom.uuid }
run_test!
end
response '422', 'validation error' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:message_params) { { content: '' } }
run_test!
end
end
end
path '/api/v1/chats/{chat_id}/messages/retry' do
parameter name: :Authorization, in: :header, required: true, schema: { type: :string },
description: 'Bearer token with write scope'
parameter name: :chat_id, in: :path, type: :string, required: true, description: 'Chat ID'
post 'Retry the last assistant response' do
tags 'Chat Messages'
security [ { bearerAuth: [] } ]
produces 'application/json'
let(:chat_id) { chat.id }
response '202', 'retry started' do
schema '$ref' => '#/components/schemas/RetryResponse'
before do
allow_any_instance_of(AssistantMessage).to receive(:valid?).and_return(true)
end
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('message')).to eq('Retry initiated')
end
end
response '404', 'chat not found' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:chat_id) { SecureRandom.uuid }
run_test!
end
response '422', 'no assistant message available' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:chat) do
user.chats.create!(title: 'Empty conversation')
end
let(:chat_id) { chat.id }
run_test!
end
end
end
end

View File

@@ -0,0 +1,360 @@
# frozen_string_literal: true
require 'swagger_helper'
RSpec.describe 'API V1 Transactions', type: :request do
let(:family) do
Family.create!(
name: 'API Family',
currency: 'USD',
locale: 'en',
date_format: '%m-%d-%Y'
)
end
let(:user) do
family.users.create!(
email: 'api-user@example.com',
password: 'password123',
password_confirmation: 'password123'
)
end
let(:oauth_application) do
Doorkeeper::Application.create!(
name: 'API Docs',
redirect_uri: 'https://example.com/callback',
scopes: 'read read_write'
)
end
let(:access_token) do
Doorkeeper::AccessToken.create!(
application: oauth_application,
resource_owner_id: user.id,
scopes: 'read_write',
expires_in: 2.hours,
token: SecureRandom.hex(32)
)
end
let(:Authorization) { "Bearer #{access_token.token}" }
let(:account) do
Account.create!(
family: family,
name: 'Checking Account',
balance: 1000,
currency: 'USD',
accountable: Depository.create!
)
end
let(:category) do
family.categories.create!(
name: 'Groceries',
classification: 'expense',
color: '#4CAF50',
lucide_icon: 'shopping-cart'
)
end
let(:merchant) do
family.merchants.create!(name: 'Whole Foods')
end
let(:tag) do
family.tags.create!(name: 'Essential', color: '#2196F3')
end
let!(:transaction) do
entry = account.entries.create!(
name: 'Grocery shopping',
date: Date.current,
amount: 75.50,
currency: 'USD',
entryable: Transaction.new(
category: category,
merchant: merchant
)
)
entry.transaction.tags << tag
entry.transaction
end
let!(:another_transaction) do
entry = account.entries.create!(
name: 'Coffee',
date: Date.current - 1.day,
amount: 5.00,
currency: 'USD',
entryable: Transaction.new
)
entry.transaction
end
path '/api/v1/transactions' do
get 'List transactions' do
tags 'Transactions'
security [ { bearerAuth: [] } ]
produces 'application/json'
parameter name: :Authorization, in: :header, required: true, schema: { type: :string },
description: 'Bearer token with read scope'
parameter name: :page, in: :query, type: :integer, required: false,
description: 'Page number (default: 1)'
parameter name: :per_page, in: :query, type: :integer, required: false,
description: 'Items per page (default: 25, max: 100)'
parameter name: :account_id, in: :query, type: :string, required: false,
description: 'Filter by account ID'
parameter name: :category_id, in: :query, type: :string, required: false,
description: 'Filter by category ID'
parameter name: :merchant_id, in: :query, type: :string, required: false,
description: 'Filter by merchant ID'
parameter name: :start_date, in: :query, type: :string, format: :date, required: false,
description: 'Filter transactions from this date'
parameter name: :end_date, in: :query, type: :string, format: :date, required: false,
description: 'Filter transactions until this date'
parameter name: :min_amount, in: :query, type: :number, required: false,
description: 'Filter by minimum amount'
parameter name: :max_amount, in: :query, type: :number, required: false,
description: 'Filter by maximum amount'
parameter name: :type, in: :query, type: :string, enum: %w[income expense], required: false,
description: 'Filter by transaction type'
parameter name: :search, in: :query, type: :string, required: false,
description: 'Search by name, notes, or merchant name'
response '200', 'transactions listed' do
schema '$ref' => '#/components/schemas/TransactionCollection'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('transactions')).to be_present
expect(payload.fetch('pagination')).to include('page', 'per_page', 'total_count', 'total_pages')
end
end
response '200', 'transactions filtered by account' do
schema '$ref' => '#/components/schemas/TransactionCollection'
let(:account_id) { account.id }
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('transactions')).to be_present
end
end
response '200', 'transactions filtered by date range' do
schema '$ref' => '#/components/schemas/TransactionCollection'
let(:start_date) { (Date.current - 7.days).to_s }
let(:end_date) { Date.current.to_s }
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('transactions')).to be_present
end
end
end
post 'Create transaction' do
tags 'Transactions'
security [ { bearerAuth: [] } ]
consumes 'application/json'
produces 'application/json'
parameter name: :Authorization, in: :header, required: true, schema: { type: :string },
description: 'Bearer token with write scope'
parameter name: :body, in: :body, required: true, schema: {
type: :object,
properties: {
transaction: {
type: :object,
properties: {
account_id: { type: :string, format: :uuid, description: 'Account ID (required)' },
date: { type: :string, format: :date, description: 'Transaction date' },
amount: { type: :number, description: 'Transaction amount' },
name: { type: :string, description: 'Transaction name/description' },
notes: { type: :string, description: 'Additional notes' },
currency: { type: :string, description: 'Currency code (defaults to family currency)' },
category_id: { type: :string, format: :uuid, description: 'Category ID' },
merchant_id: { type: :string, format: :uuid, description: 'Merchant ID' },
nature: { type: :string, enum: %w[income expense inflow outflow], description: 'Transaction nature (determines sign)' },
tag_ids: { type: :array, items: { type: :string, format: :uuid }, description: 'Array of tag IDs' }
},
required: %w[account_id date amount name]
}
},
required: %w[transaction]
}
let(:body) do
{
transaction: {
account_id: account.id,
date: Date.current.to_s,
amount: 50.00,
name: 'Test purchase',
nature: 'expense',
category_id: category.id,
merchant_id: merchant.id
}
}
end
response '201', 'transaction created' do
schema '$ref' => '#/components/schemas/Transaction'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('name')).to eq('Test purchase')
expect(payload.fetch('account').fetch('id')).to eq(account.id)
end
end
response '422', 'validation error - missing account_id' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:body) do
{
transaction: {
date: Date.current.to_s,
amount: 50.00,
name: 'Test purchase'
}
}
end
run_test!
end
response '422', 'validation error - missing required fields' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:body) do
{
transaction: {
account_id: account.id
}
}
end
run_test!
end
end
end
path '/api/v1/transactions/{id}' do
parameter name: :Authorization, in: :header, required: true, schema: { type: :string },
description: 'Bearer token'
parameter name: :id, in: :path, type: :string, required: true, description: 'Transaction ID'
get 'Retrieve a transaction' do
tags 'Transactions'
security [ { bearerAuth: [] } ]
produces 'application/json'
let(:id) { transaction.id }
response '200', 'transaction retrieved' do
schema '$ref' => '#/components/schemas/Transaction'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('id')).to eq(transaction.id)
expect(payload.fetch('name')).to eq('Grocery shopping')
expect(payload.fetch('category').fetch('name')).to eq('Groceries')
expect(payload.fetch('merchant').fetch('name')).to eq('Whole Foods')
expect(payload.fetch('tags').first.fetch('name')).to eq('Essential')
end
end
response '404', 'transaction not found' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:id) { SecureRandom.uuid }
run_test!
end
end
patch 'Update a transaction' do
tags 'Transactions'
security [ { bearerAuth: [] } ]
consumes 'application/json'
produces 'application/json'
let(:id) { transaction.id }
parameter name: :body, in: :body, required: true, schema: {
type: :object,
properties: {
transaction: {
type: :object,
properties: {
date: { type: :string, format: :date },
amount: { type: :number },
name: { type: :string },
notes: { type: :string },
category_id: { type: :string, format: :uuid },
merchant_id: { type: :string, format: :uuid },
nature: { type: :string, enum: %w[income expense inflow outflow] },
tag_ids: { type: :array, items: { type: :string, format: :uuid } }
}
}
}
}
let(:body) do
{
transaction: {
name: 'Updated grocery shopping',
notes: 'Weekly groceries'
}
}
end
response '200', 'transaction updated' do
schema '$ref' => '#/components/schemas/Transaction'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('name')).to eq('Updated grocery shopping')
expect(payload.fetch('notes')).to eq('Weekly groceries')
end
end
response '404', 'transaction not found' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:id) { SecureRandom.uuid }
run_test!
end
end
delete 'Delete a transaction' do
tags 'Transactions'
security [ { bearerAuth: [] } ]
produces 'application/json'
let(:id) { another_transaction.id }
response '200', 'transaction deleted' do
schema '$ref' => '#/components/schemas/DeleteResponse'
run_test! do |response|
payload = JSON.parse(response.body)
expect(payload.fetch('message')).to eq('Transaction deleted successfully')
end
end
response '404', 'transaction not found' do
schema '$ref' => '#/components/schemas/ErrorResponse'
let(:id) { SecureRandom.uuid }
run_test!
end
end
end
end