|
| 1 | +package preference |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/uuid" |
| 8 | + "github.com/raystack/frontier/internal/bootstrap/schema" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/mock" |
| 11 | +) |
| 12 | + |
| 13 | +type MockRepository struct { |
| 14 | + mock.Mock |
| 15 | +} |
| 16 | + |
| 17 | +func (m *MockRepository) Set(ctx context.Context, preference Preference) (Preference, error) { |
| 18 | + args := m.Called(ctx, preference) |
| 19 | + return args.Get(0).(Preference), args.Error(1) |
| 20 | +} |
| 21 | + |
| 22 | +func (m *MockRepository) Get(ctx context.Context, id uuid.UUID) (Preference, error) { |
| 23 | + args := m.Called(ctx, id) |
| 24 | + return args.Get(0).(Preference), args.Error(1) |
| 25 | +} |
| 26 | + |
| 27 | +func (m *MockRepository) List(ctx context.Context, filter Filter) ([]Preference, error) { |
| 28 | + args := m.Called(ctx, filter) |
| 29 | + if args.Get(0) == nil { |
| 30 | + return nil, args.Error(1) |
| 31 | + } |
| 32 | + return args.Get(0).([]Preference), args.Error(1) |
| 33 | +} |
| 34 | + |
| 35 | +func TestLoadUserPreferences(t *testing.T) { |
| 36 | + ctx := context.Background() |
| 37 | + userID := "user-123" |
| 38 | + orgID := "org-456" |
| 39 | + |
| 40 | + // Define test traits with defaults |
| 41 | + testTraits := []Trait{ |
| 42 | + { |
| 43 | + ResourceType: schema.UserPrincipal, |
| 44 | + Name: "theme", |
| 45 | + Default: "light", |
| 46 | + }, |
| 47 | + { |
| 48 | + ResourceType: schema.UserPrincipal, |
| 49 | + Name: "language", |
| 50 | + Default: "en", |
| 51 | + }, |
| 52 | + { |
| 53 | + ResourceType: schema.UserPrincipal, |
| 54 | + Name: "notifications", |
| 55 | + Default: "", // No default |
| 56 | + }, |
| 57 | + { |
| 58 | + ResourceType: schema.OrganizationNamespace, // Should be ignored for user prefs |
| 59 | + Name: "org_setting", |
| 60 | + Default: "default", |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + t.Run("without scope returns global DB preferences plus defaults", func(t *testing.T) { |
| 65 | + mockRepo := new(MockRepository) |
| 66 | + svc := NewService(mockRepo, testTraits) |
| 67 | + |
| 68 | + filter := Filter{UserID: userID} |
| 69 | + dbPrefs := []Preference{ |
| 70 | + {ID: "1", Name: "theme", Value: "dark", ResourceType: schema.UserPrincipal, ResourceID: userID}, |
| 71 | + } |
| 72 | + mockRepo.On("List", ctx, filter).Return(dbPrefs, nil) |
| 73 | + |
| 74 | + result, err := svc.LoadUserPreferences(ctx, filter) |
| 75 | + |
| 76 | + assert.NoError(t, err) |
| 77 | + // Should have "theme" (from DB) and "language" (default) |
| 78 | + assert.Len(t, result, 2) |
| 79 | + |
| 80 | + resultMap := make(map[string]Preference) |
| 81 | + for _, p := range result { |
| 82 | + resultMap[p.Name] = p |
| 83 | + } |
| 84 | + |
| 85 | + assert.Equal(t, "dark", resultMap["theme"].Value) |
| 86 | + assert.Equal(t, "1", resultMap["theme"].ID) |
| 87 | + assert.Equal(t, "en", resultMap["language"].Value) // default |
| 88 | + assert.Equal(t, "", resultMap["language"].ID) // no ID for default |
| 89 | + mockRepo.AssertExpectations(t) |
| 90 | + }) |
| 91 | + |
| 92 | + t.Run("with scope returns complete preference set with scoped, global, and defaults", func(t *testing.T) { |
| 93 | + mockRepo := new(MockRepository) |
| 94 | + svc := NewService(mockRepo, testTraits) |
| 95 | + |
| 96 | + scopedFilter := Filter{ |
| 97 | + UserID: userID, |
| 98 | + ScopeType: schema.OrganizationNamespace, |
| 99 | + ScopeID: orgID, |
| 100 | + } |
| 101 | + globalFilter := Filter{ |
| 102 | + UserID: userID, |
| 103 | + } |
| 104 | + |
| 105 | + // Scoped: "theme" is set for this org |
| 106 | + scopedPrefs := []Preference{ |
| 107 | + {ID: "1", Name: "theme", Value: "dark", ResourceType: schema.UserPrincipal, ResourceID: userID, ScopeType: schema.OrganizationNamespace, ScopeID: orgID}, |
| 108 | + } |
| 109 | + // Global: "language" is set globally |
| 110 | + globalPrefs := []Preference{ |
| 111 | + {ID: "2", Name: "language", Value: "fr", ResourceType: schema.UserPrincipal, ResourceID: userID}, |
| 112 | + } |
| 113 | + |
| 114 | + mockRepo.On("List", ctx, scopedFilter).Return(scopedPrefs, nil) |
| 115 | + mockRepo.On("List", ctx, globalFilter).Return(globalPrefs, nil) |
| 116 | + |
| 117 | + result, err := svc.LoadUserPreferences(ctx, scopedFilter) |
| 118 | + |
| 119 | + assert.NoError(t, err) |
| 120 | + // Should have "theme" (scoped DB), "language" (global DB) |
| 121 | + // "notifications" has no default so should not be included |
| 122 | + assert.Len(t, result, 2) |
| 123 | + |
| 124 | + // Build a map for easier assertions |
| 125 | + resultMap := make(map[string]Preference) |
| 126 | + for _, p := range result { |
| 127 | + resultMap[p.Name] = p |
| 128 | + } |
| 129 | + |
| 130 | + // theme should be from scoped DB (dark) |
| 131 | + assert.Equal(t, "dark", resultMap["theme"].Value) |
| 132 | + assert.Equal(t, "1", resultMap["theme"].ID) |
| 133 | + |
| 134 | + // language should be from global DB (fr) |
| 135 | + assert.Equal(t, "fr", resultMap["language"].Value) |
| 136 | + assert.Equal(t, "2", resultMap["language"].ID) |
| 137 | + |
| 138 | + mockRepo.AssertExpectations(t) |
| 139 | + }) |
| 140 | + |
| 141 | + t.Run("scoped preference takes priority over global", func(t *testing.T) { |
| 142 | + mockRepo := new(MockRepository) |
| 143 | + svc := NewService(mockRepo, testTraits) |
| 144 | + |
| 145 | + scopedFilter := Filter{ |
| 146 | + UserID: userID, |
| 147 | + ScopeType: schema.OrganizationNamespace, |
| 148 | + ScopeID: orgID, |
| 149 | + } |
| 150 | + globalFilter := Filter{ |
| 151 | + UserID: userID, |
| 152 | + } |
| 153 | + |
| 154 | + // Both scoped and global have "theme" |
| 155 | + scopedPrefs := []Preference{ |
| 156 | + {ID: "1", Name: "theme", Value: "dark", ResourceType: schema.UserPrincipal, ResourceID: userID, ScopeType: schema.OrganizationNamespace, ScopeID: orgID}, |
| 157 | + } |
| 158 | + globalPrefs := []Preference{ |
| 159 | + {ID: "2", Name: "theme", Value: "light", ResourceType: schema.UserPrincipal, ResourceID: userID}, |
| 160 | + } |
| 161 | + |
| 162 | + mockRepo.On("List", ctx, scopedFilter).Return(scopedPrefs, nil) |
| 163 | + mockRepo.On("List", ctx, globalFilter).Return(globalPrefs, nil) |
| 164 | + |
| 165 | + result, err := svc.LoadUserPreferences(ctx, scopedFilter) |
| 166 | + |
| 167 | + assert.NoError(t, err) |
| 168 | + |
| 169 | + resultMap := make(map[string]Preference) |
| 170 | + for _, p := range result { |
| 171 | + resultMap[p.Name] = p |
| 172 | + } |
| 173 | + |
| 174 | + // Scoped value should win |
| 175 | + assert.Equal(t, "dark", resultMap["theme"].Value) |
| 176 | + assert.Equal(t, "1", resultMap["theme"].ID) |
| 177 | + |
| 178 | + mockRepo.AssertExpectations(t) |
| 179 | + }) |
| 180 | + |
| 181 | + t.Run("with scope but all prefs set returns no defaults", func(t *testing.T) { |
| 182 | + mockRepo := new(MockRepository) |
| 183 | + svc := NewService(mockRepo, testTraits) |
| 184 | + |
| 185 | + scopedFilter := Filter{ |
| 186 | + UserID: userID, |
| 187 | + ScopeType: schema.OrganizationNamespace, |
| 188 | + ScopeID: orgID, |
| 189 | + } |
| 190 | + globalFilter := Filter{ |
| 191 | + UserID: userID, |
| 192 | + } |
| 193 | + |
| 194 | + // All traits with defaults are set in scoped DB |
| 195 | + scopedPrefs := []Preference{ |
| 196 | + {ID: "1", Name: "theme", Value: "dark", ResourceType: schema.UserPrincipal, ResourceID: userID}, |
| 197 | + {ID: "2", Name: "language", Value: "de", ResourceType: schema.UserPrincipal, ResourceID: userID}, |
| 198 | + } |
| 199 | + mockRepo.On("List", ctx, scopedFilter).Return(scopedPrefs, nil) |
| 200 | + mockRepo.On("List", ctx, globalFilter).Return([]Preference{}, nil) |
| 201 | + |
| 202 | + result, err := svc.LoadUserPreferences(ctx, scopedFilter) |
| 203 | + |
| 204 | + assert.NoError(t, err) |
| 205 | + assert.Len(t, result, 2) |
| 206 | + |
| 207 | + resultMap := make(map[string]Preference) |
| 208 | + for _, p := range result { |
| 209 | + resultMap[p.Name] = p |
| 210 | + } |
| 211 | + |
| 212 | + // Both should be from DB |
| 213 | + assert.Equal(t, "dark", resultMap["theme"].Value) |
| 214 | + assert.Equal(t, "de", resultMap["language"].Value) |
| 215 | + mockRepo.AssertExpectations(t) |
| 216 | + }) |
| 217 | + |
| 218 | + t.Run("repository error is propagated", func(t *testing.T) { |
| 219 | + mockRepo := new(MockRepository) |
| 220 | + svc := NewService(mockRepo, testTraits) |
| 221 | + |
| 222 | + filter := Filter{UserID: userID} |
| 223 | + mockRepo.On("List", ctx, filter).Return(nil, ErrInvalidFilter) |
| 224 | + |
| 225 | + result, err := svc.LoadUserPreferences(ctx, filter) |
| 226 | + |
| 227 | + assert.Error(t, err) |
| 228 | + assert.Nil(t, result) |
| 229 | + assert.Equal(t, ErrInvalidFilter, err) |
| 230 | + mockRepo.AssertExpectations(t) |
| 231 | + }) |
| 232 | +} |
0 commit comments