From 59272f5d108e2a2e6ce9ea5b53614200b3bda15c Mon Sep 17 00:00:00 2001 From: Markus Wiegand Date: Mon, 14 Aug 2023 22:30:39 +0200 Subject: [PATCH] improve test --- middleware/jwt/jwt_test.go | 39 ++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/middleware/jwt/jwt_test.go b/middleware/jwt/jwt_test.go index c1a90ac..569839c 100644 --- a/middleware/jwt/jwt_test.go +++ b/middleware/jwt/jwt_test.go @@ -64,11 +64,12 @@ func TestHmacSignVerify(t *testing.T) { func TestEd25519Verify(t *testing.T) { tests := []struct { - name string - claims *Claims - certFile string - keyFile string - expectError bool + name string + claims *Claims + certFile string + keyFile string + expectedErr error + expectedCustomErr interface{} }{ { name: "valid token", @@ -77,11 +78,11 @@ func TestEd25519Verify(t *testing.T) { keyFile: "testdata/key.pem", }, { - name: "invalid token", - claims: &Claims{}, - certFile: "testdata/certs_invalid.crt", - keyFile: "testdata/key_invalid.pem", - expectError: true, + name: "invalid token", + claims: &Claims{}, + certFile: "testdata/certs_invalid.crt", + keyFile: "testdata/key_invalid.pem", + expectedCustomErr: x509.UnknownAuthorityError{}, }, } @@ -93,14 +94,16 @@ func TestEd25519Verify(t *testing.T) { } _, err = VerifyToken(token) - if tt.expectError { - if errors.Is(err, x509.UnknownAuthorityError{}) { - t.Error("Token verification failed: expected error not returned") - } - } else { - if err != nil { - t.Errorf("Token verification failed: %v", err) - } + if tt.expectedErr != nil && !errors.Is(err, tt.expectedErr) { + t.Errorf("Token verification failed: expected error %v, got %v", tt.expectedErr, err) + return + } + if tt.expectedCustomErr != nil && !errors.As(err, &tt.expectedCustomErr) { + t.Errorf("Token verification failed: expected error type %T, got %v", tt.expectedCustomErr, err) + return + } + if tt.expectedErr == nil && tt.expectedCustomErr == nil && err != nil { + t.Errorf("Token verification failed: %v", err) } }) }