-
Notifications
You must be signed in to change notification settings - Fork 9
/
authentication_test.go
149 lines (127 loc) · 3.73 KB
/
authentication_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
package codeship_test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
codeship "github.com/codeship/codeship-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAuthenticate(t *testing.T) {
tests := []struct {
name string
handler http.HandlerFunc
status int
err string
}{
{
name: "successful auth",
handler: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, fixture("auth/success.json"))
},
status: http.StatusOK,
},
{
name: "invalid JSON",
handler: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "{ \"foo\": }")
},
status: http.StatusOK,
err: "unable to unmarshal JSON: invalid character '}' looking for beginning of value",
},
{
name: "unauthorized auth",
handler: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprint(w, fixture("auth/unauthorized.json"))
},
status: http.StatusUnauthorized,
err: "invalid credentials",
},
{
name: "two-factor enabled auth",
handler: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, fixture("auth/two_factor.json"))
},
status: http.StatusOK,
err: "your account has two-factor authentication enabled, which is not possible to support with the API.",
},
{
name: "rate limit exceeded",
handler: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusForbidden)
},
status: http.StatusForbidden,
err: "rate limit exceeded",
},
{
name: "server error",
handler: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
},
status: http.StatusInternalServerError,
err: "HTTP status: 500",
},
{
name: "other status code",
handler: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusTeapot)
},
status: http.StatusTeapot,
err: "HTTP status: 418",
},
{
name: "other status code with body",
handler: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusTeapot)
fmt.Fprint(w, "I'm a teapot")
},
status: http.StatusTeapot,
err: "HTTP status: 418; content \"I'm a teapot\"",
},
}
assert := assert.New(t)
require := require.New(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mux = http.NewServeMux()
server = httptest.NewServer(mux)
mux.HandleFunc("/auth", tt.handler)
client, _ = codeship.New(codeship.NewBasicAuth("username", "password"), codeship.BaseURL(server.URL))
org, _ = client.Organization(context.Background(), "codeship")
defer func() {
server.Close()
}()
resp, err := client.Authenticate(context.Background())
require.NotNil(resp)
assert.Equal(tt.status, resp.StatusCode)
if tt.err == "" {
require.NoError(err)
} else {
require.Error(err)
assert.EqualError(err, tt.err)
}
})
}
}