From bafa32108d45b6facdcbd1591767d9f815c50c0c Mon Sep 17 00:00:00 2001 From: dave Date: Thu, 22 Jul 2021 19:56:25 +0800 Subject: [PATCH] [GH-428] Add unit tests for ./store/sqlstore/session.go (#729) * fix can't delete session * Add unit tests for ./server/store/sqlstore/session.go * fix ci lint complain Co-authored-by: Doug Lauder Co-authored-by: Hossein --- server/services/store/sqlstore/session.go | 4 +- .../services/store/sqlstore/sqlstore_test.go | 1 + server/services/store/storetests/session.go | 105 ++++++++++++++++++ 3 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 server/services/store/storetests/session.go diff --git a/server/services/store/sqlstore/session.go b/server/services/store/sqlstore/session.go index 0039fb4fd..6894ede25 100644 --- a/server/services/store/sqlstore/session.go +++ b/server/services/store/sqlstore/session.go @@ -95,8 +95,8 @@ func (s *SQLStore) UpdateSession(session *model.Session) error { } func (s *SQLStore) DeleteSession(sessionID string) error { - query := s.getQueryBuilder().Delete(s.tablePrefix+"sessions"). - Where("id", sessionID) + query := s.getQueryBuilder().Delete(s.tablePrefix + "sessions"). + Where(sq.Eq{"id": sessionID}) _, err := query.Exec() return err diff --git a/server/services/store/sqlstore/sqlstore_test.go b/server/services/store/sqlstore/sqlstore_test.go index 3869f1e11..1bc9685ad 100644 --- a/server/services/store/sqlstore/sqlstore_test.go +++ b/server/services/store/sqlstore/sqlstore_test.go @@ -45,4 +45,5 @@ func TestBlocksStore(t *testing.T) { t.Run("SharingStore", func(t *testing.T) { storetests.StoreTestSharingStore(t, SetupTests) }) t.Run("SystemStore", func(t *testing.T) { storetests.StoreTestSystemStore(t, SetupTests) }) t.Run("UserStore", func(t *testing.T) { storetests.StoreTestUserStore(t, SetupTests) }) + t.Run("SessionStore", func(t *testing.T) { storetests.StoreTestSessionStore(t, SetupTests) }) } diff --git a/server/services/store/storetests/session.go b/server/services/store/storetests/session.go new file mode 100644 index 000000000..b213f0c4e --- /dev/null +++ b/server/services/store/storetests/session.go @@ -0,0 +1,105 @@ +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())) { + container := store.Container{ + WorkspaceID: "0", + } + + t.Run("CreateAndGetAndDeleteSession", func(t *testing.T) { + store, tearDown := setup(t) + defer tearDown() + testCreateAndGetAndDeleteSession(t, store, container) + }) + + t.Run("GetActiveUserCount", func(t *testing.T) { + store, tearDown := setup(t) + defer tearDown() + testGetActiveUserCount(t, store, container) + }) + + t.Run("UpdateSession", func(t *testing.T) { + store, tearDown := setup(t) + defer tearDown() + testUpdateSession(t, store, container) + }) +} + +func testCreateAndGetAndDeleteSession(t *testing.T, store store.Store, _ store.Container) { + 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("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, _ store.Container) { + 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, _ store.Container) { + 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) +}