|
| 1 | +# Facebook OAuth Tests and Service Logic |
| 2 | +First version of this file was targeted only on Tests update but after some research, |
| 3 | +decided to change some logic in `OAuth` handler too. |
| 4 | + |
| 5 | +General changes were intended to add additional tests for Facebook OAuth |
| 6 | +and provide a better WebExecuter error handling when Facebook throttling occurs. |
| 7 | + |
| 8 | +After some checks on OAuth endpoint logic, I found that error handling must be updated. |
| 9 | +Current error handling was passing a lot of extra data in response, that's shouldn't be shown to the client. |
| 10 | + |
| 11 | +## Tests changes |
| 12 | +### OAuth Error tests |
| 13 | +Tests use stub as `http.auth.test` method with custom error and check that `service` |
| 14 | +produces the correct response. |
| 15 | + |
| 16 | +These tests aim that OAuth Errors coming from `@hapi/bell` exist in service response: |
| 17 | +1. The Test checking that service responds with correct error in case of OAuth error. |
| 18 | +2. The Test checking that additional data do not exist in response. |
| 19 | + |
| 20 | +Also added one integrity test checking that `WebExecuter` will throw a different error when |
| 21 | +`waitFor...` timeout error is thrown. This error appears in the situation when `Page` wait's for some content, but this content or response not received because of some unpredicted errors. |
| 22 | + |
| 23 | +### WebExecuter Timeout Error |
| 24 | +Added Additional error processing logic to the `helpers/oauth/facebook/web-executer.js`, this will |
| 25 | +provide more informative output from tests. Now if `page.waitFor...` Timeout happens, the error message will |
| 26 | +contain last Service response or page contents. |
| 27 | + |
| 28 | +## Service logic changes |
| 29 | +### @hapi/bell Boom error handling |
| 30 | +In `production` ENV the Service still renders full error data coming from `@hapi/bell`. Stubbed OAuth call with simulated errors |
| 31 | +returns such response: |
| 32 | + |
| 33 | +```javascript |
| 34 | +const $ms_users_inj_post_messsge = { payload: |
| 35 | + { data: |
| 36 | + { data: { i_am_very_long_body: true, }, |
| 37 | + isBoom: true, |
| 38 | + isServer: false, |
| 39 | + output: |
| 40 | + { statusCode: 403, |
| 41 | + payload: |
| 42 | + { statusCode: 403, error: 'Forbidden', message: 'X-Throttled' }, |
| 43 | + headers: {} }, |
| 44 | + name: 'Error', |
| 45 | + message: 'X-Throttled', |
| 46 | + stack: |
| 47 | + 'Error: X-Throttled\n at Context.forbidden (/src/test/suites/oauth/facebook.js:97:34)\n at process._tickCallback (internal/process/next_tick.js:68:7)' }, |
| 48 | + isBoom: true, |
| 49 | + isServer: true, |
| 50 | + output: |
| 51 | + { statusCode: 500, |
| 52 | + payload: |
| 53 | + { statusCode: 500, |
| 54 | + error: 'Internal Server Error', |
| 55 | + message: 'An internal server error occurred' }, |
| 56 | + headers: {} }, |
| 57 | + name: 'Error', |
| 58 | + message: 'BadError' }, |
| 59 | + error: true, |
| 60 | + type: 'ms-users:attached', |
| 61 | + title: 'Failed to attach account', |
| 62 | + meta: {} } |
| 63 | +``` |
| 64 | + |
| 65 | +Even if `HTTP.IncomingMessage` deleted from error, too much data passed in response. |
| 66 | +Rendering of the Error is performed by `serialize-error` in Hook instead `Hapi` server and this shows all error contents. |
| 67 | + |
| 68 | +This great for debug purposes but not for passing to the client. |
| 69 | +I decided to add a new error `OAuthError` with custom `toJSON` serializer, which removes all unnecessary data |
| 70 | +and returns Simplified Object. This error is only for the `auth/OAuth` scope. |
| 71 | + |
| 72 | +After this change Errors render in this way: |
| 73 | + |
| 74 | +```javascript |
| 75 | +const p = { |
| 76 | + payload: { |
| 77 | + message: 'BadError', |
| 78 | + name: 'OAuthError', |
| 79 | + inner_error: { |
| 80 | + message: 'BadError', |
| 81 | + name: 'Error', |
| 82 | + stack: |
| 83 | + 'Error: BadError\n at Object.internal (/src/test/suites/oauth/facebook.js:106:28)\n at Object.invoke (/src/node_modules/sinon/lib/sinon/behavior.js:151:35)\n at module.exports.internals.Auth.functionStub (/src/node_modules/sinon/lib/sinon/stub.js:130:47)\n at Function.invoke (/src/node_modules/sinon/lib/sinon/spy.js:297:51)\n at module.exports.internals.Auth.functionStub (/src/node_modules/sinon/lib/sinon/spy.js:90:30)\n at Users.test (/src/src/auth/oauth/index.js:149:45)\n at Users.tryCatcher (/src/node_modules/bluebird/js/release/util.js:16:23)\n at Promise._settlePromiseFromHandler (/src/node_modules/bluebird/js/release/promise.js:517:31)\n at Promise._settlePromise (/src/node_modules/bluebird/js/release/promise.js:574:18)\n at Promise._settlePromiseCtx (/src/node_modules/bluebird/js/release/promise.js:611:10)\n at _drainQueueStep (/src/node_modules/bluebird/js/release/async.js:142:12)\n at _drainQueue (/src/node_modules/bluebird/js/release/async.js:131:9)\n at Async._drainQueues (/src/node_modules/bluebird/js/release/async.js:147:5)\n at Immediate.Async.drainQueues [as _onImmediate] (/src/node_modules/bluebird/js/release/async.js:17:14)\n at runCallback (timers.js:705:18)\n at tryOnImmediate (timers.js:676:5)\n at processImmediate (timers.js:658:5)', |
| 84 | + data: |
| 85 | + { message: 'X-Throttled', |
| 86 | + name: 'Error', |
| 87 | + stack: |
| 88 | + 'Error: X-Throttled\n at Context.forbidden (/src/test/suites/oauth/facebook.js:97:34)\n at process._tickCallback (internal/process/next_tick.js:68:7)', |
| 89 | + data: { i_am_very_long_body: true } } } }, |
| 90 | + error: true, |
| 91 | + type: 'ms-users:attached', |
| 92 | + title: 'Failed to attach account', |
| 93 | + meta: {} |
| 94 | +} |
| 95 | + |
| 96 | +``` |
| 97 | + |
| 98 | +## TODO |
| 99 | +- [x] WebExecuter Timeout handling |
| 100 | +- [x] Test checking that service return error |
| 101 | +- [x] `@hapi/boom` error serialization on `OAuth preResponse` hook |
| 102 | +- [x] Clean up code |
| 103 | +- [x] Cleanup docs |
| 104 | + |
0 commit comments