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