grafana/pkg/services/sqlstore/preferences_test.go
Hugo Häggmark 4e1059649a
Admin: Fix so Timezone is saved when saving preferences (#41583)
* Configuration: Fix so Timezone is saved when saving preferences

* Updating package.json and yarn.lock

* Chore: force drone reload

* Chore: trying with the git protocol

* Chore: revert back to main

* Chore: deleted cached pa11y file

* Chore: revert

* Chore: trying with YARN_CHECKSUM_BEHAVIOR=reset

* Chore: YARN_CHECKSUM_BEHAVIOR=reset worked but reverting back waiting for other fix
2021-11-11 11:13:01 +01:00

143 lines
6.6 KiB
Go

//go:build integration
// +build integration
package sqlstore
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/models"
)
func TestPreferencesDataAccess(t *testing.T) {
ss := InitTestDB(t)
t.Run("GetPreferencesWithDefaults with no saved preferences should return defaults", func(t *testing.T) {
ss.Cfg.DefaultTheme = "light"
ss.Cfg.DateFormats.DefaultTimezone = "UTC"
query := &models.GetPreferencesWithDefaultsQuery{User: &models.SignedInUser{}}
err := ss.GetPreferencesWithDefaults(context.Background(), query)
require.NoError(t, err)
require.Equal(t, "light", query.Result.Theme)
require.Equal(t, "UTC", query.Result.Timezone)
require.Equal(t, int64(0), query.Result.HomeDashboardId)
})
t.Run("GetPreferencesWithDefaults with saved org and user home dashboard should return user home dashboard", func(t *testing.T) {
err := ss.SavePreferences(context.Background(), &models.SavePreferencesCommand{OrgId: 1, HomeDashboardId: 1})
require.NoError(t, err)
err = ss.SavePreferences(context.Background(), &models.SavePreferencesCommand{OrgId: 1, UserId: 1, HomeDashboardId: 4})
require.NoError(t, err)
query := &models.GetPreferencesWithDefaultsQuery{User: &models.SignedInUser{OrgId: 1, UserId: 1}}
err = ss.GetPreferencesWithDefaults(context.Background(), query)
require.NoError(t, err)
require.Equal(t, int64(4), query.Result.HomeDashboardId)
})
t.Run("GetPreferencesWithDefaults with saved org and other user home dashboard should return org home dashboard", func(t *testing.T) {
err := ss.SavePreferences(context.Background(), &models.SavePreferencesCommand{OrgId: 1, HomeDashboardId: 1})
require.NoError(t, err)
err = ss.SavePreferences(context.Background(), &models.SavePreferencesCommand{OrgId: 1, UserId: 1, HomeDashboardId: 4})
require.NoError(t, err)
query := &models.GetPreferencesWithDefaultsQuery{User: &models.SignedInUser{OrgId: 1, UserId: 2}}
err = ss.GetPreferencesWithDefaults(context.Background(), query)
require.NoError(t, err)
require.Equal(t, int64(1), query.Result.HomeDashboardId)
})
t.Run("GetPreferencesWithDefaults with saved org and teams home dashboard should return last team home dashboard", func(t *testing.T) {
err := ss.SavePreferences(context.Background(), &models.SavePreferencesCommand{OrgId: 1, HomeDashboardId: 1})
require.NoError(t, err)
err = ss.SavePreferences(context.Background(), &models.SavePreferencesCommand{OrgId: 1, TeamId: 2, HomeDashboardId: 2})
require.NoError(t, err)
err = ss.SavePreferences(context.Background(), &models.SavePreferencesCommand{OrgId: 1, TeamId: 3, HomeDashboardId: 3})
require.NoError(t, err)
query := &models.GetPreferencesWithDefaultsQuery{
User: &models.SignedInUser{OrgId: 1, Teams: []int64{2, 3}},
}
err = ss.GetPreferencesWithDefaults(context.Background(), query)
require.NoError(t, err)
require.Equal(t, int64(3), query.Result.HomeDashboardId)
})
t.Run("GetPreferencesWithDefaults with saved org and other teams home dashboard should return org home dashboard", func(t *testing.T) {
err := ss.SavePreferences(context.Background(), &models.SavePreferencesCommand{OrgId: 1, HomeDashboardId: 1})
require.NoError(t, err)
err = ss.SavePreferences(context.Background(), &models.SavePreferencesCommand{OrgId: 1, TeamId: 2, HomeDashboardId: 2})
require.NoError(t, err)
err = ss.SavePreferences(context.Background(), &models.SavePreferencesCommand{OrgId: 1, TeamId: 3, HomeDashboardId: 3})
require.NoError(t, err)
query := &models.GetPreferencesWithDefaultsQuery{User: &models.SignedInUser{OrgId: 1}}
err = ss.GetPreferencesWithDefaults(context.Background(), query)
require.NoError(t, err)
require.Equal(t, int64(1), query.Result.HomeDashboardId)
})
t.Run("GetPreferencesWithDefaults with saved org, teams and user home dashboard should return user home dashboard", func(t *testing.T) {
err := ss.SavePreferences(context.Background(), &models.SavePreferencesCommand{OrgId: 1, HomeDashboardId: 1})
require.NoError(t, err)
err = ss.SavePreferences(context.Background(), &models.SavePreferencesCommand{OrgId: 1, TeamId: 2, HomeDashboardId: 2})
require.NoError(t, err)
err = ss.SavePreferences(context.Background(), &models.SavePreferencesCommand{OrgId: 1, TeamId: 3, HomeDashboardId: 3})
require.NoError(t, err)
err = ss.SavePreferences(context.Background(), &models.SavePreferencesCommand{OrgId: 1, UserId: 1, HomeDashboardId: 4})
require.NoError(t, err)
query := &models.GetPreferencesWithDefaultsQuery{
User: &models.SignedInUser{OrgId: 1, UserId: 1, Teams: []int64{2, 3}},
}
err = ss.GetPreferencesWithDefaults(context.Background(), query)
require.NoError(t, err)
require.Equal(t, int64(4), query.Result.HomeDashboardId)
})
t.Run("GetPreferencesWithDefaults with saved org, other teams and user home dashboard should return org home dashboard", func(t *testing.T) {
err := ss.SavePreferences(context.Background(), &models.SavePreferencesCommand{OrgId: 1, HomeDashboardId: 1})
require.NoError(t, err)
err = ss.SavePreferences(context.Background(), &models.SavePreferencesCommand{OrgId: 1, TeamId: 2, HomeDashboardId: 2})
require.NoError(t, err)
err = ss.SavePreferences(context.Background(), &models.SavePreferencesCommand{OrgId: 1, TeamId: 3, HomeDashboardId: 3})
require.NoError(t, err)
err = ss.SavePreferences(context.Background(), &models.SavePreferencesCommand{OrgId: 1, UserId: 1, HomeDashboardId: 4})
require.NoError(t, err)
query := &models.GetPreferencesWithDefaultsQuery{
User: &models.SignedInUser{OrgId: 1, UserId: 2},
}
err = ss.GetPreferencesWithDefaults(context.Background(), query)
require.NoError(t, err)
require.Equal(t, int64(1), query.Result.HomeDashboardId)
})
t.Run("SavePreferences for a user should store correct values", func(t *testing.T) {
err := ss.SavePreferences(context.Background(), &models.SavePreferencesCommand{UserId: models.SignedInUser{}.UserId, Theme: "dark", Timezone: "browser", HomeDashboardId: 5, WeekStart: "1"})
require.NoError(t, err)
query := &models.GetPreferencesWithDefaultsQuery{User: &models.SignedInUser{}}
err = ss.GetPreferencesWithDefaults(context.Background(), query)
require.NoError(t, err)
expected := &models.Preferences{
Id: query.Result.Id,
Version: query.Result.Version,
HomeDashboardId: 5,
Timezone: "browser",
WeekStart: "1",
Theme: "dark",
Created: query.Result.Created,
Updated: query.Result.Updated,
}
if diff := cmp.Diff(expected, query.Result); diff != "" {
t.Fatalf("Result mismatch (-want +got):\n%s", diff)
}
})
}