2021-11-22 15:17:23 +05:30
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2021-11-22 15:17:23 +05:30
|
|
|
|
2022-06-15 12:32:00 +05:30
|
|
|
package user_test
|
2021-11-22 15:17:23 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2022-06-15 12:32:00 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-22 15:17:23 +05:30
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSettings(t *testing.T) {
|
|
|
|
keyName := "test_user_setting"
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
2022-06-15 12:32:00 +05:30
|
|
|
newSetting := &user_model.Setting{UserID: 99, SettingKey: keyName, SettingValue: "Gitea User Setting Test"}
|
2021-11-22 15:17:23 +05:30
|
|
|
|
|
|
|
// create setting
|
2022-06-15 12:32:00 +05:30
|
|
|
err := user_model.SetUserSetting(newSetting.UserID, newSetting.SettingKey, newSetting.SettingValue)
|
2021-11-22 15:17:23 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
// test about saving unchanged values
|
2022-06-15 12:32:00 +05:30
|
|
|
err = user_model.SetUserSetting(newSetting.UserID, newSetting.SettingKey, newSetting.SettingValue)
|
2021-11-22 15:17:23 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// get specific setting
|
2022-11-10 12:13:53 +05:30
|
|
|
settings, err := user_model.GetSettings(99, []string{keyName})
|
2021-11-22 15:17:23 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, settings, 1)
|
|
|
|
assert.EqualValues(t, newSetting.SettingValue, settings[keyName].SettingValue)
|
|
|
|
|
2022-06-15 12:32:00 +05:30
|
|
|
settingValue, err := user_model.GetUserSetting(99, keyName)
|
2022-01-21 23:29:26 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, newSetting.SettingValue, settingValue)
|
|
|
|
|
2022-06-15 12:32:00 +05:30
|
|
|
settingValue, err = user_model.GetUserSetting(99, "no_such")
|
2022-01-21 23:29:26 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, "", settingValue)
|
|
|
|
|
2021-11-22 15:17:23 +05:30
|
|
|
// updated setting
|
2022-06-15 12:32:00 +05:30
|
|
|
updatedSetting := &user_model.Setting{UserID: 99, SettingKey: keyName, SettingValue: "Updated"}
|
|
|
|
err = user_model.SetUserSetting(updatedSetting.UserID, updatedSetting.SettingKey, updatedSetting.SettingValue)
|
2021-11-22 15:17:23 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// get all settings
|
2022-06-15 12:32:00 +05:30
|
|
|
settings, err = user_model.GetUserAllSettings(99)
|
2021-11-22 15:17:23 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, settings, 1)
|
|
|
|
assert.EqualValues(t, updatedSetting.SettingValue, settings[updatedSetting.SettingKey].SettingValue)
|
|
|
|
|
|
|
|
// delete setting
|
2022-06-15 12:32:00 +05:30
|
|
|
err = user_model.DeleteUserSetting(99, keyName)
|
2021-11-22 15:17:23 +05:30
|
|
|
assert.NoError(t, err)
|
2022-06-15 12:32:00 +05:30
|
|
|
settings, err = user_model.GetUserAllSettings(99)
|
2021-11-22 15:17:23 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, settings, 0)
|
|
|
|
}
|