|  | 
| 1 | 1 | import nock from 'nock'; | 
| 2 | 2 | import util from 'util'; | 
|  | 3 | +import { AccountSidConfig, UsernameConfig } from '../../../dist'; | 
| 3 | 4 | import { createGotClient } from '../../client'; | 
|  | 5 | +import { | 
|  | 6 | +  DEFAULT_TEST_CLIENT_CONFIG, | 
|  | 7 | +  DEFAULT_TEST_CLIENT_CONFIG_USERNAME_PASSWORD, | 
|  | 8 | +} from '../../__fixtures__/base-fixtures'; | 
| 4 | 9 | import { ClientApiError } from '../error'; | 
| 5 |  | -import { DEFAULT_TEST_CLIENT_CONFIG } from '../../__fixtures__/base-fixtures'; | 
| 6 | 10 | 
 | 
| 7 | 11 | describe('ClientApiError', () => { | 
| 8 | 12 |   let apiNock = nock('https://serverless.twilio.com'); | 
| 9 |  | -  let config = DEFAULT_TEST_CLIENT_CONFIG; | 
|  | 13 | +  let config: | 
|  | 14 | +    | UsernameConfig | 
|  | 15 | +    | AccountSidConfig = DEFAULT_TEST_CLIENT_CONFIG_USERNAME_PASSWORD; | 
| 10 | 16 |   let apiClient = createGotClient(config); | 
| 11 | 17 | 
 | 
| 12 | 18 |   beforeEach(() => { | 
| 13 | 19 |     apiNock = nock('https://serverless.twilio.com'); | 
|  | 20 | +    config = DEFAULT_TEST_CLIENT_CONFIG_USERNAME_PASSWORD; | 
| 14 | 21 |     apiClient = createGotClient(config); | 
| 15 | 22 |   }); | 
| 16 | 23 | 
 | 
| @@ -40,12 +47,73 @@ describe('ClientApiError', () => { | 
| 40 | 47 |       status: 400, | 
| 41 | 48 |     }); | 
| 42 | 49 | 
 | 
|  | 50 | +    config = DEFAULT_TEST_CLIENT_CONFIG; | 
|  | 51 | +    apiClient = createGotClient(config); | 
|  | 52 | + | 
|  | 53 | +    try { | 
|  | 54 | +      const resp = await apiClient.get('Services'); | 
|  | 55 | +    } catch (err) { | 
|  | 56 | +      const newError = new ClientApiError(err); | 
|  | 57 | +      const stringVersion = util.inspect(newError); | 
|  | 58 | +      expect(stringVersion.includes(config.authToken)).toBe(false); | 
|  | 59 | +    } | 
|  | 60 | +  }); | 
|  | 61 | + | 
|  | 62 | +  test('does not contain any password', async () => { | 
|  | 63 | +    const scope = apiNock.get('/v1/Services').reply(400, { | 
|  | 64 | +      code: 20001, | 
|  | 65 | +      message: 'Services are limited to less than or equal to 9000', | 
|  | 66 | +      more_info: 'https://www.twilio.com/docs/errors/20001', | 
|  | 67 | +      status: 400, | 
|  | 68 | +    }); | 
|  | 69 | + | 
| 43 | 70 |     try { | 
| 44 | 71 |       const resp = await apiClient.get('Services'); | 
| 45 | 72 |     } catch (err) { | 
| 46 | 73 |       const newError = new ClientApiError(err); | 
| 47 | 74 |       const stringVersion = util.inspect(newError); | 
| 48 |  | -      expect(stringVersion).not.toContain(config.authToken); | 
|  | 75 | +      expect(stringVersion.includes((config as UsernameConfig).password)).toBe( | 
|  | 76 | +        false | 
|  | 77 | +      ); | 
|  | 78 | +    } | 
|  | 79 | +  }); | 
|  | 80 | + | 
|  | 81 | +  test('contains URL and status code for basic HTTP Errors', async () => { | 
|  | 82 | +    const scope = apiNock.get('/v1/Services/ZS1111').reply(404, {}); | 
|  | 83 | + | 
|  | 84 | +    try { | 
|  | 85 | +      const resp = await apiClient.get('Services/ZS1111'); | 
|  | 86 | +    } catch (err) { | 
|  | 87 | +      const newError = new ClientApiError(err); | 
|  | 88 | +      const expectedUrl = 'https://serverless.twilio.com/v1/Services/ZS1111'; | 
|  | 89 | +      expect(newError.code).toEqual(404); | 
|  | 90 | +      expect(newError.url).toEqual(expectedUrl); | 
|  | 91 | + | 
|  | 92 | +      const stringVersion = util.inspect(newError); | 
|  | 93 | +      expect(stringVersion.includes(expectedUrl)).toBe(true); | 
|  | 94 | +      expect(stringVersion.includes('404')).toBe(true); | 
|  | 95 | +    } | 
|  | 96 | +  }); | 
|  | 97 | + | 
|  | 98 | +  test('contains URL and API code for Twilio API Errrors', async () => { | 
|  | 99 | +    const scope = apiNock.get('/v1/Services/ZS1111').reply(400, { | 
|  | 100 | +      code: 20001, | 
|  | 101 | +      message: 'Services are limited to less than or equal to 9000', | 
|  | 102 | +      more_info: 'https://www.twilio.com/docs/errors/20001', | 
|  | 103 | +      status: 400, | 
|  | 104 | +    }); | 
|  | 105 | + | 
|  | 106 | +    try { | 
|  | 107 | +      const resp = await apiClient.get('Services/ZS1111'); | 
|  | 108 | +    } catch (err) { | 
|  | 109 | +      const newError = new ClientApiError(err); | 
|  | 110 | +      const expectedUrl = 'https://serverless.twilio.com/v1/Services/ZS1111'; | 
|  | 111 | +      expect(newError.code).toEqual(20001); | 
|  | 112 | +      expect(newError.url).toEqual(expectedUrl); | 
|  | 113 | + | 
|  | 114 | +      const stringVersion = util.inspect(newError); | 
|  | 115 | +      expect(stringVersion.includes(expectedUrl)).toBe(true); | 
|  | 116 | +      expect(stringVersion.includes('20001')).toBe(true); | 
| 49 | 117 |     } | 
| 50 | 118 |   }); | 
| 51 | 119 | }); | 
0 commit comments