Skip to content

Replaced account single index key to use compound index #35

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
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
61 changes: 27 additions & 34 deletions src/controller/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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"))
Expand All @@ -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)

Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 6 additions & 2 deletions src/repository/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type AccountRepositoryMongo struct {
}

const domainIdFilter = "domain.id"
const environmentFilter = "environment"
const settingsActiveFilter = "settings.active"

func NewAccountRepositoryMongo(db *mongo.Database) *AccountRepositoryMongo {
Expand Down Expand Up @@ -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())
}
}

Expand Down
51 changes: 44 additions & 7 deletions src/repository/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down