github.com/go-kivik/kivik/v4@v4.3.2/x/kivikd/couchserver/session_test.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 //go:build !js 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 }