github.com/go-kivik/kivik/v4@v4.3.2/couchdb/auth_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 package couchdb 14 15 import ( 16 "context" 17 "net/http" 18 "testing" 19 20 "gitlab.com/flimzy/testy" 21 22 kivik "github.com/go-kivik/kivik/v4" 23 internal "github.com/go-kivik/kivik/v4/int/errors" 24 "github.com/go-kivik/kivik/v4/internal/nettest" 25 ) 26 27 func TestAuthenticationOptions(t *testing.T) { 28 type test struct { 29 handler func(*testing.T) http.Handler 30 setup func(*testing.T, *client) 31 options kivik.Option 32 status int 33 err string 34 } 35 36 tests := testy.NewTable() 37 tests.Add("BasicAuth", test{ 38 handler: func(t *testing.T) http.Handler { //nolint:thelper // Not a helper 39 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 40 if h := r.Header.Get("Authorization"); h != "Basic Ym9iOmFiYzEyMw==" { 41 t.Errorf("Unexpected Auth header: %s\n", h) 42 } 43 w.WriteHeader(200) 44 _, _ = w.Write([]byte(`{}`)) 45 }) 46 }, 47 options: BasicAuth("bob", "abc123"), 48 }) 49 tests.Add("CookieAuth", test{ 50 handler: func(t *testing.T) http.Handler { //nolint:thelper // Not a helper 51 expectedPaths := []string{"/_session", "/"} 52 i := -1 53 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 54 i++ 55 if p := r.URL.Path; p != expectedPaths[i] { 56 t.Errorf("Unexpected path: %s\n", p) 57 } 58 w.WriteHeader(200) 59 _, _ = w.Write([]byte(`{}`)) 60 }) 61 }, 62 options: CookieAuth("bob", "abc123"), 63 }) 64 tests.Add("ProxyAuth", test{ 65 handler: func(t *testing.T) http.Handler { //nolint:thelper // Not a helper 66 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 67 if h := r.Header.Get("X-Auth-CouchDB-UserName"); h != "bob" { 68 t.Errorf("Unexpected X-Auth-CouchDB-UserName header: %s", h) 69 } 70 if h := r.Header.Get("X-Auth-CouchDB-Roles"); h != "users,admins" { 71 t.Errorf("Unexpected X-Auth-CouchDB-Roles header: %s", h) 72 } 73 if h := r.Header.Get("Moo"); h != "adedb8d002eb53a52faba80e82cb1fc6d57bca74" { 74 t.Errorf("Token header override failed: %s instead of 'adedb8d002eb53a52faba80e82cb1fc6d57bca74'", h) 75 } 76 w.WriteHeader(200) 77 _, _ = w.Write([]byte(`{}`)) 78 }) 79 }, 80 options: ProxyAuth( 81 "bob", 82 "abc123", 83 []string{"users", "admins"}, 84 map[string]string{"X-Auth-CouchDB-Token": "moo"}, 85 ), 86 }) 87 tests.Add("JWTAuth", test{ 88 handler: func(t *testing.T) http.Handler { //nolint:thelper // Not a helper 89 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 90 if h := r.Header.Get("Authorization"); h != "Bearer tokentoken" { 91 t.Errorf("Unexpected Auth header: %s\n", h) 92 } 93 w.WriteHeader(200) 94 _, _ = w.Write([]byte(`{}`)) 95 }) 96 }, 97 options: JWTAuth("tokentoken"), 98 }) 99 100 driver := &couch{} 101 tests.Run(t, func(t *testing.T, tt test) { 102 s := nettest.NewHTTPTestServer(t, tt.handler(t)) 103 t.Cleanup(s.Close) 104 driverClient, err := driver.NewClient(s.URL, tt.options) 105 if err != nil { 106 t.Fatal(err) 107 } 108 client := driverClient.(*client) 109 if tt.setup != nil { 110 tt.setup(t, client) 111 } 112 _, err = client.Version(context.Background()) 113 if d := internal.StatusErrorDiffRE(tt.err, tt.status, err); d != "" { 114 t.Error(d) 115 } 116 }) 117 }