Skip to content
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

fix(daemon): handle invalid notice types in filter #329

Merged
merged 3 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions internals/daemon/api_notices.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ func v1GetNotices(c *Command, r *http.Request, _ *UserState) Response {
}
types = append(types, noticeType)
}
if len(types) == 0 && len(typeStrs) > 0 {
// Only requested invalid notice types. Return no notices, rather than
// all, the latter of which would occur if the types filter was empty.
olivercalder marked this conversation as resolved.
Show resolved Hide resolved
return SyncResponse([]*state.Notice{})
}

keys := strutil.MultiCommaSeparatedList(query["keys"])

Expand Down
41 changes: 41 additions & 0 deletions internals/daemon/api_notices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,47 @@ func (s *apiSuite) TestNoticesFilterMultipleKeys(c *C) {
c.Assert(n["key"], Equals, "danger")
}

func (s *apiSuite) TestNoticesFilterInvalidTypes(c *C) {
s.daemon(c)

st := s.d.Overlord().State()
st.Lock()
addNotice(c, st, state.ChangeUpdateNotice, "123", nil)
time.Sleep(time.Microsecond)
olivercalder marked this conversation as resolved.
Show resolved Hide resolved
addNotice(c, st, state.WarningNotice, "danger", nil)
st.Unlock()

// Check that invalid types are discarded, and notices with remaining
// types are requested as expected, without error.
req, err := http.NewRequest("GET", "/v1/notices?types=foo&types=warning&types=bar,baz", nil)
c.Assert(err, IsNil)
noticesCmd := apiCmd("/v1/notices")
rsp, ok := noticesCmd.GET(noticesCmd, req, nil).(*resp)
c.Assert(ok, Equals, true)

c.Check(rsp.Type, Equals, ResponseTypeSync)
c.Check(rsp.Status, Equals, http.StatusOK)
notices, ok := rsp.Result.([]*state.Notice)
c.Assert(ok, Equals, true)
c.Assert(notices, HasLen, 1)
n := noticeToMap(c, notices[0])
c.Assert(n["type"], Equals, "warning")

// Check that if all types are invalid, no notices are returned, and there
// is no error.
req, err = http.NewRequest("GET", "/v1/notices?types=foo&types=bar,baz", nil)
c.Assert(err, IsNil)
noticesCmd = apiCmd("/v1/notices")
rsp, ok = noticesCmd.GET(noticesCmd, req, nil).(*resp)
c.Assert(ok, Equals, true)

c.Check(rsp.Type, Equals, ResponseTypeSync)
c.Check(rsp.Status, Equals, http.StatusOK)
notices, ok = rsp.Result.([]*state.Notice)
c.Assert(ok, Equals, true)
c.Assert(notices, HasLen, 0)
}

func (s *apiSuite) TestNoticesWait(c *C) {
s.daemon(c)

Expand Down
Loading