Skip to content

Fixed update account tokens moving domainId to param query #53

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 19, 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
8 changes: 5 additions & 3 deletions resources/postman/Switcher GitOps.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,22 @@
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n\t\"domainId\": \"{{domain_id}}\",\r\n\t\"token\": \"{{github_pat}}\",\r\n \"environments\": [\r\n \"default\",\r\n \"staging\"\r\n ]\r\n}",
"raw": "{\r\n\t\"token\": \"{{github_pat}}\",\r\n \"environments\": [\r\n \"default\",\r\n \"staging\"\r\n ]\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{url}}/account",
"raw": "{{url}}/account/tokens/{{domain_id}}",
"host": [
"{{url}}"
],
"path": [
"account"
"account",
"tokens",
"{{domain_id}}"
]
}
},
Expand Down
7 changes: 2 additions & 5 deletions resources/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
/account/tokens/{domainId}:
put:
tags:
- Account API
Expand Down Expand Up @@ -210,7 +211,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
$ref: '#/components/schemas/ErrorResponse'
/account/{domainId}/{environment}:
get:
tags:
Expand Down Expand Up @@ -407,10 +408,6 @@ components:
token:
type: string
description: Git token
domainId:
type: string
format: uuid
description: Domain ID
environments:
type: array
items:
Expand Down
7 changes: 4 additions & 3 deletions src/controller/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type AccountController struct {

type AccountTokensRequest struct {
Token string `json:"token"`
DomainId string `json:"domainId"`
Environments []string `json:"environments"`
}

Expand All @@ -50,7 +49,7 @@ func (controller *AccountController) RegisterRoutes(r *mux.Router) http.Handler
ValidateToken(http.HandlerFunc(controller.CreateAccountHandler))).Methods(http.MethodPost)
r.NewRoute().Path(controller.routeAccountPath).Name("UpdateAccount").Handler(
ValidateToken(http.HandlerFunc(controller.UpdateAccountHandler))).Methods(http.MethodPut)
r.NewRoute().Path(controller.routeAccountPath + "/{domainId}").Name("UpdateAccountTokens").Handler(
r.NewRoute().Path(controller.routeAccountPath + "/tokens/{domainId}").Name("UpdateAccountTokens").Handler(
ValidateToken(http.HandlerFunc(controller.UpdateAccountTokensHandler))).Methods(http.MethodPut)
r.NewRoute().Path(controller.routeAccountPath + "/{domainId}").Name("GelAllAccountsByDomainId").Handler(
ValidateToken(http.HandlerFunc(controller.FetchAllAccountsByDomainIdHandler))).Methods(http.MethodGet)
Expand Down Expand Up @@ -150,6 +149,8 @@ func (controller *AccountController) UpdateAccountHandler(w http.ResponseWriter,
}

func (controller *AccountController) UpdateAccountTokensHandler(w http.ResponseWriter, r *http.Request) {
domainId := mux.Vars(r)["domainId"]

var accountTokensRequest AccountTokensRequest
err := json.NewDecoder(r.Body).Decode(&accountTokensRequest)
if err != nil {
Expand All @@ -169,7 +170,7 @@ func (controller *AccountController) UpdateAccountTokensHandler(w http.ResponseW

// Update account tokens
for _, environment := range accountTokensRequest.Environments {
account, err := controller.accountRepository.FetchByDomainIdEnvironment(accountTokensRequest.DomainId, environment)
account, err := controller.accountRepository.FetchByDomainIdEnvironment(domainId, environment)
if err != nil {
utils.LogError("Error fetching account: %s", err.Error())
utils.ResponseJSON(w, ErrorResponse{Error: "Error fetching account"}, http.StatusNotFound)
Expand Down
11 changes: 4 additions & 7 deletions src/controller/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,11 @@ func TestUpdateAccountTokensHandler(t *testing.T) {

// Test
payload, _ := json.Marshal(AccountTokensRequest{
DomainId: account1.Domain.ID,
Environments: []string{account1.Environment, account2.Environment},
Token: "new-token",
})

req, _ := http.NewRequest(http.MethodPut, accountController.routeAccountPath+"/"+account1.Domain.ID, bytes.NewBuffer(payload))
req, _ := http.NewRequest(http.MethodPut, accountController.routeAccountPath+"/tokens/"+account1.Domain.ID, bytes.NewBuffer(payload))
response := executeRequest(req, r, token)

// Assert
Expand All @@ -271,12 +270,11 @@ func TestUpdateAccountTokensHandler(t *testing.T) {
t.Run("Should not update account tokens - token is required", func(t *testing.T) {
// Test
payload, _ := json.Marshal(AccountTokensRequest{
DomainId: "123-controller-update-account-tokens",
Environments: []string{"default"},
Token: "",
})

req, _ := http.NewRequest(http.MethodPut, accountController.routeAccountPath+"/123-controller-update-account-tokens", bytes.NewBuffer(payload))
req, _ := http.NewRequest(http.MethodPut, accountController.routeAccountPath+"/tokens/123-controller-update-account-tokens", bytes.NewBuffer(payload))
response := executeRequest(req, r, token)

// Assert
Expand All @@ -287,7 +285,7 @@ func TestUpdateAccountTokensHandler(t *testing.T) {
t.Run("Should not update account tokens - invalid request", func(t *testing.T) {
// Test
payload := []byte("")
req, _ := http.NewRequest(http.MethodPut, accountController.routeAccountPath+"/invalid", bytes.NewBuffer(payload))
req, _ := http.NewRequest(http.MethodPut, accountController.routeAccountPath+"/tokens/invalid", bytes.NewBuffer(payload))
response := executeRequest(req, r, token)

// Assert
Expand All @@ -298,12 +296,11 @@ func TestUpdateAccountTokensHandler(t *testing.T) {
t.Run("Should not update account tokens - not found", func(t *testing.T) {
// Test
payload, _ := json.Marshal(AccountTokensRequest{
DomainId: "not-found",
Environments: []string{"default"},
Token: "new-token",
})

req, _ := http.NewRequest(http.MethodPut, accountController.routeAccountPath+"/not-found", bytes.NewBuffer(payload))
req, _ := http.NewRequest(http.MethodPut, accountController.routeAccountPath+"/tokens/not-found", bytes.NewBuffer(payload))
response := executeRequest(req, r, token)

// Assert
Expand Down
Loading