Skip to content

Commit 0133ccf

Browse files
authored
Refactored account controller handler to support environment (#36)
1 parent 90857fd commit 0133ccf

File tree

6 files changed

+54
-54
lines changed

6 files changed

+54
-54
lines changed

src/controller/account.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,10 @@ func NewAccountController(repo repository.AccountRepository, coreHandler *core.C
3131
}
3232

3333
func (controller *AccountController) RegisterRoutes(r *mux.Router) http.Handler {
34-
const routesDomainVar = "/{domainId}"
35-
36-
r.NewRoute().Path(controller.RouteAccountPath + routesDomainVar).Name("GetAccount").HandlerFunc(controller.FetchAccountHandler).Methods(http.MethodGet)
3734
r.NewRoute().Path(controller.RouteAccountPath).Name("CreateAccount").HandlerFunc(controller.CreateAccountHandler).Methods(http.MethodPost)
38-
r.NewRoute().Path(controller.RouteAccountPath + routesDomainVar).Name("UpdateAccount").HandlerFunc(controller.UpdateAccountHandler).Methods(http.MethodPut)
39-
r.NewRoute().Path(controller.RouteAccountPath + routesDomainVar).Name("DeleteAccount").HandlerFunc(controller.DeleteAccountHandler).Methods(http.MethodDelete)
35+
r.NewRoute().Path(controller.RouteAccountPath).Name("UpdateAccount").HandlerFunc(controller.UpdateAccountHandler).Methods(http.MethodPut)
36+
r.NewRoute().Path(controller.RouteAccountPath + "/{domainId}/{enviroment}").Name("GetAccount").HandlerFunc(controller.FetchAccountHandler).Methods(http.MethodGet)
37+
r.NewRoute().Path(controller.RouteAccountPath + "/{domainId}/{enviroment}").Name("DeleteAccount").HandlerFunc(controller.DeleteAccountHandler).Methods(http.MethodDelete)
4038

4139
return r
4240
}
@@ -51,7 +49,6 @@ func (controller *AccountController) CreateAccountHandler(w http.ResponseWriter,
5149

5250
// Encrypt token before saving
5351
if accountRequest.Token != "" {
54-
println("Encrypting token", accountRequest.Token)
5552
accountRequest.Token = utils.Encrypt(accountRequest.Token, config.GetEnv("GIT_TOKEN_PRIVATE_KEY"))
5653
}
5754

@@ -70,8 +67,10 @@ func (controller *AccountController) CreateAccountHandler(w http.ResponseWriter,
7067
}
7168

7269
func (controller *AccountController) FetchAccountHandler(w http.ResponseWriter, r *http.Request) {
73-
domainId := r.URL.Path[len(controller.RouteAccountPath+"/"):]
74-
account, err := controller.AccountRepository.FetchByDomainId(domainId)
70+
domainId := mux.Vars(r)["domainId"]
71+
enviroment := mux.Vars(r)["enviroment"]
72+
73+
account, err := controller.AccountRepository.FetchByDomainIdEnvironment(domainId, enviroment)
7574
if err != nil {
7675
utils.Log(utils.LogLevelError, "Error fetching account: %s", err.Error())
7776
utils.ResponseJSON(w, ErrorResponse{Error: "Account not found"}, http.StatusNotFound)
@@ -106,8 +105,10 @@ func (controller *AccountController) UpdateAccountHandler(w http.ResponseWriter,
106105
}
107106

108107
func (controller *AccountController) DeleteAccountHandler(w http.ResponseWriter, r *http.Request) {
109-
domainId := r.URL.Path[len(controller.RouteAccountPath+"/"):]
110-
err := controller.AccountRepository.DeleteByDomainId(domainId)
108+
domainId := mux.Vars(r)["domainId"]
109+
enviroment := mux.Vars(r)["enviroment"]
110+
111+
err := controller.AccountRepository.DeleteByDomainIdEnvironment(domainId, enviroment)
111112
if err != nil {
112113
utils.Log(utils.LogLevelError, "Error deleting account: %s", err.Error())
113114
utils.ResponseJSON(w, ErrorResponse{Error: "Error deleting account: " + err.Error()}, http.StatusInternalServerError)

src/controller/account_test.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import (
1414
"github.com/switcherapi/switcher-gitops/src/utils"
1515
)
1616

17-
const NOT_FOUND = "/not-found"
18-
1917
func TestCreateAccountHandler(t *testing.T) {
2018
t.Run("Should create an account", func(t *testing.T) {
2119
// Test
@@ -63,14 +61,14 @@ func TestCreateAccountHandler(t *testing.T) {
6361
}
6462

6563
func TestFetchAccountHandler(t *testing.T) {
66-
t.Run("Should fetch an account by domain ID", func(t *testing.T) {
64+
t.Run("Should fetch an account by domain ID / environment", func(t *testing.T) {
6765
// Create an account
6866
accountV1.Domain.ID = "123-controller-fetch-account"
6967
accountController.CreateAccountHandler(givenAccountRequest(accountV1))
7068

7169
// Test
7270
payload := []byte("")
73-
req, _ := http.NewRequest(http.MethodGet, accountController.RouteAccountPath+"/"+accountV1.Domain.ID, bytes.NewBuffer(payload))
71+
req, _ := http.NewRequest(http.MethodGet, accountController.RouteAccountPath+"/"+accountV1.Domain.ID+"/"+accountV1.Environment, bytes.NewBuffer(payload))
7472
response := executeRequest(req)
7573

7674
// Assert
@@ -82,10 +80,10 @@ func TestFetchAccountHandler(t *testing.T) {
8280
assert.Equal(t, accountV1.Repository, accountResponse.Repository)
8381
})
8482

85-
t.Run("Should not fetch an account by domain ID - not found", func(t *testing.T) {
83+
t.Run("Should not fetch an account by domain ID / environment - not found", func(t *testing.T) {
8684
// Test
8785
payload := []byte("")
88-
req, _ := http.NewRequest(http.MethodGet, accountController.RouteAccountPath+NOT_FOUND, bytes.NewBuffer(payload))
86+
req, _ := http.NewRequest(http.MethodGet, accountController.RouteAccountPath+"/not-found/default", bytes.NewBuffer(payload))
8987
response := executeRequest(req)
9088

9189
// Assert
@@ -104,7 +102,7 @@ func TestUpdateAccountHandler(t *testing.T) {
104102
accountV2.Domain.ID = accountV1.Domain.ID
105103
accountV2.Domain.Message = "Updated successfully"
106104
payload, _ := json.Marshal(accountV2)
107-
req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath+"/"+accountV2.Domain.ID, bytes.NewBuffer(payload))
105+
req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath, bytes.NewBuffer(payload))
108106
response := executeRequest(req)
109107

110108
// Assert
@@ -130,7 +128,7 @@ func TestUpdateAccountHandler(t *testing.T) {
130128
accountRequest.Token = "new-token"
131129

132130
payload, _ := json.Marshal(accountRequest)
133-
req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath+"/"+accountV1.Domain.ID, bytes.NewBuffer(payload))
131+
req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath, bytes.NewBuffer(payload))
134132
response := executeRequest(req)
135133

136134
// Assert
@@ -151,7 +149,7 @@ func TestUpdateAccountHandler(t *testing.T) {
151149
t.Run("Should not update an account - invalid request", func(t *testing.T) {
152150
// Test
153151
payload := []byte("")
154-
req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath+"/invalid-request", bytes.NewBuffer(payload))
152+
req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath, bytes.NewBuffer(payload))
155153
response := executeRequest(req)
156154

157155
// Assert
@@ -169,7 +167,7 @@ func TestUpdateAccountHandler(t *testing.T) {
169167

170168
// Test
171169
payload, _ := json.Marshal(accountV1)
172-
req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath+NOT_FOUND, bytes.NewBuffer(payload))
170+
req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath, bytes.NewBuffer(payload))
173171
response := executeRequest(req)
174172

175173
// Assert
@@ -179,22 +177,22 @@ func TestUpdateAccountHandler(t *testing.T) {
179177
}
180178

181179
func TestDeleteAccountHandler(t *testing.T) {
182-
t.Run("Should delete an account", func(t *testing.T) {
180+
t.Run("Should delete an account by domain ID / environment", func(t *testing.T) {
183181
// Create an account
184182
accountV1.Domain.ID = "123-controller-delete-account"
185183
accountController.CreateAccountHandler(givenAccountRequest(accountV1))
186184

187185
// Test
188-
req, _ := http.NewRequest(http.MethodDelete, accountController.RouteAccountPath+"/"+accountV1.Domain.ID, nil)
186+
req, _ := http.NewRequest(http.MethodDelete, accountController.RouteAccountPath+"/"+accountV1.Domain.ID+"/"+accountV1.Environment, nil)
189187
response := executeRequest(req)
190188

191189
// Assert
192190
assert.Equal(t, http.StatusNoContent, response.Code)
193191
})
194192

195-
t.Run("Should not delete an account - not found", func(t *testing.T) {
193+
t.Run("Should not delete an account by domain ID / environment - not found", func(t *testing.T) {
196194
// Test
197-
req, _ := http.NewRequest(http.MethodDelete, accountController.RouteAccountPath+NOT_FOUND, nil)
195+
req, _ := http.NewRequest(http.MethodDelete, accountController.RouteAccountPath+"/not-found/default", nil)
198196
response := executeRequest(req)
199197

200198
// Assert

src/controller/controller_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ var accountV1 = model.Account{
8181
}
8282

8383
var accountV2 = model.Account{
84-
Repository: "switcherapi/switcher-gitops",
85-
Branch: "main",
84+
Repository: "switcherapi/switcher-gitops",
85+
Branch: "main",
86+
Environment: "default",
8687
Domain: model.DomainDetails{
8788
ID: "123-controller-test",
8889
Name: "Switcher GitOps",

src/core/handler_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestInitCoreHandlerCoroutine(t *testing.T) {
2626
status, err := coreHandler.InitCoreHandlerCoroutine()
2727

2828
// Terminate the goroutine
29-
coreHandler.AccountRepository.DeleteByDomainId(accountCreated.Domain.ID)
29+
coreHandler.AccountRepository.DeleteByDomainIdEnvironment(accountCreated.Domain.ID, accountCreated.Environment)
3030
time.Sleep(1 * time.Second)
3131

3232
// Assert
@@ -50,7 +50,7 @@ func TestInitCoreHandlerCoroutine(t *testing.T) {
5050
status, _ := coreHandler.InitCoreHandlerCoroutine()
5151

5252
// Terminate the goroutine
53-
coreHandler.AccountRepository.DeleteByDomainId(accountCreated.Domain.ID)
53+
coreHandler.AccountRepository.DeleteByDomainIdEnvironment(accountCreated.Domain.ID, accountCreated.Environment)
5454
time.Sleep(1 * time.Second)
5555

5656
// Assert
@@ -76,7 +76,7 @@ func TestStartAccountHandler(t *testing.T) {
7676
time.Sleep(1 * time.Second)
7777

7878
// Assert
79-
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(accountCreated.Domain.ID)
79+
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainIdEnvironment(accountCreated.Domain.ID, accountCreated.Environment)
8080
assert.Equal(t, model.StatusPending, accountFromDb.Domain.Status)
8181
assert.Equal(t, "Account was deactivated", accountFromDb.Domain.Message)
8282
assert.Equal(t, "", accountFromDb.Domain.LastCommit)
@@ -99,7 +99,7 @@ func TestStartAccountHandler(t *testing.T) {
9999
time.Sleep(1 * time.Second)
100100

101101
// Assert
102-
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(accountCreated.Domain.ID)
102+
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainIdEnvironment(accountCreated.Domain.ID, accountCreated.Environment)
103103
assert.Equal(t, model.StatusError, accountFromDb.Domain.Status)
104104
assert.Contains(t, accountFromDb.Domain.Message, "Failed to fetch repository data")
105105
assert.Equal(t, "", accountFromDb.Domain.LastCommit)
@@ -125,7 +125,7 @@ func TestStartAccountHandler(t *testing.T) {
125125
time.Sleep(1 * time.Second)
126126

127127
// Assert
128-
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(accountCreated.Domain.ID)
128+
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainIdEnvironment(accountCreated.Domain.ID, accountCreated.Environment)
129129
assert.Equal(t, model.StatusError, accountFromDb.Domain.Status)
130130
assert.Contains(t, accountFromDb.Domain.Message, "Failed to fetch snapshot version")
131131
assert.Equal(t, "", accountFromDb.Domain.LastCommit)
@@ -146,12 +146,12 @@ func TestStartAccountHandler(t *testing.T) {
146146
numGoroutinesBefore := runtime.NumGoroutine()
147147

148148
// Terminate the goroutine
149-
coreHandler.AccountRepository.DeleteByDomainId(accountCreated.Domain.ID)
149+
coreHandler.AccountRepository.DeleteByDomainIdEnvironment(accountCreated.Domain.ID, accountCreated.Environment)
150150
time.Sleep(1 * time.Second)
151151
numGoroutinesAfter := runtime.NumGoroutine()
152152

153153
// Assert
154-
_, err := coreHandler.AccountRepository.FetchByDomainId(accountCreated.Domain.ID)
154+
_, err := coreHandler.AccountRepository.FetchByDomainIdEnvironment(accountCreated.Domain.ID, accountCreated.Environment)
155155
assert.LessOrEqual(t, numGoroutinesAfter, numGoroutinesBefore)
156156
assert.NotNil(t, err)
157157

@@ -175,7 +175,7 @@ func TestStartAccountHandler(t *testing.T) {
175175
time.Sleep(1 * time.Second)
176176

177177
// Assert
178-
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(accountCreated.Domain.ID)
178+
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainIdEnvironment(accountCreated.Domain.ID, accountCreated.Environment)
179179
assert.Equal(t, model.StatusSynced, accountFromDb.Domain.Status)
180180
assert.Contains(t, accountFromDb.Domain.Message, model.MessageSynced)
181181
assert.Equal(t, "123", accountFromDb.Domain.LastCommit)
@@ -221,7 +221,7 @@ func TestStartAccountHandler(t *testing.T) {
221221
time.Sleep(1 * time.Second)
222222

223223
// Assert
224-
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(accountCreated.Domain.ID)
224+
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainIdEnvironment(accountCreated.Domain.ID, accountCreated.Environment)
225225
assert.Equal(t, model.StatusSynced, accountFromDb.Domain.Status)
226226
assert.Contains(t, accountFromDb.Domain.Message, model.MessageSynced)
227227
assert.Equal(t, "123", accountFromDb.Domain.LastCommit)
@@ -255,7 +255,7 @@ func TestStartAccountHandler(t *testing.T) {
255255
time.Sleep(1 * time.Second)
256256

257257
// Assert
258-
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(accountCreated.Domain.ID)
258+
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainIdEnvironment(accountCreated.Domain.ID, accountCreated.Environment)
259259
assert.Equal(t, model.StatusSynced, accountFromDb.Domain.Status)
260260
assert.Contains(t, accountFromDb.Domain.Message, model.MessageSynced)
261261
assert.Equal(t, "123", accountFromDb.Domain.LastCommit)
@@ -289,7 +289,7 @@ func TestStartAccountHandler(t *testing.T) {
289289
time.Sleep(1 * time.Second)
290290

291291
// Assert
292-
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(accountCreated.Domain.ID)
292+
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainIdEnvironment(accountCreated.Domain.ID, accountCreated.Environment)
293293
assert.Equal(t, model.StatusSynced, accountFromDb.Domain.Status)
294294
assert.Contains(t, accountFromDb.Domain.Message, model.MessageSynced)
295295
assert.Equal(t, "123", accountFromDb.Domain.LastCommit)
@@ -347,7 +347,7 @@ func TestStartAccountHandler(t *testing.T) {
347347
time.Sleep(1 * time.Second)
348348

349349
// Assert
350-
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(accountCreated.Domain.ID)
350+
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainIdEnvironment(accountCreated.Domain.ID, accountCreated.Environment)
351351
assert.Equal(t, model.StatusError, accountFromDb.Domain.Status)
352352
assert.Contains(t, accountFromDb.Domain.Message, "Failed to check for changes")
353353
assert.Equal(t, "", accountFromDb.Domain.LastCommit)
@@ -375,7 +375,7 @@ func TestStartAccountHandler(t *testing.T) {
375375
time.Sleep(1 * time.Second)
376376

377377
// Assert
378-
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(accountCreated.Domain.ID)
378+
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainIdEnvironment(accountCreated.Domain.ID, accountCreated.Environment)
379379
assert.Equal(t, model.StatusError, accountFromDb.Domain.Status)
380380
assert.Contains(t, accountFromDb.Domain.Message, "authorization failed")
381381
assert.Contains(t, accountFromDb.Domain.Message, "Failed to apply changes [Repository]")

src/repository/account.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ import (
1616
type AccountRepository interface {
1717
Create(account *model.Account) (*model.Account, error)
1818
FetchByAccountId(accountId string) (*model.Account, error)
19-
FetchByDomainId(domainId string) (*model.Account, error)
19+
FetchByDomainIdEnvironment(domainId string, environment string) (*model.Account, error)
2020
FetchAllActiveAccounts() ([]model.Account, error)
2121
Update(account *model.Account) (*model.Account, error)
2222
DeleteByAccountId(accountId string) error
23-
DeleteByDomainId(domainId string) error
23+
DeleteByDomainIdEnvironment(domainId string, environment string) error
2424
}
2525

2626
type AccountRepositoryMongo struct {
@@ -64,7 +64,7 @@ func (repo *AccountRepositoryMongo) FetchByAccountId(accountId string) (*model.A
6464
return &account, nil
6565
}
6666

67-
func (repo *AccountRepositoryMongo) FetchByDomainId(domainId string) (*model.Account, error) {
67+
func (repo *AccountRepositoryMongo) FetchByDomainIdEnvironment(domainId string, environment string) (*model.Account, error) {
6868
collection, ctx, cancel := getDbContext(repo)
6969
defer cancel()
7070

@@ -101,7 +101,7 @@ func (repo *AccountRepositoryMongo) Update(account *model.Account) (*model.Accou
101101
collection, ctx, cancel := getDbContext(repo)
102102
defer cancel()
103103

104-
filter := primitive.M{domainIdFilter: account.Domain.ID}
104+
filter := primitive.M{domainIdFilter: account.Domain.ID, environmentFilter: account.Environment}
105105
update := getUpdateFields(account)
106106

107107
var updatedAccount model.Account
@@ -131,11 +131,11 @@ func (repo *AccountRepositoryMongo) DeleteByAccountId(accountId string) error {
131131
return err
132132
}
133133

134-
func (repo *AccountRepositoryMongo) DeleteByDomainId(domainId string) error {
134+
func (repo *AccountRepositoryMongo) DeleteByDomainIdEnvironment(domainId string, environment string) error {
135135
collection, ctx, cancel := getDbContext(repo)
136136
defer cancel()
137137

138-
filter := primitive.M{domainIdFilter: domainId}
138+
filter := primitive.M{domainIdFilter: domainId, environmentFilter: environment}
139139
result, err := collection.DeleteOne(ctx, filter)
140140

141141
if result.DeletedCount == 0 {

src/repository/account_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestCreateAccount(t *testing.T) {
2121
assert.NotNil(t, createdAccount.ID)
2222
})
2323

24-
t.Run("Should create 2 accounts for each environment", func(t *testing.T) {
24+
t.Run("Should create accounts for each environment", func(t *testing.T) {
2525
// Given
2626
account1 := givenAccount(true)
2727
account2 := givenAccount(true)
@@ -79,23 +79,23 @@ func TestFetchAccount(t *testing.T) {
7979
assert.NotNil(t, err)
8080
})
8181

82-
t.Run("Should fetch an account by domain ID", func(t *testing.T) {
82+
t.Run("Should fetch an account by domain ID and environment", func(t *testing.T) {
8383
// Given
8484
account := givenAccount(true)
8585
account.Domain.ID = "123-fetch-account-by-domain-id"
8686
accountCreated, _ := accountRepository.Create(&account)
8787

8888
// Test
89-
fetchedAccount, err := accountRepository.FetchByDomainId(accountCreated.Domain.ID)
89+
fetchedAccount, err := accountRepository.FetchByDomainIdEnvironment(accountCreated.Domain.ID, accountCreated.Environment)
9090

9191
// Assert
9292
assert.Nil(t, err)
9393
assert.NotNil(t, fetchedAccount.ID)
9494
})
9595

96-
t.Run("Should not fetch an account by domain ID - not found", func(t *testing.T) {
96+
t.Run("Should not fetch an account by domain ID and environment - not found", func(t *testing.T) {
9797
// Test
98-
_, err := accountRepository.FetchByDomainId("non_existent_domain_id")
98+
_, err := accountRepository.FetchByDomainIdEnvironment("non_existent_domain_id", "default")
9999

100100
// Assert
101101
assert.NotNil(t, err)
@@ -175,22 +175,22 @@ func TestDeleteAccount(t *testing.T) {
175175
assert.Equal(t, "Account not found for id: non_existent_id", err.Error())
176176
})
177177

178-
t.Run("Should delete an account by domain ID", func(t *testing.T) {
178+
t.Run("Should delete an account by domain ID and environment", func(t *testing.T) {
179179
// Given
180180
account := givenAccount(true)
181181
account.Domain.ID = "123-delete-account-by-domain-id"
182182
accountCreated, _ := accountRepository.Create(&account)
183183

184184
// Test
185-
err := accountRepository.DeleteByDomainId(accountCreated.Domain.ID)
185+
err := accountRepository.DeleteByDomainIdEnvironment(accountCreated.Domain.ID, accountCreated.Environment)
186186

187187
// Assert
188188
assert.Nil(t, err)
189189
})
190190

191-
t.Run("Should not delete an account by domain ID - not found", func(t *testing.T) {
191+
t.Run("Should not delete an account by domain ID and environment - not found", func(t *testing.T) {
192192
// Test
193-
err := accountRepository.DeleteByDomainId("non_existent_domain_id")
193+
err := accountRepository.DeleteByDomainIdEnvironment("non_existent_domain_id", "default")
194194

195195
// Assert
196196
assert.NotNil(t, err)

0 commit comments

Comments
 (0)