All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #8
76 lines
2.2 KiB
Go
76 lines
2.2 KiB
Go
package bundb_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.dwysokinski.me/twhelp/sessions/internal/bundb"
|
|
"gitea.dwysokinski.me/twhelp/sessions/internal/domain"
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestSession_CreateOrUpdate(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
db := newDB(t)
|
|
fixture := loadFixtures(t, db)
|
|
repo := bundb.NewSession(db)
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
createParams, err := domain.NewCreateSessionParams(
|
|
"pl151",
|
|
base64.StdEncoding.EncodeToString([]byte(uuid.NewString())),
|
|
fixture.user(t, "user-1").ID,
|
|
)
|
|
require.NoError(t, err)
|
|
|
|
createdSess, err := repo.CreateOrUpdate(context.Background(), createParams)
|
|
assert.NoError(t, err)
|
|
assert.Greater(t, createdSess.ID, int64(0))
|
|
assert.Equal(t, createParams.UserID(), createdSess.UserID)
|
|
assert.Equal(t, createParams.SID(), createdSess.SID)
|
|
assert.Equal(t, createParams.ServerKey(), createdSess.ServerKey)
|
|
assert.WithinDuration(t, time.Now(), createdSess.CreatedAt, time.Second)
|
|
assert.WithinDuration(t, time.Now(), createdSess.UpdatedAt, time.Second)
|
|
|
|
updateParams, err := domain.NewCreateSessionParams(
|
|
createParams.ServerKey(),
|
|
base64.StdEncoding.EncodeToString([]byte(uuid.NewString())),
|
|
createParams.UserID(),
|
|
)
|
|
require.NoError(t, err)
|
|
|
|
updatedSess, err := repo.CreateOrUpdate(context.Background(), updateParams)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, createdSess.ID, updatedSess.ID)
|
|
assert.Equal(t, createdSess.UserID, updatedSess.UserID)
|
|
assert.Equal(t, updateParams.SID(), updatedSess.SID)
|
|
assert.Equal(t, createdSess.ServerKey, updatedSess.ServerKey)
|
|
assert.Equal(t, createdSess.CreatedAt, updatedSess.CreatedAt)
|
|
assert.True(t, updatedSess.UpdatedAt.After(createdSess.UpdatedAt))
|
|
})
|
|
|
|
t.Run("ERR: user doesn't exist", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
params, err := domain.NewCreateSessionParams(
|
|
"pl151",
|
|
base64.StdEncoding.EncodeToString([]byte(uuid.NewString())),
|
|
fixture.user(t, "user-1").ID+1111,
|
|
)
|
|
require.NoError(t, err)
|
|
|
|
sess, err := repo.CreateOrUpdate(context.Background(), params)
|
|
assert.ErrorIs(t, err, domain.UserDoesNotExistError{
|
|
ID: params.UserID(),
|
|
})
|
|
assert.Zero(t, sess)
|
|
})
|
|
}
|