forked from ory/fosite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_test.go
122 lines (107 loc) · 3.25 KB
/
helper_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
/*
* Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Aeneas Rekkas <aeneas+oss@aeneas.io>
* @copyright 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
* @license Apache-2.0
*
*/
package openid
import (
"net/url"
"testing"
"github.com/golang/mock/gomock"
"github.com/ory/fosite"
"github.com/ory/fosite/internal"
"github.com/ory/fosite/token/jwt"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
var strat = &DefaultStrategy{
JWTStrategy: &jwt.RS256JWTStrategy{
PrivateKey: internal.MustRSAKey(),
},
}
var fooErr = errors.New("foo")
func TestGenerateIDToken(t *testing.T) {
ctrl := gomock.NewController(t)
chgen := internal.NewMockOpenIDConnectTokenStrategy(ctrl)
defer ctrl.Finish()
ar := fosite.NewAccessRequest(nil)
sess := &DefaultSession{
Claims: &jwt.IDTokenClaims{
Subject: "peter",
},
Headers: &jwt.Headers{},
}
h := &IDTokenHandleHelper{IDTokenStrategy: chgen}
for k, c := range []struct {
description string
setup func()
expectErr error
}{
{
description: "should fail because generator failed",
setup: func() {
ar.Form.Set("nonce", "11111111111111111111111111111111111")
ar.SetSession(sess)
chgen.EXPECT().GenerateIDToken(nil, ar).Return("", fooErr)
},
expectErr: fooErr,
},
{
description: "should pass",
setup: func() {
chgen.EXPECT().GenerateIDToken(nil, ar).AnyTimes().Return("asdf", nil)
},
},
} {
c.setup()
token, err := h.generateIDToken(nil, ar)
assert.True(t, errors.Cause(err) == c.expectErr, "(%d) %s\n%s\n%s", k, c.description, err, c.expectErr)
if err == nil {
assert.NotEmpty(t, token, "(%d) %s", k, c.description)
}
t.Logf("Passed test case %d", k)
}
}
func TestIssueExplicitToken(t *testing.T) {
ctrl := gomock.NewController(t)
resp := internal.NewMockAccessResponder(ctrl)
defer ctrl.Finish()
ar := fosite.NewAuthorizeRequest()
ar.Form = url.Values{"nonce": {"111111111111"}}
ar.SetSession(&DefaultSession{Claims: &jwt.IDTokenClaims{
Subject: "peter",
}, Headers: &jwt.Headers{}})
resp.EXPECT().SetExtra("id_token", gomock.Any())
h := &IDTokenHandleHelper{IDTokenStrategy: strat}
err := h.IssueExplicitIDToken(nil, ar, resp)
assert.NoError(t, err)
}
func TestIssueImplicitToken(t *testing.T) {
ctrl := gomock.NewController(t)
resp := internal.NewMockAuthorizeResponder(ctrl)
defer ctrl.Finish()
ar := fosite.NewAuthorizeRequest()
ar.Form = url.Values{"nonce": {"111111111111"}}
ar.SetSession(&DefaultSession{Claims: &jwt.IDTokenClaims{
Subject: "peter",
}, Headers: &jwt.Headers{}})
resp.EXPECT().AddFragment("id_token", gomock.Any())
h := &IDTokenHandleHelper{IDTokenStrategy: strat}
err := h.IssueImplicitIDToken(nil, ar, resp)
assert.NoError(t, err)
}