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

Function TestSessionLogin

rest/session_test.go:239–276  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

237}
238
239func TestSessionLogin(t *testing.T) {
240 rt := NewRestTester(t, &RestTesterConfig{GuestEnabled: false})
241 defer rt.Close()
242
243 // ensure guest is actually disabled
244 resp := rt.SendRequest(http.MethodPut, "/db/doc", `{"hi": "there"}`)
245 RequireStatus(t, resp, http.StatusUnauthorized)
246
247 rt.CreateUser("pupshaw", []string{"*"})
248
249 resp = rt.SendRequest(http.MethodGet, "/db/_session", "")
250 RequireStatus(t, resp, http.StatusOK)
251
252 resp = rt.SendRequest(http.MethodPost, "/db/_session", `{"name":"pupshaw", "password":"letmein"}`)
253 RequireStatus(t, resp, http.StatusOK)
254 cookie := resp.Header().Get("Set-Cookie")
255 require.NotEmptyf(t, cookie, "Set-Cookie header not found in response: %v", resp)
256 t.Logf("Set-Cookie: %s", cookie)
257
258 headers := map[string]string{
259 "Cookie": cookie,
260 }
261 resp = rt.SendUserRequestWithHeaders(http.MethodGet, "/db/", "", headers, "", "")
262 RequireStatus(t, resp, http.StatusOK)
263
264 // invalid password
265 resp = rt.SendRequest(http.MethodPost, "/db/_session", `{"name":"pupshaw", "password":"incorrectpassword"}`)
266 RequireStatus(t, resp, http.StatusUnauthorized)
267 t.Logf("Set-Cookie: %s", resp.Header().Get("Set-Cookie"))
268 require.Emptyf(t, resp.Header().Get("Set-Cookie"), "Set-Cookie header should not be set in response: %v", resp)
269
270 // invalid cookie (no username available)
271 headers = map[string]string{
272 "Cookie": "SyncGatewaySession=123456abcdef; Path=/db; Expires=Sat, 17 Aug 2024 15:20:52 GMT",
273 }
274 resp = rt.SendUserRequestWithHeaders(http.MethodGet, "/db/", "", headers, "", "")
275 RequireStatus(t, resp, http.StatusUnauthorized)
276}
277
278func TestCustomCookieName(t *testing.T) {
279

Callers

nothing calls this directly

Calls 9

CloseMethod · 0.95
SendRequestMethod · 0.95
CreateUserMethod · 0.95
NewRestTesterFunction · 0.85
RequireStatusFunction · 0.85
HeaderMethod · 0.80
GetMethod · 0.45
LogfMethod · 0.45

Tested by

no test coverage detected