Skip to content

Commit fe4f4f7

Browse files
authored
Fixed settings being overridden when not provided (#56)
1 parent 96999c4 commit fe4f4f7

File tree

7 files changed

+45
-9
lines changed

7 files changed

+45
-9
lines changed

src/controller/account_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,41 @@ func TestUpdateAccountHandler(t *testing.T) {
177177
assert.False(t, accountResponse.Settings.Active)
178178
})
179179

180+
t.Run("Should update an account (only message) - ignore settings", func(t *testing.T) {
181+
// Create an account
182+
account1 := accountV1
183+
account1.Domain.ID = "123-controller-update-account-message"
184+
account1.Environment = "default"
185+
account1.Settings.Active = true
186+
account1.Settings.Window = "1m"
187+
accountController.accountRepository.Create(&account1)
188+
189+
// Update the account
190+
account1.Domain.Message = "Updated successfully"
191+
192+
// Test
193+
payload, _ := json.Marshal(&model.Account{
194+
Environment: account1.Environment,
195+
Domain: account1.Domain,
196+
})
197+
198+
req, _ := http.NewRequest(http.MethodPut, accountController.routeAccountPath, bytes.NewBuffer(payload))
199+
response := executeRequest(req, r, token)
200+
201+
// Assert
202+
var accountResponse model.Account
203+
err := json.NewDecoder(response.Body).Decode(&accountResponse)
204+
205+
assert.Equal(t, http.StatusOK, response.Code)
206+
assert.Nil(t, err)
207+
assert.NotEmpty(t, accountResponse.Token)
208+
assert.Equal(t, account1.Repository, accountResponse.Repository)
209+
assert.Equal(t, model.StatusSynced, accountResponse.Domain.Status)
210+
assert.Equal(t, "Updated successfully", accountResponse.Domain.Message)
211+
assert.Equal(t, "1m", accountResponse.Settings.Window)
212+
assert.True(t, accountResponse.Settings.Active)
213+
})
214+
180215
t.Run("Should update account token only", func(t *testing.T) {
181216
// Create an account
182217
account1 := accountV1

src/controller/controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ var accountV1 = model.Account{
101101
Status: model.StatusSynced,
102102
Message: "Synced successfully",
103103
},
104-
Settings: model.Settings{
104+
Settings: &model.Settings{
105105
Active: true,
106106
Window: "10m",
107107
ForcePrune: false,

src/core/core_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func givenAccount() model.Account {
6464
Status: model.StatusOutSync,
6565
Message: "",
6666
},
67-
Settings: model.Settings{
67+
Settings: &model.Settings{
6868
Active: true,
6969
Window: "5s",
7070
ForcePrune: false,

src/model/account.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type Account struct {
2626
Token string `json:"token"`
2727
Environment string `json:"environment"`
2828
Domain DomainDetails `json:"domain"`
29-
Settings Settings `json:"settings"`
29+
Settings *Settings `json:"settings"`
3030
}
3131

3232
type DomainDetails struct {

src/repository/account.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,11 @@ func getUpdateFields(account *model.Account) bson.M {
182182
setIfNotEmpty(setMap, "domain.status", account.Domain.Status)
183183
setIfNotEmpty(setMap, "domain.message", account.Domain.Message)
184184

185-
setIfNotEmpty(setMap, "settings.window", account.Settings.Window)
186-
187-
setMap["settings.active"] = account.Settings.Active
188-
setMap["settings.forcePrune"] = account.Settings.ForcePrune
185+
if account.Settings != nil {
186+
setIfNotEmpty(setMap, "settings.window", account.Settings.Window)
187+
setMap["settings.active"] = account.Settings.Active
188+
setMap["settings.forcePrune"] = account.Settings.ForcePrune
189+
}
189190

190191
update := bson.M{"$set": setMap}
191192
return update

src/repository/repository_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func givenAccount(active bool) model.Account {
5151
Status: model.StatusSynced,
5252
Message: "Synced successfully",
5353
},
54-
Settings: model.Settings{
54+
Settings: &model.Settings{
5555
Active: active,
5656
Window: "10m",
5757
ForcePrune: false,

src/utils/util_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func givenAccount(active bool) model.Account {
127127
Status: model.StatusSynced,
128128
Message: "Synced successfully",
129129
},
130-
Settings: model.Settings{
130+
Settings: &model.Settings{
131131
Active: active,
132132
Window: "10m",
133133
ForcePrune: false,

0 commit comments

Comments
 (0)