Skip to content

Fixed account update and other minor issues with syncing #34

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 2 commits into from
Sep 14, 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
2 changes: 1 addition & 1 deletion .env.test
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PORT=8000
LOG_LEVEL=INFO
LOG_LEVEL=DEBUG
MONGO_URI=mongodb://localhost:27017
MONGO_DB=switcher-gitops-test
GIT_TOKEN_PRIVATE_KEY=SecretSecretSecretSecretSecretSe
Expand Down
12 changes: 12 additions & 0 deletions src/controller/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

"github.com/gorilla/mux"
"github.com/switcherapi/switcher-gitops/src/config"
"github.com/switcherapi/switcher-gitops/src/core"
"github.com/switcherapi/switcher-gitops/src/model"
"github.com/switcherapi/switcher-gitops/src/repository"
Expand Down Expand Up @@ -48,6 +49,12 @@ func (controller *AccountController) CreateAccountHandler(w http.ResponseWriter,
return
}

// Encrypt token before saving
if accountRequest.Token != "" {
println("Encrypting token", accountRequest.Token)
accountRequest.Token = utils.Encrypt(accountRequest.Token, config.GetEnv("GIT_TOKEN_PRIVATE_KEY"))
}

accountCreated, err := controller.AccountRepository.Create(&accountRequest)
if err != nil {
utils.Log(utils.LogLevelError, "Error creating account: %s", err.Error())
Expand Down Expand Up @@ -83,6 +90,11 @@ func (controller *AccountController) UpdateAccountHandler(w http.ResponseWriter,
return
}

// Encrypt token before saving
if accountRequest.Token != "" {
accountRequest.Token = utils.Encrypt(accountRequest.Token, config.GetEnv("GIT_TOKEN_PRIVATE_KEY"))
}

accountUpdated, err := controller.AccountRepository.Update(&accountRequest)
if err != nil {
utils.Log(utils.LogLevelError, "Error updating account: %s", err.Error())
Expand Down
17 changes: 11 additions & 6 deletions src/controller/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ func TestUpdateAccountHandler(t *testing.T) {
accountController.CreateAccountHandler(givenAccountRequest(accountV1))

// Test
payload, _ := json.Marshal(accountV2)
req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath+"/"+accountV2.Domain.ID, bytes.NewBuffer(payload))
accountV2Copy := accountV2
accountV2Copy.Domain.Message = "Updated successfully"
payload, _ := json.Marshal(accountV2Copy)
req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath+"/"+accountV2Copy.Domain.ID, bytes.NewBuffer(payload))
response := executeRequest(req)

// Assert
Expand All @@ -108,11 +110,11 @@ func TestUpdateAccountHandler(t *testing.T) {

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

t.Run("Should update account token only", func(t *testing.T) {
Expand All @@ -137,6 +139,9 @@ func TestUpdateAccountHandler(t *testing.T) {

encryptedToken := utils.Encrypt(accountV1.Token, config.GetEnv("GIT_TOKEN_PRIVATE_KEY"))
assert.NotEqual(t, encryptedToken, accountResponse.Token)

decryptedToken, _ := utils.Decrypt(accountResponse.Token, config.GetEnv("GIT_TOKEN_PRIVATE_KEY"))
assert.Equal(t, "new-token", decryptedToken)
})

t.Run("Should not update an account - invalid request", func(t *testing.T) {
Expand Down
8 changes: 3 additions & 5 deletions src/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ var accountV1 = model.Account{
Name: "Switcher GitOps",
Version: 123,
LastCommit: "123",
LastDate: "2021-01-01",
Status: model.StatusSynced,
Message: "Synced successfully",
},
Expand All @@ -82,11 +83,8 @@ var accountV2 = model.Account{
Repository: "switcherapi/switcher-gitops",
Branch: "main",
Domain: model.DomainDetails{
ID: "123-controller-test",
Name: "Switcher GitOps",
Version: 123,
LastCommit: "123",
Status: model.StatusSynced,
ID: "123-controller-test",
Name: "Switcher GitOps",
},
Settings: model.Settings{
Active: false,
Expand Down
12 changes: 12 additions & 0 deletions src/core/comparator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type IComparatorService interface {
CheckSnapshotDiff(left model.Snapshot, right model.Snapshot, diffType DiffType) model.DiffResult
MergeResults(diffResults []model.DiffResult) model.DiffResult
NewSnapshotFromJson(jsonData []byte) model.Snapshot
RemoveDeleted(diffResult model.DiffResult) model.DiffResult
}

type ComparatorService struct{}
Expand Down Expand Up @@ -55,6 +56,17 @@ func (c *ComparatorService) MergeResults(diffResults []model.DiffResult) model.D
return result
}

func (c *ComparatorService) RemoveDeleted(diffResult model.DiffResult) model.DiffResult {
diff := model.DiffResult{Changes: []model.DiffDetails{}}
for _, change := range diffResult.Changes {
if change.Action != string(DELETED) {
diff.Changes = append(diff.Changes, change)
}
}

return diff
}

func checkGroupDiff(left model.Snapshot, right model.Snapshot, diffType DiffType, diffResult model.DiffResult) model.DiffResult {
for _, leftGroup := range left.Domain.Group {
if !slices.Contains(model.GroupNames(right.Domain.Group), leftGroup.Name) {
Expand Down
Loading