Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
morphy2k committed Aug 14, 2023
1 parent 59272f5 commit 019d9e1
Showing 1 changed file with 58 additions and 66 deletions.
124 changes: 58 additions & 66 deletions middleware/jwt/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,29 @@ func init() {

const badToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"

func TestHmacSignVerify(t *testing.T) {
func TestSymmetricSignVerify(t *testing.T) {
claimsIn := &Claims{}

token, err := SignToken(claimsIn, nil)
if err != nil {
t.Errorf("Token creation failed: %v", err)
t.Fatalf("Token creation failed: %v", err)
}

claimsOut, err := VerifyToken(token)
if err != nil {
t.Errorf("Token verification failed: %v", err)
t.Fatalf("Token verification failed: %v", err)
}

if !reflect.DeepEqual(claimsIn, claimsOut) {
t.Errorf("Token claim validation failed: claims %v and %v unequal", claimsIn, claimsOut)
t.Fatalf("Token claim validation failed: claims %v and %v unequal", claimsIn, claimsOut)
}

if _, err := VerifyToken(badToken); err == nil {
t.Error("Token verification failed: invalid token verified as valid")
}
}

func TestEd25519Verify(t *testing.T) {
func TestAsymmetricVerify(t *testing.T) {
tests := []struct {
name string
claims *Claims
Expand Down Expand Up @@ -95,12 +95,10 @@ func TestEd25519Verify(t *testing.T) {

_, err = VerifyToken(token)
if tt.expectedErr != nil && !errors.Is(err, tt.expectedErr) {
t.Errorf("Token verification failed: expected error %v, got %v", tt.expectedErr, err)
return
t.Fatalf("Token verification failed: expected error %v, got %v", tt.expectedErr, err)
}
if tt.expectedCustomErr != nil && !errors.As(err, &tt.expectedCustomErr) {
t.Errorf("Token verification failed: expected error type %T, got %v", tt.expectedCustomErr, err)
return
t.Fatalf("Token verification failed: expected error type %T, got %v", tt.expectedCustomErr, err)
}
if tt.expectedErr == nil && tt.expectedCustomErr == nil && err != nil {
t.Errorf("Token verification failed: %v", err)
Expand All @@ -115,79 +113,73 @@ func TestExtractToken(t *testing.T) {

token, err := ExtractToken(&http.Request{Header: header})
if err != nil {
t.Errorf("Getting token failed: %v", err)
t.Fatalf("Getting token failed: %v", err)
}

if token != badToken {
t.Errorf("Getting token failed: token %s and %s unequal", token, badToken)
t.Fatalf("Getting token failed: token %s and %s unequal", token, badToken)
}
}

func TestAuhtorizationHandler(t *testing.T) {
validClaims := &Claims{
Scope: []string{
ScopeUserRead,
tests := []struct {
name string
claims *Claims
certFile string
keyFile string
expectedCode int
}{
{
name: "valid token",
claims: &Claims{
Scope: []string{ScopeUserRead},
},
certFile: "testdata/certs.crt",
keyFile: "testdata/key.pem",
expectedCode: http.StatusOK,
},
}

validToken, err := signTestingToken(validClaims, "testdata/certs.crt", "testdata/key.pem")
if err != nil {
t.Fatalf("Getting token failed: %s", err)
}

header := http.Header{}
header.Add("Authorization", fmt.Sprintf("Bearer %s", validToken))

handle := AuhtorizationHandler(ScopeUserRead, func(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
w.WriteHeader(http.StatusOK)
})

w := httptest.NewRecorder()

handle(w, &http.Request{Header: header}, httprouter.Params{})

resp := w.Result()
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
t.Fatalf("Authorization handler failed: unexpcted response code %v", resp.StatusCode)
}

header.Set("Authorization", fmt.Sprintf("Bearer %s", badToken))

w = httptest.NewRecorder()

handle(w, &http.Request{Header: header}, httprouter.Params{})

resp = w.Result()
defer resp.Body.Close()

if resp.StatusCode != http.StatusUnauthorized {
t.Fatalf("Authorization handler failed: unexpcted response code %v", resp.StatusCode)
}

invalidClaims := &Claims{
Scope: []string{
ScopeItemRead,
{
name: "invalid token",
claims: &Claims{},
certFile: "testdata/certs_invalid.crt",
keyFile: "testdata/key_invalid.pem",
expectedCode: http.StatusUnauthorized,
},
{
name: "invalid scope token",
claims: &Claims{
Scope: []string{ScopeItemRead},
},
certFile: "testdata/certs.crt",
keyFile: "testdata/key.pem",
expectedCode: http.StatusForbidden,
},
}

invalidToken, err := signTestingToken(invalidClaims, "testdata/certs.crt", "testdata/key.pem")
if err != nil {
t.Fatalf("Authorization handler failed: %s", err)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
token, err := signTestingToken(tt.claims, tt.certFile, tt.keyFile)
if err != nil {
t.Fatalf("Getting token failed: %s", err)
}

header.Set("Authorization", fmt.Sprintf("Bearer %s", invalidToken))
header := http.Header{}
header.Add("Authorization", fmt.Sprintf("Bearer %s", token))

w = httptest.NewRecorder()
handle := AuhtorizationHandler(ScopeUserRead, func(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
w.WriteHeader(http.StatusOK)
})

handle(w, &http.Request{Header: header}, httprouter.Params{})
w := httptest.NewRecorder()
handle(w, &http.Request{Header: header}, httprouter.Params{})

resp = w.Result()
defer resp.Body.Close()
resp := w.Result()
defer resp.Body.Close()

if resp.StatusCode != http.StatusForbidden {
t.Fatalf("Authorization handler failed: unexpcted response code %v", resp.StatusCode)
if resp.StatusCode != tt.expectedCode {
t.Fatalf("Authorization handler failed: unexpected response code %v", resp.StatusCode)
}
})
}
}

Expand Down

0 comments on commit 019d9e1

Please sign in to comment.