(t *testing.T)
| 237 | } |
| 238 | |
| 239 | func 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 | |
| 278 | func TestCustomCookieName(t *testing.T) { |
| 279 |
nothing calls this directly
no test coverage detected