...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package couchserver
16
17 import (
18 "context"
19 "net/http/httptest"
20 "testing"
21
22 "gitlab.com/flimzy/testy"
23
24 "github.com/go-kivik/kivik/v4/x/kivikd/auth"
25 "github.com/go-kivik/kivik/v4/x/kivikd/authdb"
26 )
27
28 type testKey struct {
29 string
30 }
31
32 func TestGetSession(t *testing.T) {
33 key := &testKey{"key"}
34 h := Handler{
35 SessionKey: key,
36 }
37 req := httptest.NewRequest("GET", "/_session", nil)
38 session := &auth.Session{
39 AuthMethod: "magic",
40 AuthDB: "_users",
41 User: &authdb.UserContext{
42 Name: "bob",
43 },
44 }
45 req = req.WithContext(context.WithValue(req.Context(), key, &session))
46 w := httptest.NewRecorder()
47 handler := h.GetSession()
48 handler(w, req)
49 expected := map[string]interface{}{
50 "info": map[string]interface{}{
51 "authenticated": "magic",
52 "authentication_db": "_users",
53 "authentication_handlers": nil,
54 },
55 "ok": true,
56 "userCtx": map[string]interface{}{
57 "name": "bob",
58 "roles": []string{},
59 },
60 }
61 resp := w.Result()
62 defer resp.Body.Close()
63 if d := testy.DiffAsJSON(expected, resp.Body); d != nil {
64 t.Error(d)
65 }
66 }
67
View as plain text