test_api.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 apps.sourcecode.models import DebugSymbolBundle
  8. from glitchtip.test_utils.test_case import GlitchTipTestCaseMixin
  9. from ..models import File, FileBlob
  10. def generate_file():
  11. im_io = BytesIO()
  12. return InMemoryUploadedFile(
  13. im_io, None, "random-name.jpg", "image/jpeg", len(im_io.getvalue()), None
  14. )
  15. class ChunkUploadAPITestCase(GlitchTipTestCaseMixin, TestCase):
  16. def setUp(self):
  17. self.create_logged_in_user()
  18. self.url = reverse("api:get_chunk_upload_info", args=[self.organization.slug])
  19. def test_get(self):
  20. res = self.client.get(self.url)
  21. self.assertContains(res, self.organization.slug)
  22. def test_post(self):
  23. data = {"file_gzip": generate_file()}
  24. res = self.client.post(self.url, data)
  25. self.assertEqual(res.status_code, 200)
  26. res = self.client.post(self.url, data) # Should do nothing
  27. self.assertEqual(FileBlob.objects.count(), 1)
  28. class ReleaseAssembleAPITests(GlitchTipTestCaseMixin, TestCase):
  29. def setUp(self):
  30. self.create_logged_in_user()
  31. self.organization.slug = "whab"
  32. self.organization.save()
  33. self.release = baker.make(
  34. "releases.Release", version="lol", organization=self.organization
  35. )
  36. self.url = reverse(
  37. "api:assemble_release", args=[self.organization.slug, self.release.version]
  38. )
  39. def test_post(self):
  40. checksum = "e56191dcd7d54035f26f7dec999de2b1e4f10129"
  41. filename = "runtime-es2015.456e9ca9da400255beb4.js"
  42. map_filename = filename + ".map"
  43. zip_file = SimpleUploadedFile(
  44. checksum,
  45. open(os.path.dirname(__file__) + "/test_zip/" + checksum, "rb").read(),
  46. )
  47. FileBlob.objects.create(blob=zip_file, size=3635, checksum=checksum)
  48. res = self.client.post(
  49. self.url,
  50. {"checksum": checksum, "chunks": [checksum]},
  51. content_type="application/json",
  52. )
  53. self.assertEqual(res.status_code, 200)
  54. self.assertTrue(File.objects.get(name=filename))
  55. map_file = File.objects.get(name=map_filename)
  56. self.assertTrue(map_file)
  57. self.assertTrue(
  58. DebugSymbolBundle.objects.filter(
  59. sourcemap_file=map_file, release=self.release
  60. ).exists()
  61. )