MCPcopy Index your code
hub / github.com/dnote/dnote / TestGuestOnly

Function TestGuestOnly

pkg/server/middleware/auth_test.go:29–62  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

27)
28
29func TestGuestOnly(t *testing.T) {
30 db := testutils.InitMemoryDB(t)
31
32 handler := func(w http.ResponseWriter, r *http.Request) {
33 w.WriteHeader(http.StatusOK)
34 }
35
36 server := httptest.NewServer(GuestOnly(db, handler))
37 defer server.Close()
38
39 t.Run("guest", func(t *testing.T) {
40 req := testutils.MakeReq(server.URL, "GET", "/", "")
41 res := testutils.HTTPDo(t, req)
42
43 assert.Equal(t, res.StatusCode, http.StatusOK, "status code mismatch")
44 })
45
46 t.Run("logged in", func(t *testing.T) {
47 user := testutils.SetupUserData(db, "user@test.com", "password123")
48 req := testutils.MakeReq(server.URL, "GET", "/", "")
49 res := testutils.HTTPAuthDo(t, db, req, user)
50
51 assert.Equal(t, res.StatusCode, http.StatusFound, "status code mismatch")
52 assert.Equal(t, res.Header.Get("Location"), "/", "location mismatch")
53 })
54
55 t.Run("error getting credential", func(t *testing.T) {
56 req := testutils.MakeReq(server.URL, "GET", "/", "")
57 req.Header.Set("Authorization", "InvalidFormat")
58 res := testutils.HTTPDo(t, req)
59
60 assert.Equal(t, res.StatusCode, http.StatusOK, "status code mismatch")
61 })
62}
63
64func TestAuth(t *testing.T) {
65 db := testutils.InitMemoryDB(t)

Callers

nothing calls this directly

Calls 9

InitMemoryDBFunction · 0.92
MakeReqFunction · 0.92
HTTPDoFunction · 0.92
EqualFunction · 0.92
SetupUserDataFunction · 0.92
HTTPAuthDoFunction · 0.92
GuestOnlyFunction · 0.85
WriteHeaderMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected