Skip to content
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
7 changes: 6 additions & 1 deletion pkg/cmd/notification/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type ListOptions struct {
HTTPClient *http.Client
Host string
Token string
All bool
JSON cmdutil.JSONFlags
}

Expand Down Expand Up @@ -57,12 +58,16 @@ func NewCmdList(f *cmdutil.Factory) *cobra.Command {
},
}

cmd.Flags().BoolVar(&opts.All, "all", false, "Show read and unread notifications")
cmdutil.AddJSONFlags(cmd, &opts.JSON, []string{"id", "subject", "repository", "unread"})
return cmd
}

func ListRun(opts *ListOptions) error {
url := fmt.Sprintf("https://%s/api/v1/notifications", opts.Host)
url := fmt.Sprintf("https://%s/api/v1/notifications?page=1", opts.Host)
if opts.All {
url += "&all=true"
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
Expand Down
49 changes: 49 additions & 0 deletions pkg/cmd/notification/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,52 @@ func TestListRun_Success(t *testing.T) {
assert.Contains(t, stdout.String(), "Fix PLC timeout")
assert.Contains(t, stdout.String(), "Add sensor")
}

func TestListRun_Empty(t *testing.T) {
reg := &httpmock.Registry{}
defer reg.Verify(t)

reg.Register(
httpmock.REST("GET", "/api/v1/notifications"),
httpmock.StringResponse(http.StatusOK, `[]`),
)

ios, _, stdout, _ := iostreams.Test()

opts := &ListOptions{
IO: ios,
HTTPClient: &http.Client{Transport: reg},
Host: "app.copia.io",
Token: "test-token",
}

err := ListRun(opts)
require.NoError(t, err)
assert.Contains(t, stdout.String(), "No unread notifications")
}

func TestListRun_AllFlag(t *testing.T) {
reg := &httpmock.Registry{}
defer reg.Verify(t)

reg.Register(
httpmock.REST("GET", "/api/v1/notifications"),
httpmock.StringResponse(http.StatusOK, `[
{"id":1,"subject":{"title":"Old PR","type":"Pull"},"repository":{"full_name":"my-org/plc"},"unread":false}
]`),
)

ios, _, stdout, _ := iostreams.Test()

opts := &ListOptions{
IO: ios,
HTTPClient: &http.Client{Transport: reg},
Host: "app.copia.io",
Token: "test-token",
All: true,
}

err := ListRun(opts)
require.NoError(t, err)
assert.Contains(t, stdout.String(), "Old PR")
}
Loading