homebox/backend/internal/repo/repo_tokens_test.go

140 lines
3.2 KiB
Go
Raw Normal View History

2022-08-30 02:30:36 +00:00
package repo
import (
"context"
"testing"
"time"
"github.com/hay-kot/homebox/backend/internal/types"
"github.com/hay-kot/homebox/backend/pkgs/hasher"
2022-08-30 02:30:36 +00:00
"github.com/stretchr/testify/assert"
)
func TestAuthTokenRepo_CreateToken(t *testing.T) {
asrt := assert.New(t)
2022-08-30 02:30:36 +00:00
ctx := context.Background()
user := userFactory()
2022-08-30 02:30:36 +00:00
userOut, err := tRepos.Users.Create(ctx, user)
asrt.NoError(err)
2022-08-30 02:30:36 +00:00
expiresAt := time.Now().Add(time.Hour)
generatedToken := hasher.GenerateToken()
token, err := tRepos.AuthTokens.CreateToken(ctx, types.UserAuthTokenCreate{
2022-08-30 02:30:36 +00:00
TokenHash: generatedToken.Hash,
ExpiresAt: expiresAt,
UserID: userOut.ID,
})
asrt.NoError(err)
asrt.Equal(userOut.ID, token.UserID)
asrt.Equal(expiresAt, token.ExpiresAt)
2022-08-30 02:30:36 +00:00
// Cleanup
asrt.NoError(tRepos.Users.Delete(ctx, userOut.ID))
_, err = tRepos.AuthTokens.DeleteAll(ctx)
asrt.NoError(err)
}
func TestAuthTokenRepo_DeleteToken(t *testing.T) {
asrt := assert.New(t)
ctx := context.Background()
user := userFactory()
userOut, err := tRepos.Users.Create(ctx, user)
asrt.NoError(err)
expiresAt := time.Now().Add(time.Hour)
generatedToken := hasher.GenerateToken()
_, err = tRepos.AuthTokens.CreateToken(ctx, types.UserAuthTokenCreate{
TokenHash: generatedToken.Hash,
ExpiresAt: expiresAt,
UserID: userOut.ID,
})
asrt.NoError(err)
// Delete token
err = tRepos.AuthTokens.DeleteToken(ctx, []byte(generatedToken.Raw))
asrt.NoError(err)
2022-08-30 02:30:36 +00:00
}
func TestAuthTokenRepo_GetUserByToken(t *testing.T) {
2022-08-30 02:30:36 +00:00
assert := assert.New(t)
ctx := context.Background()
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()
token, err := tRepos.AuthTokens.CreateToken(ctx, types.UserAuthTokenCreate{
2022-08-30 02:30:36 +00:00
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 := tRepos.AuthTokens.GetUserFromToken(ctx, token.TokenHash)
2022-08-30 02:30:36 +00:00
assert.NoError(err)
assert.Equal(userOut.ID, foundUser.ID)
assert.Equal(userOut.Name, foundUser.Name)
assert.Equal(userOut.Email, foundUser.Email)
// Cleanup
assert.NoError(tRepos.Users.Delete(ctx, userOut.ID))
_, err = tRepos.AuthTokens.DeleteAll(ctx)
2022-09-03 09:52:05 +00:00
assert.NoError(err)
2022-08-30 02:30:36 +00:00
}
func TestAuthTokenRepo_PurgeExpiredTokens(t *testing.T) {
2022-08-30 02:30:36 +00:00
assert := assert.New(t)
ctx := context.Background()
user := userFactory()
userOut, _ := tRepos.Users.Create(ctx, user)
2022-08-30 02:30:36 +00:00
createdTokens := []types.UserAuthToken{}
for i := 0; i < 5; i++ {
expiresAt := time.Now()
generatedToken := hasher.GenerateToken()
createdToken, err := tRepos.AuthTokens.CreateToken(ctx, types.UserAuthTokenCreate{
2022-08-30 02:30:36 +00:00
TokenHash: generatedToken.Hash,
ExpiresAt: expiresAt,
UserID: userOut.ID,
})
assert.NoError(err)
assert.NotNil(createdToken)
createdTokens = append(createdTokens, createdToken)
}
// Purge expired tokens
tokensDeleted, err := tRepos.AuthTokens.PurgeExpiredTokens(ctx)
2022-08-30 02:30:36 +00:00
assert.NoError(err)
assert.Equal(5, tokensDeleted)
// Check if tokens are deleted
for _, token := range createdTokens {
_, err := tRepos.AuthTokens.GetUserFromToken(ctx, token.TokenHash)
2022-08-30 02:30:36 +00:00
assert.Error(err)
}
// Cleanup
assert.NoError(tRepos.Users.Delete(ctx, userOut.ID))
_, err = tRepos.AuthTokens.DeleteAll(ctx)
2022-09-03 09:52:05 +00:00
assert.NoError(err)
2022-08-30 02:30:36 +00:00
}