Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

Fix selected checks in settings endpoint being deserialized to null #437

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: 7 additions & 1 deletion web/checks_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,13 @@ func ApiCheckGetSettingsByIdHandler(consul consul.Client, s services.ChecksServi
var jsonCheckSetting JSONChecksSettings
jsonCheckSetting.ConnectionSettings = make(map[string]string)

jsonCheckSetting.SelectedChecks = selectedChecks.SelectedChecks
if len(selectedChecks.SelectedChecks) == 0 {
jsonCheckSetting.SelectedChecks = make([]string, 0)

} else {
jsonCheckSetting.SelectedChecks = selectedChecks.SelectedChecks
}

jsonCheckSetting.Hosts = nodes
for node, settings := range connSettings {
jsonCheckSetting.ConnectionSettings[node] = settings.User
Expand Down
29 changes: 27 additions & 2 deletions web/checks_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package web
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -256,6 +257,11 @@ func TestApiCheckGetSettingsByIdHandler(t *testing.T) {
mockChecksService.On(
"GetConnectionSettingsById", "47d1190ffb4f781974c8356d7f863b03").Return(expectedConnSettings, nil)

mockChecksService.On(
"GetSelectedChecksById", "a615a35f65627be5a757319a0741127f").Return(models.SelectedChecks{}, errors.New("error"))
mockChecksService.On(
"GetConnectionSettingsById", "a615a35f65627be5a757319a0741127f").Return(expectedConnSettings, nil)

deps := setupTestDependencies()
deps.checksService = mockChecksService
deps.consul = consulInst
Expand Down Expand Up @@ -290,12 +296,31 @@ func TestApiCheckGetSettingsByIdHandler(t *testing.T) {
assert.Equal(t, expectedSelectedChecks, settings.SelectedChecks)
assert.Equal(t, expectedConnectionSettings, settings.ConnectionSettings)

// not found scenario, still 200 but returns an empty set
// 200 OK but the selected checks call gone bad scenario
resp = httptest.NewRecorder()

req, err = http.NewRequest("GET", "/api/checks/a615a35f65627be5a757319a0741127f/settings", nil)
if err != nil {
t.Fatal(err)
}

emptySelectedChecks := []string{}

app.webEngine.ServeHTTP(resp, req)

var emptySettingsResponse *JSONChecksSettings
json.Unmarshal(resp.Body.Bytes(), &emptySettingsResponse)

assert.Equal(t, 200, resp.Code)
assert.Equal(t, emptySelectedChecks, emptySettingsResponse.SelectedChecks)
assert.Equal(t, expectedConnectionSettings, emptySettingsResponse.ConnectionSettings)

// 404 not found scenario
resp = httptest.NewRecorder()

req = httptest.NewRequest("GET", "/api/checks/otherId/settings", nil)

emptySelectedChecks := []string(nil)
emptySelectedChecks = []string(nil)
emptyConnectionSettings := map[string]string(nil)

app.webEngine.ServeHTTP(resp, req)
Expand Down