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

Function TestUserAPIReadOnlyFields

rest/oidc_api_test.go:1222–1281  ·  view source on GitHub ↗

TestUserAPIReadOnlyFields: - Test regression behaviour found in CBG-3641 - Create OIDC config and token to grant a user some channel and roles - Retrieve the user and use this output in PUT request again t the same user, assert no error - Fetch user again and assert that the user config is unchanged

(t *testing.T)

Source from the content-addressed store, hash-verified

1220// - Change the fetched config to omit one of the read only fields and use this as payload against a new PUT to the same user
1221// - Assert that the update doesn't error and user config is unchanged
1222func TestUserAPIReadOnlyFields(t *testing.T) {
1223
1224 testProviders := auth.OIDCProviderMap{
1225 "foo": mockProviderWith("foo", mockProviderUserPrefix{"foo"}, mockProviderChannelsClaim{"channels"}, mockProviderRolesClaim{"roles"}),
1226 }
1227 defaultProvider := "foo"
1228
1229 mockAuthServer, err := newMockAuthServer()
1230 require.NoError(t, err, "Error creating mock oauth2 server")
1231 mockAuthServer.Start()
1232 defer mockAuthServer.Shutdown()
1233 mockAuthServer.options.issuer = mockAuthServer.URL + "/" + defaultProvider
1234 refreshProviderConfig(testProviders, mockAuthServer.URL)
1235
1236 opts := auth.OIDCOptions{Providers: testProviders, DefaultProvider: &defaultProvider}
1237 restTesterConfig := RestTesterConfig{SyncFn: channels.DocChannelsSyncFunction, DatabaseConfig: &DatabaseConfig{DbConfig: DbConfig{OIDCConfig: &opts}}}
1238
1239 // JWT claim based grants do not support named collections
1240 restTester := NewRestTesterDefaultCollection(t, &restTesterConfig)
1241 defer restTester.Close()
1242
1243 createUser(t, restTester, "foo_noah")
1244
1245 token, err := mockAuthServer.makeToken(claimsAuthenticWithExtraClaims(map[string]interface{}{"channels": []string{"foo"}, "roles": []string{"fooRole"}}))
1246 require.NoError(t, err, "Error obtaining signed token from OpenID Connect provider")
1247 require.NotEmpty(t, token, "Empty token retrieved from OpenID Connect provider")
1248
1249 // use token
1250 resp := restTester.SendRequestWithHeaders(http.MethodPut, "/{{.keyspace}}/doc1", `{"channels":"foo"}`, map[string]string{"Authorization": BearerToken + " " + token})
1251 RequireStatus(t, resp, http.StatusCreated)
1252
1253 // Get user to provide payload for a PUT request
1254 resp = restTester.SendAdminRequest(http.MethodGet, "/{{.db}}/_user/foo_noah", "")
1255 RequireStatus(t, resp, http.StatusOK)
1256 userOutput := resp.Body.String()
1257
1258 resp = restTester.SendAdminRequest(http.MethodPut, "/{{.db}}/_user/foo_noah", userOutput)
1259 RequireStatus(t, resp, http.StatusOK)
1260
1261 // Get user again and unmarshal output
1262 resp = restTester.SendAdminRequest(http.MethodGet, "/{{.db}}/_user/foo_noah", "")
1263 RequireStatus(t, resp, http.StatusOK)
1264 assert.Equal(t, userOutput, resp.Body.String())
1265
1266 var newInfo auth.PrincipalConfig
1267 require.NoError(t, base.JSONUnmarshal(resp.Body.Bytes(), &newInfo))
1268
1269 // omit one of the read only fields to ensure PUT request still works even without all read only fields specified
1270 newInfo.JWTIssuer = nil
1271 updatedRequest, err := base.JSONMarshal(&newInfo)
1272 require.NoError(t, err)
1273
1274 resp = restTester.SendAdminRequest(http.MethodPut, "/{{.db}}/_user/foo_noah", string(updatedRequest))
1275 RequireStatus(t, resp, http.StatusOK)
1276
1277 // assert that the user out put remains unchanged
1278 resp = restTester.SendAdminRequest(http.MethodGet, "/{{.db}}/_user/foo_noah", "")
1279 RequireStatus(t, resp, http.StatusOK)

Callers

nothing calls this directly

Calls 15

JSONUnmarshalFunction · 0.92
JSONMarshalFunction · 0.92
mockProviderWithFunction · 0.85
newMockAuthServerFunction · 0.85
refreshProviderConfigFunction · 0.85
RequireStatusFunction · 0.85
ShutdownMethod · 0.80
makeTokenMethod · 0.80
SendAdminRequestMethod · 0.80

Tested by

no test coverage detected