diff --git a/pkg/cmd/device_groups/list/.fixtures/resp.json b/pkg/cmd/device_groups/list/.fixtures/resp.json new file mode 100644 index 000000000..d7bb25742 --- /dev/null +++ b/pkg/cmd/device_groups/list/.fixtures/resp.json @@ -0,0 +1,16 @@ +{ + "count": 1, + "total_pages": 1, + "schema_version": 3, + "links": { + "previous": null, + "next": null + }, + "results": [ + { + "id": 2257, + "name": "Mob1le", + "user_agent": "Mobile|Android|iPhone" + } + ] +} \ No newline at end of file diff --git a/pkg/cmd/device_groups/list/.fixtures/resp_without_items.json b/pkg/cmd/device_groups/list/.fixtures/resp_without_items.json new file mode 100644 index 000000000..ef2d7e6d2 --- /dev/null +++ b/pkg/cmd/device_groups/list/.fixtures/resp_without_items.json @@ -0,0 +1,10 @@ +{ + "count": 0, + "total_pages": 1, + "schema_version": 3, + "links": { + "previous": null, + "next": null + }, + "results": [] +} \ No newline at end of file diff --git a/pkg/cmd/device_groups/list/list.go b/pkg/cmd/device_groups/list/list.go index 8dff20f77..8e2bbe73c 100644 --- a/pkg/cmd/device_groups/list/list.go +++ b/pkg/cmd/device_groups/list/list.go @@ -74,6 +74,7 @@ func PrintTable(cmd *cobra.Command, f *cmdutil.Factory, opts *contracts.ListOpti } tbl := table.New("ID", "NAME") + tbl.WithWriter(f.IOStreams.Out) table.DefaultWriter = f.IOStreams.Out if cmd.Flags().Changed("details") { tbl = table.New("ID", "NAME", "USER AGENT") @@ -85,9 +86,9 @@ func PrintTable(cmd *cobra.Command, f *cmdutil.Factory, opts *contracts.ListOpti for _, v := range applications.Results { if cmd.Flags().Changed("details") { - tbl.AddRow(v.Id, v.Name, v.UserAgent) + tbl.AddRow(*v.Id, v.Name, v.UserAgent) } else { - tbl.AddRow(v.Id, v.Name) + tbl.AddRow(*v.Id, v.Name) } } @@ -103,6 +104,5 @@ func PrintTable(cmd *cobra.Command, f *cmdutil.Factory, opts *contracts.ListOpti *numberPage += 1 opts.Page = *numberPage - f.IOStreams.Out = table.DefaultWriter return applications.TotalPages, nil } diff --git a/pkg/cmd/device_groups/list/list_test.go b/pkg/cmd/device_groups/list/list_test.go new file mode 100644 index 000000000..05f099c46 --- /dev/null +++ b/pkg/cmd/device_groups/list/list_test.go @@ -0,0 +1,47 @@ +package list + +import ( + "github.com/aziontech/azion-cli/pkg/httpmock" + "github.com/aziontech/azion-cli/pkg/testutils" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "testing" +) + +func TestList(t *testing.T) { + t.Run("command list with successes", func(t *testing.T) { + mock := &httpmock.Registry{} + + mock.Register( + httpmock.REST("GET", "edge_applications/1673635846/device_groups"), + httpmock.JSONFromFile(".fixtures/resp.json"), + ) + + f, stdout, _ := testutils.NewFactory(mock) + cmd := NewCmd(f) + + cmd.SetArgs([]string{"-a", "1673635846"}) + + _, err := cmd.ExecuteC() + require.NoError(t, err) + assert.Equal(t, "ID NAME \n2257 Mob1le \n", stdout.String()) + }) + + t.Run("command list response without items", func(t *testing.T) { + mock := &httpmock.Registry{} + + mock.Register( + httpmock.REST("GET", "edge_applications/1673635847/device_groups"), + httpmock.JSONFromFile(".fixtures/resp_without_items.json"), + ) + + f, stdout, _ := testutils.NewFactory(mock) + cmd := NewCmd(f) + + cmd.SetArgs([]string{"-a", "1673635847"}) + + _, err := cmd.ExecuteC() + require.NoError(t, err) + assert.Equal(t, "ID NAME \n", stdout.String()) + }) +}