Small modifications. Enhencement of tests.

This commit is contained in:
Norman Jäckel 2013-03-18 21:50:50 +01:00
parent ad0e157bd1
commit d82bb6ee33
3 changed files with 30 additions and 11 deletions

View File

@ -43,7 +43,7 @@ class MediafileCreateView(CreateView):
def get_form(self, form_class): def get_form(self, form_class):
form_kwargs = self.get_form_kwargs() form_kwargs = self.get_form_kwargs()
if self.request.method == 'GET': if self.request.method == 'GET':
form_kwargs['initial'].update({'uploader': self.request.user.person_id}) # TODO: Check this. form_kwargs['initial'].update({'uploader': self.request.user.person_id})
if not self.request.user.has_perm('mediafile.can_manage'): if not self.request.user.has_perm('mediafile.can_manage'):
# Return our own ModelForm # Return our own ModelForm
return MediafileNormalUserCreateForm(**form_kwargs) return MediafileNormalUserCreateForm(**form_kwargs)
@ -52,6 +52,13 @@ class MediafileCreateView(CreateView):
return form_class(**form_kwargs) return form_class(**form_kwargs)
def manipulate_object(self, *args, **kwargs): def manipulate_object(self, *args, **kwargs):
"""Method to handle the uploader
If a user has manager permissions, he has to set the uploader
in the given form field. Then this method only calls super.
Else it sets the requesting user as uploader.
"""
if not self.request.user.has_perm('mediafile.can_manage'): if not self.request.user.has_perm('mediafile.can_manage'):
self.object.uploader = self.request.user self.object.uploader = self.request.user
return super(MediafileCreateView, self).manipulate_object(*args, **kwargs) return super(MediafileCreateView, self).manipulate_object(*args, **kwargs)

View File

