Skip to content

Commit 4090f74

Browse files
committed
PR fixes
1 parent 8dab1b2 commit 4090f74

File tree

5 files changed

+19
-14
lines changed

5 files changed

+19
-14
lines changed

src/app-check/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export namespace appCheck {
9797
}
9898

9999
/**
100-
* Interface representing an App Check token options.
100+
* Interface representing App Check token options.
101101
*/
102102
export interface AppCheckTokenOptions {
103103
/**

src/app-check/token-generator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ export class AppCheckTokenGenerator {
124124
'AppCheckTokenOptions must be a non-null object.');
125125
}
126126
if (typeof options.ttlMillis !== 'undefined') {
127-
if (!validator.isNumber(options.ttlMillis) || options.ttlMillis < 0) {
127+
if (!validator.isNumber(options.ttlMillis)) {
128128
throw new FirebaseAppCheckError('invalid-argument',
129-
'ttlMillis must be a non-negative duration in milliseconds.');
129+
'ttlMillis must be a duration in milliseconds.');
130130
}
131131
// ttlMillis must be between 30 minutes and 7 days (inclusive)
132132
if (options.ttlMillis < (ONE_MINUTE_IN_MILLIS * 30) || options.ttlMillis > (ONE_DAY_IN_MILLIS * 7)) {

src/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export function generateUpdateMask(
204204
export function transformMillisecondsToSecondsString(milliseconds: number): string {
205205
let duration: string;
206206
const seconds = Math.floor(milliseconds / 1000);
207-
const nanos = Math.round((milliseconds - seconds * 1000) * 1000000);
207+
const nanos = Math.floor((milliseconds - seconds * 1000) * 1000000);
208208
if (nanos > 0) {
209209
let nanoString = nanos.toString();
210210
while (nanoString.length < 9) {

test/unit/app-check/token-generator.spec.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,19 @@ describe('AppCheckTokenGenerator', () => {
131131
});
132132
});
133133

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

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

160-
[THIRTY_MIN_IN_MS, THIRTY_MIN_IN_MS + 1, SEVEN_DAYS_IN_MS / 2, SEVEN_DAYS_IN_MS - 1, SEVEN_DAYS_IN_MS]
161-
.forEach((ttlMillis) => {
162-
it('should be fulfilled with a Firebase Custom JWT with a valid custom ttl' + JSON.stringify(ttlMillis), () => {
163-
return tokenGenerator.createCustomToken(APP_ID, { ttlMillis })
164-
.should.eventually.be.a('string').and.not.be.empty;
160+
[[THIRTY_MIN_IN_MS, '1800s'], [THIRTY_MIN_IN_MS + 1, '1800.001000000s'],
161+
[SEVEN_DAYS_IN_MS / 2, '302400s'], [SEVEN_DAYS_IN_MS - 1, '604799.999000000s'], [SEVEN_DAYS_IN_MS, '604800s']]
162+
.forEach((ttl) => {
163+
it('should be fulfilled with a Firebase Custom JWT with a valid custom ttl' + JSON.stringify(ttl[0]), () => {
164+
return tokenGenerator.createCustomToken(APP_ID, { ttlMillis: ttl[0] as number })
165+
.then((token) => {
166+
const decoded = jwt.decode(token) as { [key: string]: any };
167+
168+
expect(decoded['ttl']).to.equal(ttl[1]);
169+
});
165170
});
166171
});
167172

@@ -207,7 +212,7 @@ describe('AppCheckTokenGenerator', () => {
207212
});
208213
});
209214

210-
[[1800000.000001, '1800.000000001s'], [1800000.001, '1800.000001000s'], [172800000, '172800s'],
215+
[[1800000.000001, '1800.000000001s'], [1800000.001, '1800.000000999s'], [172800000, '172800s'],
211216
[604799999, '604799.999000000s'], [604800000, '604800s']
212217
].forEach((ttl) => {
213218
it('should be fulfilled with a JWT with custom ttl in decoded payload', () => {

test/unit/utils/index.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ describe('generateUpdateMask()', () => {
387387

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

0 commit comments

Comments
 (0)