Skip to content

Commit

Permalink
cleanup unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lahirumaramba committed Apr 26, 2023
1 parent c7e4046 commit d95bac6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/app-check/app-check-api-client-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,12 @@ export class AppCheckApiClient {
return this.httpClient.send(request);
})
.then((resp) => {
if (!validator.isBoolean(resp.data?.already_consumed)) {
if (typeof resp.data.alreadyConsumed !== 'undefined'
&& !validator.isBoolean(resp.data?.alreadyConsumed)) {
throw new FirebaseAppCheckError(
'invalid-argument', '`already_consumed` must be a boolean value.');
'invalid-argument', '`alreadyConsumed` must be a boolean value.');
}
return resp.data.already_consumed;
return resp.data.alreadyConsumed || false;
})
.catch((err) => {
throw this.toFirebaseError(err);
Expand Down
27 changes: 20 additions & 7 deletions test/unit/app-check/app-check-api-client-internal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,23 +305,23 @@ describe('AppCheckApiClient', () => {

['', 'abc', '3s2', 'sssa', '3.000000001', '3.2', null, NaN, [], {}, 100, 1.2, -200, -2.4]
.forEach((invalidAlreadyConsumed) => {
it(`should throw if the returned already_consumed value is: ${invalidAlreadyConsumed}`, () => {
const response = { already_consumed: invalidAlreadyConsumed };
it(`should throw if the returned alreadyConsumed value is: ${invalidAlreadyConsumed}`, () => {
const response = { alreadyConsumed: invalidAlreadyConsumed };
const stub = sinon
.stub(HttpClient.prototype, 'send')
.resolves(utils.responseFrom(response, 200));
stubs.push(stub);
const expected = new FirebaseAppCheckError(
'invalid-argument', '`already_consumed` must be a boolean value.');
'invalid-argument', '`alreadyConsumed` must be a boolean value.');
return apiClient.verifyReplayProtection(TEST_TOKEN_TO_EXCHANGE)
.should.eventually.be.rejected.and.deep.include(expected);
});
});

it('should resolve with the already_consumed status on success', () => {
it('should resolve with the alreadyConsumed status on success', () => {
const stub = sinon
.stub(HttpClient.prototype, 'send')
.resolves(utils.responseFrom({ already_consumed: true }, 200));
.resolves(utils.responseFrom({ alreadyConsumed: true }, 200));
stubs.push(stub);
return apiClient.verifyReplayProtection(TEST_TOKEN_TO_EXCHANGE)
.then((alreadyConsumed) => {
Expand All @@ -336,9 +336,9 @@ describe('AppCheckApiClient', () => {
});

[true, false].forEach((expectedAlreadyConsumed) => {
it(`should resolve with alreadyConsumed as ${expectedAlreadyConsumed} when already_consumed
it(`should resolve with alreadyConsumed as ${expectedAlreadyConsumed} when alreadyConsumed
from server is: ${expectedAlreadyConsumed}`, () => {
const response = { already_consumed: expectedAlreadyConsumed };
const response = { alreadyConsumed: expectedAlreadyConsumed };
const stub = sinon
.stub(HttpClient.prototype, 'send')
.resolves(utils.responseFrom(response, 200));
Expand All @@ -349,6 +349,19 @@ describe('AppCheckApiClient', () => {
});
});
});

it(`should resolve with alreadyConsumed as false when alreadyConsumed
from server is: undefined`, () => {
const response = { };
const stub = sinon
.stub(HttpClient.prototype, 'send')
.resolves(utils.responseFrom(response, 200));
stubs.push(stub);
return apiClient.verifyReplayProtection(TEST_TOKEN_TO_EXCHANGE)
.then((alreadyConsumed) => {
expect(alreadyConsumed).to.equal(false);
});
});
});

});
2 changes: 1 addition & 1 deletion test/unit/app-check/app-check.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ describe('AppCheck', () => {
return expect(replayStub).to.not.have.been.called;
});

it('should resolve with VerifyAppCheckTokenResponse on success with already_consumed set', () => {
it('should resolve with VerifyAppCheckTokenResponse on success with alreadyConsumed set', () => {
const response = {
sub: 'app-id',
iss: 'https://firebaseappcheck.googleapis.com/123456',
Expand Down

0 comments on commit d95bac6

Please sign in to comment.