Add API v1 finance_overview endpoint
This commit is contained in:
parent
4752a0aaa9
commit
7a6779ebfd
5 changed files with 140 additions and 0 deletions
|
|
@ -27,6 +27,16 @@ describe 'API v1', type: :apivore, order: :defined do
|
|||
it_handles_invalid_token_and_scope(:get, '/user')
|
||||
end
|
||||
|
||||
context 'user/financial_overview' do
|
||||
let(:api_scopes) { ['finance:user'] }
|
||||
let!(:user) { create :user, :ordergroup }
|
||||
|
||||
it { is_expected.to validate(:get, '/user/financial_overview', 200, api_auth) }
|
||||
it { is_expected.to validate(:get, '/user/financial_overview', 401) }
|
||||
|
||||
it_handles_invalid_token_and_scope(:get, '/user/financial_overview')
|
||||
end
|
||||
|
||||
context 'user/financial_transactions' do
|
||||
let(:api_scopes) { ['finance:user'] }
|
||||
let(:other_user) { create :user, :ordergroup }
|
||||
|
|
|
|||
55
spec/api/v1/user/ordergroup_spec.rb
Normal file
55
spec/api/v1/user/ordergroup_spec.rb
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Api::V1::User::OrdergroupController, type: :controller do
|
||||
include ApiOAuth
|
||||
let(:user) { create :user, :ordergroup }
|
||||
let(:api_scopes) { ['finance:user'] }
|
||||
|
||||
let(:ftc1) { create :financial_transaction_class }
|
||||
let(:ftc2) { create :financial_transaction_class }
|
||||
let(:ftt1) { create :financial_transaction_type, financial_transaction_class: ftc1 }
|
||||
let(:ftt2) { create :financial_transaction_type, financial_transaction_class: ftc2 }
|
||||
let(:ftt3) { create :financial_transaction_type, financial_transaction_class: ftc2 }
|
||||
|
||||
describe "GET :financial_overview" do
|
||||
let(:order) { create(:order, article_count: 1) }
|
||||
let(:oa_1) { order.order_articles.first }
|
||||
|
||||
let!(:go) { create(:group_order, order: order, ordergroup: user.ordergroup) }
|
||||
let!(:goa) { create(:group_order_article, group_order: go, order_article: oa_1, quantity: 1, tolerance: 0) }
|
||||
before { go.update_price!; user.ordergroup.update_stats! }
|
||||
|
||||
let(:json_financial_overview) { json_response['financial_overview'] }
|
||||
|
||||
before do
|
||||
og = user.ordergroup
|
||||
og.add_financial_transaction!(-1, '-1', user, ftt1)
|
||||
og.add_financial_transaction!(2, '2', user, ftt1)
|
||||
og.add_financial_transaction!(3, '3', user, ftt1)
|
||||
|
||||
og.add_financial_transaction!(-10, '-10', user, ftt2)
|
||||
og.add_financial_transaction!(20, '20', user, ftt2)
|
||||
og.add_financial_transaction!(30, '30', user, ftt2)
|
||||
|
||||
og.add_financial_transaction!(-100, '-100', user, ftt3)
|
||||
og.add_financial_transaction!(200, '200', user, ftt3)
|
||||
og.add_financial_transaction!(300, '300', user, ftt3)
|
||||
end
|
||||
|
||||
it "returns correct values" do
|
||||
get :financial_overview, params: { foodcoop: 'f' }
|
||||
expect(json_financial_overview['account_balance']).to eq 444
|
||||
expect(json_financial_overview['available_funds']).to eq 444 - go.price
|
||||
|
||||
ftcs = Hash[ json_financial_overview['financial_transaction_class_sums'].map { |x| [x['id'], x] } ]
|
||||
|
||||
ftcs1 = ftcs[ftc1.id]
|
||||
expect(ftcs1['name']).to eq ftc1.name
|
||||
expect(ftcs1['amount']).to eq 4
|
||||
|
||||
ftcs2 = ftcs[ftc2.id]
|
||||
expect(ftcs2['name']).to eq ftc2.name
|
||||
expect(ftcs2['amount']).to eq 440
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue