Skip to content

Commit

Permalink
Update TestValidateToken for token expiration
Browse files Browse the repository at this point in the history
Update the TestValidateToken function to include testing token expiration. The test now includes waiting for the token to expire, generating an expired token, and ensuring that validation returns an error with the appropriate message.
  • Loading branch information
mtnmunuklu committed Feb 6, 2024
1 parent 2f862bf commit f50789c
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion security/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,32 @@ func TestNewToken(t *testing.T) {

// TestValidateToken tests the validation of a token.
func TestValidateToken(t *testing.T) {
// Generate a new ObjectId for testing purposes
id := bson.NewObjectId()

// Create a new token with the generated ObjectId
token, err := NewToken(id.Hex())
assert.NoError(t, err)
assert.NotEmpty(t, token)

// Validate the token and ensure no errors
userId, err := ValidateToken(token)
assert.NoError(t, err)
assert.NotNil(t, userId)
assert.Equal(t, id.Hex(), userId)

// Wait for the token to expire (e.g., 1 second) to test expiration
time.Sleep(time.Second)

// Generate an expired token for testing purposes
tokenExpired := GetTokenExpired(id.Hex())

// Attempt to validate the expired token and ensure it returns an error
userId, err = ValidateToken(tokenExpired)
assert.Error(t, err)
assert.Nil(t, userId)
assert.Equal(t, userId, "")
assert.EqualError(t, err, "token has invalid claims: token is expired")

}

// GetTokenExpired tests the operation of receiving the token expire duration.
Expand Down

0 comments on commit f50789c

Please sign in to comment.