diff --git a/src/auth/googleauth.ts b/src/auth/googleauth.ts index 65d288ed..5a3fb94f 100644 --- a/src/auth/googleauth.ts +++ b/src/auth/googleauth.ts @@ -379,42 +379,22 @@ export class GoogleAuth { /** * Create a credentials instance using the given input options. * @param {object=} json The input object. - * @param {function=} callback Optional callback. - * @returns Promise that resolves with the OAuth2Client (if no callback is - * passed) + * @returns JWT or UserRefresh Client with data */ - // TODO: Remove the overloads and just keep this a sync API - fromJSON(json: JWTInput): JWT|UserRefreshClient; - fromJSON(json: JWTInput, callback: CredentialCallback): void; - fromJSON(json: JWTInput, callback?: CredentialCallback): JWT|UserRefreshClient - |void { + fromJSON(json: JWTInput): JWT|UserRefreshClient { let client: UserRefreshClient|JWT; - try { - if (!json) { - throw new Error( - 'Must pass in a JSON object containing the Google auth settings.'); - } - - this.jsonContent = json; - if (json.type === 'authorized_user') { - client = new UserRefreshClient(); - } else { - client = new JWT(); - } - client.fromJSON(json); - if (callback) { - return callback(null, client); - } else { - return client; - } - - } catch (e) { - if (callback) { - return callback(e); - } else { - throw e; - } + if (!json) { + throw new Error( + 'Must pass in a JSON object containing the Google auth settings.'); + } + this.jsonContent = json; + if (json.type === 'authorized_user') { + client = new UserRefreshClient(); + } else { + client = new JWT(); } + client.fromJSON(json); + return client; } /** @@ -462,26 +442,12 @@ export class GoogleAuth { /** * Create a credentials instance using the given API key string. * @param {string} - The API key string - * @param {function=} - Optional callback function + * @returns A JWT loaded from the key */ - fromAPIKey( - apiKey: string, - callback?: (err?: Error|null, client?: JWT) => void): void|JWT { + fromAPIKey(apiKey: string): JWT { const client = new JWT(); - try { - client.fromAPIKey(apiKey); - if (callback) { - callback(null, client); - } else { - return client; - } - } catch (e) { - if (callback) { - callback(e); - } else { - throw e; - } - } + client.fromAPIKey(apiKey); + return client; } /** diff --git a/src/auth/jwtaccess.ts b/src/auth/jwtaccess.ts index 07cd7324..755a2ed1 100644 --- a/src/auth/jwtaccess.ts +++ b/src/auth/jwtaccess.ts @@ -57,74 +57,47 @@ export class JWTAccess { * @param {function} metadataCb a callback invoked with the jwt request metadata. * @returns a Promise that resolves with the request metadata response */ - getRequestMetadata(authURI: string): RequestMetadataResponse; - getRequestMetadata(authURI: string, callback: RequestMetadataCallback): void; - getRequestMetadata(authURI: string, callback?: RequestMetadataCallback): - void|RequestMetadataResponse { - try { - const iat = Math.floor(new Date().getTime() / 1000); - const exp = iat + 3600; // 3600 seconds = 1 hour + getRequestMetadata(authURI: string): RequestMetadataResponse { + const iat = Math.floor(new Date().getTime() / 1000); + const exp = iat + 3600; // 3600 seconds = 1 hour - // The payload used for signed JWT headers has: - // iss == sub == - // aud == - const payload = - {iss: this.email, sub: this.email, aud: authURI, exp, iat}; - const assertion = { - header: {alg: 'RS256'} as jws.Header, - payload, - secret: this.key - }; + // The payload used for signed JWT headers has: + // iss == sub == + // aud == + const payload = {iss: this.email, sub: this.email, aud: authURI, exp, iat}; + const assertion = { + header: {alg: 'RS256'} as jws.Header, + payload, + secret: this.key + }; - // Sign the jwt and invoke metadataCb with it. - const signedJWT = - jws.sign({header: {alg: 'RS256'}, payload, secret: this.key}); - const result = { - res: null, - headers: {Authorization: 'Bearer ' + signedJWT} - }; - if (callback) { - callback(null, result.headers, null); - } else { - return result; - } - } catch (e) { - if (callback) { - callback(e); - } else { - throw e; - } - } + // Sign the jwt and invoke metadataCb with it. + const signedJWT = + jws.sign({header: {alg: 'RS256'}, payload, secret: this.key}); + return {headers: {Authorization: 'Bearer ' + signedJWT}}; } /** * Create a JWTAccess credentials instance using the given input options. * @param {object=} json The input object. - * @param {function=} callback Optional callback. */ - fromJSON(json: JWTInput, callback?: (err: Error|null) => void) { - try { - if (!json) { - throw new Error( - 'Must pass in a JSON object containing the service account auth settings.'); - } - if (!json.client_email) { - throw new Error( - 'The incoming JSON object does not contain a client_email field'); - } - if (!json.private_key) { - throw new Error( - 'The incoming JSON object does not contain a private_key field'); - } - // Extract the relevant information from the json key file. - this.email = json.client_email; - this.key = json.private_key; - this.projectId = json.project_id; - if (callback) callback(null); - } catch (e) { - if (callback) return callback(e); - throw e; + fromJSON(json: JWTInput): void { + if (!json) { + throw new Error( + 'Must pass in a JSON object containing the service account auth settings.'); + } + if (!json.client_email) { + throw new Error( + 'The incoming JSON object does not contain a client_email field'); + } + if (!json.private_key) { + throw new Error( + 'The incoming JSON object does not contain a private_key field'); } + // Extract the relevant information from the json key file. + this.email = json.client_email; + this.key = json.private_key; + this.projectId = json.project_id; } /** diff --git a/src/auth/jwtclient.ts b/src/auth/jwtclient.ts index 07a11864..47241f36 100644 --- a/src/auth/jwtclient.ts +++ b/src/auth/jwtclient.ts @@ -147,34 +147,24 @@ export class JWT extends OAuth2Client { /** * Create a JWT credentials instance using the given input options. * @param {object=} json The input object. - * @param {function=} callback Optional callback. */ - fromJSON(json: JWTInput, callback?: (err?: Error) => void): void { - try { - if (!json) { - throw new Error( - 'Must pass in a JSON object containing the service account auth settings.'); - } - if (!json.client_email) { - throw new Error( - 'The incoming JSON object does not contain a client_email field'); - } - if (!json.private_key) { - throw new Error( - 'The incoming JSON object does not contain a private_key field'); - } - // Extract the relevant information from the json key file. - this.email = json.client_email; - this.key = json.private_key; - this.projectId = json.project_id; - } catch (e) { - if (callback) { - callback(e); - } else { - throw e; - } + fromJSON(json: JWTInput): void { + if (!json) { + throw new Error( + 'Must pass in a JSON object containing the service account auth settings.'); + } + if (!json.client_email) { + throw new Error( + 'The incoming JSON object does not contain a client_email field'); + } + if (!json.private_key) { + throw new Error( + 'The incoming JSON object does not contain a private_key field'); } - if (callback) callback(); + // Extract the relevant information from the json key file. + this.email = json.client_email; + this.key = json.private_key; + this.projectId = json.project_id; } /** @@ -221,22 +211,12 @@ export class JWT extends OAuth2Client { /** * Creates a JWT credentials instance using an API Key for authentication. * @param {string} apiKey - the API Key in string form. - * @param {function=} callback - Optional callback to be invoked after - * initialization. */ - fromAPIKey(apiKey: string, callback?: (err?: Error) => void): void { + fromAPIKey(apiKey: string): void { if (!isString(apiKey)) { - const e = new Error('Must provide an API Key string.'); - if (callback) { - return setImmediate(callback, e); - } else { - throw e; - } + throw new Error('Must provide an API Key string.'); } this.apiKey = apiKey; - if (callback) { - return callback(); - } } /** diff --git a/src/auth/refreshclient.ts b/src/auth/refreshclient.ts index 7e699cd5..75c38663 100644 --- a/src/auth/refreshclient.ts +++ b/src/auth/refreshclient.ts @@ -52,42 +52,32 @@ export class UserRefreshClient extends OAuth2Client { * Create a UserRefreshClient credentials instance using the given input * options. * @param {object=} json The input object. - * @param {function=} callback Optional callback. */ - fromJSON(json: JWTInput, callback?: (err?: Error) => void) { - try { - if (!json) { - throw new Error( - 'Must pass in a JSON object containing the user refresh token'); - } - if (json.type !== 'authorized_user') { - throw new Error( - 'The incoming JSON object does not have the "authorized_user" type'); - } - if (!json.client_id) { - throw new Error( - 'The incoming JSON object does not contain a client_id field'); - } - if (!json.client_secret) { - throw new Error( - 'The incoming JSON object does not contain a client_secret field'); - } - if (!json.refresh_token) { - throw new Error( - 'The incoming JSON object does not contain a refresh_token field'); - } - this._clientId = json.client_id; - this._clientSecret = json.client_secret; - this._refreshToken = json.refresh_token; - this.credentials.refresh_token = json.refresh_token; - } catch (e) { - if (callback) { - callback(e); - } else { - throw e; - } + fromJSON(json: JWTInput): void { + if (!json) { + throw new Error( + 'Must pass in a JSON object containing the user refresh token'); + } + if (json.type !== 'authorized_user') { + throw new Error( + 'The incoming JSON object does not have the "authorized_user" type'); + } + if (!json.client_id) { + throw new Error( + 'The incoming JSON object does not contain a client_id field'); + } + if (!json.client_secret) { + throw new Error( + 'The incoming JSON object does not contain a client_secret field'); + } + if (!json.refresh_token) { + throw new Error( + 'The incoming JSON object does not contain a refresh_token field'); } - if (callback) callback(); + this._clientId = json.client_id; + this._clientSecret = json.client_secret; + this._refreshToken = json.refresh_token; + this.credentials.refresh_token = json.refresh_token; } /** diff --git a/test/test.googleauth.ts b/test/test.googleauth.ts index 75b7cffe..eb83096f 100644 --- a/test/test.googleauth.ts +++ b/test/test.googleauth.ts @@ -158,13 +158,12 @@ function insertWellKnownFilePathIntoAuth( describe('GoogleAuth', () => { describe('.fromJson', () => { - it('should error on null json', (done) => { + it('should error on null json', () => { const auth = new GoogleAuth(); - // Test verifies invalid parameter tests, which requires cast to any. - // tslint:disable-next-line no-any - (auth as any).fromJSON(null, (err: Error) => { - assert.equal(true, err instanceof Error); - done(); + assert.throws(() => { + // Test verifies invalid parameter tests, which requires cast to any. + // tslint:disable-next-line no-any + (auth as any).fromJSON(null); }); }); @@ -176,12 +175,12 @@ describe('GoogleAuth', () => { before(() => { auth = new GoogleAuth(); }); - it('Should error given an invalid api key', done => { - // Test verifies invalid parameter tests, which requires cast to any. - // tslint:disable-next-line no-any - (auth as any).fromAPIKey(null, (err: Error) => { - assert(err instanceof Error); - done(); + it('Should error given an invalid api key', () => { + assert.throws(() => { + // Test verifies invalid parameter tests, which requires cast to + // any. + // tslint:disable-next-line no-any + (auth as any).fromAPIKey(null); }); }); }); @@ -212,23 +211,19 @@ describe('GoogleAuth', () => { return [200, RESPONSE_BODY]; }); - auth.fromAPIKey(API_KEY, (err, client) => { - assert.strictEqual(err, null); - if (client) { - client.request( - { - url: BASE_URL + ENDPOINT, - method: 'POST', - data: {'test': true} - }, - (err2, res) => { - assert.strictEqual(err2, null); - assert.strictEqual(RESPONSE_BODY, res!.data); - fakeService.done(); - done(); - }); - } - }); + const client = auth.fromAPIKey(API_KEY); + client.request( + { + url: BASE_URL + ENDPOINT, + method: 'POST', + data: {'test': true} + }, + (err2, res) => { + assert.strictEqual(err2, null); + assert.strictEqual(RESPONSE_BODY, res!.data); + fakeService.done(); + done(); + }); }); }); @@ -247,153 +242,128 @@ describe('GoogleAuth', () => { uri.indexOf('test=' + OTHER_QS_PARAM.test) > -1); return [200, RESPONSE_BODY]; }); - auth.fromAPIKey(API_KEY, (err, client) => { - assert.strictEqual(err, null); - if (client) { - client.request( - { - url: BASE_URL + ENDPOINT, - method: 'POST', - data: {'test': true}, - params: OTHER_QS_PARAM - }, - (err2, res) => { - assert.strictEqual(err2, null); - assert.strictEqual(RESPONSE_BODY, res!.data); - fakeService.done(); - done(); - }); - } - }); + const client = auth.fromAPIKey(API_KEY); + client.request( + { + url: BASE_URL + ENDPOINT, + method: 'POST', + data: {'test': true}, + params: OTHER_QS_PARAM + }, + (err2, res) => { + assert.strictEqual(err2, null); + assert.strictEqual(RESPONSE_BODY, res!.data); + fakeService.done(); + done(); + }); }); }); }); }); describe('JWT token', () => { - it('should error on empty json', (done) => { + it('should error on empty json', () => { const auth = new GoogleAuth(); - auth.fromJSON({}, (err) => { - assert.equal(true, err instanceof Error); - done(); + assert.throws(() => { + auth.fromJSON({}); }); }); - it('should error on missing client_email', (done) => { + it('should error on missing client_email', () => { const json = createJwtJSON(); delete json.client_email; const auth = new GoogleAuth(); - auth.fromJSON(json, (err) => { - assert.equal(true, err instanceof Error); - done(); + assert.throws(() => { + auth.fromJSON(json); }); }); - it('should error on missing private_key', (done) => { + it('should error on missing private_key', () => { const json = createJwtJSON(); delete json.private_key; const auth = new GoogleAuth(); - auth.fromJSON(json, (err) => { - assert.equal(true, err instanceof Error); - done(); + assert.throws(() => { + auth.fromJSON(json); }); }); - it('should create JWT with client_email', (done) => { + it('should create JWT with client_email', () => { const json = createJwtJSON(); const auth = new GoogleAuth(); - auth.fromJSON(json, (err, result) => { - assert.equal(null, err); - assert.equal(json.client_email, (result as JWT).email); - done(); - }); + const result = auth.fromJSON(json); + assert.equal(json.client_email, (result as JWT).email); }); - it('should create JWT with private_key', (done) => { + it('should create JWT with private_key', () => { const json = createJwtJSON(); const auth = new GoogleAuth(); - auth.fromJSON(json, (err, result) => { - assert.equal(null, err); - assert.equal(json.private_key, (result as JWT).key); - done(); - }); + const result = auth.fromJSON(json); + assert.equal(json.private_key, (result as JWT).key); }); - it('should create JWT with null scopes', (done) => { + it('should create JWT with null scopes', () => { const json = createJwtJSON(); const auth = new GoogleAuth(); - auth.fromJSON(json, (err, result) => { - assert.equal(null, err); - assert.equal(null, (result as JWT).scopes); - done(); - }); + const result = auth.fromJSON(json); + assert.equal(null, (result as JWT).scopes); }); - it('should create JWT with null subject', (done) => { + it('should create JWT with null subject', () => { const json = createJwtJSON(); const auth = new GoogleAuth(); - auth.fromJSON(json, (err, result) => { - assert.equal(null, err); - assert.equal(null, (result as JWT).subject); - done(); - }); + const result = auth.fromJSON(json); + assert.equal(null, (result as JWT).subject); }); - it('should create JWT with null keyFile', (done) => { + it('should create JWT with null keyFile', () => { const json = createJwtJSON(); const auth = new GoogleAuth(); - auth.fromJSON(json, (err, result) => { - assert.equal(null, err); - assert.equal(null, (result as JWT).keyFile); - done(); - }); + const result = auth.fromJSON(json); + assert.equal(null, (result as JWT).keyFile); }); }); + describe('Refresh token', () => { - it('should error on empty json', (done) => { + it('should error on empty json', () => { const auth = new GoogleAuth(); const jwt = new auth.JWT(); - jwt.fromJSON({}, (err) => { - assert.equal(true, err instanceof Error); - done(); + assert.throws(() => { + jwt.fromJSON({}); }); }); - it('should error on missing client_id', (done) => { + it('should error on missing client_id', () => { const json = createRefreshJSON(); delete json.client_id; const auth = new GoogleAuth(); const jwt = new auth.JWT(); - jwt.fromJSON(json, (err) => { - assert.equal(true, err instanceof Error); - done(); + assert.throws(() => { + jwt.fromJSON(json); }); }); - it('should error on missing client_secret', (done) => { + it('should error on missing client_secret', () => { const json = createRefreshJSON(); delete json.client_secret; const auth = new GoogleAuth(); const jwt = new auth.JWT(); - jwt.fromJSON(json, (err) => { - assert.equal(true, err instanceof Error); - done(); + assert.throws(() => { + jwt.fromJSON(json); }); }); - it('should error on missing refresh_token', done => { + it('should error on missing refresh_token', () => { const json = createRefreshJSON(); delete json.refresh_token; const auth = new GoogleAuth(); const jwt = new auth.JWT(); - jwt.fromJSON(json, (err) => { - assert.equal(true, err instanceof Error); - done(); + assert.throws(() => { + jwt.fromJSON(json); }); }); }); diff --git a/test/test.jwt.ts b/test/test.jwt.ts index 09b17dbd..db519d43 100644 --- a/test/test.jwt.ts +++ b/test/test.jwt.ts @@ -456,78 +456,57 @@ describe('.fromJson', () => { jwt = new auth.JWT(); }); - it('should error on null json', (done) => { - // Test verifies invalid parameter tests, which requires cast to any. - // tslint:disable-next-line no-any - (jwt as any).fromJSON(null, (err: Error) => { - assert.equal(true, err instanceof Error); - done(); + it('should error on null json', () => { + assert.throws(() => { + // Test verifies invalid parameter tests, which requires cast to any. + // tslint:disable-next-line no-any + (jwt as any).fromJSON(null); }); }); - it('should error on empty json', (done) => { - jwt.fromJSON({}, (err) => { - assert.equal(true, err instanceof Error); - done(); + it('should error on empty json', () => { + assert.throws(() => { + jwt.fromJSON({}); }); }); - it('should error on missing client_email', (done) => { + it('should error on missing client_email', () => { delete json.client_email; - - jwt.fromJSON(json, (err) => { - assert.equal(true, err instanceof Error); - done(); + assert.throws(() => { + jwt.fromJSON(json); }); }); - it('should error on missing private_key', (done) => { + it('should error on missing private_key', () => { delete json.private_key; - - jwt.fromJSON(json, (err) => { - assert.equal(true, err instanceof Error); - done(); + assert.throws(() => { + jwt.fromJSON(json); }); }); - it('should create JWT with client_email', (done) => { - jwt.fromJSON(json, (err) => { - assert.equal(null, err); - assert.equal(json.client_email, jwt.email); - done(); - }); + it('should create JWT with client_email', () => { + const result = jwt.fromJSON(json); + assert.equal(json.client_email, jwt.email); }); - it('should create JWT with private_key', (done) => { - jwt.fromJSON(json, (err) => { - assert.equal(null, err); - assert.equal(json.private_key, jwt.key); - done(); - }); + it('should create JWT with private_key', () => { + const result = jwt.fromJSON(json); + assert.equal(json.private_key, jwt.key); }); - it('should create JWT with null scopes', (done) => { - jwt.fromJSON(json, (err) => { - assert.equal(null, err); - assert.equal(null, jwt.scopes); - done(); - }); + it('should create JWT with null scopes', () => { + const result = jwt.fromJSON(json); + assert.equal(null, jwt.scopes); }); - it('should create JWT with null subject', (done) => { - jwt.fromJSON(json, (err) => { - assert.equal(null, err); - assert.equal(null, jwt.subject); - done(); - }); + it('should create JWT with null subject', () => { + const result = jwt.fromJSON(json); + assert.equal(null, jwt.subject); }); - it('should create JWT with null keyFile', (done) => { - jwt.fromJSON(json, (err) => { - assert.equal(null, err); - assert.equal(null, jwt.keyFile); - done(); - }); + it('should create JWT with null keyFile', () => { + const result = jwt.fromJSON(json); + assert.equal(null, jwt.keyFile); }); }); @@ -581,31 +560,26 @@ describe('.fromAPIKey', () => { jwt = new auth.JWT(); }); describe('exception behaviour', () => { - it('should error without api key', (done) => { - // Test verifies invalid parameter tests, which requires cast to any. - // tslint:disable-next-line no-any - (jwt as any).fromAPIKey(undefined, (err: Error) => { - assert(err instanceof Error); - done(); + it('should error without api key', () => { + assert.throws(() => { + // Test verifies invalid parameter tests, which requires cast to any. + // tslint:disable-next-line no-any + (jwt as any).fromAPIKey(undefined); }); }); - it('should error with invalid api key type', (done) => { - // Test verifies invalid parameter tests, which requires cast to any. - // tslint:disable-next-line no-any - jwt.fromAPIKey(({key: KEY} as any), (err) => { - assert(err instanceof Error); - done(); + it('should error with invalid api key type', () => { + assert.throws(() => { + // Test verifies invalid parameter tests, which requires cast to any. + // tslint:disable-next-line no-any + jwt.fromAPIKey({key: KEY} as any); }); }); }); describe('Valid behaviour', () => { - it('should set the .apiKey property on the instance', (done) => { - jwt.fromAPIKey(KEY, (err) => { - assert.strictEqual(jwt.apiKey, KEY); - assert.equal(err, null); - done(); - }); + it('should set the .apiKey property on the instance', () => { + const result = jwt.fromAPIKey(KEY); + assert.strictEqual(jwt.apiKey, KEY); }); }); }); diff --git a/test/test.jwtaccess.ts b/test/test.jwtaccess.ts index c2bab1ce..437d7e14 100644 --- a/test/test.jwtaccess.ts +++ b/test/test.jwtaccess.ts @@ -37,30 +37,21 @@ function createJSON() { } describe('.getRequestMetadata', () => { - it('create a signed JWT token as the access token', (done) => { + it('create a signed JWT token as the access token', () => { const keys = keypair(1024 /* bitsize of private key */); const testUri = 'http:/example.com/my_test_service'; const email = 'foo@serviceaccount.com'; const auth = new GoogleAuth(); const client = new auth.JWTAccess(email, keys.private); - - const retValue = 'dummy'; - const expectAuth = - (err: Error|null, headers?: http.IncomingHttpHeaders|null) => { - assert.strictEqual(null, err, 'no error was expected: got\n' + err); - assert.notStrictEqual( - null, headers, 'an creds object should be present'); - const decoded = jws.decode( - (headers!.Authorization as string).replace('Bearer ', '')); - const payload = JSON.parse(decoded.payload); - assert.strictEqual(email, payload.iss); - assert.strictEqual(email, payload.sub); - assert.strictEqual(testUri, payload.aud); - done(); - return retValue; - }; - const res = client.getRequestMetadata(testUri, expectAuth); - assert.strictEqual(res, retValue); + const res = client.getRequestMetadata(testUri); + assert.notStrictEqual( + null, res.headers, 'an creds object should be present'); + const decoded = jws.decode( + (res.headers!.Authorization as string).replace('Bearer ', '')); + const payload = JSON.parse(decoded.payload); + assert.strictEqual(email, payload.iss); + assert.strictEqual(email, payload.sub); + assert.strictEqual(testUri, payload.aud); }); }); @@ -82,54 +73,42 @@ describe('.fromJson', () => { client = new auth.JWTAccess(); }); - it('should error on null json', (done) => { - // Test verifies invalid parameter tests, which requires cast to any. - // tslint:disable-next-line no-any - (client as any).fromJSON(null, (err: Error) => { - assert.equal(true, err instanceof Error); - done(); + it('should error on null json', () => { + assert.throws(() => { + // Test verifies invalid parameter tests, which requires cast to any. + // tslint:disable-next-line no-any + (client as any).fromJSON(null); }); }); - it('should error on empty json', (done) => { - client.fromJSON({}, (err) => { - assert.equal(true, err instanceof Error); - done(); + it('should error on empty json', () => { + assert.throws(() => { + client.fromJSON({}); }); }); - it('should error on missing client_email', (done) => { + it('should error on missing client_email', () => { delete json.client_email; - - client.fromJSON(json, (err) => { - assert.equal(true, err instanceof Error); - done(); + assert.throws(() => { + client.fromJSON(json); }); }); - it('should error on missing private_key', (done) => { + it('should error on missing private_key', () => { delete json.private_key; - - client.fromJSON(json, (err) => { - assert.equal(true, err instanceof Error); - done(); + assert.throws(() => { + client.fromJSON(json); }); }); - it('should create JWT with client_email', (done) => { - client.fromJSON(json, (err) => { - assert.equal(null, err); - assert.equal(json.client_email, client.email); - done(); - }); + it('should create JWT with client_email', () => { + const result = client.fromJSON(json); + assert.equal(json.client_email, client.email); }); - it('should create JWT with private_key', (done) => { - client.fromJSON(json, (err) => { - assert.equal(null, err); - assert.equal(json.private_key, client.key); - done(); - }); + it('should create JWT with private_key', () => { + const result = client.fromJSON(json); + assert.equal(json.private_key, client.key); }); }); diff --git a/test/test.refresh.ts b/test/test.refresh.ts index 778b9a57..aed9cb63 100644 --- a/test/test.refresh.ts +++ b/test/test.refresh.ts @@ -33,95 +33,78 @@ function createJSON() { } describe('.fromJson', () => { - it('should error on null json', (done) => { + it('should error on null json', () => { const auth = new GoogleAuth(); const refresh = new auth.UserRefreshClient(); - // Test verifies invalid parameter tests, which requires cast to any. - // tslint:disable-next-line no-any - (refresh as any).fromJSON(null, (err: Error) => { - assert.equal(true, err instanceof Error); - done(); + assert.throws(() => { + // Test verifies invalid parameter tests, which requires cast to any. + // tslint:disable-next-line no-any + (refresh as any).fromJSON(null); }); }); - it('should error on empty json', (done) => { + it('should error on empty json', () => { const auth = new GoogleAuth(); const refresh = new auth.UserRefreshClient(); - // Test verifies invalid parameter tests, which requires cast to any. - // tslint:disable-next-line no-any - refresh.fromJSON(({} as any), (err) => { - assert.equal(true, err instanceof Error); - done(); + assert.throws(() => { + // Test verifies invalid parameter tests, which requires cast to any. + // tslint:disable-next-line no-any + refresh.fromJSON({}); }); }); - it('should error on missing client_id', (done) => { + it('should error on missing client_id', () => { const json = createJSON(); delete json.client_id; - const auth = new GoogleAuth(); const refresh = new auth.UserRefreshClient(); - refresh.fromJSON(json, (err) => { - assert.equal(true, err instanceof Error); - done(); + assert.throws(() => { + refresh.fromJSON(json); }); }); - it('should error on missing client_secret', (done) => { + it('should error on missing client_secret', () => { const json = createJSON(); delete json.client_secret; - const auth = new GoogleAuth(); const refresh = new auth.UserRefreshClient(); - refresh.fromJSON(json, (err) => { - assert.equal(true, err instanceof Error); - done(); + assert.throws(() => { + refresh.fromJSON(json); }); }); - it('should error on missing refresh_token', (done) => { + it('should error on missing refresh_token', () => { const json = createJSON(); delete json.refresh_token; - const auth = new GoogleAuth(); const refresh = new auth.UserRefreshClient(); - refresh.fromJSON(json, (err) => { - assert.equal(true, err instanceof Error); - done(); + assert.throws(() => { + refresh.fromJSON(json); }); }); - it('should create UserRefreshClient with clientId_', (done) => { + it('should create UserRefreshClient with clientId_', () => { const json = createJSON(); const auth = new GoogleAuth(); const refresh = new auth.UserRefreshClient(); - refresh.fromJSON(json, (err) => { - assert.ifError(err); - assert.equal(json.client_id, refresh._clientId); - done(); - }); + const result = refresh.fromJSON(json); + assert.equal(json.client_id, refresh._clientId); }); - it('should create UserRefreshClient with clientSecret_', (done) => { + it('should create UserRefreshClient with clientSecret_', () => { const json = createJSON(); const auth = new GoogleAuth(); const refresh = new auth.UserRefreshClient(); - refresh.fromJSON(json, (err) => { - assert.ifError(err); - assert.equal(json.client_secret, refresh._clientSecret); - done(); - }); + const result = refresh.fromJSON(json); + assert.equal(json.client_secret, refresh._clientSecret); }); - it('should create UserRefreshClient with _refreshToken', (done) => { + it('should create UserRefreshClient with _refreshToken', () => { const json = createJSON(); const auth = new GoogleAuth(); const refresh = new auth.UserRefreshClient(); - refresh.fromJSON(json, (err) => { - assert.ifError(err); - assert.equal(json.refresh_token, refresh._refreshToken); - done(); - }); + const result = refresh.fromJSON(json); + assert.equal(json.refresh_token, refresh._refreshToken); }); });