Skip to content

Commit

Permalink
improve test
Browse files Browse the repository at this point in the history
  • Loading branch information
morphy2k committed Aug 14, 2023
1 parent 5f9f387 commit 59272f5
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions middleware/jwt/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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{},
},
}

Expand All @@ -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)
}
})
}
Expand Down

0 comments on commit 59272f5

Please sign in to comment.