-
Notifications
You must be signed in to change notification settings - Fork 0
/
suite.go
155 lines (133 loc) · 4.39 KB
/
suite.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
150
151
152
153
154
155
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package idptest
import (
"html/template"
"net/http"
"golang.org/x/net/context"
gc "gopkg.in/check.v1"
"gopkg.in/macaroon-bakery.v2/bakery"
"gopkg.in/macaroon-bakery.v2/httpbakery"
"github.com/CanonicalLtd/blues-identity/idp"
"github.com/CanonicalLtd/blues-identity/internal/idmtest"
"github.com/CanonicalLtd/blues-identity/store"
)
// Suite provides a test suite that is helpful for testing identity
// providers.
type Suite struct {
idmtest.StoreSuite
// Template contains a template that will be passed in the
// idp.InitParams.
Template *template.Template
// The following fields will be available after calling SetUpTest.
// Ctx contains a context.Context that has been initialised with
// the stores.
Ctx context.Context
// Oven contains a bakery.Oven that will be passed in the
// idp.InitParams. Tests can use this to mint macaroons if
// necessary.
Oven *bakery.Oven
dischargeTokenCreator *dischargeTokenCreator
visitCompleter *visitCompleter
closeStore func()
closeMeetingStore func()
}
func (s *Suite) SetUpTest(c *gc.C) {
s.StoreSuite.SetUpTest(c)
s.Ctx, s.closeStore = s.Store.Context(context.Background())
s.Ctx, s.closeMeetingStore = s.MeetingStore.Context(s.Ctx)
key, err := bakery.GenerateKey()
c.Assert(err, gc.Equals, nil)
s.Oven = bakery.NewOven(bakery.OvenParams{
Key: key,
Location: "idptest",
})
}
func (s *Suite) TearDownTest(c *gc.C) {
s.closeMeetingStore()
s.closeStore()
s.StoreSuite.TearDownTest(c)
}
// InitParams returns a completed InitParams that a test can use to pass
// to idp.Init.
func (s *Suite) InitParams(c *gc.C, prefix string) idp.InitParams {
s.dischargeTokenCreator = &dischargeTokenCreator{}
s.visitCompleter = &visitCompleter{
c: c,
}
kv, err := s.ProviderDataStore.KeyValueStore(s.Ctx, "idptest")
c.Assert(err, gc.Equals, nil)
return idp.InitParams{
Store: s.Store,
KeyValueStore: kv,
Oven: s.Oven,
Key: s.Oven.Key(),
URLPrefix: prefix,
DischargeTokenCreator: s.dischargeTokenCreator,
VisitCompleter: s.visitCompleter,
Template: s.Template,
}
}
// AssertLoginSuccess asserts that the login test has resulted in a
// successful login of the given user.
func (s *Suite) AssertLoginSuccess(c *gc.C, username string) {
c.Assert(s.visitCompleter.called, gc.Equals, true)
c.Check(s.visitCompleter.err, gc.Equals, nil)
c.Assert(s.visitCompleter.id, gc.NotNil)
c.Assert(s.visitCompleter.id.Username, gc.Equals, username)
}
// AssertLoginFailure asserts taht the login test has resulted in a
// failure with an error that matches the given regex.
func (s *Suite) AssertLoginFailureMatches(c *gc.C, regex string) {
c.Assert(s.visitCompleter.called, gc.Equals, true)
c.Assert(s.visitCompleter.err, gc.ErrorMatches, regex)
}
// AssertLoginNotComplete asserts that the login attempt has not yet
// completed.
func (s *Suite) AssertLoginNotComplete(c *gc.C) {
c.Assert(s.visitCompleter.called, gc.Equals, false)
}
// AssertUser asserts that the specified user is stored in the store.
// It returns the stored identity.
func (s *Suite) AssertUser(c *gc.C, id *store.Identity) *store.Identity {
id1 := store.Identity{
ProviderID: id.ProviderID,
Username: id.Username,
}
err := s.Store.Identity(s.Ctx, &id1)
c.Assert(err, gc.Equals, nil)
idmtest.AssertEqualIdentity(c, &id1, id)
return &id1
}
type visitCompleter struct {
c *gc.C
called bool
dischargeID string
id *store.Identity
err error
}
func (l *visitCompleter) Success(_ context.Context, _ http.ResponseWriter, _ *http.Request, dischargeID string, id *store.Identity) {
if l.called {
l.c.Error("login completion method called more that once")
return
}
l.called = true
l.dischargeID = dischargeID
l.id = id
}
func (l *visitCompleter) Failure(_ context.Context, _ http.ResponseWriter, _ *http.Request, dischargeID string, err error) {
if l.called {
l.c.Error("login completion method called more that once")
return
}
l.called = true
l.dischargeID = dischargeID
l.err = err
}
type dischargeTokenCreator struct{}
func (d *dischargeTokenCreator) DischargeToken(_ context.Context, _ string, id *store.Identity) (*httpbakery.DischargeToken, error) {
return &httpbakery.DischargeToken{
Kind: "test",
Value: []byte(id.Username),
}, nil
}