test_views.py 2.2 KB

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