# -*- coding: utf-8 -*-
import os
import tempfile
from django.conf import settings
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test.client import Client
from openslides.mediafile.models import Mediafile
from openslides.participant.models import User
from openslides.utils.test import TestCase
class MediafileTest(TestCase):
"""
Unit test for the mediafile model.
"""
def setUp(self):
# Setup the three permissions
ct = ContentType.objects.get(app_label='mediafile', model='mediafile')
perm_1 = Permission.objects.get(content_type=ct, codename='can_see')
perm_2 = Permission.objects.get(content_type=ct, codename='can_upload')
# Setup three different users
self.manager = User.objects.get(pk=1)
self.vip_user = User.objects.create(username='mediafile_test_vip_user')
self.vip_user.reset_password('default')
self.vip_user.user_permissions.add(perm_1, perm_2)
self.normal_user = User.objects.create(username='mediafile_test_normal_user')
self.normal_user.reset_password('default')
# Setup a mediafile object
self.tmp_dir = settings.MEDIA_ROOT
tmpfile_no, mediafile_path = tempfile.mkstemp(prefix='tmp_openslides_test_', dir=self.tmp_dir)
self.object = Mediafile.objects.create(title='Title File 1', mediafile=mediafile_path, uploader=self.normal_user)
os.close(tmpfile_no)
def tearDown(self):
self.object.mediafile.delete()
def test_unicode(self):
self.assertEqual(self.object.__unicode__(), 'Title File 1')
def test_absolute_url(self):
self.assertEqual(self.object.get_absolute_url(), '/mediafile/1/edit/')
self.assertEqual(self.object.get_absolute_url('edit'), '/mediafile/1/edit/')
self.assertEqual(self.object.get_absolute_url('update'), '/mediafile/1/edit/')
self.assertEqual(self.object.get_absolute_url(link='delete'), '/mediafile/1/del/')
def login_clients(self):
"""
Helper function to login all three test users.
"""
client_manager = Client()
client_manager.login(username='admin', password='admin')
client_vip_user = Client()
client_vip_user.login(username='mediafile_test_vip_user', password='default')
client_normal_user = Client()
client_normal_user.login(username='mediafile_test_normal_user', password='default')
return {'client_manager': client_manager,
'client_vip_user': client_vip_user,
'client_normal_user': client_normal_user}
def test_see_mediafilelist(self):
for client in self.login_clients().itervalues():
response = client.get('/mediafile/')
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'mediafile/mediafile_list.html')
bad_client = Client()
response = bad_client.get('/mediafile/')
self.assertRedirects(response, expected_url='/login/?next=/mediafile/', status_code=302, target_status_code=200)
def test_upload_mediafile_get_request(self):
clients = self.login_clients()
response = clients['client_manager'].get('/mediafile/new/')
self.assertContains(response, '---------', status_code=200)
self.assertContains(response, '', status_code=200)
self.assertTemplateUsed(response, 'mediafile/mediafile_form.html')
response = clients['client_vip_user'].get('/mediafile/new/')
self.assertNotContains(response, '