diff --git a/app/db.sqlite3 b/app/db.sqlite3 index 33293457..9eb11bb7 100644 Binary files a/app/db.sqlite3 and b/app/db.sqlite3 differ diff --git a/app/server/tests/test_doc_api.py b/app/server/tests/test_doc_api.py index 02ed58f8..d3bfba02 100644 --- a/app/server/tests/test_doc_api.py +++ b/app/server/tests/test_doc_api.py @@ -63,35 +63,3 @@ class TestDocAPI(APITestCase): response = self.client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) - - def test_create_doc_by_admin(self): - """ - Ensure we can create a new document object by admin. - """ - # user = self.create_superuser() - # project = self.create_project() - # project.users.add(user) - # - # data = {'text': 'doc', 'labels': []} - # url = reverse('docs', args=[project.id]) - # self.client.login(username=self.username, password=self.password) - # response = self.client.post(url, data, format='json') - # - # self.assertEqual(response.status_code, status.HTTP_201_CREATED) - # self.assertEqual(Document.objects.count(), 1) - # self.assertEqual(Document.objects.get().text, 'doc') - - def test_create_doc_by_user(self): - """ - Ensure we cannot create a new project object by user. - """ - user = self.create_user() - project = self.create_project() - project.users.add(user) - - data = {'text': 'doc', 'labels': []} - url = reverse('docs', args=[project.id]) - self.client.login(username=self.username, password=self.password) - response = self.client.post(url, data, format='json') - - self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) diff --git a/app/server/tests/test_views.py b/app/server/tests/test_views.py new file mode 100644 index 00000000..faccd65e --- /dev/null +++ b/app/server/tests/test_views.py @@ -0,0 +1,62 @@ +import os +from django.urls import reverse +from django.test import TestCase, Client +from rest_framework import status +from mixer.backend.django import mixer +from ..models import * + + +class TestUpload(TestCase): + + def setUp(self): + self.username, self.password = 'user', 'pass' + self.client = Client() + self.filepath = os.path.join(os.path.dirname(__file__), 'data/test.csv') + + def create_user(self): + user = User.objects.create_user(username=self.username, password=self.password) + + return user + + def create_superuser(self): + user = User.objects.create_superuser(username=self.username, + password=self.password, + email='hoge@example.com') + return user + + def create_project(self): + project = mixer.blend('server.Project') + + return project + + def test_create_doc_by_admin(self): + """ + Ensure we can create a new document object by admin. + """ + user = self.create_superuser() + project = self.create_project() + project.users.add(user) + + with open(self.filepath) as f: + url = reverse('upload', args=[project.id]) + self.client.login(username=self.username, password=self.password) + response = self.client.post(url, {'csv_file': f}) + + self.assertEqual(response.status_code, status.HTTP_302_FOUND) + self.assertEqual(Document.objects.count(), 3) + + def test_create_doc_by_user(self): + """ + Ensure we cannot create a new project object by user. + """ + user = self.create_user() + project = self.create_project() + project.users.add(user) + + with open(self.filepath) as f: + url = reverse('upload', args=[project.id]) + self.client.login(username=self.username, password=self.password) + response = self.client.post(url, {'csv_file': f}) + + self.assertEqual(response.status_code, status.HTTP_302_FOUND) + self.assertEqual(Document.objects.count(), 0)