-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_test.go
More file actions
331 lines (287 loc) · 8.33 KB
/
app_test.go
File metadata and controls
331 lines (287 loc) · 8.33 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package app
import (
"context"
"log/slog"
"os"
"testing"
"github.com/cruxstack/github-ops-app/internal/config"
"github.com/cruxstack/github-ops-app/internal/github/client"
"github.com/cruxstack/github-ops-app/internal/okta"
)
func TestHandleSlackTest_NotConfigured(t *testing.T) {
app := &App{
Config: &config.Config{},
Logger: slog.New(slog.NewTextHandler(os.Stderr, nil)),
Notifier: nil,
}
err := app.handleSlackTest(context.Background())
if err == nil {
t.Error("expected error when slack is not configured")
}
if err.Error() != "slack is not configured" {
t.Errorf("expected 'slack is not configured' error, got: %v", err)
}
}
func TestFakePRComplianceResult(t *testing.T) {
result := fakePRComplianceResult()
if result == nil {
t.Fatal("expected non-nil result")
}
if result.PR == nil {
t.Fatal("expected non-nil PR")
}
if result.PR.Number == nil || *result.PR.Number != 42 {
t.Error("expected PR number to be 42")
}
if result.PR.Title == nil || *result.PR.Title != "Add new authentication feature" {
t.Error("expected PR title to be 'Add new authentication feature'")
}
if result.BaseBranch != "main" {
t.Errorf("expected base branch 'main', got %q", result.BaseBranch)
}
if !result.UserHasBypass {
t.Error("expected UserHasBypass to be true")
}
if result.UserBypassReason != "repository admin" {
t.Errorf("expected bypass reason 'repository admin', got %q", result.UserBypassReason)
}
if len(result.Violations) != 2 {
t.Errorf("expected 2 violations, got %d", len(result.Violations))
}
// verify it passes the WasBypassed check (used by notifier)
if !result.WasBypassed() {
t.Error("expected WasBypassed() to return true")
}
}
func TestFakeOktaSyncReports(t *testing.T) {
reports := fakeOktaSyncReports()
if len(reports) != 3 {
t.Fatalf("expected 3 reports, got %d", len(reports))
}
// first report: has changes
if !reports[0].HasChanges() {
t.Error("expected first report to have changes")
}
if len(reports[0].MembersAdded) != 2 {
t.Errorf("expected 2 members added, got %d", len(reports[0].MembersAdded))
}
if len(reports[0].MembersRemoved) != 1 {
t.Errorf("expected 1 member removed, got %d", len(reports[0].MembersRemoved))
}
// second report: no changes
if reports[1].HasChanges() {
t.Error("expected second report to have no changes")
}
// third report: has changes, errors, and skipped members
if !reports[2].HasChanges() {
t.Error("expected third report to have changes")
}
if !reports[2].HasErrors() {
t.Error("expected third report to have errors")
}
if len(reports[2].MembersSkippedExternal) != 1 {
t.Errorf("expected 1 skipped external member, got %d",
len(reports[2].MembersSkippedExternal))
}
if len(reports[2].MembersSkippedNoGHUsername) != 1 {
t.Errorf("expected 1 skipped member without GH username, got %d",
len(reports[2].MembersSkippedNoGHUsername))
}
}
func TestFakeOrphanedUsersReport(t *testing.T) {
report := fakeOrphanedUsersReport()
if report == nil {
t.Fatal("expected non-nil report")
}
if len(report.OrphanedUsers) != 3 {
t.Errorf("expected 3 orphaned users, got %d", len(report.OrphanedUsers))
}
expectedUsers := []string{"orphan-user-1", "orphan-user-2", "legacy-bot"}
for i, user := range report.OrphanedUsers {
if user != expectedUsers[i] {
t.Errorf("expected user %q at index %d, got %q", expectedUsers[i], i, user)
}
}
}
func TestProcessScheduledEvent_SlackTest(t *testing.T) {
app := &App{
Config: &config.Config{},
Logger: slog.New(slog.NewTextHandler(os.Stderr, nil)),
Notifier: nil,
}
evt := ScheduledEvent{Action: "slack-test"}
err := app.ProcessScheduledEvent(context.Background(), evt)
// should fail because slack is not configured
if err == nil {
t.Error("expected error when slack is not configured")
}
}
func TestProcessScheduledEvent_UnknownAction(t *testing.T) {
app := &App{
Config: &config.Config{},
Logger: slog.New(slog.NewTextHandler(os.Stderr, nil)),
}
evt := ScheduledEvent{Action: "unknown-action"}
err := app.ProcessScheduledEvent(context.Background(), evt)
if err == nil {
t.Error("expected error for unknown action")
}
}
// verify fake data types match expected interfaces
func TestFakeDataTypes(t *testing.T) {
// ensure fake PR result is compatible with notifier
var _ *client.PRComplianceResult = fakePRComplianceResult()
// ensure fake sync reports are compatible with notifier
var _ []*okta.SyncReport = fakeOktaSyncReports()
// ensure fake orphaned users report is compatible with notifier
var _ *okta.OrphanedUsersReport = fakeOrphanedUsersReport()
}
func TestCheckAdminAuth(t *testing.T) {
tests := []struct {
name string
adminToken string
authHeader string
expectError bool
}{
{
name: "no token configured, no header",
adminToken: "",
authHeader: "",
expectError: false,
},
{
name: "no token configured, with header",
adminToken: "",
authHeader: "Bearer some-token",
expectError: false,
},
{
name: "token configured, no header",
adminToken: "secret-token",
authHeader: "",
expectError: true,
},
{
name: "token configured, wrong token",
adminToken: "secret-token",
authHeader: "Bearer wrong-token",
expectError: true,
},
{
name: "token configured, correct token",
adminToken: "secret-token",
authHeader: "Bearer secret-token",
expectError: false,
},
{
name: "token configured, lowercase bearer",
adminToken: "secret-token",
authHeader: "bearer secret-token",
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
app := &App{
Config: &config.Config{AdminToken: tt.adminToken},
Logger: slog.New(slog.NewTextHandler(os.Stderr, nil)),
}
headers := map[string]string{}
if tt.authHeader != "" {
headers["authorization"] = tt.authHeader
}
req := Request{Headers: headers}
resp := app.checkAdminAuth(req)
if tt.expectError && resp == nil {
t.Error("expected error response, got nil")
}
if !tt.expectError && resp != nil {
t.Errorf("expected no error, got status %d", resp.StatusCode)
}
if tt.expectError && resp != nil && resp.StatusCode != 401 {
t.Errorf("expected status 401, got %d", resp.StatusCode)
}
})
}
}
func TestHandleRequest_AdminAuthOnProtectedEndpoints(t *testing.T) {
tests := []struct {
name string
path string
method string
adminToken string
authHeader string
expectedStatus int
}{
{
name: "status endpoint, no token configured",
path: "/server/status",
method: "GET",
adminToken: "",
authHeader: "",
expectedStatus: 200,
},
{
name: "status endpoint, token required, missing",
path: "/server/status",
method: "GET",
adminToken: "secret",
authHeader: "",
expectedStatus: 401,
},
{
name: "status endpoint, token required, correct",
path: "/server/status",
method: "GET",
adminToken: "secret",
authHeader: "Bearer secret",
expectedStatus: 200,
},
{
name: "config endpoint, token required, missing",
path: "/server/config",
method: "GET",
adminToken: "secret",
authHeader: "",
expectedStatus: 401,
},
{
name: "config endpoint, token required, correct",
path: "/server/config",
method: "GET",
adminToken: "secret",
authHeader: "Bearer secret",
expectedStatus: 200,
},
{
name: "scheduled endpoint, token required, missing",
path: "/scheduled/slack-test",
method: "POST",
adminToken: "secret",
authHeader: "",
expectedStatus: 401,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
app := &App{
Config: &config.Config{AdminToken: tt.adminToken},
Logger: slog.New(slog.NewTextHandler(os.Stderr, nil)),
}
headers := map[string]string{}
if tt.authHeader != "" {
headers["authorization"] = tt.authHeader
}
req := Request{
Type: RequestTypeHTTP,
Method: tt.method,
Path: tt.path,
Headers: headers,
}
resp := app.HandleRequest(context.Background(), req)
if resp.StatusCode != tt.expectedStatus {
t.Errorf("expected status %d, got %d", tt.expectedStatus, resp.StatusCode)
}
})
}
}