-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
127 lines (99 loc) · 2.7 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
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
package loginsrv_grpc
import (
"testing"
"time"
md "google.golang.org/grpc/metadata"
)
func TestAuthenticateWithoutTokenFails(t *testing.T) {
srv := NewLoginSrvServer("http://localhost:8080")
token := ""
ctx := &contextWithAuthorizationStub{authToken: token}
_, err := srv.Authenticate(ctx)
if err == nil {
t.Error("Authenticate should fail", err)
}
}
func TestAuthenticateWithTokenSucceeds(t *testing.T) {
srv := NewLoginSrvServer("http://localhost:8080")
token := obtainTokenOrFail(t, srv)
ctx := &contextWithAuthorizationStub{authToken: token}
x, err := srv.Authenticate(ctx)
if err != nil {
t.Error("Token could not be obtained", err)
}
if x == nil {
t.Error("Context should be returned")
}
}
func TestGetProfile(t *testing.T) {
srv := NewLoginSrvServer("http://localhost:8080")
token := obtainTokenOrFail(t, srv)
ctx := &contextWithAuthorizationStub{authToken: token}
profile, err := srv.GetProfile(ctx, &ProfileRequest{})
if err != nil {
t.Error("Error while loading profile", err)
}
if profile == nil {
t.Error("Profile should not be nil", err)
}
if profile.Sub != "bob" {
t.Error("Expected 'bob' profile but got " + profile.Sub)
}
}
func TestAttemptLoginThenRefresh(t *testing.T) {
srv := NewLoginSrvServer("http://localhost:8080")
loginReply, err := srv.AttemptLogin(nil, &LoginRequest{
Username: "bob",
Password: "secret",
})
if err != nil {
t.Error("Login failed", err)
return
}
assertHasAccessToken(t, loginReply)
// md, ok = ctx.Value(mdIncomingKey{}).(MD)
refreshCtx := &contextWithAuthorizationStub{
authToken: loginReply.AccessToken,
}
refreshReply, _ := srv.RefreshToken(refreshCtx, &RefreshRequest{})
if err != nil {
t.Error("Refresh failed ", err)
return
}
assertHasAccessToken(t, refreshReply)
}
func obtainTokenOrFail(t *testing.T, srv *LoginSrvServer) string {
loginReply, err := srv.AttemptLogin(nil, &LoginRequest{
Username: "bob",
Password: "secret",
})
if err != nil {
t.Error("Token could not be obtained", err)
}
return loginReply.AccessToken
}
func assertHasAccessToken(t *testing.T, r *LoginReply) {
if r.GetAccessToken() == "" {
t.Error("No access token in reply")
}
}
type contextWithAuthorizationStub struct {
authToken string
}
func (*contextWithAuthorizationStub) Deadline() (deadline time.Time, ok bool) {
return time.Time{}, true
}
func (*contextWithAuthorizationStub) Done() <-chan struct{} {
return make(<-chan struct{})
}
func (*contextWithAuthorizationStub) Err() error {
return nil
}
func (m *contextWithAuthorizationStub) Value(key interface{}) interface{} {
if m.authToken == "" {
return md.New(map[string]string{})
}
return md.New(map[string]string{
AuthTokenMetadataKey: "bearer " + m.authToken,
})
}