Yimaru-BackEnd/internal/web_server/jwt/jwt_test.go
2025-03-28 01:30:55 +03:00

90 lines
3.4 KiB
Go

package user
// func TestCreateJwt(t *testing.T) {
// // Define a user to test
// user := &domain.User{
// ID: 123,
// }
// // Secret key used for signing the JWT
// secretKey := "secret"
// // Token expiry time (in seconds)
// expiry := 3600 // 1 hour
// // Call CreateJwt function
// tokenString, err := CreateJwt(user, secretKey, expiry)
// // Assertions
// assert.NoError(t, err, "Error should be nil when creating a JWT")
// assert.NotEmpty(t, tokenString, "Token string should not be empty")
// // Parse the token back and verify its claims
// claims, err := ParseJwt(tokenString, secretKey)
// assert.NoError(t, err, "Error should be nil when parsing the JWT")
// assert.Equal(t, strconv.Itoa(int(user.ID)), claims.UserId, "User ID should match")
// assert.Equal(t, "github.com/lafetz/snippitstash", claims.Issuer, "Issuer should match")
// assert.True(t, claims.ExpiresAt.Time.After(time.Now()), "Token should not be expired yet")
// expectedExpiryTime := time.Now().Add(time.Duration(expiry) * time.Second)
// // Allow for a small margin of error due to the time delay in generating the token
// assert.True(t, claims.ExpiresAt.Time.Before(expectedExpiryTime.Add(1*time.Second)),
// "Token expiry time should be within the expected range")
// assert.True(t, claims.ExpiresAt.Time.After(expectedExpiryTime.Add(-1*time.Second)),
// "Token expiry time should be within the expected range")
// }
// func TestParseJwt(t *testing.T) {
// // Define a user to test
// user := &domain.User{
// ID: 123,
// }
// // Secret key used for signing the JWT
// secretKey := "secret"
// // Token expiry time (in seconds)
// expiry := 3600 // 1 hour
// // Generate a token using the CreateJwt function
// tokenString, err := CreateJwt(user, secretKey, expiry)
// assert.NoError(t, err, "Error should be nil when creating a JWT")
// assert.NotEmpty(t, tokenString, "Token string should not be empty")
// // Now, we will parse the token
// claims, err := ParseJwt(tokenString, secretKey)
// assert.NoError(t, err, "Error should be nil when parsing the JWT")
// assert.NotNil(t, claims, "Claims should not be nil")
// // Verify that the claims match the user and other values
// assert.Equal(t, strconv.Itoa(int(user.ID)), claims.UserId, "User ID should match")
// assert.Equal(t, "github.com/lafetz/snippitstash", claims.Issuer, "Issuer should match")
// assert.Equal(t, "fortune.com", claims.Audience[0], "Audience should match")
// assert.True(t, claims.ExpiresAt.Time.After(time.Now()), "Token should not be expired yet")
// // Ensure the parsing fails when using an invalid token
// invalidToken := tokenString + "invalid"
// _, err = ParseJwt(invalidToken, secretKey)
// assert.Error(t, err, "Parsing an invalid token should return an error")
// }
// func TestParseJwte(t *testing.T) {
// // Define user and key
// user := &domain.User{ID: 1}
// key := "secretkey"
// // Test valid token (not expired)
// validJwt, err := CreateJwt(user, key, 4) // Set expiry to 10 seconds
// assert.NoError(t, err)
// // Test if the token is parsed correctly
// claims, err := ParseJwt(validJwt, key)
// assert.NoError(t, err)
// assert.Equal(t, "1", claims.UserId)
// // Wait for token to expire
// time.Sleep(5 * time.Second) // Wait longer than the expiry time to test expiration
// // Test expired token
// _, err = ParseJwt(validJwt, key)
// assert.Error(t, jwt.ErrTokenExpired) // Expect an error because the token should be expired
// }