preauth.js 1.7 KB

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