-
Notifications
You must be signed in to change notification settings - Fork 8
/
domains_test.go
163 lines (154 loc) · 4.78 KB
/
domains_test.go
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package scalingo
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDomainsClient_Domain_Updates(t *testing.T) {
ctx := context.Background()
appName := "my-app"
domainID := "domain-id"
tests := map[string]struct {
testedClientCall func(c DomainsService) error
expectedEndpoint string
expectedMethod string
expectedParams string
responseStatus int
mockDomainsList func(t *testing.T, w http.ResponseWriter, r *http.Request)
expectedError string
}{
// Canonical status update
"it should set the domain as canonical": {
testedClientCall: func(c DomainsService) error {
_, err := c.DomainSetCanonical(ctx, appName, domainID)
return err
},
expectedEndpoint: "/v1/apps/my-app/domains/domain-id",
expectedMethod: "PATCH",
expectedParams: `"canonical":true`,
responseStatus: 200,
},
"it should unset the domain as canonical": {
mockDomainsList: func(t *testing.T, w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(200)
err := json.NewEncoder(w).Encode(DomainsRes{Domains: []Domain{
{
ID: "domain-id",
Canonical: true,
},
}})
assert.NoError(t, err)
},
testedClientCall: func(c DomainsService) error {
_, err := c.DomainUnsetCanonical(ctx, appName)
return err
},
expectedEndpoint: "/v1/apps/my-app/domains/domain-id",
expectedMethod: "PATCH",
expectedParams: `"canonical":false`,
responseStatus: 200,
},
"it should return an error if there is no canonical domain": {
mockDomainsList: func(t *testing.T, w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(200)
err := json.NewEncoder(w).Encode(DomainsRes{Domains: []Domain{}})
assert.NoError(t, err)
},
testedClientCall: func(c DomainsService) error {
_, err := c.DomainUnsetCanonical(ctx, appName)
return err
},
expectedError: "no canonical domain configured",
},
// Domain certificate update
"it should set the domain certificate": {
testedClientCall: func(c DomainsService) error {
_, err := c.DomainSetCertificate(ctx, appName, domainID, "cert", "key")
return err
},
expectedEndpoint: "/v1/apps/my-app/domains/domain-id",
expectedMethod: "PATCH",
expectedParams: `"tlscert":"cert","tlskey":"key"`,
responseStatus: 200,
},
"it should unset the domain certificate": {
testedClientCall: func(c DomainsService) error {
_, err := c.DomainUnsetCertificate(ctx, appName, domainID)
return err
},
expectedEndpoint: "/v1/apps/my-app/domains/domain-id",
expectedMethod: "PATCH",
expectedParams: `"tlscert":"","tlskey":""`,
responseStatus: 200,
},
// Letsencrypt certificate generation
"it should update letsencrypt_enabled to false": {
testedClientCall: func(c DomainsService) error {
f := false
_, err := c.DomainsUpdate(ctx, appName, domainID, DomainsUpdateParams{
LetsEncryptEnabled: &f,
})
return err
},
expectedEndpoint: "/v1/apps/my-app/domains/domain-id",
expectedMethod: "PATCH",
expectedParams: `"letsencrypt_enabled":false`,
responseStatus: 200,
},
"it should not update anything if no params": {
testedClientCall: func(c DomainsService) error {
_, err := c.DomainsUpdate(ctx, appName, domainID, DomainsUpdateParams{})
return err
},
expectedEndpoint: "/v1/apps/my-app/domains/domain-id",
expectedMethod: "PATCH",
expectedParams: `{"domain":{}}`,
responseStatus: 200,
},
}
for msg, test := range tests {
t.Run(msg, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// If request domains list
if r.Method == "GET" && r.URL.Path == "/v1/apps/my-app/domains" {
require.NotNil(t, test.mockDomainsList)
test.mockDomainsList(t, w, r)
return
}
assert.Equal(t, test.expectedMethod, r.Method)
assert.Equal(t, test.expectedEndpoint, r.URL.Path)
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(r.Body)
require.NoError(t, err)
assert.Contains(t, buf.String(), test.expectedParams)
if test.responseStatus != 0 {
w.WriteHeader(test.responseStatus)
}
err = json.NewEncoder(w).Encode(DomainRes{})
assert.NoError(t, err)
}))
defer ts.Close()
scalingoClient, err := New(ctx, ClientConfig{
APIEndpoint: ts.URL,
APIToken: "test",
})
scalingoClient.authClient = MockAuth(ctrl)
require.NoError(t, err)
err = test.testedClientCall(scalingoClient)
if test.expectedError != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), test.expectedError)
} else {
require.NoError(t, err)
}
})
}
}