Skip to content

Fixed settings being overridden when not provided #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/controller/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,41 @@ func TestUpdateAccountHandler(t *testing.T) {
assert.False(t, accountResponse.Settings.Active)
})

t.Run("Should update an account (only message) - ignore settings", func(t *testing.T) {
// Create an account
account1 := accountV1
account1.Domain.ID = "123-controller-update-account-message"
account1.Environment = "default"
account1.Settings.Active = true
account1.Settings.Window = "1m"
accountController.accountRepository.Create(&account1)

// Update the account
account1.Domain.Message = "Updated successfully"

// Test
payload, _ := json.Marshal(&model.Account{
Environment: account1.Environment,
Domain: account1.Domain,
})

req, _ := http.NewRequest(http.MethodPut, accountController.routeAccountPath, bytes.NewBuffer(payload))
response := executeRequest(req, r, token)

// Assert
var accountResponse model.Account
err := json.NewDecoder(response.Body).Decode(&accountResponse)

assert.Equal(t, http.StatusOK, response.Code)
assert.Nil(t, err)
assert.NotEmpty(t, accountResponse.Token)
assert.Equal(t, account1.Repository, accountResponse.Repository)
assert.Equal(t, model.StatusSynced, accountResponse.Domain.Status)
assert.Equal(t, "Updated successfully", accountResponse.Domain.Message)
assert.Equal(t, "1m", accountResponse.Settings.Window)
assert.True(t, accountResponse.Settings.Active)
})

t.Run("Should update account token only", func(t *testing.T) {
// Create an account
account1 := accountV1
Expand Down
2 changes: 1 addition & 1 deletion src/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ var accountV1 = model.Account{
Status: model.StatusSynced,
Message: "Synced successfully",
},
Settings: model.Settings{
Settings: &model.Settings{
Active: true,
Window: "10m",
ForcePrune: false,
Expand Down
2 changes: 1 addition & 1 deletion src/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func givenAccount() model.Account {
Status: model.StatusOutSync,
Message: "",
},
Settings: model.Settings{
Settings: &model.Settings{
Active: true,
Window: "5s",
ForcePrune: false,
Expand Down
2 changes: 1 addition & 1 deletion src/model/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Account struct {
Token string `json:"token"`
Environment string `json:"environment"`
Domain DomainDetails `json:"domain"`
Settings Settings `json:"settings"`
Settings *Settings `json:"settings"`
}

type DomainDetails struct {
Expand Down
9 changes: 5 additions & 4 deletions src/repository/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,11 @@ func getUpdateFields(account *model.Account) bson.M {
setIfNotEmpty(setMap, "domain.status", account.Domain.Status)
setIfNotEmpty(setMap, "domain.message", account.Domain.Message)

setIfNotEmpty(setMap, "settings.window", account.Settings.Window)

setMap["settings.active"] = account.Settings.Active
setMap["settings.forcePrune"] = account.Settings.ForcePrune
if account.Settings != nil {
setIfNotEmpty(setMap, "settings.window", account.Settings.Window)
setMap["settings.active"] = account.Settings.Active
setMap["settings.forcePrune"] = account.Settings.ForcePrune
}

update := bson.M{"$set": setMap}
return update
Expand Down
2 changes: 1 addition & 1 deletion src/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func givenAccount(active bool) model.Account {
Status: model.StatusSynced,
Message: "Synced successfully",
},
Settings: model.Settings{
Settings: &model.Settings{
Active: active,
Window: "10m",
ForcePrune: false,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func givenAccount(active bool) model.Account {
Status: model.StatusSynced,
Message: "Synced successfully",
},
Settings: model.Settings{
Settings: &model.Settings{
Active: active,
Window: "10m",
ForcePrune: false,
Expand Down
Loading