Skip to content

Commit

Permalink
Add test for cli countries
Browse files Browse the repository at this point in the history
Signed-off-by: Marius Sincovici <[email protected]>
  • Loading branch information
mariusSincovici committed Oct 12, 2023
1 parent 1c3b180 commit d933864
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
5 changes: 4 additions & 1 deletion cli/cli_countries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
53 changes: 53 additions & 0 deletions cli/cli_countries_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}

0 comments on commit d933864

Please sign in to comment.