Commit fc694a9d authored by Alexis POYEN's avatar Alexis POYEN
Browse files

Feat : Update API and add more tests

parent faadbe0c
Pipeline #4489 failed with stages
in 3 minutes and 11 seconds
......@@ -112,6 +112,9 @@ func (d *DataHandler) HandleClients(w http.ResponseWriter, r *http.Request) {
http.Error(w, "id does not exist", http.StatusNotFound)
return
}
// Delete bank accounts of user
d.db.Where("user_client_id = ?", o.ID).Delete(&BankAccount{})
d.db.Delete(&o)
} else {
http.Error(w, "id is missing", http.StatusNotFound)
......@@ -201,7 +204,7 @@ func (d *DataHandler) HandleOperations(w http.ResponseWriter, r *http.Request) {
// Add the operation to creditor
op := Operation{
Debtor: o.Creditor,
Amount: o.Amount,
Amount: -o.Amount,
Date: now,
Creditor: o.Debtor,
}
......@@ -218,7 +221,29 @@ func (d *DataHandler) HandleOperations(w http.ResponseWriter, r *http.Request) {
http.Error(w, "id does not exist", http.StatusNotFound)
return
}
d.db.Delete(&o)
// update BankAccounts
var debtor BankAccount
var creditor BankAccount
if err := d.db.First(&debtor, o.Debtor).Error; err == nil {
if err := d.db.First(&creditor, o.Creditor).Error; err == nil {
// Update BankAccounts
debtor.Amount -= o.Amount
creditor.Amount += o.Amount
d.db.Save(&debtor)
d.db.Save(&creditor)
// Get the operation of the creditor
var op Operation
if err := d.db.First(&op, id+1).Error; err != nil {
http.Error(w, "id does not exist", http.StatusNotFound)
return
}
// Delete the operations
d.db.Delete(&o)
d.db.Delete(&op)
}
}
} else {
http.Error(w, "id is missing", http.StatusNotFound)
}
......
......@@ -75,14 +75,14 @@ func TestHandleOperations(t *testing.T) {
do("POST", "/api/Operations", noH, `{"Debtor":1,"Amount":-100,"Creditor":3}`, 200, "")
// Get operation where id=1
do("GET", "/api/Operations/1", noH, "", 200, `{"ID":1,"Debtor":1,"Amount":-100`)
// Get all Bank account
do("GET", "/api/Operations/", noH, "", 200, `[{"ID":1,"Debtor":1,"Amount":-100`)
// Get Operations for creditor (opposite operation should have been registered)
do("GET", "/api/Operations/2", noH, "", 200, `{"ID":2,"Debtor":3,"Amount":100`)
// Add invalid operation between client and Bakery must be refused with 417 (Expectation failed)
do("POST", "/api/Operations", noH, `{"Debtor":1,"Amount":-1789,"Creditor":3}`, 417, "Not enough money")
}
func TestHandleUserClientWithAccounts(t *testing.T) {
func TestClientWithAccountsAndOperations(t *testing.T) {
// Create the handler
dh := NewDataHandler()
ts := httptest.NewServer(http.HandlerFunc(dh.HandleClients))
......@@ -103,3 +103,97 @@ func TestHandleUserClientWithAccounts(t *testing.T) {
// // Try to get the first user again
// do("GET", "/api/UserBanks/1", noH, "", 404, `id does not exist`)
}
func TestAccountsWithOperations(t *testing.T) {
// Create the handler
dh := NewDataHandler()
ts := httptest.NewServer(http.HandlerFunc(dh.HandleBankAccounts))
defer ts.Close()
url, _ := url.Parse(ts.URL)
port := url.Port()
// Wrap the testing function
do := tester.CreateServerTester(t, port, "sdk-go.io", nil)
noH := tester.Header{Key: "", Value: ""}
// Verify operation on client Dupond checking-account
do("GET", "/api/BankAccounts/1", noH, "", 200, `{"ID":1,"Number":"01-01","UserClientID":1,"Type":"checking-account","Amount":358,"BankOverdraft":-100,"Operations":[{"ID":1,"Debtor":1,"Amount":-100`)
// Verify operation on client Bakery checking-account
do("GET", "/api/BankAccounts/3", noH, "", 200, `{"ID":3,"Number":"02-01","UserClientID":2,"Type":"checking-account","Amount":4845,"BankOverdraft":-500,"Operations":[{"ID":2,"Debtor":3,"Amount":100`)
}
func TestDeleteOperations(t *testing.T) {
// Create the handler
dh := NewDataHandler()
ts := httptest.NewServer(http.HandlerFunc(dh.HandleOperations))
defer ts.Close()
url, _ := url.Parse(ts.URL)
port := url.Port()
// Wrap the testing function
do := tester.CreateServerTester(t, port, "sdk-go.io", nil)
noH := tester.Header{Key: "", Value: ""}
// Try to delete the first operation
do("DELETE", "/api/Operations/1", noH, ``, 200, "")
// Try to get the first user again
do("GET", "/api/Operations/1", noH, "", 404, `id does not exist`)
// The operation id=2 (opposite operation) should also have been deleted
do("GET", "/api/Operations/2", noH, "", 404, `id does not exist`)
}
func TestDeleteBankAccount(t *testing.T) {
// Create the handler
dh := NewDataHandler()
ts := httptest.NewServer(http.HandlerFunc(dh.HandleBankAccounts))
defer ts.Close()
url, _ := url.Parse(ts.URL)
port := url.Port()
// Wrap the testing function
do := tester.CreateServerTester(t, port, "sdk-go.io", nil)
noH := tester.Header{Key: "", Value: ""}
// Test that bank account have been updated after operations deletion
do("GET", "/api/BankAccounts/1", noH, "", 200, `{"ID":1,"Number":"01-01","UserClientID":1,"Type":"checking-account","Amount":458,"BankOverdraft":-100,"Operations":[]}`)
do("GET", "/api/BankAccounts/3", noH, "", 200, `{"ID":3,"Number":"02-01","UserClientID":2,"Type":"checking-account","Amount":4745,"BankOverdraft":-500,"Operations":[]}`)
// Try to delete the saving account of Dupond
do("DELETE", "/api/BankAccounts/2", noH, ``, 200, "")
// Try to get the first user again
do("GET", "/api/BankAccounts/2", noH, "", 404, `id does not exist`)
}
func TestUserAfterDeletions(t *testing.T) {
// Create the handler
dh := NewDataHandler()
ts := httptest.NewServer(http.HandlerFunc(dh.HandleClients))
defer ts.Close()
url, _ := url.Parse(ts.URL)
port := url.Port()
// Wrap the testing function
do := tester.CreateServerTester(t, port, "sdk-go.io", nil)
noH := tester.Header{Key: "", Value: ""}
// Test that client have been updated after operations deletion and bank account deletion
// Get client Dupond with his banks accounts and operations up to date (-100€ on checking-account)
do("GET", "/api/UserClients/1", noH, "", 200, `{"ID":1,"Name":"Dupond","BankAccounts":[{"ID":1,"Number":"01-01","UserClientID":1,"Type":"checking-account","Amount":458,"BankOverdraft":-100,"Operations":null}]}`)
// Try to delete the saving account of Dupond
do("DELETE", "/api/UserClients/2", noH, ``, 200, "")
// Try to get the first user again
do("GET", "/api/UserClients/2", noH, "", 404, `id does not exist`)
}
func TestBankAccountDeletion(t *testing.T) {
// Create the handler
dh := NewDataHandler()
ts := httptest.NewServer(http.HandlerFunc(dh.HandleBankAccounts))
defer ts.Close()
url, _ := url.Parse(ts.URL)
port := url.Port()
// Wrap the testing function
do := tester.CreateServerTester(t, port, "sdk-go.io", nil)
noH := tester.Header{Key: "", Value: ""}
// Test that bank account have been deleted after client deletion
do("GET", "/api/BankAccounts/3", noH, "", 404, `id does not exist`)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment