From 36497ade1be479db71dd5ecd274a1a05b498fa1e Mon Sep 17 00:00:00 2001
From: Alexis Poyen <apoyen@mail.apoyen.fr>
Date: Thu, 9 Apr 2020 09:48:18 +0200
Subject: [PATCH] Test : add app test

---
 internal/rootmux/rootmux_test.go | 45 +++++++++++++++++++++++++++-----
 1 file changed, 39 insertions(+), 6 deletions(-)

diff --git a/internal/rootmux/rootmux_test.go b/internal/rootmux/rootmux_test.go
index f505692..4fb2054 100644
--- a/internal/rootmux/rootmux_test.go
+++ b/internal/rootmux/rootmux_test.go
@@ -62,6 +62,8 @@ func TestAll(t *testing.T) {
 	BankerTests(t)
 	resetData(t)
 	AdminTests(t)
+	resetData(t)
+	appTests(t)
 
 	os.RemoveAll("./data")
 }
@@ -77,15 +79,46 @@ func Oauth2Tests(t *testing.T) {
 	do("GET", "/OAuth2Login", noH, "", 500, "invalid oauth state")
 }
 
-// func createUserTests(t *testing.T) {
+func appTests(t *testing.T) {
+
+	ts, do, _ := createTester(t)
+	defer ts.Close() // Close the tester
+	tests := func() {
+		// Get the XSRF Token
+		response := do("GET", "/api/common/WhoAmI", noH, "", 200, "")
+		token := auth.TokenData{}
+		json.Unmarshal([]byte(response), &token)
+		xsrfHeader := tester.Header{Key: "XSRF-TOKEN", Value: token.XSRFToken}
+
+		// Add invalid operation between client and Bakery must be refused with 417 (Expectation failed)
+		do("POST", "/api/Operations", xsrfHeader, `{"Debtor":1,"Amount":-1789,"Creditor":2}`, 417, "Not enough money")
 
-// }
+		// Add an operation between Dupond and Bakery and verify that bank accounts are updated and opposite operation is created
+		do("POST", "/api/Operations", xsrfHeader, `{"Debtor":1,"Amount":-100,"Creditor":2}`, 200, "")
+		do("GET", "/api/Operations/1", xsrfHeader, "", 200, `{"ID":1,"Debtor":1,"Amount":-100`)
+		do("GET", "/api/Operations/2", xsrfHeader, "", 200, `{"ID":2,"Debtor":2,"Amount":100`)
+		do("GET", "/api/BankAccounts/1", xsrfHeader, "", 200, `{"ID":1,"Number":"01-01","UserClientID":1,"Type":"checking-account","Amount":358,"BankOverdraft":-100,"Operations":[{"ID":1,"Debtor":1,"Amount":-100,"Date":"`)
+		do("GET", "/api/BankAccounts/2", xsrfHeader, "", 200, `{"ID":2,"Number":"02-01","UserClientID":2,"Type":"checking-account","Amount":4845,"BankOverdraft":-500,"Operations":[{"ID":2,"Debtor":2,"Amount":100,"Date":`)
 
-// func appTests(t *testing.T) {
-// 	// Add invalid operation between client and Bakery must be refused with 417 (Expectation failed)
-// 	// do("POST", "/api/Operations", xsrfHeader, `{"Debtor":1,"Amount":-1789,"Creditor":3}`, 417, "Not enough money")
+		// Try to delete the first operation, the opposite operation should also have been deleted and bank accounts updated
+		do("DELETE", "/api/Operations/1", xsrfHeader, ``, 200, "")
+		do("GET", "/api/Operations/1", xsrfHeader, "", 404, `id does not exist`)
+		do("GET", "/api/Operations/2", xsrfHeader, "", 404, `id does not exist`)
+		do("GET", "/api/BankAccounts/1", xsrfHeader, "", 200, `{"ID":1,"Number":"01-01","UserClientID":1,"Type":"checking-account","Amount":458,"BankOverdraft":-100,"Operations":[]}`)
+		do("GET", "/api/BankAccounts/2", xsrfHeader, "", 200, `{"ID":2,"Number":"02-01","UserClientID":2,"Type":"checking-account","Amount":4745,"BankOverdraft":-500,"Operations":[]}`)
 
-// }
+		// Delete a client should also delete his banks accounts
+		do("DELETE", "/api/UserClients/1", xsrfHeader, ``, 200, "")
+		do("GET", "/api/BankAccounts/1", xsrfHeader, "", 404, `id does not exist`)
+
+	}
+	// Do an OAuth2 login with an known admin
+	do("GET", "/OAuth2Login", noH, "", 200, "<!DOCTYPE html>")
+	tests()
+	// Try to logout (must pass)
+	do("GET", "/Logout", noH, "", 200, "Logout OK")
+
+}
 
 func resetData(t *testing.T) {
 	os.RemoveAll("./data")
-- 
GitLab