summaryrefslogtreecommitdiff
path: root/internal/server/api_test.go
blob: b1e35503bef2a2b5850186543e1c1dab0ad2b752 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package server

import (
	"encoding/json"
	"fmt"
	"net/http"
	"testing"

	"github.com/eduvpn/eduvpn-common/internal/test"
	"github.com/go-errors/errors"
)

func getErrorMsg(err error) string {
	if err == nil {
		return ""
	}
	return err.Error()
}

func compareEndpoints(ep1 Endpoints, ep2 Endpoints) bool {
	v3_1 := ep1.API.V3
	v3_2 := ep2.API.V3
	return v3_1.API == v3_2.API && v3_1.Authorization == v3_2.Authorization && v3_1.Token == v3_2.Token
}

func Test_APIGetEndpoints(t *testing.T) {
	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "Hello!")
	})
	hs := &test.HandlerSet{}
	hs.SetHandler(handler)
	s := test.NewServer(hs)
	defer s.Close()

	c, err := s.Client()
	if err != nil {
		t.Fatalf("failed to get client for test server endpoints: %v", err)
	}

	testCases := []struct {
		epl EndpointList
		err error
	}{
		{
			epl: EndpointList{
				API:           "https://example.com/1",
				Authorization: "https://example.com/2",
				Token:         "https://example.com/3",
			},
			err: nil,
		},
		{
			epl: EndpointList{
				API:           "http://example.com/1",
				Authorization: "https://example.com/2",
				Token:         "https://example.com/3",
			},
			err: errors.New("API scheme: 'http', is not equal to authorization scheme: 'https'"),
		},
		{
			epl: EndpointList{
				API:           "https://example.com/1",
				Authorization: "https://example.com/2",
				Token:         "ftp://example.com/3",
			},
			err: errors.New("API scheme: 'https', is not equal to token scheme: 'ftp'"),
		},
		{
			epl: EndpointList{
				API:           "https://malicious.com/1",
				Authorization: "https://example.com/2",
				Token:         "https://example.com/3",
			},
			err: errors.New("API host: 'malicious.com', is not equal to authorization host: 'example.com'"),
		},
		{
			epl: EndpointList{
				API:           "https://example.com/1",
				Authorization: "https://example.com/2",
				Token:         "https://malicious.com/3",
			},
			err: errors.New("API host: 'example.com', is not equal to token host: 'malicious.com'"),
		},
		{
			epl: EndpointList{
				API:           "https://example.com/1",
				Authorization: "https://malicious.com/2",
				Token:         "https://example.com/3",
			},
			err: errors.New("API host: 'example.com', is not equal to authorization host: 'malicious.com'"),
		},
	}

	for _, tc := range testCases {
		ep := &Endpoints{
			API: EndpointsVersions{
				V3: tc.epl,
			},
		}
		// Update the handler
		hs.SetHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.Header().Set("Content-Type", "application/json")

			jsonStr, err := json.Marshal(ep)
			if err != nil {
				t.Fatalf("failed to marshal JSON for test case: %v, err: %v", tc, err)
			}

			fmt.Fprintln(w, string(jsonStr))
		}))
		gotEP, err := APIGetEndpoints(s.URL, c)
		if getErrorMsg(err) != getErrorMsg(tc.err) {
			t.Fatalf("Errors not equal, want err: %v, got: %v", tc.err, err)
		}
		// The error was not nil, continue because endpoints should not be compared
		if tc.err != nil {
			continue
		}
		if ep == nil {
			t.Fatalf("No test case endpoints")
		}
		if gotEP == nil {
			t.Fatalf("Got no endpoints for nil error")
		}
		// if no error then the endpoints should be equal
		if !compareEndpoints(*ep, *gotEP) {
			t.Fatalf("Endpoints are not equal, got: %v, want: %v", gotEP, ep)
		}
	}
}