forked from mirrors/homebox
31b34241e0
* change /content/ -> /homebox/ * add cache to code generators * update env variables to set data storage * update env variables * set env variables in prod container * implement attachment post route (WIP) * get attachment endpoint * attachment download * implement string utilities lib * implement generic drop zone * use explicit truncate * remove clean dir * drop strings composable for lib * update item types and add attachments * add attachment API * implement service context * consolidate API code * implement editing attachments * implement upload limit configuration * improve error handling * add docs for max upload size * fix test cases
139 lines
3.2 KiB
Go
139 lines
3.2 KiB
Go
package repo
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/types"
|
|
"github.com/hay-kot/homebox/backend/pkgs/hasher"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestAuthTokenRepo_CreateToken(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()
|
|
|
|
token, err := tRepos.AuthTokens.CreateToken(ctx, types.UserAuthTokenCreate{
|
|
TokenHash: generatedToken.Hash,
|
|
ExpiresAt: expiresAt,
|
|
UserID: userOut.ID,
|
|
})
|
|
|
|
asrt.NoError(err)
|
|
asrt.Equal(userOut.ID, token.UserID)
|
|
asrt.Equal(expiresAt, token.ExpiresAt)
|
|
|
|
// 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)
|
|
}
|
|
|
|
func TestAuthTokenRepo_GetUserByToken(t *testing.T) {
|
|
assert := assert.New(t)
|
|
ctx := context.Background()
|
|
|
|
user := userFactory()
|
|
userOut, _ := tRepos.Users.Create(ctx, user)
|
|
|
|
expiresAt := time.Now().Add(time.Hour)
|
|
generatedToken := hasher.GenerateToken()
|
|
|
|
token, err := tRepos.AuthTokens.CreateToken(ctx, types.UserAuthTokenCreate{
|
|
TokenHash: generatedToken.Hash,
|
|
ExpiresAt: expiresAt,
|
|
UserID: userOut.ID,
|
|
})
|
|
|
|
assert.NoError(err)
|
|
|
|
// Get User from token
|
|
foundUser, err := tRepos.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
|
|
assert.NoError(tRepos.Users.Delete(ctx, userOut.ID))
|
|
_, err = tRepos.AuthTokens.DeleteAll(ctx)
|
|
assert.NoError(err)
|
|
}
|
|
|
|
func TestAuthTokenRepo_PurgeExpiredTokens(t *testing.T) {
|
|
assert := assert.New(t)
|
|
ctx := context.Background()
|
|
|
|
user := userFactory()
|
|
userOut, _ := tRepos.Users.Create(ctx, user)
|
|
|
|
createdTokens := []types.UserAuthToken{}
|
|
|
|
for i := 0; i < 5; i++ {
|
|
expiresAt := time.Now()
|
|
generatedToken := hasher.GenerateToken()
|
|
|
|
createdToken, err := tRepos.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 := tRepos.AuthTokens.PurgeExpiredTokens(ctx)
|
|
|
|
assert.NoError(err)
|
|
assert.Equal(5, tokensDeleted)
|
|
|
|
// Check if tokens are deleted
|
|
for _, token := range createdTokens {
|
|
_, err := tRepos.AuthTokens.GetUserFromToken(ctx, token.TokenHash)
|
|
assert.Error(err)
|
|
}
|
|
|
|
// Cleanup
|
|
assert.NoError(tRepos.Users.Delete(ctx, userOut.ID))
|
|
_, err = tRepos.AuthTokens.DeleteAll(ctx)
|
|
assert.NoError(err)
|
|
}
|