-
Notifications
You must be signed in to change notification settings - Fork 4
/
mocks_test.go
156 lines (119 loc) · 3.39 KB
/
mocks_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
150
151
152
153
154
155
156
// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package bascule
import (
"context"
"github.com/stretchr/testify/mock"
)
type mockToken struct {
mock.Mock
}
func (m *mockToken) Principal() string {
return m.Called().String(0)
}
func (m *mockToken) ExpectPrincipal(v string) *mock.Call {
return m.On("Principal").Return(v)
}
type mockTokenWithPassword struct {
mockToken
}
func (m *mockTokenWithPassword) Password() string {
return m.Called().String(0)
}
func (m *mockTokenWithPassword) ExpectPassword(v string) *mock.Call {
return m.On("Password").Return(v)
}
type mockTokenWithCapabilities struct {
mockToken
}
func (m *mockTokenWithCapabilities) Capabilities() []string {
args := m.Called()
caps, _ := args.Get(0).([]string)
return caps
}
func (m *mockTokenWithCapabilities) ExpectCapabilities(caps ...string) *mock.Call {
return m.On("Capabilities").Return(caps)
}
type mockTokenUnwrapOne struct {
mockToken
}
func (m *mockTokenUnwrapOne) Unwrap() Token {
args := m.Called()
t, _ := args.Get(0).(Token)
return t
}
func (m *mockTokenUnwrapOne) ExpectUnwrap(t Token) *mock.Call {
return m.On("Unwrap").Return(t)
}
type mockTokenUnwrapMany struct {
mockToken
}
func (m *mockTokenUnwrapMany) Unwrap() []Token {
args := m.Called()
t, _ := args.Get(0).([]Token)
return t
}
func (m *mockTokenUnwrapMany) ExpectUnwrap(t ...Token) *mock.Call {
return m.On("Unwrap").Return(t)
}
type mockValidator[S any] struct {
mock.Mock
}
func (m *mockValidator[S]) Validate(ctx context.Context, source S, token Token) (Token, error) {
args := m.Called(ctx, source, token)
t, _ := args.Get(0).(Token)
return t, args.Error(1)
}
func (m *mockValidator[S]) ExpectValidate(ctx context.Context, source S, token Token) *mock.Call {
return m.On("Validate", ctx, source, token)
}
func assertValidators[S any](t mock.TestingT, vs ...Validator[S]) (passed bool) {
for _, v := range vs {
passed = v.(*mockValidator[S]).AssertExpectations(t) && passed
}
return
}
type mockTokenParser[S any] struct {
mock.Mock
}
func (m *mockTokenParser[S]) Parse(ctx context.Context, source S) (Token, error) {
args := m.Called(ctx, source)
t, _ := args.Get(0).(Token)
return t, args.Error(1)
}
func (m *mockTokenParser[S]) ExpectParse(ctx context.Context, source S) *mock.Call {
return m.On("Parse", ctx, source)
}
func assertTokenParsers[S any](t mock.TestingT, tps ...TokenParser[S]) (passed bool) {
for _, p := range tps {
passed = p.(*mockTokenParser[S]).AssertExpectations(t) && passed
}
return
}
type mockApprover[R any] struct {
mock.Mock
}
func (m *mockApprover[R]) Approve(ctx context.Context, resource R, token Token) error {
return m.Called(ctx, resource, token).Error(0)
}
func (m *mockApprover[R]) ExpectApprove(ctx context.Context, resource R, token Token) *mock.Call {
return m.On("Approve", ctx, resource, token)
}
type mockAuthenticateListener[E any] struct {
mock.Mock
}
func (m *mockAuthenticateListener[E]) OnEvent(e AuthenticateEvent[E]) {
m.Called(e)
}
func (m *mockAuthenticateListener[E]) ExpectOnEvent(expected AuthenticateEvent[E]) *mock.Call {
return m.On("OnEvent", expected)
}
type mockAuthorizeListener[E any] struct {
mock.Mock
}
func (m *mockAuthorizeListener[E]) OnEvent(e AuthorizeEvent[E]) {
m.Called(e)
}
func (m *mockAuthorizeListener[E]) ExpectOnEvent(expected AuthorizeEvent[E]) *mock.Call {
return m.On("OnEvent", expected)
}