|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { default as assert } from 'assert'; |
| 7 | +import { isAuthError, RateLogger } from '../../github/loggingOctokit'; |
| 8 | +import { MockTelemetry } from '../mocks/mockTelemetry'; |
| 9 | + |
| 10 | +describe('loggingOctokit', () => { |
| 11 | + describe('isAuthError', () => { |
| 12 | + it('returns true for an Octokit-style 401 with "Bad credentials" message', () => { |
| 13 | + const e: any = new Error('Bad credentials'); |
| 14 | + e.status = 401; |
| 15 | + assert.strictEqual(isAuthError(e), true); |
| 16 | + }); |
| 17 | + |
| 18 | + it('returns true for an error whose message is exactly "Bad credentials"', () => { |
| 19 | + assert.strictEqual(isAuthError(new Error('Bad credentials')), true); |
| 20 | + }); |
| 21 | + |
| 22 | + it('returns true for an error whose message includes "Bad credentials"', () => { |
| 23 | + assert.strictEqual(isAuthError(new Error('HttpError: Bad credentials - https://docs.github.com/rest')), true); |
| 24 | + }); |
| 25 | + |
| 26 | + it('returns true for a GraphQL networkError with statusCode 401', () => { |
| 27 | + const e: any = new Error('Network error'); |
| 28 | + e.networkError = { statusCode: 401 }; |
| 29 | + assert.strictEqual(isAuthError(e), true); |
| 30 | + }); |
| 31 | + |
| 32 | + it('returns true for a GraphQL error message including "401 Unauthorized"', () => { |
| 33 | + assert.strictEqual(isAuthError(new Error('Response not successful: Received status code 401 Unauthorized')), true); |
| 34 | + }); |
| 35 | + |
| 36 | + it('returns false for unrelated errors', () => { |
| 37 | + const e: any = new Error('Not Found'); |
| 38 | + e.status = 404; |
| 39 | + assert.strictEqual(isAuthError(e), false); |
| 40 | + assert.strictEqual(isAuthError(new Error('Server Error')), false); |
| 41 | + assert.strictEqual(isAuthError(undefined), false); |
| 42 | + assert.strictEqual(isAuthError(null), false); |
| 43 | + assert.strictEqual(isAuthError('Bad credentials'), false); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('RateLogger.logApiError', () => { |
| 48 | + it('invokes the auth error handler when the API call fails with a 401/Bad credentials', async () => { |
| 49 | + const telemetry = new MockTelemetry(); |
| 50 | + let handlerCalls = 0; |
| 51 | + const handler = () => { handlerCalls++; }; |
| 52 | + const rateLogger = new RateLogger(telemetry, false, handler); |
| 53 | + |
| 54 | + const e: any = new Error('Bad credentials'); |
| 55 | + e.status = 401; |
| 56 | + rateLogger.logApiError('/test', Promise.reject(e)); |
| 57 | + |
| 58 | + // allow microtasks to run |
| 59 | + await new Promise(resolve => setImmediate(resolve)); |
| 60 | + assert.strictEqual(handlerCalls, 1); |
| 61 | + }); |
| 62 | + |
| 63 | + it('does not invoke the auth error handler for non-auth errors', async () => { |
| 64 | + const telemetry = new MockTelemetry(); |
| 65 | + let handlerCalls = 0; |
| 66 | + const handler = () => { handlerCalls++; }; |
| 67 | + const rateLogger = new RateLogger(telemetry, false, handler); |
| 68 | + |
| 69 | + const e: any = new Error('Not Found'); |
| 70 | + e.status = 404; |
| 71 | + rateLogger.logApiError('/test', Promise.reject(e)); |
| 72 | + |
| 73 | + await new Promise(resolve => setImmediate(resolve)); |
| 74 | + assert.strictEqual(handlerCalls, 0); |
| 75 | + }); |
| 76 | + |
| 77 | + it('swallows exceptions thrown by the auth error handler', async () => { |
| 78 | + const telemetry = new MockTelemetry(); |
| 79 | + const handler = () => { throw new Error('handler failure'); }; |
| 80 | + const rateLogger = new RateLogger(telemetry, false, handler); |
| 81 | + |
| 82 | + const e: any = new Error('Bad credentials'); |
| 83 | + e.status = 401; |
| 84 | + // Should not throw. |
| 85 | + rateLogger.logApiError('/test', Promise.reject(e)); |
| 86 | + await new Promise(resolve => setImmediate(resolve)); |
| 87 | + }); |
| 88 | + |
| 89 | + it('works without an auth error handler', async () => { |
| 90 | + const telemetry = new MockTelemetry(); |
| 91 | + const rateLogger = new RateLogger(telemetry, false); |
| 92 | + |
| 93 | + const e: any = new Error('Bad credentials'); |
| 94 | + e.status = 401; |
| 95 | + rateLogger.logApiError('/test', Promise.reject(e)); |
| 96 | + await new Promise(resolve => setImmediate(resolve)); |
| 97 | + }); |
| 98 | + }); |
| 99 | +}); |
0 commit comments