From 39015963e6132f57422ff19738103070f85fb278 Mon Sep 17 00:00:00 2001 From: Daniel Krol Date: Tue, 21 Jun 2022 19:27:54 -0400 Subject: [PATCH] wallet request validation test. loopify auth request validation test. --- server/auth_test.go | 52 ++++++++++++++++++++++--------------------- server/wallet_test.go | 31 ++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 27 deletions(-) diff --git a/server/auth_test.go b/server/auth_test.go index 234cc33..0fe96c8 100644 --- a/server/auth_test.go +++ b/server/auth_test.go @@ -101,31 +101,33 @@ func TestServerValidateAuthRequest(t *testing.T) { t.Fatalf("Expected valid AuthRequest to successfully validate") } - authRequest = AuthRequest{Email: "joe@example.com", Password: "aoeu"} - if authRequest.validate() { - t.Fatalf("Expected AuthRequest with missing device to not successfully validate") + tt := []struct { + authRequest AuthRequest + failureDescription string + }{ + { + AuthRequest{Email: "joe@example.com", Password: "aoeu"}, + "Expected AuthRequest with missing device to not successfully validate", + }, { + AuthRequest{DeviceId: "dId", Email: "joe-example.com", Password: "aoeu"}, + "Expected AuthRequest with invalid email to not successfully validate", + }, { + // Note that Golang's email address parser, which I use, will accept + // "Joe " so we need to make sure to avoid accepting it. See + // the implementation. + AuthRequest{DeviceId: "dId", Email: "Joe ", Password: "aoeu"}, + "Expected AuthRequest with email with unexpected formatting to not successfully validate", + }, { + AuthRequest{DeviceId: "dId", Password: "aoeu"}, + "Expected AuthRequest with missing email to not successfully validate", + }, { + AuthRequest{DeviceId: "dId", Email: "joe@example.com"}, + "Expected AuthRequest with missing password to not successfully validate", + }, } - - authRequest = AuthRequest{DeviceId: "dId", Email: "joe-example.com", Password: "aoeu"} - if authRequest.validate() { - t.Fatalf("Expected AuthRequest with invalid email to not successfully validate") - } - - // Note that Golang's email address parser, which I use, will accept - // "Joe " so we need to make sure to avoid accepting it. See - // the implementation. - authRequest = AuthRequest{DeviceId: "dId", Email: "Joe ", Password: "aoeu"} - if authRequest.validate() { - t.Fatalf("Expected AuthRequest with email with unexpected formatting to not successfully validate") - } - - authRequest = AuthRequest{DeviceId: "dId", Password: "aoeu"} - if authRequest.validate() { - t.Fatalf("Expected AuthRequest with missing email to not successfully validate") - } - - authRequest = AuthRequest{DeviceId: "dId", Email: "joe@example.com"} - if authRequest.validate() { - t.Fatalf("Expected AuthRequest with missing password to not successfully validate") + for _, tc := range tt { + if tc.authRequest.validate() { + t.Errorf(tc.failureDescription) + } } } diff --git a/server/wallet_test.go b/server/wallet_test.go index cd9931b..7de1037 100644 --- a/server/wallet_test.go +++ b/server/wallet_test.go @@ -357,6 +357,33 @@ func TestServerPostWallet(t *testing.T) { } func TestServerValidateWalletRequest(t *testing.T) { - // also add a basic test case for this in TestServerAuthHandlerSuccess to make sure it's called at all - t.Fatalf("Test me: Implement and test WalletRequest.validate()") + walletRequest := WalletRequest{Token: "seekrit", EncryptedWallet: "my-encrypted-wallet", Hmac: "my-hmac", Sequence: 2} + if !walletRequest.validate() { + t.Fatalf("Expected valid WalletRequest to successfully validate") + } + + tt := []struct { + walletRequest WalletRequest + failureDescription string + }{ + { + WalletRequest{EncryptedWallet: "my-encrypted-wallet", Hmac: "my-hmac", Sequence: 2}, + "Expected WalletRequest with missing token to not successfully validate", + }, { + WalletRequest{Token: "seekrit", Hmac: "my-hmac", Sequence: 2}, + "Expected WalletRequest with missing encrypted wallet to not successfully validate", + }, { + WalletRequest{Token: "seekrit", EncryptedWallet: "my-encrypted-wallet", Sequence: 2}, + "Expected WalletRequest with missing hmac to not successfully validate", + }, { + WalletRequest{Token: "seekrit", EncryptedWallet: "my-encrypted-wallet", Hmac: "my-hmac", Sequence: 0}, + "Expected WalletRequest with sequence < 1 to not successfully validate", + }, + } + for _, tc := range tt { + if tc.walletRequest.validate() { + t.Errorf(tc.failureDescription) + } + + } }