Skip to content

Commit

Permalink
#280 Fix poleno/attachments/tests
Browse files Browse the repository at this point in the history
  • Loading branch information
viliambalaz committed Aug 11, 2020
1 parent aece8a2 commit 7b89090
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 48 deletions.
46 changes: 21 additions & 25 deletions poleno/attachments/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# vim: expandtab
# -*- coding: utf-8 -*-
import unittest
from testfixtures import TempDirectory

from django import forms
Expand All @@ -14,6 +13,7 @@
from ..models import Attachment
from ..forms import AttachmentsField


class AttachmentsFieldTest(TestCase):
u"""
Tests ``AttachmentsField`` form field with ``AttachmentsWidget`` form widget.
Expand Down Expand Up @@ -44,12 +44,12 @@ def setUp(self):


self.user1 = User.objects.create_user(u'john', u'[email protected]', u'johnpassword')
self.attachment1a = Attachment.objects.create(generic_object=self.user1, file=ContentFile(u'content1a'), name=u'filename1a', content_type=u'text/plain')
self.attachment1b = Attachment.objects.create(generic_object=self.user1, file=ContentFile(u'content1b'), name=u'filename1b', content_type=u'text/plain')
self.attachment1c = Attachment.objects.create(generic_object=self.user1, file=ContentFile(u'content1c'), name=u'filename1c', content_type=u'text/plain')
self.attachment1a = Attachment.objects.create(generic_object=self.user1, file=ContentFile(u'content1a'), name=u'filename1a.txt', content_type=u'text/plain')
self.attachment1b = Attachment.objects.create(generic_object=self.user1, file=ContentFile(u'content1b'), name=u'filename1b.txt', content_type=u'text/plain')
self.attachment1c = Attachment.objects.create(generic_object=self.user1, file=ContentFile(u'content1c'), name=u'filename1c.txt', content_type=u'text/plain')

self.user2 = User.objects.create_user(u'smith', u'[email protected]', u'big_secret')
self.attachment2 = Attachment.objects.create(generic_object=self.user2, file=ContentFile(u'content2'), name=u'filename2', content_type=u'text/plain')
self.attachment2 = Attachment.objects.create(generic_object=self.user2, file=ContentFile(u'content2'), name=u'filename2.txt', content_type=u'text/plain')

def tearDown(self):
self.settings_override.disable()
Expand All @@ -60,36 +60,35 @@ def _render(self, template, **context):
return Template(template).render(Context(context))


@unittest.skip(u'FIXME')
def test_new_form(self):
form = self.AttachmentsFieldForm(attached_to=self.user1)
rendered = self._render(u'{{ form }}', form=form)
self.assertInHTML(u'<label for="id_attachments">Attachments:</label>', rendered)
self.assertInHTML(u'<input id="id_attachments" name="attachments" type="hidden" value=",,">', rendered)
self.assertInHTML(u"""
<span class="btn btn-default btn-file"><i class="icon-folder-open"></i> Browse
<input class="fileupload" type="file" name="files" multiple="multiple" data-url="/upload/"
data-field="#id_attachments" data-target="#attachments-target">
</span>
<div class="btn btn-default pln-attachments-btn">
<i class="chv-icon chv-icon-lg icon-attach"></i>
&nbsp;
Browse
<input type="file" name="files" multiple="multiple" data-url="/upload/">
</div>
""", rendered)

@unittest.skip(u'FIXME')
def test_new_form_with_initial_value_as_list_of_attachment_instances(self):
attachments = [self.attachment1a, self.attachment1b]
form = self.AttachmentsFieldForm(initial={u'attachments': attachments}, attached_to=self.user1)
rendered = self._render(u'{{ form }}', form=form)
self.assertInHTML(u'<input id="id_attachments" name="attachments" type="hidden" value=",%s,%s,">' % (self.attachment1a.pk, self.attachment1b.pk), rendered)
self.assertInHTML(u'<a href="/download/%s/">filename1a</a>' % self.attachment1a.pk, rendered)
self.assertInHTML(u'<a href="/download/%s/">filename1b</a>' % self.attachment1b.pk, rendered)
self.assertInHTML(u'<a href="/download/%s/">filename1a.txt</a>' % self.attachment1a.pk, rendered)
self.assertInHTML(u'<a href="/download/%s/">filename1b.txt</a>' % self.attachment1b.pk, rendered)

@unittest.skip(u'FIXME')
def test_new_form_with_initial_value_as_comma_separated_string_of_attachment_pks(self):
attachments = u'%s,%s' % (self.attachment1a.pk, self.attachment1b.pk)
form = self.AttachmentsFieldForm(initial={u'attachments': attachments}, attached_to=self.user1)
rendered = self._render(u'{{ form }}', form=form)
self.assertInHTML(u'<input id="id_attachments" name="attachments" type="hidden" value=",%s,">' % attachments, rendered)
self.assertInHTML(u'<a href="/download/%s/">filename1a</a>' % self.attachment1a.pk, rendered)
self.assertInHTML(u'<a href="/download/%s/">filename1b</a>' % self.attachment1b.pk, rendered)
self.assertInHTML(u'<a href="/download/%s/">filename1a.txt</a>' % self.attachment1a.pk, rendered)
self.assertInHTML(u'<a href="/download/%s/">filename1b.txt</a>' % self.attachment1b.pk, rendered)

def test_submitted_form_with_no_attachments_but_required(self):
attachments = u',,,,'
Expand All @@ -111,7 +110,6 @@ def test_submitted_form_with_no_attachments_but_not_required(self):
self.assertInHTML(u'<li>This field is required.</li>', rendered, count=0)
self.assertInHTML(u'<input id="id_attachments" name="attachments" type="hidden" value=",,">', rendered)

@unittest.skip(u'FIXME')
def test_submitted_form_with_one_attachment(self):
attachments = u',,%s,,' % self.attachment1a.pk
form = self.AttachmentsFieldForm({u'attachments': attachments}, attached_to=self.user1)
Expand All @@ -121,9 +119,8 @@ def test_submitted_form_with_one_attachment(self):
rendered = self._render(u'{{ form }}', form=form)
self.assertInHTML(u'<li>This field is required.</li>', rendered, count=0)
self.assertInHTML(u'<input id="id_attachments" name="attachments" type="hidden" value=",%s,">' % self.attachment1a.pk, rendered)
self.assertInHTML(u'<a href="/download/%s/">filename1a</a>' % self.attachment1a.pk, rendered)
self.assertInHTML(u'<a href="/download/%s/">filename1a.txt</a>' % self.attachment1a.pk, rendered)

@unittest.skip(u'FIXME')
def test_submitted_form_with_multiple_attachments(self):
attachments = u'%s,%s,%s' % (self.attachment1a.pk, self.attachment1b.pk, self.attachment1c.pk)
form = self.AttachmentsFieldForm({u'attachments': attachments}, attached_to=self.user1)
Expand All @@ -132,9 +129,9 @@ def test_submitted_form_with_multiple_attachments(self):

rendered = self._render(u'{{ form }}', form=form)
self.assertInHTML(u'<input id="id_attachments" name="attachments" type="hidden" value=",%s,">' % attachments, rendered)
self.assertInHTML(u'<a href="/download/%s/">filename1a</a>' % self.attachment1a.pk, rendered)
self.assertInHTML(u'<a href="/download/%s/">filename1b</a>' % self.attachment1b.pk, rendered)
self.assertInHTML(u'<a href="/download/%s/">filename1c</a>' % self.attachment1c.pk, rendered)
self.assertInHTML(u'<a href="/download/%s/">filename1a.txt</a>' % self.attachment1a.pk, rendered)
self.assertInHTML(u'<a href="/download/%s/">filename1b.txt</a>' % self.attachment1b.pk, rendered)
self.assertInHTML(u'<a href="/download/%s/">filename1c.txt</a>' % self.attachment1c.pk, rendered)

def test_submitted_form_with_forbidden_attachment(self):
attachments = u',,%s,,' % self.attachment2.pk
Expand Down Expand Up @@ -165,7 +162,6 @@ def test_submitted_form_with_missing_argument(self):
self.assertInHTML(u'<ul class="errorlist"><li>This field is required.</li></ul>', rendered)
self.assertInHTML(u'<input id="id_attachments" name="attachments" type="hidden" value=",,">', rendered)

@unittest.skip(u'FIXME')
def test_form_with_attached_to_as_list(self):
attachments = u'%s,%s' % (self.attachment1b.pk, self.attachment2.pk)
form = self.AttachmentsFieldForm({u'attachments': attachments}, attached_to=[self.user1, self.user2])
Expand All @@ -174,8 +170,8 @@ def test_form_with_attached_to_as_list(self):

rendered = self._render(u'{{ form }}', form=form)
self.assertInHTML(u'<input id="id_attachments" name="attachments" type="hidden" value=",%s,">' % attachments, rendered)
self.assertInHTML(u'<a href="/download/%s/">filename1b</a>' % self.attachment1b.pk, rendered)
self.assertInHTML(u'<a href="/download/%s/">filename2</a>' % self.attachment2.pk, rendered)
self.assertInHTML(u'<a href="/download/%s/">filename1b.txt</a>' % self.attachment1b.pk, rendered)
self.assertInHTML(u'<a href="/download/%s/">filename2.txt</a>' % self.attachment2.pk, rendered)

def test_attachments_field_properties(self):
u"""
Expand Down
16 changes: 6 additions & 10 deletions poleno/attachments/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# -*- coding: utf-8 -*-
import random
import datetime
import unittest
from testfixtures import TempDirectory

