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

Function TestOneTimeSessionBlipSyncAuthentication

rest/blip_sync_test.go:81–193  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

79}
80
81func TestOneTimeSessionBlipSyncAuthentication(t *testing.T) {
82 rt := NewRestTesterPersistentConfig(t)
83 defer rt.Close()
84
85 const username = "alice"
86 rt.CreateUser(username, []string{"*"})
87
88 resp := rt.SendUserRequest(http.MethodPost, "/{{.db}}/_session?one_time=true", "", username)
89 RequireStatus(t, resp, http.StatusOK)
90
91 var sessionResp struct {
92 SessionID string `json:"one_time_session_id"`
93 }
94
95 require.NoError(t, base.JSONUnmarshal(resp.BodyBytes(), &sessionResp))
96
97 require.NotEmpty(t, sessionResp.SessionID, "Expected non-empty session ID for %s", resp.BodyString())
98
99 resp = rt.SendRequestWithHeaders(http.MethodGet, "/{{.db}}/_blipsync", "", nil)
100 RequireStatus(t, resp, http.StatusUnauthorized)
101
102 resp = rt.SendUserRequest(http.MethodGet, "/{{.db}}/_blipsync", "", username)
103 RequireStatus(t, resp, http.StatusUpgradeRequired)
104
105 // no header should show database not found
106 resp = rt.SendRequestWithHeaders(http.MethodGet, "/{{.db}}/_blipsync", "", nil)
107 RequireStatus(t, resp, http.StatusUnauthorized)
108
109 // invalid token is header not known
110 resp = rt.SendRequestWithHeaders(http.MethodGet, "/{{.db}}/_blipsync", "", map[string]string{
111 secWebSocketProtocolHeader: blipSessionIDPrefix + "badtoken",
112 })
113 RequireStatus(t, resp, http.StatusUnauthorized)
114
115 // first request will succeed
116 resp = rt.SendRequestWithHeaders(http.MethodGet, "/{{.db}}/_blipsync", "", map[string]string{
117 secWebSocketProtocolHeader: blipSessionIDPrefix + sessionResp.SessionID,
118 })
119 RequireStatus(t, resp, http.StatusUpgradeRequired)
120
121 // one time token is expired
122 resp = rt.SendRequestWithHeaders(http.MethodGet, "/{{.db}}/_blipsync", "", map[string]string{
123 secWebSocketProtocolHeader: blipSessionIDPrefix + sessionResp.SessionID,
124 })
125 RequireStatus(t, resp, http.StatusUnauthorized)
126
127 srv := httptest.NewServer(rt.TestPublicHandler())
128 defer srv.Close()
129
130 // Construct URL to connect to blipsync target endpoint
131 destURL := fmt.Sprintf("%s/%s/_blipsync", srv.URL, rt.GetDatabase().Name)
132 u, err := url.Parse(destURL)
133 require.NoError(t, err)
134 u.Scheme = "ws"
135
136 blipProtocolPrefix := "BLIP_3+"
137 testCases := []struct {
138 name string

Callers

nothing calls this directly

Calls 14

JSONUnmarshalFunction · 0.92
RequireStatusFunction · 0.85
CreateUserMethod · 0.80
SendUserRequestMethod · 0.80
BodyStringMethod · 0.80
TestPublicHandlerMethod · 0.80
SubprotocolStringMethod · 0.80
CloseMethod · 0.65
RunMethod · 0.65
ContextMethod · 0.65

Tested by

no test coverage detected