From 1b3cfce7befcb21d4938d33b3dc2704d9f3c302d Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Sat, 14 Sep 2024 12:15:34 -0700 Subject: [PATCH] Replaced account single index key to use compound index --- src/controller/account_test.go | 61 ++++++++++++++----------------- src/controller/controller_test.go | 7 ++-- src/repository/account.go | 8 +++- src/repository/account_test.go | 51 ++++++++++++++++++++++---- 4 files changed, 81 insertions(+), 46 deletions(-) diff --git a/src/controller/account_test.go b/src/controller/account_test.go index a483799..85fc5eb 100644 --- a/src/controller/account_test.go +++ b/src/controller/account_test.go @@ -19,9 +19,8 @@ const NOT_FOUND = "/not-found" func TestCreateAccountHandler(t *testing.T) { t.Run("Should create an account", func(t *testing.T) { // Test - accountV1Copy := &accountV1 - accountV1Copy.Domain.ID = "123-controller-create-account" - payload, _ := json.Marshal(accountV1Copy) + accountV1.Domain.ID = "123-controller-create-account" + payload, _ := json.Marshal(accountV1) req, _ := http.NewRequest(http.MethodPost, accountController.RouteAccountPath, bytes.NewBuffer(payload)) response := executeRequest(req) @@ -31,10 +30,10 @@ func TestCreateAccountHandler(t *testing.T) { assert.Equal(t, http.StatusCreated, response.Code) assert.Nil(t, err) - assert.Equal(t, accountV1Copy.Repository, accountResponse.Repository) + assert.Equal(t, accountV1.Repository, accountResponse.Repository) token, _ := utils.Decrypt(accountResponse.Token, config.GetEnv("GIT_TOKEN_PRIVATE_KEY")) - assert.Equal(t, accountV1Copy.Token, token) + assert.Equal(t, accountV1.Token, token) }) t.Run("Should not create an account - invalid request", func(t *testing.T) { @@ -66,13 +65,12 @@ func TestCreateAccountHandler(t *testing.T) { func TestFetchAccountHandler(t *testing.T) { t.Run("Should fetch an account by domain ID", func(t *testing.T) { // Create an account - accountV1Copy := &accountV1 - accountV1Copy.Domain.ID = "123-controller-fetch-account" - accountController.CreateAccountHandler(givenAccountRequest(*accountV1Copy)) + accountV1.Domain.ID = "123-controller-fetch-account" + accountController.CreateAccountHandler(givenAccountRequest(accountV1)) // Test payload := []byte("") - req, _ := http.NewRequest(http.MethodGet, accountController.RouteAccountPath+"/"+accountV1Copy.Domain.ID, bytes.NewBuffer(payload)) + req, _ := http.NewRequest(http.MethodGet, accountController.RouteAccountPath+"/"+accountV1.Domain.ID, bytes.NewBuffer(payload)) response := executeRequest(req) // Assert @@ -81,7 +79,7 @@ func TestFetchAccountHandler(t *testing.T) { assert.Equal(t, http.StatusOK, response.Code) assert.Nil(t, err) - assert.Equal(t, accountV1Copy.Repository, accountResponse.Repository) + assert.Equal(t, accountV1.Repository, accountResponse.Repository) }) t.Run("Should not fetch an account by domain ID - not found", func(t *testing.T) { @@ -99,16 +97,14 @@ func TestFetchAccountHandler(t *testing.T) { func TestUpdateAccountHandler(t *testing.T) { t.Run("Should update an account", func(t *testing.T) { // Create an account - accountV1Copy := &accountV1 - accountV1Copy.Domain.ID = "123-controller-update-account" - accountController.CreateAccountHandler(givenAccountRequest(*accountV1Copy)) + accountV1.Domain.ID = "123-controller-update-account" + accountController.CreateAccountHandler(givenAccountRequest(accountV1)) // Test - accountV2Copy := &accountV2 - accountV2Copy.Domain.ID = accountV1Copy.Domain.ID - accountV2Copy.Domain.Message = "Updated successfully" - payload, _ := json.Marshal(accountV2Copy) - req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath+"/"+accountV2Copy.Domain.ID, bytes.NewBuffer(payload)) + accountV2.Domain.ID = accountV1.Domain.ID + accountV2.Domain.Message = "Updated successfully" + payload, _ := json.Marshal(accountV2) + req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath+"/"+accountV2.Domain.ID, bytes.NewBuffer(payload)) response := executeRequest(req) // Assert @@ -126,16 +122,15 @@ func TestUpdateAccountHandler(t *testing.T) { t.Run("Should update account token only", func(t *testing.T) { // Create an account - accountV1Copy := &accountV1 - accountV1Copy.Domain.ID = "123-controller-update-account-token" - accountController.CreateAccountHandler(givenAccountRequest(*accountV1Copy)) + accountV1.Domain.ID = "123-controller-update-account-token" + accountController.CreateAccountHandler(givenAccountRequest(accountV1)) // Test - accountRequest := *accountV1Copy + accountRequest := accountV1 accountRequest.Token = "new-token" payload, _ := json.Marshal(accountRequest) - req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath+"/"+accountV1Copy.Domain.ID, bytes.NewBuffer(payload)) + req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath+"/"+accountV1.Domain.ID, bytes.NewBuffer(payload)) response := executeRequest(req) // Assert @@ -144,9 +139,9 @@ func TestUpdateAccountHandler(t *testing.T) { assert.Equal(t, http.StatusOK, response.Code) assert.Nil(t, err) - assert.Equal(t, accountV1Copy.Repository, accountResponse.Repository) + assert.Equal(t, accountV1.Repository, accountResponse.Repository) - encryptedToken := utils.Encrypt(accountV1Copy.Token, config.GetEnv("GIT_TOKEN_PRIVATE_KEY")) + 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")) @@ -166,15 +161,14 @@ func TestUpdateAccountHandler(t *testing.T) { t.Run("Should not update an account - not found", func(t *testing.T) { // Create an account - accountV1Copy := &accountV1 - accountV1Copy.Domain.ID = "123-controller-update-account-not-found" - accountController.CreateAccountHandler(givenAccountRequest(*accountV1Copy)) + accountV1.Domain.ID = "123-controller-update-account-not-found" + accountController.CreateAccountHandler(givenAccountRequest(accountV1)) // Replace the domain ID to force an error - accountV1Copy.Domain.ID = "111" + accountV1.Domain.ID = "111" // Test - payload, _ := json.Marshal(accountV1Copy) + payload, _ := json.Marshal(accountV1) req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath+NOT_FOUND, bytes.NewBuffer(payload)) response := executeRequest(req) @@ -187,12 +181,11 @@ func TestUpdateAccountHandler(t *testing.T) { func TestDeleteAccountHandler(t *testing.T) { t.Run("Should delete an account", func(t *testing.T) { // Create an account - accountV1Copy := &accountV1 - accountV1Copy.Domain.ID = "123-controller-delete-account" - accountController.CreateAccountHandler(givenAccountRequest(*accountV1Copy)) + accountV1.Domain.ID = "123-controller-delete-account" + accountController.CreateAccountHandler(givenAccountRequest(accountV1)) // Test - req, _ := http.NewRequest(http.MethodDelete, accountController.RouteAccountPath+"/"+accountV1Copy.Domain.ID, nil) + req, _ := http.NewRequest(http.MethodDelete, accountController.RouteAccountPath+"/"+accountV1.Domain.ID, nil) response := executeRequest(req) // Assert diff --git a/src/controller/controller_test.go b/src/controller/controller_test.go index cb4aad8..6992850 100644 --- a/src/controller/controller_test.go +++ b/src/controller/controller_test.go @@ -60,9 +60,10 @@ func executeRequest(req *http.Request) *httptest.ResponseRecorder { // Fixtures var accountV1 = model.Account{ - Repository: "switcherapi/switcher-gitops", - Branch: "master", - Token: "github_pat_123", + Repository: "switcherapi/switcher-gitops", + Branch: "master", + Token: "github_pat_123", + Environment: "default", Domain: model.DomainDetails{ ID: "123-controller-test", Name: "Switcher GitOps", diff --git a/src/repository/account.go b/src/repository/account.go index 3138485..f75f836 100644 --- a/src/repository/account.go +++ b/src/repository/account.go @@ -28,6 +28,7 @@ type AccountRepositoryMongo struct { } const domainIdFilter = "domain.id" +const environmentFilter = "environment" const settingsActiveFilter = "settings.active" func NewAccountRepositoryMongo(db *mongo.Database) *AccountRepositoryMongo { @@ -154,13 +155,16 @@ func registerAccountRepositoryValidators(db *mongo.Database) { collection := db.Collection(model.CollectionName) indexOptions := options.Index().SetUnique(true) indexModel := mongo.IndexModel{ - Keys: bson.M{domainIdFilter: 1}, + Keys: bson.D{ + {Key: domainIdFilter, Value: 1}, + {Key: environmentFilter, Value: 1}, + }, Options: indexOptions, } _, err := collection.Indexes().CreateOne(context.Background(), indexModel) if err != nil { - utils.Log(utils.LogLevelError, "Error creating index for domain.id: %s", err.Error()) + utils.Log(utils.LogLevelError, "Error creating index for account (environment, domain.id): %s", err.Error()) } } diff --git a/src/repository/account_test.go b/src/repository/account_test.go index f0a280c..92a433f 100644 --- a/src/repository/account_test.go +++ b/src/repository/account_test.go @@ -8,15 +8,52 @@ import ( ) func TestCreateAccount(t *testing.T) { - // Given - account := givenAccount(true) + t.Run("Should create an account", func(t *testing.T) { + // Given + account := givenAccount(true) + account.Domain.ID = "123-create-account" + + // Test + createdAccount, err := accountRepository.Create(&account) + + // Assert + assert.Nil(t, err) + assert.NotNil(t, createdAccount.ID) + }) + + t.Run("Should create 2 accounts for each environment", func(t *testing.T) { + // Given + account1 := givenAccount(true) + account2 := givenAccount(true) + account1.Domain.ID = "123-create-multiple-accounts" + account1.Environment = "default" + account2.Domain.ID = "123-create-multiple-accounts" + account2.Environment = "staging" - // Test - createdAccount, err := accountRepository.Create(&account) + // Test + createdAccount1, _ := accountRepository.Create(&account1) + createdAccount2, _ := accountRepository.Create(&account2) - // Assert - assert.Nil(t, err) - assert.NotNil(t, createdAccount.ID) + // Assert + assert.NotNil(t, createdAccount1.ID) + assert.NotNil(t, createdAccount2.ID) + assert.NotEqual(t, createdAccount1.ID, createdAccount2.ID) + }) + + t.Run("Should not create an account - same domain ID and environment", func(t *testing.T) { + // Given + account := givenAccount(true) + account.Domain.ID = "123-create-account-same-domain-id" + account.Environment = "default" + + // Test + accountRepository.Create(&account) + _, err := accountRepository.Create(&account) + + // Assert + assert.NotNil(t, err) + assert.Contains(t, err.Error(), "duplicate key error") + }) } func TestFetchAccount(t *testing.T) {