|
| 1 | +/*! |
| 2 | + * Copyright 2021 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as _ from 'lodash'; |
| 18 | +import * as admin from '../../lib/index'; |
| 19 | +import * as chai from 'chai'; |
| 20 | +import * as chaiAsPromised from 'chai-as-promised'; |
| 21 | +import fs = require('fs'); |
| 22 | +import path = require('path'); |
| 23 | + |
| 24 | +// eslint-disable-next-line @typescript-eslint/no-var-requires |
| 25 | +const chalk = require('chalk'); |
| 26 | + |
| 27 | +chai.should(); |
| 28 | +chai.use(chaiAsPromised); |
| 29 | + |
| 30 | +const expect = chai.expect; |
| 31 | + |
| 32 | +let appId: string; |
| 33 | + |
| 34 | +describe('admin.appCheck', () => { |
| 35 | + before(async () => { |
| 36 | + try { |
| 37 | + appId = fs.readFileSync(path.join(__dirname, '../resources/appId.txt')).toString().trim(); |
| 38 | + } catch (error) { |
| 39 | + console.log(chalk.yellow( |
| 40 | + 'Unable to find an an App ID. Skipping tests that require a valid App ID.', |
| 41 | + error, |
| 42 | + )); |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + describe('createToken', () => { |
| 47 | + it('should succeed with a vaild token', function() { |
| 48 | + if (!appId) { |
| 49 | + this.skip(); |
| 50 | + } |
| 51 | + return admin.appCheck().createToken(appId as string) |
| 52 | + .then((token) => { |
| 53 | + expect(token).to.have.keys(['token', 'ttlMillis']); |
| 54 | + expect(token.token).to.be.a('string').and.to.not.be.empty; |
| 55 | + expect(token.ttlMillis).to.be.a('number'); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should propagate API errors', () => { |
| 60 | + // rejects with invalid-argument when appId is incorrect |
| 61 | + return admin.appCheck().createToken('incorrect-app-id') |
| 62 | + .should.eventually.be.rejected.and.have.property('code', 'app-check/invalid-argument'); |
| 63 | + }); |
| 64 | + |
| 65 | + const invalidAppIds = ['', null, NaN, 0, 1, true, false, [], {}, { a: 1 }, _.noop]; |
| 66 | + invalidAppIds.forEach((invalidAppId) => { |
| 67 | + it(`should throw given an invalid appId: ${JSON.stringify(invalidAppId)}`, () => { |
| 68 | + expect(() => admin.appCheck().createToken(invalidAppId as any)) |
| 69 | + .to.throw('appId` must be a non-empty string.'); |
| 70 | + }); |
| 71 | + }); |
| 72 | + }); |
| 73 | +}); |
0 commit comments