@ -82,22 +82,25 @@ class MediafileTest(TestCase):
for client in self.login_clients().itervalues(): for client in self.login_clients().itervalues():
response = client.get('/mediafile/') response = client.get('/mediafile/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'mediafile/mediafile_list.html')
bad_client = Client() bad_client = Client()
response = bad_client.get('/mediafile/') response = bad_client.get('/mediafile/')
self.assertEqual(response.status_code, 302) self.assertRedirects(response, expected_url='/login/?next=/mediafile/', status_code=302, target_status_code=200)
def test_upload_mediafile_get_request(self): def test_upload_mediafile_get_request(self):
clients = self.login_clients() clients = self.login_clients()
response = clients['client_manager'].get('/mediafile/new/') response = clients['client_manager'].get('/mediafile/new/')
self.assertContains(response, '---------', status_code=200) self.assertContains(response, '---------', status_code=200)
self.assertContains(response, '<option value="user:1" selected="selected">mediafile_test_manager</option>', status_code=200) self.assertContains(response, '<option value="user:1" selected="selected">mediafile_test_manager</option>', status_code=200)
self.assertTemplateUsed(response, 'mediafile/mediafile_form.html')
response = clients['client_vip_user'].get('/mediafile/new/') response = clients['client_vip_user'].get('/mediafile/new/')
self.assertEqual(response.status_code, 200) self.assertNotContains(response, '<select id="id_uploader" name="uploader">', status_code=200)
self.assertTemplateUsed(response, 'mediafile/mediafile_form.html')
response = clients['client_normal_user'].get('/mediafile/new/') response = clients['client_normal_user'].get('/mediafile/new/')
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
bad_client = Client() bad_client = Client()
response = bad_client.get('/mediafile/new/') response = bad_client.get('/mediafile/new/')
self.assertEqual(response.status_code, 302) self.assertRedirects(response, expected_url='/login/?next=/mediafile/new/', status_code=302, target_status_code=200)
def test_upload_mediafile_post_request(self): def test_upload_mediafile_post_request(self):
# Test first user # Test first user
@ -106,10 +109,11 @@ class MediafileTest(TestCase):
response_1 = client_1.post('/mediafile/new/', response_1 = client_1.post('/mediafile/new/',
{'title': 'new_test_file_title_1', {'title': 'new_test_file_title_1',
'mediafile': new_file_1, 'mediafile': new_file_1,
'uploader': self.manager.person_id}) 'uploader': self.normal_user.person_id})
self.assertEqual(response_1.status_code, 302) self.assertRedirects(response_1, expected_url='/mediafile/', status_code=302, target_status_code=200)
object_1 = Mediafile.objects.latest('timestamp') object_1 = Mediafile.objects.latest('timestamp')
self.assertEqual(object_1.mediafile.url, '/media/file/new_test_file.txt') self.assertEqual(object_1.mediafile.url, '/media/file/new_test_file.txt')
self.assertEqual(object_1.uploader, self.normal_user)
path_1 = object_1.mediafile.path path_1 = object_1.mediafile.path
object_1.mediafile.delete() object_1.mediafile.delete()
self.assertFalse(os.path.exists(path_1)) self.assertFalse(os.path.exists(path_1))
@ -121,8 +125,11 @@ class MediafileTest(TestCase):
{'title': 'new_test_file_title_2', {'title': 'new_test_file_title_2',
'mediafile': new_file_2}) 'mediafile': new_file_2})
self.assertEqual(response_2.status_code, 302) self.assertEqual(response_2.status_code, 302)
# TODO: Check, why this does not work.
# self.assertRedirects(response_2, expected_url='/mediafile/', status_code=302, target_status_code=200)
object_2 = Mediafile.objects.latest('timestamp') object_2 = Mediafile.objects.latest('timestamp')
self.assertEqual(object_2.mediafile.url, '/media/file/new_test_file.txt') self.assertEqual(object_2.mediafile.url, '/media/file/new_test_file.txt')
self.assertEqual(object_2.uploader, self.vip_user)
path_2 = object_2.mediafile.path path_2 = object_2.mediafile.path
object_2.mediafile.delete() object_2.mediafile.delete()
self.assertFalse(os.path.exists(path_2)) self.assertFalse(os.path.exists(path_2))
@ -140,13 +147,14 @@ class MediafileTest(TestCase):
response = clients['client_manager'].get('/mediafile/1/edit/') response = clients['client_manager'].get('/mediafile/1/edit/')
self.assertContains(response, '---------', status_code=200) self.assertContains(response, '---------', status_code=200)
self.assertContains(response, '<option value="user:2" selected="selected">mediafile_test_vip_user</option>', status_code=200) self.assertContains(response, '<option value="user:2" selected="selected">mediafile_test_vip_user</option>', status_code=200)
self.assertTemplateUsed(response, 'mediafile/mediafile_form.html')
response = clients['client_vip_user'].get('/mediafile/1/edit/') response = clients['client_vip_user'].get('/mediafile/1/edit/')
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
response = clients['client_normal_user'].get('/mediafile/1/edit/') response = clients['client_normal_user'].get('/mediafile/1/edit/')
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
bad_client = Client() bad_client = Client()
response = bad_client.get('/mediafile/1/edit/') response = bad_client.get('/mediafile/1/edit/')
self.assertEqual(response.status_code, 302) self.assertRedirects(response, expected_url='/login/?next=/mediafile/1/edit/', status_code=302, target_status_code=200)
def test_edit_mediafile_post_request(self): def test_edit_mediafile_post_request(self):
# Test only one user # Test only one user
@ -170,14 +178,14 @@ class MediafileTest(TestCase):
def test_delete_mediafile_get_request(self): def test_delete_mediafile_get_request(self):
clients = self.login_clients() clients = self.login_clients()
response = clients['client_manager'].get('/mediafile/1/del/') response = clients['client_manager'].get('/mediafile/1/del/')
self.assertEqual(response.status_code, 302) self.assertRedirects(response, expected_url='/mediafile/1/edit/', status_code=302, target_status_code=200)
response = clients['client_vip_user'].get('/mediafile/1/del/') response = clients['client_vip_user'].get('/mediafile/1/del/')
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
response = clients['client_normal_user'].get('/mediafile/1/del/') response = clients['client_normal_user'].get('/mediafile/1/del/')
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
bad_client = Client() bad_client = Client()
response = bad_client.get('/mediafile/2/del/') response = bad_client.get('/mediafile/2/del/')
self.assertEqual(response.status_code, 302) self.assertRedirects(response, expected_url='/login/?next=/mediafile/2/del/', status_code=302, target_status_code=200)
def test_delete_mediafile_post_request(self): def test_delete_mediafile_post_request(self):
tmpfile_no, mediafile_3_path = tempfile.mkstemp(prefix='tmp_openslides_test', dir=self.tmp_dir) tmpfile_no, mediafile_3_path = tempfile.mkstemp(prefix='tmp_openslides_test', dir=self.tmp_dir)
@ -185,7 +193,7 @@ class MediafileTest(TestCase):
object_3 = Mediafile.objects.create(title='Title File 3', mediafile=mediafile_3_path) object_3 = Mediafile.objects.create(title='Title File 3', mediafile=mediafile_3_path)
client_1 = self.login_clients()['client_manager'] client_1 = self.login_clients()['client_manager']
response_1 = client_1.post('/mediafile/2/del/', {'yes': 'foo'}) response_1 = client_1.post('/mediafile/2/del/', {'yes': 'foo'})
self.assertEqual(response_1.status_code, 302) self.assertRedirects(response_1, expected_url='/mediafile/', status_code=302, target_status_code=200)
self.assertFalse(os.path.exists(object_3.mediafile.path)) self.assertFalse(os.path.exists(object_3.mediafile.path))
def test_filesize(self): def test_filesize(self):

View File

@ -9,7 +9,7 @@ from openslides.global_settings import *
DEBUG = True DEBUG = True
TEMPLATE_DEBUG = DEBUG TEMPLATE_DEBUG = DEBUG
DBPATH = u'database.sqlite' DBPATH = ''
DATABASES = { DATABASES = {
'default': { 'default': {
@ -34,3 +34,7 @@ INSTALLED_PLUGINS = (
) )
INSTALLED_APPS += INSTALLED_PLUGINS INSTALLED_APPS += INSTALLED_PLUGINS
# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = ''