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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
package cachedtvm_test
import (
"context"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/require"
"a.yandex-team.ru/library/go/yandex/tvm"
"a.yandex-team.ru/library/go/yandex/tvm/cachedtvm"
)
const (
checkPasses = 5
)
type mockTvmClient struct {
tvm.Client
checkServiceTicketCalls int
checkUserTicketCalls int
}
func (c *mockTvmClient) CheckServiceTicket(_ context.Context, ticket string) (*tvm.CheckedServiceTicket, error) {
defer func() { c.checkServiceTicketCalls++ }()
return &tvm.CheckedServiceTicket{
LogInfo: ticket,
IssuerUID: tvm.UID(c.checkServiceTicketCalls),
}, nil
}
func (c *mockTvmClient) CheckUserTicket(_ context.Context, ticket string, _ ...tvm.CheckUserTicketOption) (*tvm.CheckedUserTicket, error) {
defer func() { c.checkUserTicketCalls++ }()
return &tvm.CheckedUserTicket{
LogInfo: ticket,
DefaultUID: tvm.UID(c.checkUserTicketCalls),
}, nil
}
func (c *mockTvmClient) GetServiceTicketForAlias(_ context.Context, alias string) (string, error) {
return alias, nil
}
func checkServiceTickets(t *testing.T, client tvm.Client, equal bool) {
var prev *tvm.CheckedServiceTicket
for i := 0; i < checkPasses; i++ {
t.Run(strconv.Itoa(i), func(t *testing.T) {
cur, err := client.CheckServiceTicket(context.Background(), "3:serv:tst")
require.NoError(t, err)
if prev == nil {
return
}
if equal {
require.Equal(t, *prev, *cur)
} else {
require.NotEqual(t, *prev, *cur)
}
})
}
}
func runEqualServiceTickets(client tvm.Client) func(t *testing.T) {
return func(t *testing.T) {
checkServiceTickets(t, client, true)
}
}
func runNotEqualServiceTickets(client tvm.Client) func(t *testing.T) {
return func(t *testing.T) {
checkServiceTickets(t, client, false)
}
}
func checkUserTickets(t *testing.T, client tvm.Client, equal bool) {
var prev *tvm.CheckedServiceTicket
for i := 0; i < checkPasses; i++ {
t.Run(strconv.Itoa(i), func(t *testing.T) {
cur, err := client.CheckUserTicket(context.Background(), "3:user:tst")
require.NoError(t, err)
if prev == nil {
return
}
if equal {
require.Equal(t, *prev, *cur)
} else {
require.NotEqual(t, *prev, *cur)
}
})
}
}
func runEqualUserTickets(client tvm.Client) func(t *testing.T) {
return func(t *testing.T) {
checkUserTickets(t, client, true)
}
}
func runNotEqualUserTickets(client tvm.Client) func(t *testing.T) {
return func(t *testing.T) {
checkUserTickets(t, client, false)
}
}
func TestDefaultBehavior(t *testing.T) {
nestedClient := &mockTvmClient{}
client, err := cachedtvm.NewClient(nestedClient)
require.NoError(t, err)
t.Run("first_pass_srv", runEqualServiceTickets(client))
t.Run("first_pass_usr", runNotEqualUserTickets(client))
require.Equal(t, 1, nestedClient.checkServiceTicketCalls)
require.Equal(t, checkPasses, nestedClient.checkUserTicketCalls)
ticket, err := client.GetServiceTicketForAlias(context.Background(), "tst")
require.NoError(t, err)
require.Equal(t, "tst", ticket)
}
func TestCheckServiceTicket(t *testing.T) {
nestedClient := &mockTvmClient{}
client, err := cachedtvm.NewClient(nestedClient, cachedtvm.WithCheckServiceTicket(10*time.Second, 10))
require.NoError(t, err)
t.Run("first_pass_srv", runEqualServiceTickets(client))
t.Run("first_pass_usr", runNotEqualUserTickets(client))
time.Sleep(20 * time.Second)
t.Run("second_pass_srv", runEqualServiceTickets(client))
t.Run("second_pass_usr", runNotEqualUserTickets(client))
require.Equal(t, 2, nestedClient.checkServiceTicketCalls)
require.Equal(t, 2*checkPasses, nestedClient.checkUserTicketCalls)
ticket, err := client.GetServiceTicketForAlias(context.Background(), "tst")
require.NoError(t, err)
require.Equal(t, "tst", ticket)
}
func TestCheckUserTicket(t *testing.T) {
nestedClient := &mockTvmClient{}
client, err := cachedtvm.NewClient(nestedClient, cachedtvm.WithCheckUserTicket(10*time.Second, 10))
require.NoError(t, err)
t.Run("first_pass_usr", runEqualUserTickets(client))
time.Sleep(20 * time.Second)
t.Run("second_pass_usr", runEqualUserTickets(client))
require.Equal(t, 2, nestedClient.checkUserTicketCalls)
ticket, err := client.GetServiceTicketForAlias(context.Background(), "tst")
require.NoError(t, err)
require.Equal(t, "tst", ticket)
}
func TestCheckServiceAndUserTicket(t *testing.T) {
nestedClient := &mockTvmClient{}
client, err := cachedtvm.NewClient(nestedClient,
cachedtvm.WithCheckServiceTicket(10*time.Second, 10),
cachedtvm.WithCheckUserTicket(10*time.Second, 10),
)
require.NoError(t, err)
t.Run("first_pass_srv", runEqualServiceTickets(client))
t.Run("first_pass_usr", runEqualUserTickets(client))
time.Sleep(20 * time.Second)
t.Run("second_pass_srv", runEqualServiceTickets(client))
t.Run("second_pass_usr", runEqualUserTickets(client))
require.Equal(t, 2, nestedClient.checkUserTicketCalls)
require.Equal(t, 2, nestedClient.checkServiceTicketCalls)
ticket, err := client.GetServiceTicketForAlias(context.Background(), "tst")
require.NoError(t, err)
require.Equal(t, "tst", ticket)
}
func TestErrors(t *testing.T) {
cases := []cachedtvm.Option{
cachedtvm.WithCheckServiceTicket(12*time.Hour, 1),
cachedtvm.WithCheckUserTicket(30*time.Minute, 1),
}
nestedClient := &mockTvmClient{}
for i, tc := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
_, err := cachedtvm.NewClient(nestedClient, tc)
require.Error(t, err)
})
}
}
|