2021-06-04 15:23:15 +02:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
// See LICENSE.txt for license information.
|
|
|
|
|
|
|
|
package storetests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2021-07-27 17:42:02 +02:00
|
|
|
"testing"
|
2021-06-04 15:23:15 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-07-27 17:42:02 +02:00
|
|
|
"github.com/mattermost/focalboard/server/model"
|
2021-06-04 15:23:15 +02:00
|
|
|
"github.com/mattermost/focalboard/server/services/store"
|
2021-10-05 15:52:59 +02:00
|
|
|
"github.com/mattermost/focalboard/server/utils"
|
2021-06-04 15:23:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func StoreTestUserStore(t *testing.T, setup func(t *testing.T) (store.Store, func())) {
|
|
|
|
t.Run("SetGetSystemSettings", func(t *testing.T) {
|
|
|
|
store, tearDown := setup(t)
|
|
|
|
defer tearDown()
|
2022-03-22 15:24:34 +01:00
|
|
|
testGetTeamUsers(t, store)
|
2021-06-04 15:23:15 +02:00
|
|
|
})
|
2021-07-27 17:42:02 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
2022-02-28 12:28:16 +01:00
|
|
|
t.Run("TestPatchUserProps", func(t *testing.T) {
|
|
|
|
store, tearDown := setup(t)
|
|
|
|
defer tearDown()
|
|
|
|
testPatchUserProps(t, store)
|
|
|
|
})
|
2021-06-04 15:23:15 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func testGetTeamUsers(t *testing.T, store store.Store) {
|
|
|
|
t.Run("GetTeamUSers", func(t *testing.T) {
|
|
|
|
users, err := store.GetUsersByTeam("team_1")
|
2021-06-04 15:23:15 +02:00
|
|
|
require.Equal(t, 0, len(users))
|
|
|
|
require.Equal(t, sql.ErrNoRows, err)
|
|
|
|
|
2021-10-05 15:52:59 +02:00
|
|
|
userID := utils.NewID(utils.IDTypeUser)
|
2021-06-04 15:23:15 +02:00
|
|
|
|
|
|
|
err = store.CreateUser(&model.User{
|
|
|
|
ID: userID,
|
|
|
|
Username: "darth.vader",
|
|
|
|
})
|
2021-07-27 17:42:02 +02:00
|
|
|
require.NoError(t, err)
|
2021-06-04 15:23:15 +02:00
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
defer func() {
|
|
|
|
_ = store.UpdateUser(&model.User{
|
|
|
|
ID: userID,
|
2021-10-07 13:51:01 +02:00
|
|
|
DeleteAt: utils.GetMillis(),
|
2021-06-21 11:21:42 +02:00
|
|
|
})
|
|
|
|
}()
|
2021-06-04 15:23:15 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
users, err = store.GetUsersByTeam("team_1")
|
2021-06-04 15:23:15 +02:00
|
|
|
require.Equal(t, 1, len(users))
|
|
|
|
require.Equal(t, "darth.vader", users[0].Username)
|
2021-07-27 17:42:02 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCreateAndGetUser(t *testing.T, store store.Store) {
|
|
|
|
user := &model.User{
|
2021-10-05 15:52:59 +02:00
|
|
|
ID: utils.NewID(utils.IDTypeUser),
|
2021-07-27 17:42:02 +02:00
|
|
|
Username: "damao",
|
|
|
|
Email: "mock@email.com",
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("CreateUser", func(t *testing.T) {
|
|
|
|
err := store.CreateUser(user)
|
|
|
|
require.NoError(t, err)
|
2021-06-04 15:23:15 +02:00
|
|
|
})
|
2021-07-27 17:42:02 +02:00
|
|
|
|
|
|
|
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{
|
2021-10-05 15:52:59 +02:00
|
|
|
ID: utils.NewID(utils.IDTypeUser),
|
2021-07-27 17:42:02 +02:00
|
|
|
}
|
|
|
|
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) {
|
2021-10-05 15:52:59 +02:00
|
|
|
newPassword := utils.NewID(utils.IDTypeNone)
|
2021-07-27 17:42:02 +02:00
|
|
|
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) {
|
2021-10-05 15:52:59 +02:00
|
|
|
newPassword := utils.NewID(utils.IDTypeNone)
|
2021-07-27 17:42:02 +02:00
|
|
|
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{
|
2021-10-05 15:52:59 +02:00
|
|
|
ID: utils.NewID(utils.IDTypeUser),
|
2021-07-27 17:42:02 +02:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
got, err := store.GetRegisteredUserCount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, randomN, got)
|
2021-06-04 15:23:15 +02:00
|
|
|
}
|
2022-02-28 12:28:16 +01:00
|
|
|
|
|
|
|
func testPatchUserProps(t *testing.T, store store.Store) {
|
|
|
|
user := &model.User{
|
|
|
|
ID: utils.NewID(utils.IDTypeUser),
|
|
|
|
}
|
|
|
|
err := store.CreateUser(user)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Only update props
|
|
|
|
patch := model.UserPropPatch{
|
|
|
|
UpdatedFields: map[string]string{
|
|
|
|
"new_key_1": "new_value_1",
|
|
|
|
"new_key_2": "new_value_2",
|
|
|
|
"new_key_3": "new_value_3",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = store.PatchUserProps(user.ID, patch)
|
|
|
|
require.NoError(t, err)
|
|
|
|
fetchedUser, err := store.GetUserByID(user.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, fetchedUser.Props["new_key_1"], "new_value_1")
|
|
|
|
require.Equal(t, fetchedUser.Props["new_key_2"], "new_value_2")
|
|
|
|
require.Equal(t, fetchedUser.Props["new_key_3"], "new_value_3")
|
|
|
|
|
|
|
|
// Delete a prop
|
|
|
|
patch = model.UserPropPatch{
|
|
|
|
DeletedFields: []string{
|
|
|
|
"new_key_1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = store.PatchUserProps(user.ID, patch)
|
|
|
|
require.NoError(t, err)
|
|
|
|
fetchedUser, err = store.GetUserByID(user.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, ok := fetchedUser.Props["new_key_1"]
|
|
|
|
require.False(t, ok)
|
|
|
|
require.Equal(t, fetchedUser.Props["new_key_2"], "new_value_2")
|
|
|
|
require.Equal(t, fetchedUser.Props["new_key_3"], "new_value_3")
|
|
|
|
|
|
|
|
// update and delete together
|
|
|
|
patch = model.UserPropPatch{
|
|
|
|
UpdatedFields: map[string]string{
|
|
|
|
"new_key_3": "new_value_3_new_again",
|
|
|
|
},
|
|
|
|
DeletedFields: []string{
|
|
|
|
"new_key_2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = store.PatchUserProps(user.ID, patch)
|
|
|
|
require.NoError(t, err)
|
|
|
|
fetchedUser, err = store.GetUserByID(user.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, ok = fetchedUser.Props["new_key_2"]
|
|
|
|
require.False(t, ok)
|
|
|
|
require.Equal(t, fetchedUser.Props["new_key_3"], "new_value_3_new_again")
|
|
|
|
}
|