2018-05-01 17:16:09 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.http import QueryDict
|
2018-04-19 19:52:40 +00:00
|
|
|
from django.test import TestCase
|
2018-05-01 17:16:09 +00:00
|
|
|
from django.utils.translation import activate
|
|
|
|
from django.urls import reverse
|
2018-04-19 19:52:40 +00:00
|
|
|
|
2018-05-01 17:16:09 +00:00
|
|
|
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'), {
|
|
|
|
'file': fp
|
|
|
|
})
|
|
|
|
|
|
|
|
data = response.json()
|
2018-06-27 18:43:39 +00:00
|
|
|
self.assertIsInstance(data['id'], int)
|
2018-05-01 17:16:09 +00:00
|
|
|
self.assertEqual(data['is_valid'], True)
|
|
|
|
|
|
|
|
response = self.client.post(reverse('files:delete', kwargs={
|
|
|
|
'pk': data['id']
|
|
|
|
}))
|
|
|
|
|
2018-06-27 18:43:39 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2018-05-01 17:16:09 +00:00
|
|
|
data = response.json()
|
|
|
|
|
|
|
|
self.assertEqual(data['success'], True)
|