MCPcopy Create free account
hub / github.com/GoMudEngine/GoMud / TestDoBasicAuthRequiresAdminRole

Function TestDoBasicAuthRequiresAdminRole

internal/web/auth_test.go:19–76  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

17)
18
19func TestDoBasicAuthRequiresAdminRole(t *testing.T) {
20 const password = "correct-password"
21
22 setupAuthTestUsers(t, password, map[string]string{
23 "adminuser": users.RoleAdmin,
24 "moduser": users.RoleMod,
25 "normaluser": users.RoleUser,
26 })
27
28 tests := []struct {
29 name string
30 username string
31 wantStatus int
32 wantCalled bool
33 }{
34 {
35 name: "admin accepted",
36 username: "adminuser",
37 wantStatus: http.StatusOK,
38 wantCalled: true,
39 },
40 {
41 name: "mod accepted",
42 username: "moduser",
43 wantStatus: http.StatusOK,
44 wantCalled: true,
45 },
46 {
47 name: "user rejected",
48 username: "normaluser",
49 wantStatus: http.StatusUnauthorized,
50 },
51 }
52
53 for _, tt := range tests {
54 t.Run(tt.name, func(t *testing.T) {
55 resetAuthStateForTest()
56 called := false
57 handler := doBasicAuth(func(w http.ResponseWriter, r *http.Request) {
58 called = true
59 w.WriteHeader(http.StatusOK)
60 })
61
62 req := httptest.NewRequest(http.MethodGet, "/admin/", nil)
63 req.SetBasicAuth(tt.username, password)
64 rec := httptest.NewRecorder()
65
66 handler(rec, req)
67
68 if rec.Code != tt.wantStatus {
69 t.Fatalf("status = %d, want %d", rec.Code, tt.wantStatus)
70 }
71 if called != tt.wantCalled {
72 t.Fatalf("handler called = %t, want %t", called, tt.wantCalled)
73 }
74 })
75 }
76}

Callers

nothing calls this directly

Calls 3

setupAuthTestUsersFunction · 0.85
resetAuthStateForTestFunction · 0.85
doBasicAuthFunction · 0.85

Tested by

no test coverage detected