Add more unit tests for ./store/sqlstore/user.go (#787)

* Add more unit tests for ./store/sqlstore/user.go

* replace require.Nil to require.NoError

Co-authored-by: Doug Lauder <wiggin77@warpmail.net>
This commit is contained in:
dave 2021-07-27 23:42:02 +08:00 committed by GitHub
parent f86824b5ad
commit a98bcb98ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,15 +5,13 @@ package storetests
import ( import (
"database/sql" "database/sql"
"testing"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/mattermost/focalboard/server/model"
"testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/mattermost/focalboard/server/model"
"github.com/mattermost/focalboard/server/services/store" "github.com/mattermost/focalboard/server/services/store"
) )
@ -23,6 +21,24 @@ func StoreTestUserStore(t *testing.T, setup func(t *testing.T) (store.Store, fun
defer tearDown() defer tearDown()
testGetWorkspaceUsers(t, store) testGetWorkspaceUsers(t, store)
}) })
t.Run("CreateAndGetUser", func(t *testing.T) {
store, tearDown := setup(t)
defer tearDown()
testCreateAndGetUser(t, store)
})
t.Run("CreateAndUpateUser", func(t *testing.T) {
store, tearDown := setup(t)
defer tearDown()
testCreateAndUpdateUser(t, store)
})
t.Run("CreateAndGetRegisteredUserCount", func(t *testing.T) {
store, tearDown := setup(t)
defer tearDown()
testCreateAndGetRegisteredUserCount(t, store)
})
} }
func testGetWorkspaceUsers(t *testing.T, store store.Store) { func testGetWorkspaceUsers(t *testing.T, store store.Store) {
@ -37,7 +53,7 @@ func testGetWorkspaceUsers(t *testing.T, store store.Store) {
ID: userID, ID: userID,
Username: "darth.vader", Username: "darth.vader",
}) })
require.Nil(t, err) require.NoError(t, err)
defer func() { defer func() {
_ = store.UpdateUser(&model.User{ _ = store.UpdateUser(&model.User{
@ -49,6 +65,102 @@ func testGetWorkspaceUsers(t *testing.T, store store.Store) {
users, err = store.GetUsersByWorkspace("workspace_1") users, err = store.GetUsersByWorkspace("workspace_1")
require.Equal(t, 1, len(users)) require.Equal(t, 1, len(users))
require.Equal(t, "darth.vader", users[0].Username) require.Equal(t, "darth.vader", users[0].Username)
require.Nil(t, err) require.NoError(t, err)
}) })
} }
func testCreateAndGetUser(t *testing.T, store store.Store) {
user := &model.User{
ID: uuid.New().String(),
Username: "damao",
Email: "mock@email.com",
}
t.Run("CreateUser", func(t *testing.T) {
err := store.CreateUser(user)
require.NoError(t, err)
})
t.Run("GetUserByID", func(t *testing.T) {
got, err := store.GetUserByID(user.ID)
require.NoError(t, err)
require.Equal(t, user.ID, got.ID)
require.Equal(t, user.Username, got.Username)
require.Equal(t, user.Email, got.Email)
})
t.Run("GetUserByUsername", func(t *testing.T) {
got, err := store.GetUserByUsername(user.Username)
require.NoError(t, err)
require.Equal(t, user.ID, got.ID)
require.Equal(t, user.Username, got.Username)
require.Equal(t, user.Email, got.Email)
})
t.Run("GetUserByEmail", func(t *testing.T) {
got, err := store.GetUserByEmail(user.Email)
require.NoError(t, err)
require.Equal(t, user.ID, got.ID)
require.Equal(t, user.Username, got.Username)
require.Equal(t, user.Email, got.Email)
})
}
func testCreateAndUpdateUser(t *testing.T, store store.Store) {
user := &model.User{
ID: uuid.New().String(),
}
err := store.CreateUser(user)
require.NoError(t, err)
t.Run("UpdateUser", func(t *testing.T) {
user.Username = "damao"
user.Email = "mock@email.com"
user.Props = map[string]interface{}{"a": "b"}
err := store.UpdateUser(user)
require.NoError(t, err)
got, err := store.GetUserByID(user.ID)
require.NoError(t, err)
require.Equal(t, user.ID, got.ID)
require.Equal(t, user.Username, got.Username)
require.Equal(t, user.Email, got.Email)
require.Equal(t, user.Props, got.Props)
})
t.Run("UpdateUserPassword", func(t *testing.T) {
newPassword := uuid.New().String()
err := store.UpdateUserPassword(user.Username, newPassword)
require.NoError(t, err)
got, err := store.GetUserByUsername(user.Username)
require.NoError(t, err)
require.Equal(t, user.Username, got.Username)
require.Equal(t, newPassword, got.Password)
})
t.Run("UpdateUserPasswordByID", func(t *testing.T) {
newPassword := uuid.New().String()
err := store.UpdateUserPasswordByID(user.ID, newPassword)
require.NoError(t, err)
got, err := store.GetUserByID(user.ID)
require.NoError(t, err)
require.Equal(t, user.ID, got.ID)
require.Equal(t, newPassword, got.Password)
})
}
func testCreateAndGetRegisteredUserCount(t *testing.T, store store.Store) {
randomN := int(time.Now().Unix() % 10)
for i := 0; i < randomN; i++ {
err := store.CreateUser(&model.User{
ID: uuid.New().String(),
})
require.NoError(t, err)
}
got, err := store.GetRegisteredUserCount()
require.NoError(t, err)
require.Equal(t, randomN, got)
}