MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / TestDisablePublicBasicAuth

Function TestDisablePublicBasicAuth

rest/api_test.go:163–219  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

161}
162
163func TestDisablePublicBasicAuth(t *testing.T) {
164 rt := NewRestTester(t, &RestTesterConfig{
165 DatabaseConfig: &DatabaseConfig{
166 DbConfig: DbConfig{
167 DisablePasswordAuth: base.Ptr(true),
168 Guest: &auth.PrincipalConfig{
169 Disabled: base.Ptr(true),
170 },
171 },
172 },
173 })
174 defer rt.Close()
175 ctx := rt.Context()
176
177 response := rt.SendRequest(http.MethodGet, "/{{.db}}/", "")
178 RequireStatus(t, response, http.StatusUnauthorized)
179 require.Contains(t, response.Body.String(), ErrLoginRequired.Message)
180 assert.NotContains(t, response.Header(), "WWW-Authenticate", "expected to not receive a WWW-Auth header when password auth is disabled")
181
182 response = rt.SendRequest(http.MethodGet, "/notadb/", "")
183 RequireStatus(t, response, http.StatusUnauthorized)
184 require.Contains(t, response.Body.String(), ErrLoginRequired.Message)
185 assert.NotContains(t, response.Header(), "WWW-Authenticate", "expected to not receive a WWW-Auth header when password auth is disabled")
186
187 // Double-check that even if we provide valid credentials we still won't be let in
188 a := rt.ServerContext().Database(ctx, "db").Authenticator(ctx)
189 user, err := a.NewUser("user1", "letmein", channels.BaseSetOf(t, "foo"))
190 assert.NoError(t, err)
191 assert.NoError(t, a.Save(user))
192
193 response = rt.SendUserRequest(http.MethodGet, "/{{.db}}/", "", "user1")
194 RequireStatus(t, response, http.StatusUnauthorized)
195 require.Contains(t, response.Body.String(), ErrInvalidLogin.Message)
196 assert.NotContains(t, response.Header(), "WWW-Authenticate", "expected to not receive a WWW-Auth header when password auth is disabled")
197
198 response = rt.SendUserRequest(http.MethodGet, "/notadb/", "", "user1")
199 RequireStatus(t, response, http.StatusUnauthorized)
200 require.Contains(t, response.Body.String(), ErrInvalidLogin.Message)
201 assert.NotContains(t, response.Header(), "WWW-Authenticate", "expected to not receive a WWW-Auth header when password auth is disabled")
202
203 // Also check that we can't create a session through POST /db/_session
204 response = rt.SendRequest(http.MethodPost, "/{{.db}}/_session", `{"name":"user1","password":"letmein"}`)
205 RequireStatus(t, response, http.StatusUnauthorized)
206 require.Contains(t, response.Body.String(), ErrLoginRequired.Message)
207
208 response = rt.SendRequest(http.MethodPost, "/notadb/_session", `{"name":"user1","password":"letmein"}`)
209 RequireStatus(t, response, http.StatusUnauthorized)
210 require.Contains(t, response.Body.String(), ErrLoginRequired.Message)
211
212 // As a sanity check, ensure it does work when the setting is disabled
213 rt.ServerContext().Database(ctx, "db").Options.DisablePasswordAuthentication = false
214 response = rt.SendUserRequest(http.MethodGet, "/{{.db}}/", "", "user1")
215 RequireStatus(t, response, http.StatusOK)
216
217 response = rt.SendRequest(http.MethodPost, "/{{.db}}/_session", `{"name":"user1","password":"letmein"}`)
218 RequireStatus(t, response, http.StatusOK)
219}
220

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
ContextMethod · 0.95
SendRequestMethod · 0.95
ServerContextMethod · 0.95
SendUserRequestMethod · 0.95
PtrFunction · 0.92
BaseSetOfFunction · 0.92
NewRestTesterFunction · 0.85
RequireStatusFunction · 0.85
HeaderMethod · 0.80
NewUserMethod · 0.80
SaveMethod · 0.80

Tested by

no test coverage detected