test_api.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import os
  2. from io import BytesIO
  3. from django.core.files.uploadedfile import InMemoryUploadedFile, SimpleUploadedFile
  4. from django.test import TestCase
  5. from django.urls import reverse
  6. from model_bakery import baker
  7. from glitchtip.test_utils.test_case import GlitchTipTestCaseMixin
  8. from ..models import File, FileBlob
  9. def generate_file():
  10. im_io = BytesIO()
  11. return InMemoryUploadedFile(
  12. im_io, None, "random-name.jpg", "image/jpeg", len(im_io.getvalue()), None
  13. )
  14. class ChunkUploadAPITestCase(GlitchTipTestCaseMixin, TestCase):
  15. def setUp(self):
  16. self.create_logged_in_user()
  17. self.url = reverse("api:get_chunk_upload_info", args=[self.organization.slug])
  18. def test_get(self):
  19. res = self.client.get(self.url)
  20. self.assertContains(res, self.organization.slug)
  21. def test_post(self):
  22. data = {"file_gzip": generate_file()}
  23. res = self.client.post(self.url, data)
  24. self.assertEqual(res.status_code, 200)
  25. class ReleaseAssembleAPITests(GlitchTipTestCaseMixin, TestCase):
  26. def setUp(self):
  27. self.create_logged_in_user()
  28. self.organization.slug = "whab"
  29. self.organization.save()
  30. self.release = baker.make(
  31. "releases.Release", version="lol", organization=self.organization
  32. )
  33. self.url = reverse(
  34. "api:assemble_release", args=[self.organization.slug, self.release.version]
  35. )
  36. def test_post(self):
  37. checksum = "e56191dcd7d54035f26f7dec999de2b1e4f10129"
  38. filename = "runtime-es2015.456e9ca9da400255beb4.js"
  39. map_filename = filename + ".map"
  40. zip_file = SimpleUploadedFile(
  41. checksum,
  42. open(os.path.dirname(__file__) + "/test_zip/" + checksum, "rb").read(),
  43. )
  44. FileBlob.objects.create(blob=zip_file, size=3635, checksum=checksum)
  45. res = self.client.post(
  46. self.url,
  47. {"checksum": checksum, "chunks": [checksum]},
  48. content_type="application/json",
  49. )
  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())