Skip to content

Commit d03fc34

Browse files
committed
Addressing review comments and adding some Unit test
1 parent 41ad562 commit d03fc34

File tree

7 files changed

+108
-17
lines changed

7 files changed

+108
-17
lines changed

packages/auth/src/api/authentication/exchange_token.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2020 Google LLC
3+
* Copyright 2025 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.

packages/auth/src/api/authentication/exchange_token.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2020 Google LLC
3+
* Copyright 2025 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.

packages/auth/src/api/index.test.ts

Lines changed: 99 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ import sinonChai from 'sinon-chai';
2424
import { FirebaseError, getUA } from '@firebase/util';
2525
import * as utils from '@firebase/util';
2626

27-
import { mockEndpoint } from '../../test/helpers/api/helper';
27+
import {
28+
mockEndpoint,
29+
mockRegionalEndpointWithParent
30+
} from '../../test/helpers/api/helper';
2831
import {
2932
regionalTestAuth,
3033
testAuth,
@@ -605,26 +608,113 @@ describe('api/_performApiRequest', () => {
605608
});
606609

607610
context('throws Operation not allowed exception', () => {
608-
it('when tenantConfig is not initialized and Regional Endpoint is used', async () => {
611+
it('when tenantConfig is initialized and default Endpoint is used', async () => {
609612
await expect(
610-
_performRegionalApiRequest<typeof request, typeof serverResponse>(
611-
auth,
613+
_performApiRequest<typeof request, typeof serverResponse>(
614+
regionalAuth,
612615
HttpMethod.POST,
613-
RegionalEndpoint.EXCHANGE_TOKEN,
616+
Endpoint.SIGN_UP,
614617
request
615618
)
616619
).to.be.rejectedWith(
617620
FirebaseError,
618621
'Firebase: Operations not allowed for the auth object initialized. (auth/operation-not-allowed).'
619622
);
620623
});
624+
});
625+
});
621626

622-
it('when tenantConfig is initialized and default Endpoint is used', async () => {
627+
describe('api/_performRegionalApiRequest', () => {
628+
const request = {
629+
requestKey: 'request-value'
630+
};
631+
632+
const serverResponse = {
633+
responseKey: 'response-value'
634+
};
635+
636+
let auth: TestAuth;
637+
let regionalAuth: TestAuth;
638+
639+
beforeEach(async () => {
640+
auth = await testAuth();
641+
regionalAuth = await regionalTestAuth();
642+
});
643+
644+
afterEach(() => {
645+
sinon.restore();
646+
});
647+
648+
context('with regular requests', () => {
649+
beforeEach(mockFetch.setUp);
650+
afterEach(mockFetch.tearDown);
651+
it('should set the correct request, method and HTTP Headers', async () => {
652+
const mock = mockRegionalEndpointWithParent(
653+
RegionalEndpoint.EXCHANGE_TOKEN,
654+
'test-parent',
655+
serverResponse
656+
);
657+
const response = await _performRegionalApiRequest<
658+
typeof request,
659+
typeof serverResponse
660+
>(
661+
regionalAuth,
662+
HttpMethod.POST,
663+
RegionalEndpoint.EXCHANGE_TOKEN,
664+
request,
665+
{},
666+
'test-parent'
667+
);
668+
expect(response).to.eql(serverResponse);
669+
expect(mock.calls.length).to.eq(1);
670+
expect(mock.calls[0].method).to.eq(HttpMethod.POST);
671+
expect(mock.calls[0].request).to.eql(request);
672+
expect(mock.calls[0].headers!.get(HttpHeader.CONTENT_TYPE)).to.eq(
673+
'application/json'
674+
);
675+
expect(mock.calls[0].headers!.get(HttpHeader.X_CLIENT_VERSION)).to.eq(
676+
'testSDK/0.0.0'
677+
);
678+
expect(mock.calls[0].fullRequest?.credentials).to.be.undefined;
679+
});
680+
681+
it('should include whatever headers the auth impl attaches', async () => {
682+
sinon.stub(regionalAuth, '_getAdditionalHeaders').returns(
683+
Promise.resolve({
684+
'look-at-me-im-a-header': 'header-value',
685+
'anotherheader': 'header-value-2'
686+
})
687+
);
688+
689+
const mock = mockRegionalEndpointWithParent(
690+
RegionalEndpoint.EXCHANGE_TOKEN,
691+
'test-parent',
692+
serverResponse
693+
);
694+
await _performRegionalApiRequest<typeof request, typeof serverResponse>(
695+
regionalAuth,
696+
HttpMethod.POST,
697+
RegionalEndpoint.EXCHANGE_TOKEN,
698+
request,
699+
{},
700+
'test-parent'
701+
);
702+
expect(mock.calls[0].headers.get('look-at-me-im-a-header')).to.eq(
703+
'header-value'
704+
);
705+
expect(mock.calls[0].headers.get('anotherheader')).to.eq(
706+
'header-value-2'
707+
);
708+
});
709+
});
710+
711+
context('throws Operation not allowed exception', () => {
712+
it('when tenantConfig is not initialized and Regional Endpoint is used', async () => {
623713
await expect(
624-
_performApiRequest<typeof request, typeof serverResponse>(
625-
regionalAuth,
714+
_performRegionalApiRequest<typeof request, typeof serverResponse>(
715+
auth,
626716
HttpMethod.POST,
627-
Endpoint.SIGN_UP,
717+
RegionalEndpoint.EXCHANGE_TOKEN,
628718
request
629719
)
630720
).to.be.rejectedWith(

packages/auth/src/core/strategies/exchange_token.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2020 Google LLC
3+
* Copyright 2025 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.

packages/auth/src/core/strategies/exhange_token.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2020 Google LLC
3+
* Copyright 2025 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -27,8 +27,8 @@ import { _castAuth } from '../auth/auth_impl';
2727
* for an OidcToken i.e. Outbound Access Token.
2828
*
2929
* @remarks
30-
* This method is implemented only for {@link DefaultConfig.REGIONAL_API_HOST}
31-
* and requires {@link TenantConfig} to be configured in the {@link Auth} instance used.
30+
* This method is implemented only for `DefaultConfig.REGIONAL_API_HOST` and
31+
* requires {@link TenantConfig} to be configured in the {@link Auth} instance used.
3232
*
3333
* Fails with an error if the token is invalid, expired, or not accepted by the Firebase Auth service.
3434
*

packages/auth/src/model/public_types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export interface Auth {
185185
readonly config: Config;
186186
/**
187187
* The {@link TenantConfig} used to initialize a Regional Auth. This is only present
188-
* if regional auth is initialized and {@link DefaultConfig.REGIONAL_API_HOST}
188+
* if regional auth is initialized and `DefaultConfig.REGIONAL_API_HOST`
189189
* backend endpoint is used.
190190
*/
191191
readonly tenantConfig?: TenantConfig;
@@ -1269,7 +1269,7 @@ export interface Dependencies {
12691269
/**
12701270
* The {@link TenantConfig} to use. This dependency is only required
12711271
* if you want to use regional auth which works with
1272-
* {@link DefaultConfig.REGIONAL_API_HOST} endpoint. It should not be set otherwise.
1272+
* `DefaultConfig.REGIONAL_API_HOST`` endpoint. It should not be set otherwise.
12731273
*/
12741274
tenantConfig?: TenantConfig;
12751275
}

packages/auth/test/helpers/api/helper.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,6 @@ export function mockRegionalEndpointWithParent(
6363
status = 200
6464
): Route {
6565
const url = `${TEST_SCHEME}://${TEST_HOST}${parent}${endpoint}`;
66+
console.log('here ', url);
6667
return mock(url, response, status);
6768
}

0 commit comments

Comments
 (0)