mirror of
https://github.com/Labelbase/Labelbase.git
synced 2026-08-13 12:33:23 +02:00
68 lines
2.4 KiB
Python
68 lines
2.4 KiB
Python
# -*- encoding: utf-8 -*-
|
|
from __future__ import unicode_literals
|
|
|
|
from django.contrib.auth.models import Permission, User
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
|
from django.test import TestCase
|
|
from django.urls import reverse
|
|
|
|
from ..models import Attachment
|
|
from .testapp.models import TestModel
|
|
|
|
|
|
class BaseTestCase(TestCase):
|
|
target_model_class = TestModel
|
|
|
|
def setUp(self):
|
|
"""
|
|
Create two users with `attachments.add_attachment` permission
|
|
and one object to attach files to.
|
|
"""
|
|
content_type = ContentType.objects.get_for_model(Attachment)
|
|
self.add_permission = Permission.objects.get(
|
|
content_type=content_type, codename="add_attachment"
|
|
)
|
|
self.del_permission = Permission.objects.get(
|
|
content_type=content_type, codename="delete_attachment"
|
|
)
|
|
|
|
self.del_foreign_permission = Permission.objects.get(
|
|
content_type=content_type, codename="delete_foreign_attachments"
|
|
)
|
|
|
|
self.cred_jon = {"username": "jon", "password": "foobar"}
|
|
self.cred_jane = {"username": "jane", "password": "foobar"}
|
|
self.jon = User.objects.create_user(**self.cred_jon)
|
|
self.jon.user_permissions.add(self.add_permission)
|
|
self.jon.user_permissions.add(self.del_permission)
|
|
|
|
self.jane = User.objects.create_user(**self.cred_jane)
|
|
self.jane.user_permissions.add(self.add_permission)
|
|
self.jane.user_permissions.add(self.del_permission)
|
|
|
|
self.obj = self.target_model_class.objects.create(title="My first test item")
|
|
|
|
def _upload_testfile(self, file_obj=None, file_content=b"file content", **extra):
|
|
"""
|
|
Uploads a sample file for the given user.
|
|
"""
|
|
add_url = reverse(
|
|
"attachments:add",
|
|
kwargs={
|
|
"app_label": "testapp",
|
|
"model_name": self.target_model_class.__name__.lower(),
|
|
"pk": self.obj.pk,
|
|
},
|
|
)
|
|
|
|
if not file_obj:
|
|
file_obj = SimpleUploadedFile(
|
|
"Ünicode Filename 🙂.jpg",
|
|
file_content,
|
|
content_type="image/jpeg",
|
|
)
|
|
return self.client.post(
|
|
add_url, {"attachment_file": file_obj}, follow=True,
|
|
**extra
|
|
)
|