V1ApiTests.mjs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { expect } from 'chai'
  2. import V1Api from '../../../app/src/Features/V1/V1Api.mjs'
  3. import MockV1ApiClass from './mocks/MockV1Api.mjs'
  4. import Features from '../../../app/src/infrastructure/Features.mjs'
  5. describe('V1Api', function () {
  6. const testPath = '/api/v1/overleaf/fake_route_api_handler_tests'
  7. function testPathWithResponse(statusCode, body = '') {
  8. const encodedBody = encodeURIComponent(JSON.stringify(body))
  9. return `${testPath}?expectedStatus=${statusCode}&expectedBody=${encodedBody}`
  10. }
  11. beforeEach(function () {
  12. if (!Features.hasFeature('saas')) {
  13. this.skip()
  14. }
  15. MockV1ApiClass.instance()
  16. })
  17. it('returns errors from request handling', async function () {
  18. let error
  19. try {
  20. await V1Api.promises.request({})
  21. } catch (e) {
  22. error = e
  23. }
  24. expect(error).to.exist
  25. expect(error.cause.message).to.equal(
  26. 'options.uri must be a string when using options.baseUrl'
  27. )
  28. })
  29. it('returns 500 errors', async function () {
  30. let error
  31. const expectedBody = { testing: 'test' }
  32. const url = testPathWithResponse(500, expectedBody)
  33. try {
  34. await V1Api.promises.request({ url })
  35. } catch (e) {
  36. error = e
  37. }
  38. expect(error).to.exist
  39. expect(error.message).to.equal('error from V1 API')
  40. expect(error.info.status).to.equal(500)
  41. expect(error.info.body).to.deep.equal(expectedBody)
  42. })
  43. it('returns 2xx status responses', async function () {
  44. const expectedBody = { testing: 'success' }
  45. const url = testPathWithResponse(200, expectedBody)
  46. const response = await V1Api.promises.request({ url })
  47. expect(response.response.statusCode).to.equal(200)
  48. expect(response.body).to.deep.equal(expectedBody)
  49. })
  50. it('returns 403 errors', async function () {
  51. let error
  52. const url = testPathWithResponse(403)
  53. try {
  54. await V1Api.promises.request({ url })
  55. } catch (e) {
  56. error = e
  57. }
  58. expect(error).to.exist
  59. expect(error.message).to.equal('overleaf v1 returned forbidden')
  60. expect(error.statusCode).to.equal(403)
  61. })
  62. it('returns 404 errors', async function () {
  63. let error
  64. const url = testPathWithResponse(404)
  65. try {
  66. await V1Api.promises.request({ uri: url, method: 'GET' })
  67. } catch (e) {
  68. error = e
  69. }
  70. expect(error).to.exist
  71. expect(error.message).to.equal(
  72. `overleaf v1 returned non-success code: 404 GET ${url}`
  73. )
  74. expect(error.statusCode).to.equal(404)
  75. })
  76. it('returns errors', async function () {
  77. let error
  78. const url = testPathWithResponse(402)
  79. try {
  80. await V1Api.promises.request({ uri: url, method: 'GET' })
  81. } catch (e) {
  82. error = e
  83. }
  84. expect(error).to.exist
  85. expect(error.message).to.equal('overleaf v1 returned non-success code')
  86. expect(error.statusCode).to.equal(402)
  87. })
  88. it('does not return error if expected status', async function () {
  89. const url = testPathWithResponse(404)
  90. const response = await V1Api.promises.request({
  91. url,
  92. expectedStatusCodes: [404],
  93. })
  94. expect(response.response.statusCode).to.equal(404)
  95. })
  96. })