from django.contrib.auth.models import User from django.http import QueryDict from django.test import TestCase from django.utils.translation import activate from django.urls import reverse from override_storage import override_storage from .models import File @override_storage() class FileTests(TestCase): test_user = 'testuser1' test_pass = '12345' def setUp(self): test_user1 = User.objects.create_user(username=self.test_user, password=self.test_pass) test_user1.save() activate('en-gb') self.file = File.objects.create(user=test_user1) def login(self): return self.client.login(username=self.test_user, password=self.test_pass) def test_post_not_logged_in(self): url = reverse('files:upload') next_url = QueryDict.fromkeys(['next',], url) login_url = reverse('auth_login') + '?' + next_url.urlencode() response = self.client.post(url, follow=True) self.assertRedirects(response, login_url) def test_delete_not_logged_in(self): url = reverse('files:delete', kwargs={'pk': self.file.pk}) next_url = QueryDict.fromkeys(['next',], url) login_url = reverse('auth_login') + '?' + next_url.urlencode() response = self.client.post(url, follow=True) self.assertRedirects(response, login_url) def test_post_and_delete(self): login = self.login() with open('apps/map/static/map/ojuso-logo-white.png', 'rb') as fp: response = self.client.post(reverse('files:upload_image'), { 'file': fp }) data = response.json() self.assertIsInstance(data['id'], int) self.assertEqual(data['is_valid'], True) response = self.client.post(reverse('files:delete_image', kwargs={ 'pk': data['id'] })) self.assertEqual(response.status_code, 200) data = response.json() self.assertEqual(data['success'], True)