preauth.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. jest.mock('../../src/server/helpers/jwt', () => {
  2. return {
  3. generateToken: () => {},
  4. verifyToken: () => {},
  5. generateEncryptedToken: () => {
  6. return 'dummy token'
  7. },
  8. verifyEncryptedToken: () => {
  9. return { payload: '' }
  10. },
  11. addToCookies: () => {},
  12. removeFromCookies: () => {},
  13. }
  14. })
  15. const request = require('supertest')
  16. const { getServer } = require('../mockserver')
  17. // the order in which getServer is called matters because, once an env is passed,
  18. // it won't be overridden when you call getServer without an argument
  19. const serverWithFixedOauth = getServer()
  20. const serverWithDynamicOauth = getServer({ COMPANION_DROPBOX_KEYS_ENDPOINT: 'http://localhost:1000/endpoint' })
  21. describe('handle preauth endpoint', () => {
  22. test('happy path', () => {
  23. return request(serverWithDynamicOauth)
  24. .post('/dropbox/preauth')
  25. .set('Content-Type', 'application/json')
  26. .send({
  27. params: 'param value',
  28. })
  29. .expect(200)
  30. // see jwt.generateEncryptedToken mock above
  31. .then((res) => expect(res.body.token).toBe('dummy token'))
  32. })
  33. test('preauth request without params in body', () => {
  34. return request(serverWithDynamicOauth)
  35. .post('/dropbox/preauth')
  36. .set('Content-Type', 'application/json')
  37. .send({
  38. notParams: 'value',
  39. })
  40. .expect(400)
  41. })
  42. test('providers with dynamic credentials disabled', () => {
  43. return request(serverWithDynamicOauth)
  44. .post('/drive/preauth')
  45. .set('Content-Type', 'application/json')
  46. .send({
  47. params: 'param value',
  48. })
  49. .expect(501)
  50. })
  51. test('server with dynamic credentials disabled', () => {
  52. return request(serverWithFixedOauth)
  53. .post('/dropbox/preauth')
  54. .set('Content-Type', 'application/json')
  55. .send({
  56. params: 'param value',
  57. })
  58. .expect(501)
  59. })
  60. })