-
Notifications
You must be signed in to change notification settings - Fork 6
/
auth_test.go
115 lines (101 loc) · 2.38 KB
/
auth_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
// Copyright (c) 2019, NewReleases Go client AUTHORS.
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package newreleases_test
import (
"context"
"fmt"
"net"
"net/http"
"testing"
"newreleases.io/newreleases"
)
func TestAuthService_List(t *testing.T) {
client, mux, _, teardown := newClient(t, "")
defer teardown()
mux.HandleFunc("/v1/auth/keys", requireMethod("GET", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", jsonContentType)
fmt.Fprintln(w, authServiceList)
}))
got, err := client.Auth.List(context.Background())
if err != nil {
t.Fatal(err)
}
assertEqual(t, "", got, authServiceListWant)
}
func TestGetAuthKeys(t *testing.T) {
_, mux, baseURL, teardown := newClient(t, "")
defer teardown()
wantUsername := "[email protected]"
wantPassword := "password12345"
mux.HandleFunc("/v1/auth/keys", requireMethod("GET", func(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth()
if !ok || username != wantUsername || password != wantPassword {
w.WriteHeader(http.StatusUnauthorized)
return
}
w.Header().Set("Content-Type", jsonContentType)
fmt.Fprintln(w, authServiceList)
}))
got, err := newreleases.GetAuthKeys(
context.Background(),
wantUsername,
wantPassword,
&newreleases.ClientOptions{
BaseURL: baseURL,
})
if err != nil {
t.Fatal(err)
}
assertEqual(t, "", got, authServiceListWant)
}
var (
authServiceList = `
{
"keys": [
{
"name": "My Key",
"secret": "ewcppcsk781h1bwxplq3pe8gf7322d8n52bg",
"authorized_networks": [
"::/0",
"0.0.0.0/0"
]
},
{
"name": "Internal",
"secret": "awcppcsk781h1bwxplq3pe8gf7322d8n52b1",
"authorized_networks": [
"2001:19f0:5:5f0:5400:ff:fe72:ce59/128",
"173.199.123.52/32"
]
}
]
}
`
authServiceListWant = []newreleases.AuthKey{
{
Name: "My Key",
Secret: "ewcppcsk781h1bwxplq3pe8gf7322d8n52bg",
AuthorizedNetworks: []net.IPNet{
ipNet("::/0"),
ipNet("0.0.0.0/0"),
},
},
{
Name: "Internal",
Secret: "awcppcsk781h1bwxplq3pe8gf7322d8n52b1",
AuthorizedNetworks: []net.IPNet{
ipNet("2001:19f0:5:5f0:5400:ff:fe72:ce59/128"),
ipNet("173.199.123.52/32"),
},
},
}
)
func ipNet(s string) net.IPNet {
_, nn, err := net.ParseCIDR(s)
if err != nil {
panic(err)
}
return *nn
}