2022-08-30 02:30:36 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-09-24 19:33:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/pkgs/hasher"
|
2022-08-30 02:30:36 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-01-04 17:55:26 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-08-30 02:30:36 +00:00
|
|
|
)
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func TestAuthTokenRepo_CreateToken(t *testing.T) {
|
2022-08-30 02:30:36 +00:00
|
|
|
ctx := context.Background()
|
2022-09-05 08:26:21 +00:00
|
|
|
user := userFactory()
|
2022-08-30 02:30:36 +00:00
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
userOut, err := tRepos.Users.Create(ctx, user)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-30 02:30:36 +00:00
|
|
|
|
|
|
|
expiresAt := time.Now().Add(time.Hour)
|
|
|
|
|
|
|
|
generatedToken := hasher.GenerateToken()
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
token, err := tRepos.AuthTokens.CreateToken(ctx, UserAuthTokenCreate{
|
2022-08-30 02:30:36 +00:00
|
|
|
TokenHash: generatedToken.Hash,
|
|
|
|
ExpiresAt: expiresAt,
|
|
|
|
UserID: userOut.ID,
|
|
|
|
})
|
|
|
|
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, userOut.ID, token.UserID)
|
|
|
|
assert.Equal(t, expiresAt, token.ExpiresAt)
|
2022-08-30 02:30:36 +00:00
|
|
|
|
|
|
|
// Cleanup
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, tRepos.Users.Delete(ctx, userOut.ID))
|
2022-09-05 08:26:21 +00:00
|
|
|
_, err = tRepos.AuthTokens.DeleteAll(ctx)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-05 08:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthTokenRepo_DeleteToken(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
user := userFactory()
|
|
|
|
|
|
|
|
userOut, err := tRepos.Users.Create(ctx, user)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-05 08:26:21 +00:00
|
|
|
|
|
|
|
expiresAt := time.Now().Add(time.Hour)
|
|
|
|
|
|
|
|
generatedToken := hasher.GenerateToken()
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
_, err = tRepos.AuthTokens.CreateToken(ctx, UserAuthTokenCreate{
|
2022-09-05 08:26:21 +00:00
|
|
|
TokenHash: generatedToken.Hash,
|
|
|
|
ExpiresAt: expiresAt,
|
|
|
|
UserID: userOut.ID,
|
|
|
|
})
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-05 08:26:21 +00:00
|
|
|
|
|
|
|
// Delete token
|
|
|
|
err = tRepos.AuthTokens.DeleteToken(ctx, []byte(generatedToken.Raw))
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func TestAuthTokenRepo_GetUserByToken(t *testing.T) {
|
2022-08-30 02:30:36 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
user := userFactory()
|
|
|
|
userOut, _ := tRepos.Users.Create(ctx, user)
|
2022-08-30 02:30:36 +00:00
|
|
|
|
|
|
|
expiresAt := time.Now().Add(time.Hour)
|
|
|
|
generatedToken := hasher.GenerateToken()
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
token, err := tRepos.AuthTokens.CreateToken(ctx, UserAuthTokenCreate{
|
2022-08-30 02:30:36 +00:00
|
|
|
TokenHash: generatedToken.Hash,
|
|
|
|
ExpiresAt: expiresAt,
|
|
|
|
UserID: userOut.ID,
|
|
|
|
})
|
|
|
|
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-31 03:21:18 +00:00
|
|
|
|
2022-08-30 02:30:36 +00:00
|
|
|
// Get User from token
|
2022-09-05 08:26:21 +00:00
|
|
|
foundUser, err := tRepos.AuthTokens.GetUserFromToken(ctx, token.TokenHash)
|
2022-08-30 02:30:36 +00:00
|
|
|
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, userOut.ID, foundUser.ID)
|
|
|
|
assert.Equal(t, userOut.Name, foundUser.Name)
|
|
|
|
assert.Equal(t, userOut.Email, foundUser.Email)
|
2022-08-30 02:30:36 +00:00
|
|
|
|
|
|
|
// Cleanup
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, tRepos.Users.Delete(ctx, userOut.ID))
|
2022-09-05 08:26:21 +00:00
|
|
|
_, err = tRepos.AuthTokens.DeleteAll(ctx)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func TestAuthTokenRepo_PurgeExpiredTokens(t *testing.T) {
|
2022-08-30 02:30:36 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
user := userFactory()
|
|
|
|
userOut, _ := tRepos.Users.Create(ctx, user)
|
2022-08-30 02:30:36 +00:00
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
createdTokens := []UserAuthToken{}
|
2022-08-30 02:30:36 +00:00
|
|
|
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
expiresAt := time.Now()
|
|
|
|
generatedToken := hasher.GenerateToken()
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
createdToken, err := tRepos.AuthTokens.CreateToken(ctx, UserAuthTokenCreate{
|
2022-08-30 02:30:36 +00:00
|
|
|
TokenHash: generatedToken.Hash,
|
|
|
|
ExpiresAt: expiresAt,
|
|
|
|
UserID: userOut.ID,
|
|
|
|
})
|
|
|
|
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotNil(t, createdToken)
|
2022-08-30 02:30:36 +00:00
|
|
|
|
|
|
|
createdTokens = append(createdTokens, createdToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Purge expired tokens
|
2022-09-05 08:26:21 +00:00
|
|
|
tokensDeleted, err := tRepos.AuthTokens.PurgeExpiredTokens(ctx)
|
2022-08-30 02:30:36 +00:00
|
|
|
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 5, tokensDeleted)
|
2022-08-30 02:30:36 +00:00
|
|
|
|
|
|
|
// Check if tokens are deleted
|
|
|
|
for _, token := range createdTokens {
|
2022-09-05 08:26:21 +00:00
|
|
|
_, err := tRepos.AuthTokens.GetUserFromToken(ctx, token.TokenHash)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.Error(t, err)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, tRepos.Users.Delete(ctx, userOut.ID))
|
2022-09-05 08:26:21 +00:00
|
|
|
_, err = tRepos.AuthTokens.DeleteAll(ctx)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|