project_blobs.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const { expect } = require('chai')
  2. const config = require('config')
  3. const fs = require('fs')
  4. const fetch = require('node-fetch')
  5. const HTTPStatus = require('http-status')
  6. const cleanup = require('../storage/support/cleanup')
  7. const fixtures = require('../storage/support/fixtures')
  8. const testFiles = require('../storage/support/test_files')
  9. const testServer = require('./support/test_server')
  10. const { expectHttpError } = require('./support/expect_response')
  11. describe('Project blobs API', function () {
  12. const projectId = '123'
  13. beforeEach(cleanup.everything)
  14. beforeEach(fixtures.create)
  15. let client
  16. let token
  17. before(async function () {
  18. client = await testServer.createClientForProject(projectId)
  19. token = testServer.createTokenForProject(projectId)
  20. })
  21. it('returns 404 if the blob is not found', async function () {
  22. const testHash = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  23. await expectHttpError(
  24. client.apis.Project.getProjectBlob({
  25. project_id: projectId,
  26. hash: testHash,
  27. }),
  28. HTTPStatus.NOT_FOUND
  29. )
  30. })
  31. it('checks if file hash matches the hash parameter', async function () {
  32. const testHash = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  33. const response = await fetch(
  34. testServer.url(`/api/projects/${projectId}/blobs/${testHash}`),
  35. {
  36. method: 'PUT',
  37. headers: { Authorization: `Bearer ${token}` },
  38. body: fs.createReadStream(testFiles.path('hello.txt')),
  39. }
  40. )
  41. expect(response.status).to.equal(HTTPStatus.CONFLICT)
  42. // check that it did not store the file
  43. await expectHttpError(
  44. client.apis.Project.getProjectBlob({
  45. project_id: projectId,
  46. hash: testFiles.HELLO_TXT_HASH,
  47. }),
  48. HTTPStatus.NOT_FOUND
  49. )
  50. })
  51. it('rejects oversized files', async function () {
  52. const testHash = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  53. const buffer = Buffer.alloc(
  54. parseInt(config.get('maxFileUploadSize'), 10) + 1
  55. )
  56. const response = await fetch(
  57. testServer.url(`/api/projects/${projectId}/blobs/${testHash}`),
  58. {
  59. method: 'PUT',
  60. headers: { Authorization: `Bearer ${token}` },
  61. body: buffer,
  62. }
  63. )
  64. expect(response.status).to.equal(HTTPStatus.REQUEST_ENTITY_TOO_LARGE)
  65. })
  66. describe('with an existing blob', async function () {
  67. let fileContents
  68. beforeEach(async function () {
  69. fileContents = await fs.promises.readFile(testFiles.path('hello.txt'))
  70. const response = await fetch(
  71. testServer.url(
  72. `/api/projects/${projectId}/blobs/${testFiles.HELLO_TXT_HASH}`
  73. ),
  74. {
  75. method: 'PUT',
  76. headers: { Authorization: `Bearer ${token}` },
  77. body: fileContents,
  78. }
  79. )
  80. expect(response.ok).to.be.true
  81. })
  82. it('fulfills a request with a JWT header', async function () {
  83. const response = await client.apis.Project.getProjectBlob({
  84. project_id: projectId,
  85. hash: testFiles.HELLO_TXT_HASH,
  86. })
  87. const responseText = await response.data.text()
  88. expect(responseText).to.equal(fileContents.toString())
  89. })
  90. it('fulfills a request with a token parameter', async function () {
  91. const url = new URL(
  92. testServer.url(
  93. `/api/projects/${projectId}/blobs/${testFiles.HELLO_TXT_HASH}`
  94. )
  95. )
  96. url.searchParams.append('token', token)
  97. const response = await fetch(url)
  98. const payload = await response.text()
  99. expect(payload).to.equal(fileContents.toString())
  100. })
  101. it('rejects an unautorized request', async function () {
  102. const response = await fetch(
  103. testServer.url(
  104. `/api/projects/${projectId}/blobs/${testFiles.HELLO_TXT_HASH}`
  105. )
  106. )
  107. expect(response.status).to.equal(HTTPStatus.UNAUTHORIZED)
  108. })
  109. })
  110. })