-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandler_test.go
More file actions
311 lines (273 loc) · 7.5 KB
/
handler_test.go
File metadata and controls
311 lines (273 loc) · 7.5 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package admin
import (
"context"
"net/http"
"testing"
"github.com/graphql-go/graphql"
)
// Helper to check if enum has a value with the given name
func enumHasValue(enum *graphql.Enum, name string) bool {
for _, v := range enum.Values() {
if v.Name == name {
return true
}
}
return false
}
func TestEnumTypes(t *testing.T) {
// Test TimeRangeEnum
if TimeRangeEnum == nil {
t.Error("TimeRangeEnum is nil")
}
expectedTimeRanges := []string{"ONE_HOUR", "THREE_HOURS", "SIX_HOURS", "ONE_DAY", "SEVEN_DAYS"}
for _, name := range expectedTimeRanges {
if !enumHasValue(TimeRangeEnum, name) {
t.Errorf("Expected TimeRange value %s not found", name)
}
}
// Test LabelSeverityEnum
if LabelSeverityEnum == nil {
t.Error("LabelSeverityEnum is nil")
}
expectedSeverities := []string{"INFORM", "ALERT", "TAKEDOWN"}
for _, name := range expectedSeverities {
if !enumHasValue(LabelSeverityEnum, name) {
t.Errorf("Expected LabelSeverity value %s not found", name)
}
}
// Test LabelVisibilityEnum
if LabelVisibilityEnum == nil {
t.Error("LabelVisibilityEnum is nil")
}
expectedVisibilities := []string{"IGNORE", "SHOW", "WARN", "HIDE"}
for _, name := range expectedVisibilities {
if !enumHasValue(LabelVisibilityEnum, name) {
t.Errorf("Expected LabelVisibility value %s not found", name)
}
}
// Test ReportReasonTypeEnum
if ReportReasonTypeEnum == nil {
t.Error("ReportReasonTypeEnum is nil")
}
expectedReasons := []string{"SPAM", "VIOLATION", "MISLEADING", "SEXUAL", "RUDE", "OTHER"}
for _, name := range expectedReasons {
if !enumHasValue(ReportReasonTypeEnum, name) {
t.Errorf("Expected ReportReasonType value %s not found", name)
}
}
// Test ReportStatusEnum
if ReportStatusEnum == nil {
t.Error("ReportStatusEnum is nil")
}
expectedStatuses := []string{"PENDING", "RESOLVED", "DISMISSED"}
for _, name := range expectedStatuses {
if !enumHasValue(ReportStatusEnum, name) {
t.Errorf("Expected ReportStatus value %s not found", name)
}
}
// Test ReportActionEnum
if ReportActionEnum == nil {
t.Error("ReportActionEnum is nil")
}
expectedActions := []string{"APPLY_LABEL", "DISMISS"}
for _, name := range expectedActions {
if !enumHasValue(ReportActionEnum, name) {
t.Errorf("Expected ReportAction value %s not found", name)
}
}
}
func TestObjectTypes(t *testing.T) {
// Test that all object types are defined correctly
types := []struct {
name string
obj interface{}
}{
{"StatisticsType", StatisticsType},
{"CurrentSessionType", CurrentSessionType},
{"SettingsType", SettingsType},
{"ActivityBucketType", ActivityBucketType},
{"ActivityEntryType", ActivityEntryType},
{"LexiconType", LexiconType},
{"OAuthClientType", OAuthClientType},
{"LabelDefinitionType", LabelDefinitionType},
{"LabelPreferenceType", LabelPreferenceType},
{"LabelType", LabelType},
{"ReportType", ReportType},
{"PageInfoType", PageInfoType},
{"LabelEdgeType", LabelEdgeType},
{"LabelConnectionType", LabelConnectionType},
{"ReportEdgeType", ReportEdgeType},
{"ReportConnectionType", ReportConnectionType},
}
for _, tc := range types {
if tc.obj == nil {
t.Errorf("%s is nil", tc.name)
}
}
}
func TestStatisticsTypeFields(t *testing.T) {
expectedFields := []string{"recordCount", "actorCount", "lexiconCount"}
fields := StatisticsType.Fields()
for _, name := range expectedFields {
if fields[name] == nil {
t.Errorf("Expected field %s not found in StatisticsType", name)
}
}
}
func TestSettingsTypeFields(t *testing.T) {
expectedFields := []string{
"id",
"domainAuthority",
"adminDids",
"relayUrl",
"plcDirectoryUrl",
"jetstreamUrl",
"oauthSupportedScopes",
}
fields := SettingsType.Fields()
for _, name := range expectedFields {
if fields[name] == nil {
t.Errorf("Expected field %s not found in SettingsType", name)
}
}
}
func TestLabelTypeFields(t *testing.T) {
expectedFields := []string{"id", "src", "uri", "cid", "val", "neg", "cts", "exp"}
fields := LabelType.Fields()
for _, name := range expectedFields {
if fields[name] == nil {
t.Errorf("Expected field %s not found in LabelType", name)
}
}
}
func TestReportTypeFields(t *testing.T) {
expectedFields := []string{
"id",
"reporterDid",
"subjectUri",
"reasonType",
"reason",
"status",
"resolvedBy",
"resolvedAt",
"createdAt",
}
fields := ReportType.Fields()
for _, name := range expectedFields {
if fields[name] == nil {
t.Errorf("Expected field %s not found in ReportType", name)
}
}
}
func TestContextWithAuth(t *testing.T) {
ctx := context.Background()
// Add auth info
ctx = ContextWithAuth(ctx, "did:plc:user123", "user.handle", true, []string{"did:plc:admin1", "did:plc:admin2"})
// Verify values
userDID := ctx.Value(contextKeyUserDID).(string)
if userDID != "did:plc:user123" {
t.Errorf("Expected userDID to be 'did:plc:user123', got '%s'", userDID)
}
handle := ctx.Value(contextKeyHandle).(string)
if handle != "user.handle" {
t.Errorf("Expected handle to be 'user.handle', got '%s'", handle)
}
isAdmin := ctx.Value(contextKeyIsAdmin).(bool)
if !isAdmin {
t.Error("Expected isAdmin to be true")
}
adminDIDs := ctx.Value(contextKeyAdminDIDs).([]string)
if len(adminDIDs) != 2 {
t.Errorf("Expected 2 admin DIDs, got %d", len(adminDIDs))
}
}
func TestRequireAdmin(t *testing.T) {
// Test with admin context
adminCtx := ContextWithAuth(context.Background(), "did:plc:admin", "admin.handle", true, nil)
if err := requireAdmin(adminCtx); err != nil {
t.Errorf("Expected no error for admin, got %v", err)
}
// Test with non-admin context
userCtx := ContextWithAuth(context.Background(), "did:plc:user", "user.handle", false, nil)
if err := requireAdmin(userCtx); err == nil {
t.Error("Expected error for non-admin, got nil")
}
// Test with empty context
emptyCtx := context.Background()
if err := requireAdmin(emptyCtx); err == nil {
t.Error("Expected error for empty context, got nil")
}
}
func TestValidAPIKey(t *testing.T) {
tests := []struct {
name string
adminAPIKey string
authHeader string
want bool
}{
{
name: "no key configured allows all",
adminAPIKey: "",
authHeader: "",
want: true,
},
{
name: "valid key matches",
adminAPIKey: "secret123",
authHeader: "Bearer secret123",
want: true,
},
{
name: "wrong key rejected",
adminAPIKey: "secret123",
authHeader: "Bearer wrong",
want: false,
},
{
name: "missing auth header rejected",
adminAPIKey: "secret123",
authHeader: "",
want: false,
},
{
name: "non-Bearer scheme rejected",
adminAPIKey: "secret123",
authHeader: "Basic secret123",
want: false,
},
{
name: "Bearer prefix only rejected",
adminAPIKey: "secret123",
authHeader: "Bearer ",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := &Handler{adminAPIKey: tt.adminAPIKey}
req, _ := http.NewRequest("POST", "/admin/graphql", nil)
if tt.authHeader != "" {
req.Header.Set("Authorization", tt.authHeader)
}
if got := h.validAPIKey(req); got != tt.want {
t.Errorf("validAPIKey() = %v, want %v", got, tt.want)
}
})
}
}
func TestContextKeysAreUnique(t *testing.T) {
// Ensure context keys are unique
keys := []contextKey{
contextKeyUserDID,
contextKeyHandle,
contextKeyIsAdmin,
contextKeyAdminDIDs,
}
seen := make(map[contextKey]bool)
for _, key := range keys {
if seen[key] {
t.Errorf("Duplicate context key: %v", key)
}
seen[key] = true
}
}