/
context_test.go
187 lines (154 loc) · 4.33 KB
/
context_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
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
// SPDX-FileCopyrightText: 2020 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package bascule
import (
"context"
"testing"
"github.com/stretchr/testify/suite"
)
type ContextTestSuite struct {
TestSuite
}
func (suite *ContextTestSuite) testGetCredentialsSuccess() {
ctx := context.WithValue(
context.Background(),
credentialsContextKey{},
suite.testCredentials(),
)
creds, ok := GetCredentials(ctx)
suite.Require().True(ok)
suite.Equal(
suite.testCredentials(),
creds,
)
}
func (suite *ContextTestSuite) testGetCredentialsMissing() {
creds, ok := GetCredentials(context.Background())
suite.Equal(Credentials{}, creds)
suite.False(ok)
}
func (suite *ContextTestSuite) testGetCredentialsWrongType() {
ctx := context.WithValue(context.Background(), credentialsContextKey{}, 123)
creds, ok := GetCredentials(ctx)
suite.Equal(Credentials{}, creds)
suite.False(ok)
}
func (suite *ContextTestSuite) TestGetCredentials() {
suite.Run("Success", suite.testGetCredentialsSuccess)
suite.Run("Missing", suite.testGetCredentialsMissing)
suite.Run("WrongType", suite.testGetCredentialsWrongType)
}
func (suite *ContextTestSuite) testGetCredentialsFromSuccess() {
c := suite.contexter(
context.WithValue(
context.Background(),
credentialsContextKey{},
suite.testCredentials(),
),
)
creds, ok := GetCredentialsFrom(c)
suite.Require().True(ok)
suite.Equal(
suite.testCredentials(),
creds,
)
}
func (suite *ContextTestSuite) testGetCredentialsFromMissing() {
creds, ok := GetCredentialsFrom(
suite.contexter(context.Background()),
)
suite.Equal(Credentials{}, creds)
suite.False(ok)
}
func (suite *ContextTestSuite) testGetCredentialsFromWrongType() {
c := suite.contexter(
context.WithValue(context.Background(), credentialsContextKey{}, 123),
)
creds, ok := GetCredentialsFrom(c)
suite.Equal(Credentials{}, creds)
suite.False(ok)
}
func (suite *ContextTestSuite) TestGetCredentialsFrom() {
suite.Run("Success", suite.testGetCredentialsFromSuccess)
suite.Run("Missing", suite.testGetCredentialsFromMissing)
suite.Run("WrongType", suite.testGetCredentialsFromWrongType)
}
func (suite *ContextTestSuite) TestWithCredentials() {
ctx := WithCredentials(context.Background(), suite.testCredentials())
creds, ok := ctx.Value(credentialsContextKey{}).(Credentials)
suite.Require().True(ok)
suite.Equal(suite.testCredentials(), creds)
}
func (suite *ContextTestSuite) testGetTokenSuccess() {
ctx := context.WithValue(
context.Background(),
tokenContextKey{},
suite.testToken(),
)
token, ok := GetToken(ctx)
suite.Require().True(ok)
suite.Equal(
suite.testToken(),
token,
)
}
func (suite *ContextTestSuite) testGetTokenMissing() {
token, ok := GetToken(context.Background())
suite.Nil(token)
suite.False(ok)
}
func (suite *ContextTestSuite) testGetTokenWrongType() {
ctx := context.WithValue(context.Background(), tokenContextKey{}, 123)
token, ok := GetToken(ctx)
suite.Nil(token)
suite.False(ok)
}
func (suite *ContextTestSuite) TestGetToken() {
suite.Run("Success", suite.testGetTokenSuccess)
suite.Run("Missing", suite.testGetTokenMissing)
suite.Run("WrongType", suite.testGetTokenWrongType)
}
func (suite *ContextTestSuite) testGetTokenFromSuccess() {
c := suite.contexter(
context.WithValue(
context.Background(),
tokenContextKey{},
suite.testToken(),
),
)
token, ok := GetTokenFrom(c)
suite.Require().True(ok)
suite.Equal(
suite.testToken(),
token,
)
}
func (suite *ContextTestSuite) testGetTokenFromMissing() {
token, ok := GetTokenFrom(
suite.contexter(context.Background()),
)
suite.Nil(token)
suite.False(ok)
}
func (suite *ContextTestSuite) testGetTokenFromWrongType() {
c := suite.contexter(
context.WithValue(context.Background(), tokenContextKey{}, 123),
)
token, ok := GetTokenFrom(c)
suite.Nil(token)
suite.False(ok)
}
func (suite *ContextTestSuite) TestGetTokenFrom() {
suite.Run("Success", suite.testGetTokenFromSuccess)
suite.Run("Missing", suite.testGetTokenFromMissing)
suite.Run("WrongType", suite.testGetTokenFromWrongType)
}
func (suite *ContextTestSuite) TestWithToken() {
ctx := WithToken(context.Background(), suite.testToken())
token, ok := ctx.Value(tokenContextKey{}).(Token)
suite.Require().True(ok)
suite.Equal(suite.testToken(), token)
}
func TestContext(t *testing.T) {
suite.Run(t, new(ContextTestSuite))
}