Skip to content

Commit

Permalink
integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Shandling committed Jan 8, 2025
1 parent 205df7b commit a41be09
Showing 1 changed file with 59 additions and 2 deletions.
61 changes: 59 additions & 2 deletions server/service/integration_core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4655,6 +4655,65 @@ func (s *integrationTestSuite) TestUsers() {
assert.Len(t, getMeResp.User.Teams, 0)
assert.Len(t, getMeResp.AvailableTeams, 0)

// test user settings from 2 endpoints

// get session user with ui settings, which should be empty, two endpoints
var getResp getUserResponse
s.DoJSON("GET", fmt.Sprintf("/api/latest/fleet/users/%d", 1), nil, http.StatusOK, &getResp, "include_ui_settings", "true")
assert.Equal(t, uint(1), getResp.User.ID)
assert.Empty(t, getResp.User.Settings)

resp = s.DoRawWithHeaders("GET", "/api/latest/fleet/me", []byte(""), http.StatusOK, map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", ssn.Key),
}, "include_ui_settings", "true")
err = json.NewDecoder(resp.Body).Decode(&getMeResp)
require.NoError(t, err)
// session user id 1
assert.Equal(t, uint(1), getMeResp.User.ID)
assert.NotNil(t, getMeResp.User.GlobalRole)
assert.Empty(t, getMeResp.User.Settings)

// modify session user - add ui setting
var modResp modifyUserResponse
s.DoJSON("PATCH", fmt.Sprintf("/api/latest/fleet/users/%d", 1), json.RawMessage(`{
"settings": {
"hidden_host_columns": ["osquery_version"]}
}`), http.StatusOK, &modResp)

// get session user with ui settings, should now be present, two endpoints
s.DoJSON("GET", fmt.Sprintf("/api/latest/fleet/users/%d", 1), nil, http.StatusOK, &getResp, "include_ui_settings", "true")
assert.Equal(t, uint(1), getResp.User.ID)
assert.Equal(t, getResp.User.Settings, &fleet.UserSettings{HiddenHostColumns: []string{"osquery_version"}})

resp = s.DoRawWithHeaders("GET", "/api/latest/fleet/me", []byte(""), http.StatusOK, map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", ssn.Key),
}, "include_ui_settings", "true")
err = json.NewDecoder(resp.Body).Decode(&getMeResp)
require.NoError(t, err)
assert.Equal(t, uint(1), getMeResp.User.ID)
assert.NotNil(t, getMeResp.User.GlobalRole)
assert.Equal(t, getResp.User.Settings, &fleet.UserSettings{HiddenHostColumns: []string{"osquery_version"}})

// modify user ui settings, check they are returned modified
s.DoJSON("PATCH", fmt.Sprintf("/api/latest/fleet/users/%d", 1), json.RawMessage(`{
"settings": {
"hidden_host_columns": ["hostname", "osquery_version"]}
}`), http.StatusOK, &modResp)

// get session user with ui settings, should now be present, two endpoints
s.DoJSON("GET", fmt.Sprintf("/api/latest/fleet/users/%d", 1), nil, http.StatusOK, &getResp, "include_ui_settings", "true")
assert.Equal(t, uint(1), getResp.User.ID)
assert.Equal(t, getResp.User.Settings, &fleet.UserSettings{HiddenHostColumns: []string{"hostname", "osquery_version"}})

resp = s.DoRawWithHeaders("GET", "/api/latest/fleet/me", []byte(""), http.StatusOK, map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", ssn.Key),
}, "include_ui_settings", "true")
err = json.NewDecoder(resp.Body).Decode(&getMeResp)
require.NoError(t, err)
assert.Equal(t, uint(1), getMeResp.User.ID)
assert.NotNil(t, getMeResp.User.GlobalRole)
assert.Equal(t, getResp.User.Settings, &fleet.UserSettings{HiddenHostColumns: []string{"hostname", "osquery_version"}})

// create a new user
var createResp createUserResponse
userRawPwd := test.GoodPassword
Expand Down Expand Up @@ -4712,7 +4771,6 @@ func (s *integrationTestSuite) TestUsers() {
s.DoJSONWithoutAuth("POST", "/api/latest/fleet/sessions", sessionCreateRequest{Token: mfaToken}, http.StatusUnauthorized, &loginResp)

// turn off MFA
var modResp modifyUserResponse
s.DoJSON("PATCH", fmt.Sprintf("/api/latest/fleet/users/%d", u.ID), fleet.UserPayload{MFAEnabled: ptr.Bool(false)}, http.StatusOK, &modResp)
require.False(t, modResp.User.MFAEnabled)

Expand All @@ -4726,7 +4784,6 @@ func (s *integrationTestSuite) TestUsers() {
assert.Len(t, loginResp.AvailableTeams, 0)

// get that user from `/users` endpoint and check that teams info is empty
var getResp getUserResponse
s.DoJSON("GET", fmt.Sprintf("/api/latest/fleet/users/%d", u.ID), nil, http.StatusOK, &getResp)
assert.Equal(t, u.ID, getResp.User.ID)
assert.Len(t, getResp.User.Teams, 0)
Expand Down

0 comments on commit a41be09

Please sign in to comment.