chore(tests): modify JSON tests to not need all providers listed

This commit is contained in:
Quentin McGaw
2022-06-18 14:21:48 +00:00
parent 0c0dd10766
commit 0e9abc6e1d
3 changed files with 78 additions and 145 deletions

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"math"
"reflect"
"sort"
"github.com/qdm12/gluetun/internal/constants/providers"
)
@@ -35,12 +36,13 @@ func (a *AllServers) MarshalJSON() (data []byte, err error) {
return nil, fmt.Errorf("cannot write schema version string: %w", err)
}
for _, provider := range providers.All() {
servers, ok := a.ProviderToServers[provider]
if !ok {
panic(fmt.Sprintf("provider %s not found in all servers", provider))
}
sortedProviders := make(sort.StringSlice, 0, len(a.ProviderToServers))
for provider := range a.ProviderToServers {
sortedProviders = append(sortedProviders, provider)
}
sortedProviders.Sort()
for _, provider := range sortedProviders {
providerKey := fmt.Sprintf(`,"%s":`, provider)
_, err = buffer.WriteString(providerKey)
if err != nil {
@@ -48,6 +50,7 @@ func (a *AllServers) MarshalJSON() (data []byte, err error) {
providerKey, err)
}
servers := a.ProviderToServers[provider]
serversJSON, err := json.Marshal(servers)
if err != nil {
return nil, fmt.Errorf("failed encoding servers for provider %s: %w",

View File

@@ -19,33 +19,13 @@ func Test_AllServers_MarshalJSON(t *testing.T) {
errWrapped error
errMessage string
}{
"empty": {
"no provider": {
allServers: &AllServers{
ProviderToServers: map[string]Servers{},
},
dataString: `{"version":0,` +
`"cyberghost":{"version":0,"timestamp":0},` +
`"expressvpn":{"version":0,"timestamp":0},` +
`"fastestvpn":{"version":0,"timestamp":0},` +
`"hidemyass":{"version":0,"timestamp":0},` +
`"ipvanish":{"version":0,"timestamp":0},` +
`"ivpn":{"version":0,"timestamp":0},` +
`"mullvad":{"version":0,"timestamp":0},` +
`"nordvpn":{"version":0,"timestamp":0},` +
`"perfect privacy":{"version":0,"timestamp":0},` +
`"privado":{"version":0,"timestamp":0},` +
`"private internet access":{"version":0,"timestamp":0},` +
`"privatevpn":{"version":0,"timestamp":0},` +
`"protonvpn":{"version":0,"timestamp":0},` +
`"purevpn":{"version":0,"timestamp":0},` +
`"surfshark":{"version":0,"timestamp":0},` +
`"torguard":{"version":0,"timestamp":0},` +
`"vpn unlimited":{"version":0,"timestamp":0},` +
`"vyprvpn":{"version":0,"timestamp":0},` +
`"wevpn":{"version":0,"timestamp":0},` +
`"windscribe":{"version":0,"timestamp":0}}`,
dataString: `{"version":0}`,
},
"two known providers": {
"two providers": {
allServers: &AllServers{
Version: 1,
ProviderToServers: map[string]Servers{
@@ -69,25 +49,7 @@ func Test_AllServers_MarshalJSON(t *testing.T) {
},
dataString: `{"version":1,` +
`"cyberghost":{"version":1,"timestamp":1000,"servers":[{"country":"A"},{"country":"B"}]},` +
`"expressvpn":{"version":0,"timestamp":0},` +
`"fastestvpn":{"version":0,"timestamp":0},` +
`"hidemyass":{"version":0,"timestamp":0},` +
`"ipvanish":{"version":0,"timestamp":0},` +
`"ivpn":{"version":0,"timestamp":0},` +
`"mullvad":{"version":0,"timestamp":0},` +
`"nordvpn":{"version":0,"timestamp":0},` +
`"perfect privacy":{"version":0,"timestamp":0},` +
`"privado":{"version":2,"timestamp":2000,"servers":[{"city":"C"},{"city":"D"}]},` +
`"private internet access":{"version":0,"timestamp":0},` +
`"privatevpn":{"version":0,"timestamp":0},` +
`"protonvpn":{"version":0,"timestamp":0},` +
`"purevpn":{"version":0,"timestamp":0},` +
`"surfshark":{"version":0,"timestamp":0},` +
`"torguard":{"version":0,"timestamp":0},` +
`"vpn unlimited":{"version":0,"timestamp":0},` +
`"vyprvpn":{"version":0,"timestamp":0},` +
`"wevpn":{"version":0,"timestamp":0},` +
`"windscribe":{"version":0,"timestamp":0}}`,
`"privado":{"version":2,"timestamp":2000,"servers":[{"city":"C"},{"city":"D"}]}}`,
},
}
@@ -96,27 +58,19 @@ func Test_AllServers_MarshalJSON(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()
// Populate all providers in all servers
for _, provider := range providers.All() {
_, has := testCase.allServers.ProviderToServers[provider]
if !has {
testCase.allServers.ProviderToServers[provider] = Servers{}
}
}
data, err := testCase.allServers.MarshalJSON()
assert.ErrorIs(t, err, testCase.errWrapped)
if err != nil {
assert.EqualError(t, err, testCase.errMessage)
}
assert.Equal(t, testCase.dataString, string(data))
require.Equal(t, testCase.dataString, string(data))
data, err = json.Marshal(testCase.allServers)
assert.ErrorIs(t, err, testCase.errWrapped)
if err != nil {
assert.EqualError(t, err, testCase.errMessage)
}
assert.Equal(t, testCase.dataString, string(data))
require.Equal(t, testCase.dataString, string(data))
buffer := bytes.NewBuffer(nil)
encoder := json.NewEncoder(buffer)
@@ -187,3 +141,43 @@ func Test_AllServers_UnmarshalJSON(t *testing.T) {
})
}
}
func Test_AllServers_JSON_Marshal_Unmarshal(t *testing.T) {
t.Parallel()
allServers := &AllServers{
Version: 1,
ProviderToServers: map[string]Servers{
providers.Cyberghost: {
Version: 1,
Timestamp: 1000,
Servers: []Server{
{Country: "A"},
{Country: "B"},
},
},
providers.Privado: {
Version: 2,
Timestamp: 2000,
Servers: []Server{
{City: "C"},
{City: "D"},
},
},
},
}
buffer := bytes.NewBuffer(nil)
encoder := json.NewEncoder(buffer)
encoder.SetIndent("", " ")
err := encoder.Encode(allServers)
require.NoError(t, err)
decoder := json.NewDecoder(buffer)
var result AllServers
err = decoder.Decode(&result)
require.NoError(t, err)
assert.Equal(t, allServers, &result)
}

View File

@@ -11,8 +11,7 @@ import (
"github.com/stretchr/testify/require"
)
func populateProviderToVersion(allProviderVersion uint16,
providerToVersion map[string]uint16) map[string]uint16 {
func populateProviderToVersion(providerToVersion map[string]uint16) map[string]uint16 {
allProviders := providers.All()
for _, provider := range allProviders {
_, has := providerToVersion[provider]
@@ -20,29 +19,11 @@ func populateProviderToVersion(allProviderVersion uint16,
continue
}
providerToVersion[provider] = allProviderVersion
providerToVersion[provider] = 0
}
return providerToVersion
}
func populateAllServersVersion(allProviderVersion uint16,
servers models.AllServers) models.AllServers {
allProviders := providers.All()
if servers.ProviderToServers == nil {
servers.ProviderToServers = make(map[string]models.Servers, len(allProviders)-1)
}
for _, provider := range allProviders {
_, has := servers.ProviderToServers[provider]
if has {
continue
}
servers.ProviderToServers[provider] = models.Servers{
Version: allProviderVersion,
}
}
return servers
}
func Test_extractServersFromBytes(t *testing.T) {
t.Parallel()
@@ -59,94 +40,49 @@ func Test_extractServersFromBytes(t *testing.T) {
},
"bad provider JSON": {
b: []byte(`{"cyberghost": "garbage"}`),
hardcodedVersions: populateProviderToVersion(1, map[string]uint16{}),
hardcodedVersions: populateProviderToVersion(map[string]uint16{}),
errMessage: "cannot decode servers version for provider Cyberghost: " +
"json: cannot unmarshal string into Go value of type struct { Version uint16 \"json:\\\"version\\\"\" }",
},
"bad servers array JSON": {
b: []byte(`{"cyberghost": {"version": 1, "servers": "garbage"}}`),
hardcodedVersions: populateProviderToVersion(1, map[string]uint16{}),
b: []byte(`{"cyberghost": {"version": 1, "servers": "garbage"}}`),
hardcodedVersions: populateProviderToVersion(map[string]uint16{
providers.Cyberghost: 1,
}),
errMessage: "cannot decode servers for provider Cyberghost: " +
"json: cannot unmarshal string into Go struct field Servers.servers of type []models.Server",
},
"absent provider keys": {
b: []byte(`{}`),
hardcodedVersions: populateProviderToVersion(1, map[string]uint16{}),
b: []byte(`{}`),
hardcodedVersions: populateProviderToVersion(map[string]uint16{
providers.Cyberghost: 1,
}),
persisted: models.AllServers{
ProviderToServers: map[string]models.Servers{},
},
},
"same versions": {
b: []byte(`{
"cyberghost": {"version": 1, "timestamp": 0},
"expressvpn": {"version": 1, "timestamp": 0},
"fastestvpn": {"version": 1, "timestamp": 0},
"hidemyass": {"version": 1, "timestamp": 0},
"ipvanish": {"version": 1, "timestamp": 0},
"ivpn": {"version": 1, "timestamp": 0},
"mullvad": {"version": 1, "timestamp": 0},
"nordvpn": {"version": 1, "timestamp": 0},
"perfect privacy": {"version": 1, "timestamp": 0},
"privado": {"version": 1, "timestamp": 0},
"private internet access": {"version": 1, "timestamp": 0},
"privatevpn": {"version": 1, "timestamp": 0},
"protonvpn": {"version": 1, "timestamp": 0},
"purevpn": {"version": 1, "timestamp": 0},
"surfshark": {"version": 1, "timestamp": 0},
"torguard": {"version": 1, "timestamp": 0},
"vpn unlimited": {"version": 1, "timestamp": 0},
"vyprvpn": {"version": 1, "timestamp": 0},
"wevpn": {"version": 1, "timestamp": 0},
"windscribe": {"version": 1, "timestamp": 0}
"cyberghost": {"version": 1, "timestamp": 0}
}`),
hardcodedVersions: populateProviderToVersion(1, map[string]uint16{}),
persisted: populateAllServersVersion(1, models.AllServers{}),
hardcodedVersions: populateProviderToVersion(map[string]uint16{
providers.Cyberghost: 1,
}),
persisted: models.AllServers{
ProviderToServers: map[string]models.Servers{
providers.Cyberghost: {Version: 1},
},
},
},
"different versions": {
b: []byte(`{
"cyberghost": {"version": 1, "timestamp": 1},
"expressvpn": {"version": 1, "timestamp": 1},
"fastestvpn": {"version": 1, "timestamp": 1},
"hidemyass": {"version": 1, "timestamp": 1},
"ipvanish": {"version": 1, "timestamp": 1},
"ivpn": {"version": 1, "timestamp": 1},
"mullvad": {"version": 1, "timestamp": 1},
"nordvpn": {"version": 1, "timestamp": 1},
"perfect privacy": {"version": 1, "timestamp": 1},
"privado": {"version": 1, "timestamp": 1},
"private internet access": {"version": 1, "timestamp": 1},
"privatevpn": {"version": 1, "timestamp": 1},
"protonvpn": {"version": 1, "timestamp": 1},
"purevpn": {"version": 1, "timestamp": 1},
"surfshark": {"version": 1, "timestamp": 1},
"torguard": {"version": 1, "timestamp": 1},
"vpn unlimited": {"version": 1, "timestamp": 1},
"vyprvpn": {"version": 1, "timestamp": 1},
"wevpn": {"version": 1, "timestamp": 1},
"windscribe": {"version": 1, "timestamp": 1}
"cyberghost": {"version": 1, "timestamp": 1}
}`),
hardcodedVersions: populateProviderToVersion(2, map[string]uint16{}),
hardcodedVersions: populateProviderToVersion(map[string]uint16{
providers.Cyberghost: 2,
}),
logged: []string{
"Cyberghost servers from file discarded because they have version 1 and hardcoded servers have version 2",
"Expressvpn servers from file discarded because they have version 1 and hardcoded servers have version 2",
"Fastestvpn servers from file discarded because they have version 1 and hardcoded servers have version 2",
"Hidemyass servers from file discarded because they have version 1 and hardcoded servers have version 2",
"Ipvanish servers from file discarded because they have version 1 and hardcoded servers have version 2",
"Ivpn servers from file discarded because they have version 1 and hardcoded servers have version 2",
"Mullvad servers from file discarded because they have version 1 and hardcoded servers have version 2",
"Nordvpn servers from file discarded because they have version 1 and hardcoded servers have version 2",
"Perfect Privacy servers from file discarded because they have version 1 and hardcoded servers have version 2",
"Privado servers from file discarded because they have version 1 and hardcoded servers have version 2",
"Private Internet Access servers from file discarded because they have version 1 and hardcoded servers have version 2", //nolint:lll
"Privatevpn servers from file discarded because they have version 1 and hardcoded servers have version 2",
"Protonvpn servers from file discarded because they have version 1 and hardcoded servers have version 2",
"Purevpn servers from file discarded because they have version 1 and hardcoded servers have version 2",
"Surfshark servers from file discarded because they have version 1 and hardcoded servers have version 2",
"Torguard servers from file discarded because they have version 1 and hardcoded servers have version 2",
"Vpn Unlimited servers from file discarded because they have version 1 and hardcoded servers have version 2",
"Vyprvpn servers from file discarded because they have version 1 and hardcoded servers have version 2",
"Wevpn servers from file discarded because they have version 1 and hardcoded servers have version 2",
"Windscribe servers from file discarded because they have version 1 and hardcoded servers have version 2",
},
persisted: models.AllServers{
ProviderToServers: map[string]models.Servers{},