You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.8 KiB

  1. import os
  2. from django.urls import reverse
  3. from django.test import TestCase, Client
  4. from rest_framework import status
  5. from mixer.backend.django import mixer
  6. from ..models import User, Document
  7. class TestUpload(TestCase):
  8. def setUp(self):
  9. self.username, self.password = 'user', 'pass'
  10. self.client = Client()
  11. self.csv_path = os.path.join(os.path.dirname(__file__), 'data/test.csv')
  12. self.json_path = os.path.join(os.path.dirname(__file__), 'data/test.jsonl')
  13. def create_user(self):
  14. user = User.objects.create_user(username=self.username, password=self.password)
  15. return user
  16. def create_superuser(self):
  17. user = User.objects.create_superuser(username=self.username,
  18. password=self.password,
  19. email='hoge@example.com')
  20. return user
  21. def create_project(self):
  22. project = mixer.blend('server.Project')
  23. return project
  24. def test_upload_csv_by_admin(self):
  25. """
  26. Ensure we can create a new document object by admin.
  27. """
  28. user = self.create_superuser()
  29. project = self.create_project()
  30. project.users.add(user)
  31. with open(self.csv_path) as f:
  32. url = reverse('upload', args=[project.id])
  33. self.client.login(username=self.username, password=self.password)
  34. response = self.client.post(url, {'file': f, 'format': 'csv'})
  35. self.assertEqual(response.status_code, status.HTTP_302_FOUND)
  36. self.assertEqual(Document.objects.count(), 3)
  37. def test_upload_csv_by_user(self):
  38. """
  39. Ensure we cannot create a new project object by user.
  40. """
  41. user = self.create_user()
  42. project = self.create_project()
  43. project.users.add(user)
  44. with open(self.csv_path) as f:
  45. url = reverse('upload', args=[project.id])
  46. self.client.login(username=self.username, password=self.password)
  47. response = self.client.post(url, {'file': f, 'format': 'csv'})
  48. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  49. self.assertEqual(Document.objects.count(), 0)
  50. def test_upload_json_by_admin(self):
  51. """
  52. Ensure we can create a new document object from json by admin.
  53. """
  54. user = self.create_superuser()
  55. project = self.create_project()
  56. project.users.add(user)
  57. with open(self.json_path, 'r') as f:
  58. url = reverse('upload', args=[project.id])
  59. self.client.login(username=self.username, password=self.password)
  60. response = self.client.post(url, {'file': f, 'format': 'json'})
  61. self.assertEqual(response.status_code, status.HTTP_302_FOUND)
  62. self.assertEqual(Document.objects.count(), 18)