1
0
mirror of https://github.com/mattermost/focalboard.git synced 2024-12-27 13:48:52 +02:00
focalboard/server/services/store/storetests/session.go
Miguel de la Cruz 08c0b7a2fd
Refactor error usage from the store level up and add API helpers (#3792)
* Refactor error usage from the store level up and add API helpers

* Complete API tests

* Fix merge errorResponse calls

* Remove ensure helpers to allow for custom messages on permission errors

* Fix bad import and call

* Remove bad user check on auth that was added as part of the main merge

* Fix empty list test

* Replace deprecated proxy calls to ioutil.ReadAll with io.ReadAll

* Add information to the NotFound errors

* Add context to all remaining errors and address review comments

* Fix linter

* Adapt the new card API endpoints to the error refactor

* Remove almost all customErrorResponse calls

* Add request entity too large to errorResponse and remove customErrorResponse

* Fix linter
2022-09-13 12:18:40 +02:00

108 lines
2.6 KiB
Go

package storetests
import (
"fmt"
"testing"
"time"
"github.com/mattermost/focalboard/server/model"
"github.com/mattermost/focalboard/server/services/store"
"github.com/stretchr/testify/require"
)
func StoreTestSessionStore(t *testing.T, setup func(t *testing.T) (store.Store, func())) {
t.Run("CreateAndGetAndDeleteSession", func(t *testing.T) {
store, tearDown := setup(t)
defer tearDown()
testCreateAndGetAndDeleteSession(t, store)
})
t.Run("GetActiveUserCount", func(t *testing.T) {
store, tearDown := setup(t)
defer tearDown()
testGetActiveUserCount(t, store)
})
t.Run("UpdateSession", func(t *testing.T) {
store, tearDown := setup(t)
defer tearDown()
testUpdateSession(t, store)
})
}
func testCreateAndGetAndDeleteSession(t *testing.T, store store.Store) {
session := &model.Session{
ID: "session-id",
Token: "token",
}
t.Run("CreateAndGetSession", func(t *testing.T) {
err := store.CreateSession(session)
require.NoError(t, err)
got, err := store.GetSession(session.Token, 60*60)
require.NoError(t, err)
require.Equal(t, session, got)
})
t.Run("Get nonexistent session", func(t *testing.T) {
got, err := store.GetSession("nonexistent-token", 60*60)
require.True(t, model.IsErrNotFound(err))
require.Nil(t, got)
})
t.Run("DeleteAndGetSession", func(t *testing.T) {
err := store.DeleteSession(session.ID)
require.NoError(t, err)
_, err = store.GetSession(session.Token, 60*60)
require.Error(t, err)
})
}
func testGetActiveUserCount(t *testing.T, store store.Store) {
t.Run("no active user", func(t *testing.T) {
count, err := store.GetActiveUserCount(60)
require.NoError(t, err)
require.Equal(t, 0, count)
})
t.Run("active user", func(t *testing.T) {
// gen random count active user session
count := int(time.Now().Unix() % 10)
for i := 0; i < count; i++ {
session := &model.Session{
ID: fmt.Sprintf("id-%d", i),
UserID: fmt.Sprintf("user-id-%d", i),
Token: fmt.Sprintf("token-%d", i),
}
err := store.CreateSession(session)
require.NoError(t, err)
}
got, err := store.GetActiveUserCount(60)
require.NoError(t, err)
require.Equal(t, count, got)
})
}
func testUpdateSession(t *testing.T, store store.Store) {
session := &model.Session{
ID: "session-id",
Token: "token",
Props: map[string]interface{}{"field1": "A"},
}
err := store.CreateSession(session)
require.NoError(t, err)
// update session
session.Props["field1"] = "B"
err = store.UpdateSession(session)
require.NoError(t, err)
got, err := store.GetSession(session.Token, 60)
require.NoError(t, err)
require.Equal(t, session, got)
}