diff --git a/.env.test b/.env.test index b17dc9f..a1f3186 100644 --- a/.env.test +++ b/.env.test @@ -7,6 +7,8 @@ HANDLER_WAITING_TIME=1m SWITCHER_API_URL=https://switcherapi.com/api SWITCHER_API_JWT_SECRET=SecretSecretSecretSecretSecretSe +SWITCHER_PATH_GRAPHQL=/gitops-graphql +SWITCHER_PATH_PUSH=/gitops/v1/push # Only for testing purposes. Values are loaded from accounts GIT_USER= diff --git a/resources/fixtures/comparator/new_config_strategy.json b/resources/fixtures/comparator/new_config_strategy.json new file mode 100644 index 0000000..766ecb3 --- /dev/null +++ b/resources/fixtures/comparator/new_config_strategy.json @@ -0,0 +1,67 @@ +{ + "domain": { + "group": [ + { + "name": "Release 1", + "description": "Showcase configuration", + "activated": true, + "config": [ + { + "key": "MY_SWITCHER_1", + "description": "My first switcher", + "activated": true, + "strategies": [ + { + "strategy": "VALUE_VALIDATION", + "activated": false, + "operation": "EXIST", + "values": [ + "user_1" + ] + } + ], + "components": [ + "switcher-playground" + ] + }, + { + "key": "MY_SWITCHER_2", + "description": "", + "activated": false, + "strategies": [], + "components": [ + "switcher-playground" + ] + }, + { + "key": "MY_SWITCHER_3", + "description": "", + "activated": true, + "strategies": [], + "components": [ + "benchmark" + ] + }, + { + "key": "MY_SWITCHER_4", + "description": "", + "activated": true, + "strategies": [ + { + "strategy": "VALUE_VALIDATION", + "activated": false, + "operation": "EXIST", + "values": [ + "user_1" + ] + } + ], + "components": [ + "benchmark" + ] + } + ] + } + ] + } +} \ No newline at end of file diff --git a/src/core/api.go b/src/core/api.go index 1c0e289..c7c11f9 100644 --- a/src/core/api.go +++ b/src/core/api.go @@ -9,6 +9,7 @@ import ( "time" "github.com/golang-jwt/jwt" + "github.com/switcherapi/switcher-gitops/src/config" "github.com/switcherapi/switcher-gitops/src/model" ) @@ -24,7 +25,7 @@ type PushChangeResponse struct { type IAPIService interface { FetchSnapshotVersion(domainId string, environment string) (string, error) FetchSnapshot(domainId string, environment string) (string, error) - PushChanges(domainId string, environment string, diff model.DiffResult) (PushChangeResponse, error) + PushChanges(domainId string, diff model.DiffResult) (PushChangeResponse, error) NewDataFromJson(jsonData []byte) model.Data } @@ -68,9 +69,9 @@ func (a *ApiService) FetchSnapshot(domainId string, environment string) (string, return responseBody, nil } -func (a *ApiService) PushChanges(domainId string, environment string, diff model.DiffResult) (PushChangeResponse, error) { +func (a *ApiService) PushChanges(domainId string, diff model.DiffResult) (PushChangeResponse, error) { reqBody, _ := json.Marshal(diff) - responseBody, err := a.doPostRequest(a.apiUrl+"/gitops/push", domainId, reqBody) + responseBody, err := a.doPostRequest(a.apiUrl+config.GetEnv("SWITCHER_PATH_PUSH"), domainId, reqBody) if err != nil { return PushChangeResponse{}, err @@ -87,7 +88,7 @@ func (a *ApiService) doGraphQLRequest(domainId string, query string) (string, er // Create a new request reqBody, _ := json.Marshal(GraphQLRequest{Query: query}) - req, _ := http.NewRequest("POST", a.apiUrl+"/gitops-graphql", bytes.NewBuffer(reqBody)) + req, _ := http.NewRequest("POST", a.apiUrl+config.GetEnv("SWITCHER_PATH_GRAPHQL"), bytes.NewBuffer(reqBody)) // Set the request headers setHeaders(req, token) diff --git a/src/core/api_test.go b/src/core/api_test.go index 2879dc5..9d59fdc 100644 --- a/src/core/api_test.go +++ b/src/core/api_test.go @@ -116,7 +116,7 @@ func TestFetchSnapshot(t *testing.T) { func TestPushChangesToAPI(t *testing.T) { t.Run("Should push changes to API", func(t *testing.T) { // Given - diff := givenDiffResult() + diff := givenDiffResult("default") fakeApiServer := givenApiResponse(http.StatusOK, `{ "version": 2, "message": "Changes applied successfully" @@ -126,7 +126,7 @@ func TestPushChangesToAPI(t *testing.T) { apiService := NewApiService(SWITCHER_API_JWT_SECRET, fakeApiServer.URL) // Test - response, _ := apiService.PushChanges("domainId", "default", diff) + response, _ := apiService.PushChanges("domainId", diff) // Assert assert.NotNil(t, response) @@ -136,14 +136,14 @@ func TestPushChangesToAPI(t *testing.T) { t.Run("Should return error - invalid API key", func(t *testing.T) { // Given - diff := givenDiffResult() + diff := givenDiffResult("default") fakeApiServer := givenApiResponse(http.StatusUnauthorized, `{ "message": "Invalid API token" }`) defer fakeApiServer.Close() apiService := NewApiService("[INVALID_KEY]", fakeApiServer.URL) // Test - response, _ := apiService.PushChanges("domainId", "default", diff) + response, _ := apiService.PushChanges("domainId", diff) // Assert assert.NotNil(t, response) @@ -152,11 +152,11 @@ func TestPushChangesToAPI(t *testing.T) { t.Run("Should return error - API not accessible", func(t *testing.T) { // Given - diff := givenDiffResult() + diff := givenDiffResult("default") apiService := NewApiService("[SWITCHER_API_JWT_SECRET]", "http://localhost:8080") // Test - _, err := apiService.PushChanges("domainId", "default", diff) + _, err := apiService.PushChanges("domainId", diff) // Assert assert.NotNil(t, err) @@ -165,8 +165,9 @@ func TestPushChangesToAPI(t *testing.T) { // Helpers -func givenDiffResult() model.DiffResult { +func givenDiffResult(environment string) model.DiffResult { diffResult := model.DiffResult{} + diffResult.Environment = environment diffResult.Changes = append(diffResult.Changes, model.DiffDetails{ Action: string(NEW), Diff: string(CONFIG), diff --git a/src/core/comparator_test.go b/src/core/comparator_test.go index 905b4df..c181649 100644 --- a/src/core/comparator_test.go +++ b/src/core/comparator_test.go @@ -263,6 +263,47 @@ func TestCheckConfigSnapshot(t *testing.T) { ]}`, utils.ToJsonFromObject(actual)) }) + t.Run("Should return new config with strategy", func(t *testing.T) { + // Given + jsonApi := utils.ReadJsonFromFile(DEFAULT_JSON) + jsonRepo := utils.ReadJsonFromFile("../../resources/fixtures/comparator/new_config_strategy.json") + fromApi := c.NewSnapshotFromJson([]byte(jsonApi)) + fromRepo := c.NewSnapshotFromJson([]byte(jsonRepo)) + + // Test Check/Merge changes + diffNew := c.CheckSnapshotDiff(fromRepo, fromApi, NEW) + actual := c.MergeResults([]model.DiffResult{diffNew}) + + assert.NotNil(t, actual) + assert.JSONEq(t, `{ + "changes": [ + { + "action": "NEW", + "diff": "CONFIG", + "path": [ + "Release 1" + ], + "content": { + "key": "MY_SWITCHER_4", + "activated": true, + "strategies": [ + { + "strategy": "VALUE_VALIDATION", + "activated": false, + "operation": "EXIST", + "values": [ + "user_1" + ] + } + ], + "components": [ + "benchmark" + ] + } + } + ]}`, utils.ToJsonFromObject(actual)) + }) + t.Run("Should return deleted config", func(t *testing.T) { // Given jsonApi := utils.ReadJsonFromFile(DEFAULT_JSON) diff --git a/src/core/handler.go b/src/core/handler.go index 59ef804..2e80eaa 100644 --- a/src/core/handler.go +++ b/src/core/handler.go @@ -204,7 +204,8 @@ func (c *CoreHandler) pushChangesToAPI(account model.Account, } // Push changes to API - apiResponse, err := c.apiService.PushChanges(account.Domain.ID, account.Environment, diff) + diff.Environment = account.Environment + apiResponse, err := c.apiService.PushChanges(account.Domain.ID, diff) if err != nil { return account, err diff --git a/src/core/handler_test.go b/src/core/handler_test.go index 8f31c63..30b243f 100644 --- a/src/core/handler_test.go +++ b/src/core/handler_test.go @@ -581,7 +581,7 @@ func (f *FakeApiService) FetchSnapshot(domainId string, environment string) (str return f.responseSnapshot, nil } -func (f *FakeApiService) PushChanges(domainId string, environment string, diff model.DiffResult) (PushChangeResponse, error) { +func (f *FakeApiService) PushChanges(domainId string, diff model.DiffResult) (PushChangeResponse, error) { if f.throwErrorPush { return PushChangeResponse{}, errors.New("something went wrong") } diff --git a/src/model/diff.go b/src/model/diff.go index 2a7d79d..ab65dfb 100644 --- a/src/model/diff.go +++ b/src/model/diff.go @@ -1,7 +1,8 @@ package model type DiffResult struct { - Changes []DiffDetails `json:"changes"` + Environment string `json:"environment,omitempty"` + Changes []DiffDetails `json:"changes"` } type DiffDetails struct {