Dawid Wysokiński
786cc60e38
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #14
126 lines
3.3 KiB
Go
126 lines
3.3 KiB
Go
package bundb_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"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)
|
|
})
|
|
}
|
|
|
|
func TestSession_Get(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
db := newDB(t)
|
|
fixture := loadFixtures(t, db)
|
|
repo := bundb.NewSession(db)
|
|
sessFixture := fixture.session(t, "user-1-session-1")
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
sess, err := repo.Get(context.Background(), sessFixture.UserID, sessFixture.ServerKey)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, sessFixture, sess)
|
|
})
|
|
|
|
t.Run("ERR: session not found", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
userID int64
|
|
serverKey string
|
|
}{
|
|
{
|
|
userID: sessFixture.UserID + 11111,
|
|
serverKey: sessFixture.ServerKey,
|
|
},
|
|
{
|
|
userID: sessFixture.UserID,
|
|
serverKey: sessFixture.ServerKey + "1111",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
|
|
t.Run(fmt.Sprintf("UserID=%d,ServerKey=%s", tt.userID, tt.serverKey), func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
sess, err := repo.Get(context.Background(), tt.userID, tt.serverKey)
|
|
assert.ErrorIs(t, err, domain.SessionNotFoundError{
|
|
ServerKey: tt.serverKey,
|
|
})
|
|
assert.Zero(t, sess)
|
|
})
|
|
}
|
|
})
|
|
}
|