from django.core.files.base import ContentFile
Expand All @@ -17,6 +16,7 @@

from ..models import Attachment


class AttachmentModelTest(TestCase):
u"""
Tests ``Attachment`` model.
Expand Down Expand Up @@ -68,9 +68,8 @@ def test_generic_object_field(self):
self.assertEqual(obj.generic_id, self.user.pk)
self.assertEqual(obj.generic_object, self.user)

@unittest.skip(u'FIXME')
def test_generic_object_field_may_not_be_omitted(self):
with self.assertRaisesMessage(IntegrityError, u'attachments_attachment.generic_type_id may not be NULL'):
with self.assertRaisesMessage(IntegrityError, u'NOT NULL constraint failed: attachments_attachment.generic_type_id'):
obj = self._create_instance(_omit=[u'generic_object'])

def test_file_field(self):
Expand Down Expand Up @@ -107,17 +106,14 @@ def test_file_field_name_unchanged_when_saving_existing_instance(self):
self.assertEqual(obj.name, u'changed')
self.assertEqual(obj.file.name, original_filename)

@unittest.skip(u'FIXME')
def test_name_and_content_type_fields(self):
obj = self._create_instance(name=u'filename', content_type=u'text/plain')
self.assertEqual(obj.name, u'filename')
self.assertEqual(obj.name, u'filename.txt')
self.assertEqual(obj.content_type, u'text/plain')

@unittest.skip(u'FIXME')
def test_name_and_content_type_fields_with_empty_values_if_omitted(self):
obj = self._create_instance(_omit=[u'name', u'content_type'])
self.assertEqual(obj.name, u'')
self.assertEqual(obj.content_type, u'')
def test_name_field_with_default_value_if_omitted(self):
obj = self._create_instance(_omit=[u'name'])
self.assertEqual(obj.name, u'attachment.txt')

def test_created_field_with_explicit_value(self):
obj = self._create_instance(created=utc_datetime_from_local(u'2014-10-05 15:33:10'))
Expand Down
24 changes: 11 additions & 13 deletions poleno/attachments/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import time
import datetime
import json
import unittest
from testfixtures import TempDirectory

from django.core.files.base import ContentFile
Expand All @@ -19,6 +18,7 @@
from ..models import Attachment
from ..views import upload, download


class AttachmentViewsTest(TestCase):
u"""
Tests ``upload()`` and ``download()`` views.
Expand Down Expand Up @@ -90,37 +90,35 @@ def test_download_with_if_modified_since_with_modified_file(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(u''.join(response.streaming_content), u'content')

@unittest.skip(u'FIXME')
def test_upload(self):
response = self.client.post(u'/upload/', {u'files': ContentFile(u'uploaded', name=u'filename')})
response = self.client.post(u'/upload/', {u'files': ContentFile(u'uploaded', name=u'filename.txt')})
self.assertIs(type(response), JsonResponse)
self.assertEqual(response.status_code, 200)

self.assertEqual(json.loads(response.content), {u'files': [
{u'url': u'/download/1/', u'pk': 1, u'name': u'filename', u'size': 8},
{u'url': u'/download/1/', u'pk': 1, u'name': u'filename.txt', u'size': 8},
]})
obj = Attachment.objects.get(pk=1)
self.assertEqual(obj.generic_object, self.user)
self.assertEqual(obj.name, u'filename')
self.assertEqual(obj.content_type, u'application/octet-stream')
self.assertEqual(obj.name, u'filename.txt')
self.assertEqual(obj.content_type, u'text/plain')
self.assertAlmostEqual(obj.created, utc_now(), delta=datetime.timedelta(seconds=10))
self.assertEqual(obj.size, 8)
self.assertEqual(obj.content, u'uploaded')

@unittest.skip(u'FIXME')
def test_upload_multiple_files(self):
response = self.client.post(u'/upload/', {u'files': [
ContentFile(u'uploaded', name=u'filename'),
ContentFile(u'uploaded2', name=u'filename2'),
ContentFile(u'uploaded3', name=u'filename3'),
ContentFile(u'uploaded', name=u'filename.txt'),
ContentFile(u'uploaded2', name=u'filename2.txt'),
ContentFile(u'uploaded3', name=u'filename3.txt'),
]})
self.assertIs(type(response), JsonResponse)
self.assertEqual(response.status_code, 200)

self.assertEqual(json.loads(response.content), {u'files': [
{u'url': u'/download/1/', u'pk': 1, u'name': u'filename', u'size': 8},
{u'url': u'/download/2/', u'pk': 2, u'name': u'filename2', u'size': 9},
{u'url': u'/download/3/', u'pk': 3, u'name': u'filename3', u'size': 9},
{u'url': u'/download/1/', u'pk': 1, u'name': u'filename.txt', u'size': 8},
{u'url': u'/download/2/', u'pk': 2, u'name': u'filename2.txt', u'size': 9},
{u'url': u'/download/3/', u'pk': 3, u'name': u'filename3.txt', u'size': 9},
]})
obj = Attachment.objects.get(pk=1)
self.assertEqual(obj.content, u'uploaded')
Expand Down

0 comments on commit 7b89090

Please sign in to comment.