forked from lawilog/registry-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
100 lines (84 loc) · 2.76 KB
/
server_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
package registry
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
)
var mockToken = &Token{Token: "token", AccessToken: "token"}
type mockTokenGenerator struct {
t *Token
err error
}
func newMockTokenGenerator(t *Token, err error) *mockTokenGenerator {
return &mockTokenGenerator{t: t, err: err}
}
func (t *mockTokenGenerator) Generate(req *AuthorizationRequest, actions []string) (*Token, error) {
return t.t, t.err
}
type mockAuthenticator struct {
username, password string
}
func newMockAuthenticator(u, p string) *mockAuthenticator {
return &mockAuthenticator{username: u, password: p}
}
func (a *mockAuthenticator) Authenticate(username, password string) error {
if a.username != username || a.password != password {
return errors.New("invalid login")
}
return nil
}
type mockAuthorizer struct {
perms []string
}
func newMockAuthorizer(p []string) *mockAuthorizer {
return &mockAuthorizer{perms: p}
}
func (a *mockAuthorizer) Authorize(req *AuthorizationRequest) ([]string, error) {
return a.perms, nil
}
func TestNewAuthServerServe(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
r.RequestURI = "token?service=registry.docker.io&scope=repository:samalba/my-app:pull,push"
r.SetBasicAuth("foo", "bar")
srv := &AuthServer{
tokenGenerator: newMockTokenGenerator(mockToken, nil),
authorizer: newMockAuthorizer([]string{"pull", "push"}),
authenticator: newMockAuthenticator("foo", "bar"),
}
srv.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Fatalf("expected status code %d; got %d", http.StatusOK, w.Code)
}
}
func TestAuthServer_ServeHTTPAuthError(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
r.RequestURI = "token?service=registry.docker.io&scope=repository:samalba/my-app:pull,push"
r.SetBasicAuth("foo", "barr")
srv := &AuthServer{
tokenGenerator: &mockTokenGenerator{},
authorizer: newMockAuthorizer([]string{"pull", "push"}),
authenticator: newMockAuthenticator("foo", "bar"),
}
srv.ServeHTTP(w, r)
if w.Code != http.StatusUnauthorized {
t.Fatalf("expected status code %d; got %d", http.StatusUnauthorized, w.Code)
}
}
func TestAuthServer_ServeHTTPTokenError(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
r.RequestURI = "token?service=registry.docker.io&scope=repository:samalba/my-app:pull,push"
r.SetBasicAuth("foo", "bar")
srv := &AuthServer{
tokenGenerator: newMockTokenGenerator(nil, errors.New("fake token error")),
authorizer: newMockAuthorizer([]string{"pull", "push"}),
authenticator: newMockAuthenticator("foo", "bar"),
}
srv.ServeHTTP(w, r)
if w.Code != http.StatusInternalServerError {
t.Fatalf("expected status code %d; got %d", http.StatusInternalServerError, w.Code)
}
}