preauth.js 1.9 KB

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