def test_manage_update_with_existing_version(self): """Check if creation of a new document succeeds even if there is a version to update """ from django.core.files.base import File document = Document() document.title = "Wachtmeister Studer" document.author = "Friedrich Glauser" document.source_publisher = "Diogenes" document.save() user = User.objects.get(pk=1) versionFile = File(open(os.path.join(TEST_DATA_DIR, 'test.xml'))) version = Version.objects.create( comment = "testing 123", document = document, created_by = user) version.content.save("updated_version.xml", versionFile) versionFile.close() self.client.login(username='******', password='******') response = self.client.post(reverse('manage_update', args=[document.pk]), { 'title': 'testing 456', 'language': 'de' }) self.assertTemplateNotUsed(response, 'documents/manage_update.html') self.assertRedirects(response, reverse('manage_index'))
def setUp(self): # clean the archive os.rename(settings.MEDIA_ROOT, settings.MEDIA_ROOT + '.old') os.mkdir(settings.MEDIA_ROOT) user = User.objects.get(pk=1) self.document = Document(title="A Title", author="An Author", language="de") self.document.save() contentString = XMLContent.getInitialContent(self.document) content = ContentFile(contentString) v = Version.objects.create( comment = "Initial version created from meta data", document = self.document, created_by=user) v.content.save("initial_version.xml", content)
def test_todo_add_version_missing_metadata(self): """Check if adding a version with invalid meta data fails""" document = Document() document.title = "Wachtmeister Studer" document.author = "Friedrich Glauser" document.source_publisher = "Diogenes" document.save() self.client.login(username='******', password='******') version = open(os.path.join(TEST_DATA_DIR, 'test.xml')) post_data = { 'comment': 'testing 123', 'content': version, } original_function = django.test.client.encode_file django.test.client.encode_file = get_file_encoder('text/xml') response = self.client.post( reverse('todo_add_version', args=[document.pk]), post_data) version.close() django.test.client.encode_file = original_function self.assertContains(response, "The meta data 'dtb:uid' in the uploaded file does not correspond to the value in the document: 'ch-sbs-1' instead of 'ch-sbs-")
class ManageViewTest(TestCase): fixtures = ['state.yaml', 'user.yaml'] def setUp(self): self.document = Document() self.document.save() def test_manage_index(self): """Check if login succeeds""" self.client.login(username='******', password='******') response = self.client.get(reverse('manage_index')) self.assertTemplateUsed(response, 'documents/manage_index.html') self.failUnlessEqual(response.status_code, 200) def test_manage_details(self): """Check if login succeeds""" self.client.login(username='******', password='******') response = self.client.get(reverse('manage_detail', args=[self.document.pk])) self.assertTemplateUsed(response, 'documents/manage_detail.html') self.failUnlessEqual(response.status_code, 200) def test_manage_index_no_user(self): """Check if access w/o login fails""" response = self.client.get(reverse('manage_index')) self.assertTemplateNotUsed(response, 'documents/manage_index.html') self.failIfEqual(response.status_code, 200) def test_manage_index_user_without_perms(self): """Check if mana tab is visible w/o permissions""" self.client.login(username='******', password='******') response = self.client.get(reverse('todo_index')) self.assertNotContains(response, "Manage") def test_manage_index_user_with_perms(self): """Check if mana tab is visible with permissions""" self.client.login(username='******', password='******') response = self.client.get(reverse('todo_index')) self.assertContains(response, "Manage") def test_manage_details_no_user(self): """Check if access w/o login fails""" response = self.client.get(reverse('manage_detail', args=[self.document.pk])) self.assertTemplateNotUsed(response, 'documents/manage_detail.html') self.failIfEqual(response.status_code, 200) def test_manage_create_get(self): """Check if creation is rendered with the correct template""" self.client.login(username='******', password='******') response = self.client.get(reverse('manage_create')) self.assertTemplateUsed(response, 'documents/manage_create.html') self.failUnlessEqual(response.status_code, 200) def test_manage_create_partial(self): """Check if creation of an invalid new document fails""" self.client.login(username='******', password='******') response = self.client.post(reverse('manage_create'), { 'author': 'foo' }) self.assertFormError(response, 'form', 'title', 'This field is required.') self.assertFormError(response, 'form', 'language', 'This field is required.') self.assertTemplateUsed(response, 'documents/manage_create.html') self.failUnlessEqual(response.status_code, 200) def test_manage_create_invalid_language(self): """Check if creation of a new document with invalid language fails""" self.client.login(username='******', password='******') response = self.client.post(reverse('manage_create'), { 'title': 'testing 123', 'language': 'foo' }) self.assertFormError(response, 'form', 'language', 'Select a valid choice. foo is not one of the available choices.') self.assertTemplateUsed(response, 'documents/manage_create.html') self.failUnlessEqual(response.status_code, 200) def test_manage_create(self): """Check if creation of a new document succeeds""" self.client.login(username='******', password='******') response = self.client.post(reverse('manage_create'), { 'title': 'testing 123', 'language': 'de' }) self.assertTemplateNotUsed(response, 'documents/manage_create.html') self.assertRedirects(response, reverse('manage_index')) def test_manage_update_get(self): """Check if update is rendered with the correct template""" self.client.login(username='******', password='******') response = self.client.get(reverse('manage_update', args=[self.document.pk])) self.assertTemplateUsed(response, 'documents/manage_update.html') self.failUnlessEqual(response.status_code, 200) def test_manage_update_partial(self): """Check if update of a document with insufficient data fails""" self.client.login(username='******', password='******') response = self.client.post(reverse('manage_update', args=[self.document.pk]), { 'author': 'foo' }) self.assertFormError(response, 'form', 'title', 'This field is required.') self.assertFormError(response, 'form', 'language', 'This field is required.') self.assertTemplateUsed(response, 'documents/manage_update.html') self.failUnlessEqual(response.status_code, 200) def test_manage_update_invalid_language(self): """Check if update of a document with with invalid language fails""" self.client.login(username='******', password='******') response = self.client.post(reverse('manage_update', args=[self.document.pk]), { 'title': 'testing 123', 'language': 'foo' }) self.assertFormError(response, 'form', 'language', 'Select a valid choice. foo is not one of the available choices.') self.assertTemplateUsed(response, 'documents/manage_update.html') self.failUnlessEqual(response.status_code, 200) def test_manage_update(self): """Check if creation of a new document succeeds""" self.client.login(username='******', password='******') response = self.client.post(reverse('manage_update', args=[self.document.pk]), { 'title': 'testing 456', 'language': 'de' }) self.assertTemplateNotUsed(response, 'documents/manage_update.html') self.assertRedirects(response, reverse('manage_index')) def test_manage_update_with_existing_version(self): """Check if creation of a new document succeeds even if there is a version to update """ from django.core.files.base import File document = Document() document.title = "Wachtmeister Studer" document.author = "Friedrich Glauser" document.source_publisher = "Diogenes" document.save() user = User.objects.get(pk=1) versionFile = File(open(os.path.join(TEST_DATA_DIR, 'test.xml'))) version = Version.objects.create( comment = "testing 123", document = document, created_by = user) version.content.save("updated_version.xml", versionFile) versionFile.close() self.client.login(username='******', password='******') response = self.client.post(reverse('manage_update', args=[document.pk]), { 'title': 'testing 456', 'language': 'de' }) self.assertTemplateNotUsed(response, 'documents/manage_update.html') self.assertRedirects(response, reverse('manage_index')) def test_translation(self): """Check if i18n stuff works""" self.client.login(username='******', password='******') http_headers = {'HTTP_ACCEPT_LANGUAGE' : 'de'} response = self.client.get(reverse('manage_index'), **http_headers) self.assertEquals(response['Content-Language'],'de') self.assertContains(response, "Titel")
def setUp(self): self.document = Document() self.document.save()
class TodoViewTest(TestCase): fixtures = ['state.yaml', 'user.yaml'] def setUp(self): self.document = Document() self.document.save() def test_todo_index(self): """Check if login succeeds""" self.client.login(username='******', password='******') response = self.client.get(reverse('todo_index')) self.assertTemplateUsed(response, 'documents/todo_index.html') self.failUnlessEqual(response.status_code, 200) def test_todo_details(self): """Check if login succeeds""" self.client.login(username='******', password='******') response = self.client.get(reverse('todo_detail', args=[self.document.pk])) self.assertTemplateUsed(response, 'documents/todo_detail.html') self.failUnlessEqual(response.status_code, 200) def test_todo_index_no_user(self): """Check if access w/o login fails""" response = self.client.get(reverse('todo_index')) self.assertTemplateNotUsed(response, 'documents/todo_index.html') self.failIfEqual(response.status_code, 200) def test_todo_details_no_user(self): """Check if access w/o login fails""" response = self.client.get(reverse('todo_detail', args=[self.document.pk])) self.assertTemplateNotUsed(response, 'documents/todo_detail.html') self.failIfEqual(response.status_code, 200) def test_todo_add_attachment_get(self): """Check if get for adding an attachment is redirected""" self.client.login(username='******', password='******') response = self.client.get(reverse('todo_add_attachment', args=[self.document.pk])) self.assertRedirects(response, reverse('todo_detail', args=[self.document.pk])) def form_test(self, urlName, formParams, errorField): self.client.login(username='******', password='******') response = self.client.post(reverse(urlName, args=[self.document.pk]), formParams) self.assertFormError(response, 'form', errorField, 'This field is required.') def attachment_form_test(self, formParams, errorField): self.form_test('todo_add_attachment', formParams, errorField) def test_todo_add_attachment_missing_comment(self): """Check if adding an attachment without a comment fails""" self.attachment_form_test({'content': 'hah'}, 'comment') def test_todo_add_attachment_empty_comment(self): """Check if adding an attachment with an empty comment fails""" self.attachment_form_test({'content': '', 'content': 'hah'}, 'comment') def test_todo_add_attachment_missing_content(self): """Check if adding an attachment with no content fails""" self.attachment_form_test({'content': 'foo'}, 'content') def test_todo_add_attachment_empty_content(self): """Check if adding an attachment with an empty content fails""" self.attachment_form_test({'content': 'foo', 'content': ''}, 'content') def test_todo_add_attachment_invalid_mimetype(self): """Check if adding an attachment with wrong mime type fails""" self.client.login(username='******', password='******') attachment = open(__file__) post_data = { 'comment': 'testing 123', 'content': attachment } original_function = django.test.client.encode_file django.test.client.encode_file = get_file_encoder('text/plain') response = self.client.post( reverse('todo_add_attachment', args=[self.document.pk]), post_data) attachment.close() django.test.client.encode_file = original_function self.assertFormError(response, 'form', 'content', 'The mime type of the uploaded file must be in application/pdf, application/msword, application/rtf, text/html') def test_todo_add_attachment_valid_mimetype(self): """Check if adding an attachment with valid mime type succeeds""" self.client.login(username='******', password='******') attachment = open(os.path.join(TEST_DATA_DIR, 'test.pdf')) post_data = { 'comment': 'testing 123', 'content': attachment, } original_function = django.test.client.encode_file django.test.client.encode_file = get_file_encoder('application/pdf') response = self.client.post( reverse('todo_add_attachment', args=[self.document.pk]), post_data) attachment.close() django.test.client.encode_file = original_function self.assertRedirects(response, reverse('todo_detail', args=[self.document.pk])) def test_todo_add_version_get(self): """Check if get for adding an version is redirected""" self.client.login(username='******', password='******') response = self.client.get(reverse('todo_add_version', args=[self.document.pk])) self.assertRedirects(response, reverse('todo_detail', args=[self.document.pk])) def version_form_test(self, formParams, errorField): self.form_test('todo_add_version', formParams, errorField) def test_todo_add_version_missing_comment(self): """Check if adding a version without a comment fails""" self.version_form_test({'content': 'hah'}, 'comment') def test_todo_add_version_empty_comment(self): """Check if adding a version with an empty comment fails""" self.version_form_test({'content': '', 'content': 'hah'}, 'comment') def test_todo_add_version_missing_content(self): """Check if adding a version with no content fails""" self.version_form_test({'comment': 'foo'}, 'content') def test_todo_add_version_empty_content(self): """Check if adding a version with an empty content fails""" self.version_form_test({'comment': 'foo', 'content': ''}, 'content') def test_todo_add_version_invalid_mimetype(self): """Check if adding a version with wrong mime type fails""" self.client.login(username='******', password='******') version = open(__file__) post_data = { 'comment': 'testing 123', 'content': version } original_function = django.test.client.encode_file django.test.client.encode_file = get_file_encoder('text/plain') response = self.client.post( reverse('todo_add_version', args=[self.document.pk]), post_data) version.close() django.test.client.encode_file = original_function self.assertFormError(response, 'form', 'content', "The mime type of the uploaded file must be 'text/xml'") def test_todo_add_version_invalid_metadata(self): """Check if adding a version with invalid meta data fails""" document = Document() document.title = "foo" document.author = "Friedrich Glauser" document.source_publisher = "Diogenes" document.save() self.client.login(username='******', password='******') version = open(os.path.join(TEST_DATA_DIR, 'test.xml')) post_data = { 'comment': 'testing 123', 'content': version, } original_function = django.test.client.encode_file django.test.client.encode_file = get_file_encoder('text/xml') response = self.client.post( reverse('todo_add_version', args=[document.pk]), post_data) version.close() django.test.client.encode_file = original_function self.assertContains(response, "The meta data 'dc:Title' in the uploaded file does not correspond to the value in the document: 'Wachtmeister Studer' instead of 'foo'") def test_todo_add_version_missing_metadata(self): """Check if adding a version with invalid meta data fails""" document = Document() document.title = "Wachtmeister Studer" document.author = "Friedrich Glauser" document.source_publisher = "Diogenes" document.save() self.client.login(username='******', password='******') version = open(os.path.join(TEST_DATA_DIR, 'test.xml')) post_data = { 'comment': 'testing 123', 'content': version, } original_function = django.test.client.encode_file django.test.client.encode_file = get_file_encoder('text/xml') response = self.client.post( reverse('todo_add_version', args=[document.pk]), post_data) version.close() django.test.client.encode_file = original_function self.assertContains(response, "The meta data 'dtb:uid' in the uploaded file does not correspond to the value in the document: 'ch-sbs-1' instead of 'ch-sbs-") def test_todo_add_version(self): """Check if adding a version with valid mime type succeeds""" document = Document() document.title = "Wachtmeister Studer" document.author = "Friedrich Glauser" document.source_publisher = "Diogenes" document.publisher = "Swiss Library for the Blind, Visually Impaired and Print Disabled" document.identifier = "ch-sbs-1" document.language = "de" document.save() # unfortunately document.save sets the date to today for a new # document, so we need to save, set the date and save again document.date = "2009-04-23" document.save() self.client.login(username='******', password='******') version = open(os.path.join(TEST_DATA_DIR, 'test.xml')) post_data = { 'comment': 'testing 123', 'content': version, } original_function = django.test.client.encode_file django.test.client.encode_file = get_file_encoder('text/xml') response = self.client.post( reverse('todo_add_version', args=[document.pk]), post_data) version.close() django.test.client.encode_file = original_function self.assertRedirects(response, reverse('todo_detail', args=[document.pk])) def test_todo_transition_get(self): """Check if sending a get request to the transition url fails""" self.client.login(username='******', password='******') response = self.client.get(reverse('todo_transition', args=[self.document.pk])) self.failIfEqual(response.status_code, 200) def transition_test(self, formParams, errorMsg): self.client.login(username='******', password='******') response = self.client.post(reverse('todo_transition', args=[self.document.pk]), formParams) self.assertFormError(response, 'form', 'state', errorMsg) self.assertTemplateUsed(response, 'documents/todo_detail.html') def test_todo_transition_no_state(self): """Check if transitioning to no state results in a form error""" self.transition_test({}, 'This field is required.') # This test fails with the exception ValueError: invalid literal for int() with base 10: 'foo' # def test_todo_transition_invalid_state_with_string(self): # """Check if transitioning to an invalid state results in rendering the form again""" # self.transition_test({'state': 'foo'}, # 'Select a valid choice. foo is not one of the available choices.') def test_todo_transition_invalid_state_with_number(self): """Check if transitioning to an invalid state results in rendering the form again""" # 99 is certainly not a valid state self.transition_test({'state': 99}, u'Select a valid choice. That choice is not one of the available choices.') def test_todo_transition_invalid_state(self): """Check if an invalid transition results in an StateError exception""" self.client.login(username='******', password='******') self.failUnlessRaises(StateError, self.client.post, reverse('todo_transition', args=[self.document.pk]), {'state': 4}) def test_todo_transition_valid_state(self): """Check if an valid transition succeeds""" self.client.login(username='******', password='******') response = self.client.post(reverse('todo_transition', args=[self.document.pk]), {'state': 2}) self.assertRedirects(response, reverse('todo_index'))
def test_todo_add_version(self): """Check if adding a version with valid mime type succeeds""" document = Document() document.title = "Wachtmeister Studer" document.author = "Friedrich Glauser" document.source_publisher = "Diogenes" document.publisher = "Swiss Library for the Blind, Visually Impaired and Print Disabled" document.identifier = "ch-sbs-1" document.language = "de" document.save() # unfortunately document.save sets the date to today for a new # document, so we need to save, set the date and save again document.date = "2009-04-23" document.save() self.client.login(username='******', password='******') version = open(os.path.join(TEST_DATA_DIR, 'test.xml')) post_data = { 'comment': 'testing 123', 'content': version, } original_function = django.test.client.encode_file django.test.client.encode_file = get_file_encoder('text/xml') response = self.client.post( reverse('todo_add_version', args=[document.pk]), post_data) version.close() django.test.client.encode_file = original_function self.assertRedirects(response, reverse('todo_detail', args=[document.pk]))
class BrowseViewTest(TestCase): fixtures = ['state.yaml', 'user.yaml'] def setUp(self): # clean the archive os.rename(settings.MEDIA_ROOT, settings.MEDIA_ROOT + '.old') os.mkdir(settings.MEDIA_ROOT) user = User.objects.get(pk=1) self.document = Document(title="A Title", author="An Author", language="de") self.document.save() contentString = XMLContent.getInitialContent(self.document) content = ContentFile(contentString) v = Version.objects.create( comment = "Initial version created from meta data", document = self.document, created_by=user) v.content.save("initial_version.xml", content) def tearDown(self): # re-enable the archive shutil.rmtree(settings.MEDIA_ROOT) os.rename(settings.MEDIA_ROOT + '.old', settings.MEDIA_ROOT) def test_browse_index(self): """Check if access w/o login succeeds""" response = self.client.get(reverse('browse_index')) self.failUnlessEqual(response.status_code, 200) def test_browse_details(self): """Check if access w/o login succeeds""" response = self.client.get(reverse('browse_detail', args=[self.document.pk])) self.failUnlessEqual(response.status_code, 200) def test_browse_pdf_empty(self): """Check if post with an empty form fails""" response = self.client.post(reverse('browse_pdf', args=[self.document.pk])) self.failIfEqual(response.status_code, 200) def test_browse_pdf_partial(self): """Check if post with an partial form fails""" response = self.client.post(reverse('browse_pdf', args=[self.document.pk]), { 'alignment': 'justified', 'stock_size': 'a3paper', }) self.failIfEqual(response.status_code, 200) def test_browse_pdf_invalid(self): """Check if post with an invalid form fails""" response = self.client.post(reverse('browse_pdf', args=[self.document.pk]), { 'font_size': 'foo', 'font': 'bar', 'page_style': 'baz', 'alignment': 'not', 'stock_size': 'valid', }) self.failIfEqual(response.status_code, 200) def test_browse_pdf(self): """Check if post with a valid form succeeds""" response = self.client.post(reverse('browse_pdf', args=[self.document.pk]), { 'font_size': '14pt', 'font': 'Tiresias LPfont', 'page_style': 'withPageNums', 'alignment': 'justified', 'stock_size': 'a3paper', 'line_spacing': 'singlespacing', 'replace_em_with_quote': 'false', }) self.failUnlessEqual(response.status_code, 200) def test_browse_brl_empty(self): """Check if post with an empty form fails""" response = self.client.post(reverse('browse_brl', args=[self.document.pk])) self.failIfEqual(response.status_code, 200) def test_browse_brl_partial(self): """Check if post with an partial form fails""" response = self.client.post(reverse('browse_brl', args=[self.document.pk]), { 'cells_per_line': '40', 'lines_per_page': '28', }) self.failIfEqual(response.status_code, 200) def test_browse_brl_invalid(self): """Check if post with an invalid form fails""" response = self.client.post(reverse('browse_brl', args=[self.document.pk]), { 'cells_per_line': 'foo', 'lines_per_page': 'bar', 'contraction': 'baz', 'hyphenation': 'not', 'show_original_page_numbers': 'even', 'enable_capitalization': 'remotely', 'detailed_accented_characters': 'valid' }) self.failIfEqual(response.status_code, 200) def test_browse_brl(self): """Check if post with a valid form succeeds""" response = self.client.post(reverse('browse_brl', args=[self.document.pk]), { 'cells_per_line': '40', 'lines_per_page': '28', 'contraction': '0', 'hyphenation': '', 'show_original_page_numbers': 'True', 'enable_capitalization': 'True', 'detailed_accented_characters': 'True' }) self.failUnlessEqual(response.status_code, 200)
def testUnicode(self): d = Document(title='foo', publisher='bar', identifier='baz') self.assertEquals(d.__unicode__(), 'foo')