Skip to content

Commit

Permalink
PR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lahirumaramba committed Jul 9, 2021
1 parent 8dab1b2 commit 644ca9a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/app-check/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export namespace appCheck {
}

/**
* Interface representing an App Check token options.
* Interface representing App Check token options.
*/
export interface AppCheckTokenOptions {
/**
Expand Down
4 changes: 2 additions & 2 deletions src/app-check/token-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ export class AppCheckTokenGenerator {
'AppCheckTokenOptions must be a non-null object.');
}
if (typeof options.ttlMillis !== 'undefined') {
if (!validator.isNumber(options.ttlMillis) || options.ttlMillis < 0) {
if (!validator.isNumber(options.ttlMillis)) {
throw new FirebaseAppCheckError('invalid-argument',
'ttlMillis must be a non-negative duration in milliseconds.');
'ttlMillis must be a duration in milliseconds.');
}
// ttlMillis must be between 30 minutes and 7 days (inclusive)
if (options.ttlMillis < (ONE_MINUTE_IN_MILLIS * 30) || options.ttlMillis > (ONE_DAY_IN_MILLIS * 7)) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export function generateUpdateMask(
export function transformMillisecondsToSecondsString(milliseconds: number): string {
let duration: string;
const seconds = Math.floor(milliseconds / 1000);
const nanos = Math.round((milliseconds - seconds * 1000) * 1000000);
const nanos = Math.floor((milliseconds - seconds * 1000) * 1000000);
if (nanos > 0) {
let nanoString = nanos.toString();
while (nanoString.length < 9) {
Expand Down
23 changes: 14 additions & 9 deletions test/unit/app-check/token-generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,19 @@ describe('AppCheckTokenGenerator', () => {
});
});

const invalidTtls = [null, NaN, '0', 'abc', '', -100, -1, true, false, [], {}, { a: 1 }, _.noop];
const invalidTtls = [null, NaN, '0', 'abc', '', true, false, [], {}, { a: 1 }, _.noop];
invalidTtls.forEach((invalidTtl) => {
it('should throw given an options object with invalid ttl: ' + JSON.stringify(invalidTtl), () => {
expect(() => {
tokenGenerator.createCustomToken(APP_ID, { ttlMillis: invalidTtl as any });
}).to.throw(FirebaseAppCheckError).with.property('message',
'ttlMillis must be a non-negative duration in milliseconds.');
'ttlMillis must be a duration in milliseconds.');
});
});

const THIRTY_MIN_IN_MS = 1800000;
const SEVEN_DAYS_IN_MS = 604800000;
[0, 10, THIRTY_MIN_IN_MS - 1, SEVEN_DAYS_IN_MS + 1, SEVEN_DAYS_IN_MS * 2].forEach((ttlMillis) => {
[-100, -1, 0, 10, THIRTY_MIN_IN_MS - 1, SEVEN_DAYS_IN_MS + 1, SEVEN_DAYS_IN_MS * 2].forEach((ttlMillis) => {
it('should throw given options with ttl < 30 minutes or ttl > 7 days:' + JSON.stringify(ttlMillis), () => {
expect(() => {
tokenGenerator.createCustomToken(APP_ID, { ttlMillis });
Expand All @@ -157,11 +157,16 @@ describe('AppCheckTokenGenerator', () => {
.should.eventually.be.a('string').and.not.be.empty;
});

[THIRTY_MIN_IN_MS, THIRTY_MIN_IN_MS + 1, SEVEN_DAYS_IN_MS / 2, SEVEN_DAYS_IN_MS - 1, SEVEN_DAYS_IN_MS]
.forEach((ttlMillis) => {
it('should be fulfilled with a Firebase Custom JWT with a valid custom ttl' + JSON.stringify(ttlMillis), () => {
return tokenGenerator.createCustomToken(APP_ID, { ttlMillis })
.should.eventually.be.a('string').and.not.be.empty;
[[THIRTY_MIN_IN_MS, '1800s'], [THIRTY_MIN_IN_MS + 1, '1800.001000000s'],
[SEVEN_DAYS_IN_MS / 2, '302400s'], [SEVEN_DAYS_IN_MS - 1, '604799.999000000s'], [SEVEN_DAYS_IN_MS, '604800s']]
.forEach((ttl) => {
it('should be fulfilled with a Firebase Custom JWT with a valid custom ttl' + JSON.stringify(ttl[0]), () => {
return tokenGenerator.createCustomToken(APP_ID, { ttlMillis: ttl[0] as number })
.then((token) => {
const decoded = jwt.decode(token) as { [key: string]: any };

expect(decoded['ttl']).to.deep.equal(ttl[1]);
});
});
});

Expand Down Expand Up @@ -207,7 +212,7 @@ describe('AppCheckTokenGenerator', () => {
});
});

[[1800000.000001, '1800.000000001s'], [1800000.001, '1800.000001000s'], [172800000, '172800s'],
[[1800000.000001, '1800.000000001s'], [1800000.001, '1800.000000999s'], [172800000, '172800s'],
[604799999, '604799.999000000s'], [604800000, '604800s']
].forEach((ttl) => {
it('should be fulfilled with a JWT with custom ttl in decoded payload', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/utils/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ describe('generateUpdateMask()', () => {

describe('transformMillisecondsToSecondsString()', () => {
[
[3000.000001, '3.000000001s'], [3000.001, '3.000001000s'],
[3000.000001, '3s'], [3000.001, '3.000001000s'],
[3000, '3s'], [3500, '3.500000000s']
].forEach((duration) => {
it('should transform to protobuf duration string when provided milliseconds:' + JSON.stringify(duration[0]),
Expand Down

0 comments on commit 644ca9a

Please sign in to comment.