Skip to content

Commit 9161dd1

Browse files
committed
update tests and some comments
1 parent a8ad1a3 commit 9161dd1

File tree

2 files changed

+94
-12
lines changed

2 files changed

+94
-12
lines changed

src/data-connect/data-connect-api.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ export interface GraphqlOptions<Variables> {
5858
variables?: Variables;
5959

6060
/**
61-
* The name of the GraphQL operation. Required only if `query` contains multiple operations.
61+
* The name of the GraphQL operation.
62+
* Required for operations that interact with connectors, such as impersonateQuery and impersonateMutation.
63+
* Required for operations that interact with services, such as executeGraphql, if
64+
* `query` contains multiple operations.
6265
*/
6366
operationName?: string;
6467

test/unit/data-connect/data-connect-api-client-internal.spec.ts

Lines changed: 90 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ describe('DataConnectApiClient', () => {
234234

235235
describe('impersonateQuery', () => {
236236
const unauthenticatedOptions: GraphqlOptions<unknown> =
237-
{ operationName: 'operationName', impersonate: { unauthenticated: true } };
237+
{ operationName: 'unauthenticatedQuery', impersonate: { unauthenticated: true } };
238+
const authenticatedOptions: GraphqlOptions<unknown> =
239+
{ operationName: 'authenticatedQuery', impersonate: { authClaims: { sub: 'authenticated-UUID' } } };
238240

239241
it('should reject when no operationName is provided', () => {
240242
apiClient.impersonateQuery({ impersonate: { unauthenticated: true } })
@@ -300,7 +302,7 @@ describe('DataConnectApiClient', () => {
300302
.should.eventually.be.rejected.and.deep.include(expected);
301303
});
302304

303-
it('should resolve with the GraphQL response on success', () => {
305+
describe('should resolve with the GraphQL response on success', () => {
304306
interface UsersResponse {
305307
users: [
306308
user: {
@@ -310,11 +312,11 @@ describe('DataConnectApiClient', () => {
310312
}
311313
];
312314
}
313-
const stub = sandbox
314-
.stub(HttpClient.prototype, 'send')
315-
.resolves(utils.responseFrom(TEST_RESPONSE, 200));
316-
return apiClient.impersonateQuery<UsersResponse, unknown>(unauthenticatedOptions)
317-
.then((resp) => {
315+
it('for an unauthenticated request', () => {
316+
const stub = sandbox
317+
.stub(HttpClient.prototype, 'send')
318+
.resolves(utils.responseFrom(TEST_RESPONSE, 200));
319+
return apiClient.impersonateQuery<UsersResponse, unknown>(unauthenticatedOptions).then((resp) => {
318320
expect(resp.data.users).to.be.not.empty;
319321
expect(resp.data.users[0].name).to.be.not.undefined;
320322
expect(resp.data.users[0].address).to.be.not.undefined;
@@ -329,6 +331,27 @@ describe('DataConnectApiClient', () => {
329331
}
330332
});
331333
});
334+
});
335+
it('for an authenticated request', () => {
336+
const stub = sandbox
337+
.stub(HttpClient.prototype, 'send')
338+
.resolves(utils.responseFrom(TEST_RESPONSE, 200));
339+
return apiClient.impersonateQuery<UsersResponse, unknown>(authenticatedOptions).then((resp) => {
340+
expect(resp.data.users).to.be.not.empty;
341+
expect(resp.data.users[0].name).to.be.not.undefined;
342+
expect(resp.data.users[0].address).to.be.not.undefined;
343+
expect(resp.data.users).to.deep.equal(TEST_RESPONSE.data.users);
344+
expect(stub).to.have.been.calledOnce.and.calledWith({
345+
method: 'POST',
346+
url: `https://firebasedataconnect.googleapis.com/v1alpha/projects/test-project/locations/${connectorConfig.location}/services/${connectorConfig.serviceId}/connectors/${connectorConfig.connector}:impersonateQuery`,
347+
headers: EXPECTED_HEADERS,
348+
data: {
349+
operationName: authenticatedOptions.operationName,
350+
extensions: { impersonate: authenticatedOptions.impersonate }
351+
}
352+
});
353+
});
354+
});
332355
});
333356

334357
it('should use DATA_CONNECT_EMULATOR_HOST if set', () => {
@@ -351,10 +374,12 @@ describe('DataConnectApiClient', () => {
351374
});
352375
});
353376

354-
describe('impersonateMutation', () => {
355-
const unauthenticatedOptions: GraphqlOptions<unknown> =
356-
{ operationName: 'operationName', impersonate: { unauthenticated: true } };
377+
const unauthenticatedOptions: GraphqlOptions<unknown> =
378+
{ operationName: 'operationName', impersonate: { unauthenticated: true } };
379+
const authenticatedOptions: GraphqlOptions<unknown> =
380+
{ operationName: 'operationName', impersonate: { unauthenticated: true } };
357381

382+
describe('impersonateMutation', () => {
358383
it('should reject when no operationName is provided', () => {
359384
apiClient.impersonateMutation({ impersonate: { unauthenticated: true } })
360385
.should.eventually.be.rejectedWith('`query` must be a non-empty string.');
@@ -419,7 +444,61 @@ describe('DataConnectApiClient', () => {
419444
.should.eventually.be.rejected.and.deep.include(expected);
420445
});
421446

422-
it('should resolve with the GraphQL response on success', () => {
447+
describe('should resolve with the GraphQL response on success', () => {
448+
interface UsersResponse {
449+
users: [
450+
user: {
451+
id: string;
452+
name: string;
453+
address: string;
454+
}
455+
];
456+
}
457+
it('for an unauthenticated request', () => {
458+
const stub = sandbox
459+
.stub(HttpClient.prototype, 'send')
460+
.resolves(utils.responseFrom(TEST_RESPONSE, 200));
461+
return apiClient.impersonateMutation<UsersResponse, unknown>(unauthenticatedOptions)
462+
.then((resp) => {
463+
expect(resp.data.users).to.be.not.empty;
464+
expect(resp.data.users[0].name).to.be.not.undefined;
465+
expect(resp.data.users[0].address).to.be.not.undefined;
466+
expect(resp.data.users).to.deep.equal(TEST_RESPONSE.data.users);
467+
expect(stub).to.have.been.calledOnce.and.calledWith({
468+
method: 'POST',
469+
url: `https://firebasedataconnect.googleapis.com/v1alpha/projects/test-project/locations/${connectorConfig.location}/services/${connectorConfig.serviceId}/connectors/${connectorConfig.connector}:impersonateMutation`,
470+
headers: EXPECTED_HEADERS,
471+
data: {
472+
operationName: unauthenticatedOptions.operationName,
473+
extensions: { impersonate: unauthenticatedOptions.impersonate }
474+
}
475+
});
476+
});
477+
});
478+
it('for an authenticated request', () => {
479+
const stub = sandbox
480+
.stub(HttpClient.prototype, 'send')
481+
.resolves(utils.responseFrom(TEST_RESPONSE, 200));
482+
return apiClient.impersonateMutation<UsersResponse, unknown>(authenticatedOptions)
483+
.then((resp) => {
484+
expect(resp.data.users).to.be.not.empty;
485+
expect(resp.data.users[0].name).to.be.not.undefined;
486+
expect(resp.data.users[0].address).to.be.not.undefined;
487+
expect(resp.data.users).to.deep.equal(TEST_RESPONSE.data.users);
488+
expect(stub).to.have.been.calledOnce.and.calledWith({
489+
method: 'POST',
490+
url: `https://firebasedataconnect.googleapis.com/v1alpha/projects/test-project/locations/${connectorConfig.location}/services/${connectorConfig.serviceId}/connectors/${connectorConfig.connector}:impersonateMutation`,
491+
headers: EXPECTED_HEADERS,
492+
data: {
493+
operationName: authenticatedOptions.operationName,
494+
extensions: { impersonate: authenticatedOptions.impersonate }
495+
}
496+
});
497+
});
498+
});
499+
});
500+
501+
it('should resolve with the GraphQL response on success for an authenticated request', () => {
423502
interface UsersResponse {
424503
users: [
425504
user: {

0 commit comments

Comments
 (0)