forked from taskcluster/taskcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
credentials_update_test.go
112 lines (94 loc) · 2.4 KB
/
credentials_update_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
package main
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
tcclient "github.com/taskcluster/taskcluster/v47/clients/client-go"
)
type RoutesTest struct {
Routes
t *testing.T
}
func TestCredentialsUpdate(t *testing.T) {
newCreds := CredentialsUpdate{
ClientID: "newClientId",
AccessToken: "newAccessToken",
Certificate: `{"version":1,"scopes":["scope1"]}`,
}
body, err := json.Marshal(&newCreds)
if err != nil {
t.Fatal(err)
}
routes := NewRoutesTest(t)
response := routes.request("POST", body)
if response.Code != 405 {
t.Errorf("Should return 405, but returned %d", response.Code)
}
response = routes.request("PUT", make([]byte, 0))
if response.Code != 400 {
t.Errorf("Should return 400, but returned %d", response.Code)
}
response = routes.request("PUT", []byte("{\"badJS0n!"))
if response.Code != 400 {
content, _ := io.ReadAll(response.Body)
t.Fatalf("Request error %d: %s", response.Code, string(content))
}
response = routes.request("PUT", body)
if response.Code != 200 {
content, _ := io.ReadAll(response.Body)
t.Fatalf("Request error %d: %s", response.Code, string(content))
}
if routes.Credentials.ClientID != newCreds.ClientID {
t.Errorf(
"ClientId should be \"%s\", but got \"%s\"",
newCreds.ClientID,
routes.Credentials.ClientID,
)
}
if routes.Credentials.AccessToken != newCreds.AccessToken {
t.Errorf(
"AccessToken should be \"%s\", but got \"%s\"",
newCreds.AccessToken,
routes.Credentials.AccessToken,
)
}
if routes.Credentials.Certificate != newCreds.Certificate {
t.Errorf(
"Certificate should be \"%s\", but got \"%s\"",
newCreds.Certificate,
routes.Credentials.Certificate,
)
}
}
func (routesTest *RoutesTest) request(method string, content []byte) (res *httptest.ResponseRecorder) {
req, err := http.NewRequest(
method,
"http://localhost:8080/credentials",
bytes.NewBuffer(content),
)
if err != nil {
routesTest.t.Fatal(err)
}
req.ContentLength = int64(len(content))
res = httptest.NewRecorder()
routesTest.CredentialsHandler(res, req)
return
}
func NewRoutesTest(t *testing.T) *RoutesTest {
return &RoutesTest{
Routes: Routes{
Client: tcclient.Client{
Authenticate: true,
Credentials: &tcclient.Credentials{
ClientID: "clientId",
AccessToken: "accessToken",
Certificate: `{"version":1,"scopes":["scope2"]}`,
},
},
},
t: t,
}
}