forked from NordSecurity/nordvpn-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix crash for countries command (NordSecurity#108)
Signed-off-by: Marius Sincovici <[email protected]>
- Loading branch information
Showing
9 changed files
with
411 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: "countries 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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 TestGroupsList(t *testing.T) { | ||
category.Set(t, category.Unit) | ||
mockClient := mockDaemonClient{} | ||
c := cmd{&mockClient, nil, nil, "", nil, config.Config{}, nil} | ||
|
||
tests := []struct { | ||
name string | ||
groups []string | ||
expected string | ||
input string | ||
expectedError error | ||
}{ | ||
{ | ||
name: "error response", | ||
expectedError: formatError(fmt.Errorf(MsgListIsEmpty, "server groups")), | ||
}, | ||
{ | ||
name: "groups list", | ||
expected: "group1, group2", | ||
groups: []string{"group1", "group2"}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
app := cli.NewApp() | ||
set := flag.NewFlagSet("test", 0) | ||
mockClient.groups = test.groups | ||
ctx := cli.NewContext(app, set, &cli.Context{Context: context.Background()}) | ||
|
||
result, err := captureOutput(func() { | ||
err := c.Groups(ctx) | ||
assert.Equal(t, test.expectedError, err) | ||
}) | ||
assert.Nil(t, err) | ||
assert.Equal(t, test.expected, result) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package daemon | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/NordSecurity/nordvpn-linux/config" | ||
"github.com/NordSecurity/nordvpn-linux/daemon/pb" | ||
"github.com/NordSecurity/nordvpn-linux/events/subs" | ||
"github.com/NordSecurity/nordvpn-linux/fileshare/service" | ||
"github.com/NordSecurity/nordvpn-linux/internal" | ||
"github.com/NordSecurity/nordvpn-linux/test/category" | ||
"github.com/NordSecurity/nordvpn-linux/test/mock/networker" | ||
mapset "github.com/deckarep/golang-set" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRPCCities(t *testing.T) { | ||
category.Set(t, category.Unit) | ||
defer testsCleanup() | ||
|
||
tests := []struct { | ||
name string | ||
dm *DataManager | ||
cm config.Manager | ||
statusCode int64 | ||
}{ | ||
{ | ||
name: "missing configuration file", | ||
dm: testNewDataManager(), | ||
cm: failingConfigManager{}, | ||
statusCode: internal.CodeConfigError, | ||
}, | ||
{ | ||
name: "app data is empty", | ||
dm: testNewDataManager(), | ||
cm: newMockConfigManager(), | ||
statusCode: internal.CodeEmptyPayloadError, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
rpc := RPC{ | ||
ac: &workingLoginChecker{}, | ||
cm: test.cm, | ||
dm: test.dm, | ||
fileshare: service.NoopFileshare{}, | ||
netw: &networker.Mock{}, | ||
ncClient: mockNC{}, | ||
publisher: &subs.Subject[string]{}, | ||
api: mockApi{}, | ||
} | ||
payload, _ := rpc.Cities(context.Background(), &pb.CitiesRequest{}) | ||
|
||
assert.Equal(t, test.statusCode, payload.Type) | ||
}) | ||
} | ||
} | ||
|
||
func TestRPCCities_Successful(t *testing.T) { | ||
category.Set(t, category.Unit) | ||
defer testsCleanup() | ||
|
||
dm := testNewDataManager() | ||
|
||
cityNames := map[bool]map[config.Protocol]map[string]mapset.Set{ | ||
false: { | ||
config.Protocol_UDP: { | ||
"lt": mapset.NewSet("Vilnius"), | ||
}, | ||
config.Protocol_TCP: { | ||
"de": mapset.NewSet("Berlin"), | ||
}, | ||
}, | ||
} | ||
dm.SetAppData(nil, cityNames, nil) | ||
|
||
cm := newMockConfigManager() | ||
cm.c.AutoConnectData.Protocol = config.Protocol_UDP | ||
|
||
rpc := RPC{ | ||
ac: &workingLoginChecker{}, | ||
cm: cm, | ||
dm: dm, | ||
fileshare: service.NoopFileshare{}, | ||
netw: &networker.Mock{}, | ||
ncClient: mockNC{}, | ||
publisher: &subs.Subject[string]{}, | ||
api: mockApi{}, | ||
} | ||
|
||
request := &pb.CitiesRequest{} | ||
request.Obfuscate = false | ||
request.Country = "LT" | ||
|
||
payload, _ := rpc.Cities(context.Background(), request) | ||
assert.Equal(t, internal.CodeSuccess, payload.GetType()) | ||
assert.Equal(t, []string{"Vilnius"}, payload.GetData()) | ||
} |
Oops, something went wrong.