homebox/backend/internal/repo/token_ent_test.go

111 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()
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,
})
assert.NoError(err)
assert.Equal(userOut.ID, token.UserID)
assert.Equal(expiresAt, token.ExpiresAt)
// Cleanup
err = testRepos.Users.Delete(ctx, userOut.ID)
_, err = testRepos.AuthTokens.DeleteAll(ctx)
}
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,
})
// 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
err = testRepos.Users.Delete(ctx, userOut.ID)
_, err = testRepos.AuthTokens.DeleteAll(ctx)
}
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
err = testRepos.Users.Delete(ctx, userOut.ID)
_, err = testRepos.AuthTokens.DeleteAll(ctx)
}