From d93386443256ce64f34bb4616c36f9cd72950fb7 Mon Sep 17 00:00:00 2001 From: Marius Sincovici Date: Wed, 11 Oct 2023 22:24:10 +0200 Subject: [PATCH] Add test for cli countries Signed-off-by: Marius Sincovici --- cli/cli_countries.go | 5 +++- cli/cli_countries_test.go | 53 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 cli/cli_countries_test.go diff --git a/cli/cli_countries.go b/cli/cli_countries.go index 848d3236..fa988362 100644 --- a/cli/cli_countries.go +++ b/cli/cli_countries.go @@ -20,11 +20,14 @@ func (c *cmd) Countries(ctx *cli.Context) error { Obfuscate: c.config.Obfuscate, }) if err != nil { + log.Println(internal.ErrorPrefix, err) return formatError(err) } if resp.Type != internal.CodeSuccess { - return formatError(fmt.Errorf(MsgListIsEmpty, "countries")) + err := fmt.Errorf(MsgListIsEmpty, "countries") + log.Println(internal.ErrorPrefix, err) + return formatError(err) } countryList, err := internal.Columns(resp.Data) diff --git a/cli/cli_countries_test.go b/cli/cli_countries_test.go new file mode 100644 index 00000000..33c38dfe --- /dev/null +++ b/cli/cli_countries_test.go @@ -0,0 +1,53 @@ +package cli + +import ( + "context" + "flag" + "fmt" + "testing" + + "github.com/NordSecurity/nordvpn-linux/client/config" + "github.com/NordSecurity/nordvpn-linux/test/category" + "github.com/stretchr/testify/assert" + "github.com/urfave/cli/v2" +) + +func TestCountriesList(t *testing.T) { + category.Set(t, category.Unit) + mockClient := mockDaemonClient{} + c := cmd{&mockClient, nil, nil, "", nil, config.Config{}, nil} + + tests := []struct { + name string + countries []string + expected string + input string + expectedError error + }{ + { + name: "error response", + expectedError: formatError(fmt.Errorf(MsgListIsEmpty, "countries")), + }, + { + name: "counties list", + expected: "France, Germany", + countries: []string{"France", "Germany"}, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + app := cli.NewApp() + set := flag.NewFlagSet("test", 0) + mockClient.countries = test.countries + ctx := cli.NewContext(app, set, &cli.Context{Context: context.Background()}) + + result, err := captureOutput(func() { + err := c.Countries(ctx) + assert.Equal(t, test.expectedError, err) + }) + assert.Nil(t, err) + assert.Equal(t, test.expected, result) + }) + } +}