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

Function TestUserWithoutSessionUUID

rest/session_test.go:665–713  ·  view source on GitHub ↗

TestUserWithoutSessionUUID tests users that existed before we stamped SessionUUID into user docs

(t *testing.T)

Source from the content-addressed store, hash-verified

663
664// TestUserWithoutSessionUUID tests users that existed before we stamped SessionUUID into user docs
665func TestUserWithoutSessionUUID(t *testing.T) {
666 rt := NewRestTester(t, nil)
667 defer rt.Close()
668
669 const username = "user1"
670
671 authenticator := rt.GetDatabase().Authenticator(base.TestCtx(t))
672 user, err := authenticator.NewUser(username, RestTesterDefaultUserPassword, base.SetOf("*"))
673 require.NoError(t, err)
674 require.NotNil(t, user)
675 require.NoError(t, authenticator.Save(user))
676
677 var rawUser map[string]interface{}
678 _, err = rt.MetadataStore().Get(authenticator.DocIDForUser(user.Name()), &rawUser)
679 require.NoError(t, err)
680
681 sessionUUIDKey := "session_uuid"
682 _, exists := rawUser[sessionUUIDKey]
683 require.True(t, exists)
684 delete(rawUser, sessionUUIDKey)
685
686 err = rt.MetadataStore().Set(authenticator.DocIDForUser(user.Name()), 0, nil, rawUser)
687 require.NoError(t, err)
688
689 response := rt.SendUserRequest(http.MethodPost, "/{{.db}}/_session", "", username)
690 RequireStatus(t, response, http.StatusOK)
691
692 RequireStatus(t, response, http.StatusOK)
693 cookie := response.Header().Get("Set-Cookie")
694 require.NotEqual(t, "", cookie)
695
696 // Create a doc as the first user, with session auth, channel-restricted to first user
697 cookieHeaders := map[string]string{
698 "Cookie": cookie,
699 }
700 response = rt.SendRequestWithHeaders(http.MethodPut, "/{{.keyspace}}/doc1", `{"hi": "there"}`, cookieHeaders)
701 RequireStatus(t, response, http.StatusCreated)
702
703 response = rt.SendRequestWithHeaders(http.MethodGet, "/{{.keyspace}}/doc1", "", cookieHeaders)
704 RequireStatus(t, response, http.StatusOK)
705
706 // delete all user sesssions
707 response = rt.SendAdminRequest("DELETE", "/db/_user/user1/_session", "")
708 RequireStatus(t, response, http.StatusOK)
709
710 response = rt.SendRequestWithHeaders(http.MethodGet, "/{{.keyspace}}/doc1", "", cookieHeaders)
711 RequireStatus(t, response, http.StatusUnauthorized)
712 require.Contains(t, response.Body.String(), "Session no longer valid")
713}
714
715func createSession(t *testing.T, rt *RestTester, username string) string {
716

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
GetDatabaseMethod · 0.95
MetadataStoreMethod · 0.95
SendUserRequestMethod · 0.95
SendAdminRequestMethod · 0.95
TestCtxFunction · 0.92
SetOfFunction · 0.92
NewRestTesterFunction · 0.85
RequireStatusFunction · 0.85
NewUserMethod · 0.80
SaveMethod · 0.80

Tested by

no test coverage detected