homebox/backend/internal/repo/repo_tokens_test.go

114 lines
2.6 KiB
Go
Raw Normal View History

2022-08-30 02:30:36 +00:00
package repo
import (
"context"
"testing"
"time"
2022-08-30 02:40:54 +00:00
"github.com/hay-kot/content/backend/internal/types"
"github.com/hay-kot/content/backend/pkgs/hasher"
2022-08-30 02:30:36 +00:00
"github.com/stretchr/testify/assert"
)
func Test_EntAuthTokenRepo_CreateToken(t *testing.T) {
assert := assert.New(t)
ctx := context.Background()
user := UserFactory()
2022-08-31 03:21:18 +00:00
userOut, err := testRepos.Users.Create(ctx, user)
assert.NoError(err)
2022-08-30 02:30:36 +00:00
expiresAt := time.Now().Add(time.Hour)
generatedToken := hasher.GenerateToken()
token, err := testRepos.AuthTokens.CreateToken(ctx, types.UserAuthTokenCreate{
TokenHash: generatedToken.Hash,
ExpiresAt: expiresAt,
UserID: userOut.ID,
})
assert.NoError(err)
assert.Equal(userOut.ID, token.UserID)
assert.Equal(expiresAt, token.ExpiresAt)
// Cleanup
2022-08-31 03:21:18 +00:00
testRepos.Users.Delete(ctx, userOut.ID)
testRepos.AuthTokens.DeleteAll(ctx)
2022-08-30 02:30:36 +00:00
}
func Test_EntAuthTokenRepo_GetUserByToken(t *testing.T) {
assert := assert.New(t)
ctx := context.Background()
user := UserFactory()
userOut, _ := testRepos.Users.Create(ctx, user)
expiresAt := time.Now().Add(time.Hour)
generatedToken := hasher.GenerateToken()
token, err := testRepos.AuthTokens.CreateToken(ctx, types.UserAuthTokenCreate{
TokenHash: generatedToken.Hash,
ExpiresAt: expiresAt,
UserID: userOut.ID,
})
2022-08-31 03:21:18 +00:00
assert.NoError(err)
2022-08-30 02:30:36 +00:00
// Get User from token
foundUser, err := testRepos.AuthTokens.GetUserFromToken(ctx, token.TokenHash)
assert.NoError(err)
assert.Equal(userOut.ID, foundUser.ID)
assert.Equal(userOut.Name, foundUser.Name)
assert.Equal(userOut.Email, foundUser.Email)
// Cleanup
2022-08-31 03:21:18 +00:00
testRepos.Users.Delete(ctx, userOut.ID)
testRepos.AuthTokens.DeleteAll(ctx)
2022-08-30 02:30:36 +00:00
}
func Test_EntAuthTokenRepo_PurgeExpiredTokens(t *testing.T) {
assert := assert.New(t)
ctx := context.Background()
user := UserFactory()
userOut, _ := testRepos.Users.Create(ctx, user)
createdTokens := []types.UserAuthToken{}
for i := 0; i < 5; i++ {
expiresAt := time.Now()
generatedToken := hasher.GenerateToken()
createdToken, err := testRepos.AuthTokens.CreateToken(ctx, types.UserAuthTokenCreate{
TokenHash: generatedToken.Hash,
ExpiresAt: expiresAt,
UserID: userOut.ID,
})
assert.NoError(err)
assert.NotNil(createdToken)
createdTokens = append(createdTokens, createdToken)
}
// Purge expired tokens
tokensDeleted, err := testRepos.AuthTokens.PurgeExpiredTokens(ctx)
assert.NoError(err)
assert.Equal(5, tokensDeleted)
// Check if tokens are deleted
for _, token := range createdTokens {
_, err := testRepos.AuthTokens.GetUserFromToken(ctx, token.TokenHash)
assert.Error(err)
}
// Cleanup
2022-08-31 03:21:18 +00:00
testRepos.Users.Delete(ctx, userOut.ID)
testRepos.AuthTokens.DeleteAll(ctx)
2022-08-30 02:30:36 +00:00
}