test_views.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import os
  2. from io import BytesIO
  3. from django.core.files.uploadedfile import InMemoryUploadedFile, SimpleUploadedFile
  4. from django.urls import reverse
  5. from model_bakery import baker
  6. from glitchtip.test_utils.test_case import GlitchTipTestCase
  7. from ..models import File, FileBlob
  8. def generate_file():
  9. im_io = BytesIO()
  10. return InMemoryUploadedFile(
  11. im_io, None, "random-name.jpg", "image/jpeg", len(im_io.getvalue()), None
  12. )
  13. class ChunkUploadAPITestCase(GlitchTipTestCase):
  14. def setUp(self):
  15. self.create_user_and_project()
  16. self.url = reverse(
  17. "chunk-upload", kwargs={"organization_slug": self.organization}
  18. )
  19. def test_get(self):
  20. res = self.client.get(self.url)
  21. self.assertEqual(res.status_code, 200)
  22. def test_post(self):
  23. file = generate_file()
  24. data = {"file_gzip": file}
  25. res = self.client.post(self.url, data)
  26. self.assertEqual(res.status_code, 200)
  27. class ReleaseAssembleAPITests(GlitchTipTestCase):
  28. def setUp(self):
  29. self.create_user_and_project()
  30. self.organization.slug = "whab"
  31. self.organization.save()
  32. self.release = baker.make(
  33. "releases.Release", version="lol", organization=self.organization
  34. )
  35. self.url = reverse(
  36. "organization-releases-assemble",
  37. kwargs={
  38. "organization_slug": self.organization.slug,
  39. "version": self.release.version,
  40. },
  41. )
  42. def test_post(self):
  43. checksum = "e56191dcd7d54035f26f7dec999de2b1e4f10129"
  44. filename = "runtime-es2015.456e9ca9da400255beb4.js"
  45. map_filename = filename + ".map"
  46. zip_file = SimpleUploadedFile(
  47. checksum,
  48. open(os.path.dirname(__file__) + "/test_zip/" + checksum, "rb").read(),
  49. )
  50. FileBlob.objects.create(blob=zip_file, size=3635, checksum=checksum)
  51. res = self.client.post(self.url, {"checksum": checksum, "chunks": [checksum]})
  52. self.assertEqual(res.status_code, 200)
  53. self.assertTrue(File.objects.get(name=filename))
  54. map_file = File.objects.get(name=map_filename)
  55. self.assertTrue(map_file)
  56. self.assertTrue(map_file.releasefile_set.filter(release=self.release).exists())