Skip to content

Commit

Permalink
add tests for inputloader
Browse files Browse the repository at this point in the history
  • Loading branch information
ainghazal committed May 2, 2024
1 parent c961327 commit f6ec850
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 17 deletions.
61 changes: 60 additions & 1 deletion internal/engine/inputloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"syscall"
"testing"
"time"

"github.com/apex/log"
"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -556,6 +557,64 @@ func TestInputLoaderCheckInSuccessWithSomeURLs(t *testing.T) {
}
}

func TestInputLoaderOpenVPNSuccessWithNoInput(t *testing.T) {
il := &InputLoader{
ExperimentName: "openvpn",
InputPolicy: model.InputOrQueryBackend,
Session: &InputLoaderMockableSession{
Error: nil,
},
}
_, err := il.loadRemote(context.Background())
if err != nil {
t.Fatal("we did not expect an error")
}
}

func TestInputLoaderOpenVPNSuccessWithNoInputAndAPICall(t *testing.T) {
il := &InputLoader{
ExperimentName: "openvpn",
InputPolicy: model.InputOrQueryBackend,
Session: &InputLoaderMockableSession{
Error: nil,
FetchOpenVPNConfigOutput: &model.OOAPIVPNProviderConfig{
Provider: "riseup",
Inputs: []string{
"openvpn://foo.corp/?address=1.1.1.1:1194&transport=tcp",
},
DateUpdated: time.Now(),
},
},
}
out, err := il.loadRemote(context.Background())
if err != nil {
t.Fatal("we did not expect an error")
}
if len(out) != 1 {
t.Fatal("we expected output of len=1")
}
}

func TestInputLoaderOpenVPNWithAPIFailureAndFallback(t *testing.T) {
expected := errors.New("mocked API error")
il := &InputLoader{
ExperimentName: "openvpn",
InputPolicy: model.InputOrQueryBackend,
Session: &InputLoaderMockableSession{
Error: expected,
},
}
out, err := il.loadRemote(context.Background())
if err != nil {
t.Fatal(err)
}
if len(out) != 2 {
t.Fatal("we expected 2 fallback URLs")
}
}

// TODO: WIP marker -------------------------------

func TestPreventMistakesWithCategories(t *testing.T) {
input := []model.OOAPIURLInfo{{
CategoryCode: "NEWS",
Expand Down Expand Up @@ -692,7 +751,7 @@ func TestStringListToModelURLInfoWithError(t *testing.T) {
expected := errors.New("mocked error")
output, err := stringListToModelURLInfo(input, expected)
if !errors.Is(err, expected) {
t.Fatal("no the error we expected", err)
t.Fatal("not the error we expected", err)
}
if output != nil {
t.Fatal("unexpected nil output")
Expand Down
34 changes: 18 additions & 16 deletions internal/model/ooapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,29 +99,31 @@ type OOAPICheckReportIDResponse struct {
V int64 `json:"v"`
}

type OOAPIVPNConfig struct {
// CA is the Certificate Authority for the endpoints by this provider.
CA string `json:"ca"`

// Cert is a valid certificate, for providers that use x509 certificate authentication.
Cert string `json:"cert,omitempty"`

// Key is a valid key, for providers that use x509 certificate authentication.
Key string `json:"key,omitempty"`

// Username is a valid username, for providers that use password authentication.
Username string `json:"username,omitempty"`

// Password is a valid password, for providers that use password authentication.
Password string `json:"password,omitempty"`
}

// OOAPIVPNProviderConfig is a minimal valid configuration subset for the openvpn experiment; at the moment it provides
// credentials valid for endpoints in a provider, and a list of inputs to be tested on this provider.
type OOAPIVPNProviderConfig struct {
// Provider is the label for this provider.
Provider string `json:"provider,omitempty"`

// Config is the provider-specific VPN Config.
Config *struct {
// CA is the Certificate Authority for the endpoints by this provider.
CA string `json:"ca"`

// Cert is a valid certificate, for providers that use x509 certificate authentication.
Cert string `json:"cert,omitempty"`

// Key is a valid key, for providers that use x509 certificate authentication.
Key string `json:"key,omitempty"`

// Username is a valid username, for providers that use password authentication.
Username string `json:"username,omitempty"`

// Password is a valid password, for providers that use password authentication.
Password string `json:"password,omitempty"`
} `json:"config"`
Config *OOAPIVPNConfig `json:"config"`

// Inputs is an array of valid endpoints for this provider.
Inputs []string
Expand Down

0 comments on commit f6ec850

Please sign in to comment.