def test_fetch_comments(self): """ Test how we fetch comments for a particular msgid/file combination. """ msgid1 = u"String 1" msghash1 = get_hash_from_msgid_context(msgid1, '') msgid2 = u"String 2" msghash2 = get_hash_from_msgid_context(msgid2, '') MessageCommentFactory.create( translation_file=self.transfile, msghash=msghash1, body=u"First comment", ) MessageCommentFactory.create( translation_file=self.transfile, msghash=msghash1, body=u"Second comment", ) MessageCommentFactory.create( translation_file=self.transfile, msghash=msghash2, body=u"Third comment", ) MessageCommentFactory.create( body=u"Other file comment" ) client = APIClient() client.force_authenticate(user=self.admin_user) url = "{}?{}".format( reverse('mobetta:api:messagecomment-list'), urlencode({ 'translation_file': self.transfile.pk, 'msghash': msghash1, }) ) response = client.get(url, format='json') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 2) self.assertEqual(response.data[0]['msghash'], msghash1) self.assertEqual(response.data[0]['body'], u"First comment") self.assertEqual(response.data[0]['translation_file'], 1) self.assertEqual(response.data[1]['msghash'], msghash1) self.assertEqual(response.data[1]['body'], u"Second comment") self.assertEqual(response.data[1]['translation_file'], 1)
def populate_msghash_from_msgid(apps, schema_editor): EditLog = apps.get_model("mobetta", "EditLog") for log in EditLog.objects.all(): log.msghash = get_hash_from_msgid_context(log.msgid, '') log.save() MessageComment = apps.get_model("mobetta", "MessageComment") for comment in MessageComment.objects.all(): comment.msghash = get_hash_from_msgid_context(comment.msgid, '') comment.save()
def test_single_edit(self): """ Go to the file detail view, make an edit to a translation, and submit. Check that this translation has changed in the PO file. """ response = self.app.get(self.url, user=self.admin_user) self.assertEqual(response.status_code, 200) translation_edit_form = response.forms['translation-edit'] # First string: u'String 1' -> u'' msgid_to_edit = translation_edit_form['form-0-msgid'].value msghash_to_edit = get_hash_from_msgid_context(msgid_to_edit, '') self.assertEqual(translation_edit_form['form-0-translation'].value, u'') new_translation = u'Translatèd string' translation_edit_form['form-0-translation'] = new_translation response = translation_edit_form.submit().follow() self.assertEqual(response.status_code, 200) # Check the file pofile = self.transfile.get_polib_object() self.assertEqual(pofile.find(msgid_to_edit).msgstr, new_translation) # Check the edit history file_edits = self.transfile.edit_logs.all() self.assertEqual(file_edits.count(), 1) only_file_edit = file_edits.first() self.assertEqual(only_file_edit.msghash, msghash_to_edit) self.assertEqual(only_file_edit.new_value, new_translation)
def test_end_with_new_line_not_edited(self): self.create_poentry(u'with enter\n', u'with enters\n') response = self.app.get(self.url, user=self.admin_user) self.assertEqual(response.status_code, 200) translation_edit_form = response.forms['translation-edit'] # First string: u'String 1' -> u'' msgid_to_edit = translation_edit_form['form-5-msgid'].value msghash_to_edit = get_hash_from_msgid_context(msgid_to_edit, '') self.assertEqual(translation_edit_form['form-5-translation'].value, u'with enters\n') new_translation = u'met enters' translation_edit_form['form-5-translation'] = new_translation response = translation_edit_form.submit().follow() self.assertEqual(response.status_code, 200) # Check the file pofile = self.transfile.get_polib_object() self.assertEqual( pofile.find(msgid_to_edit).msgstr, "{}\n".format(new_translation)) # Check the edit history file_edits = self.transfile.edit_logs.all() self.assertEqual(file_edits.count(), 1) only_file_edit = file_edits.first() self.assertEqual(only_file_edit.msghash, msghash_to_edit) self.assertEqual(only_file_edit.new_value, new_translation)
def test_post_invalid_comment(self): """ Post a comment with no text in the body. """ client = APIClient() client.force_authenticate(user=self.admin_user) msgid = u"String 1" msghash = get_hash_from_msgid_context(msgid, '') postdata = { 'msghash': msghash, 'translation_file': self.transfile.pk, 'body': u"", } response = client.post(reverse('mobetta:api:messagecomment-list'), postdata, format='json') self.assertEqual(response.status_code, 400) self.assertEqual(response.data['body'], [_('This field may not be blank.')])
def test_post_comment(self): """ Post a comment on one translation in the translation file. """ client = APIClient() client.force_authenticate(user=self.admin_user) msgid = u"String 1" msghash = get_hash_from_msgid_context(msgid, '') postdata = { 'msghash': msghash, 'translation_file': self.transfile.pk, 'body': u"Test comment", } response = client.post(reverse('mobetta:api:messagecomment-list'), postdata, format='json') self.assertEqual(response.status_code, 201) # 'Created' self.assertEqual(response.data['msghash'], msghash) self.assertEqual(response.data['translation_file'], self.transfile.pk) self.assertEqual(response.data['body'], u"Test comment") self.assertEqual(response.data['comment_count'], 1) self.assertEqual(response.data['user_name'], str(self.admin_user))
def test_clean_success_with_py3_format_tokens_order_changed(self): msgid = "Order is not really {important}, {ok}" form = TranslationForm({ 'msgid': msgid, 'md5hash': util.get_hash_from_msgid_context(msgid, None), 'translation': "{ok}, l'ordre n'est pas tres {important}.", 'fuzzy': False, }) self.assertTrue(form.is_valid())
def test_clean_success_with_no_format_token(self): msgid = "Nothing to format." form = TranslationForm({ 'msgid': msgid, 'md5hash': util.get_hash_from_msgid_context(msgid, None), 'translation': "Rien a formater.", 'fuzzy': False, }) self.assertTrue(form.is_valid())
def test_clean_success_with_py3_format_tokens_source_and_empty_translation( self): msgid = "Order is not really {important}, {ok}" form = TranslationForm({ 'msgid': msgid, 'md5hash': util.get_hash_from_msgid_context(msgid, None), 'translation': "", 'fuzzy': False, }) self.assertTrue(form.is_valid())
def test_fetch_comments_unauthorised(self): """ Make sure a non-admin user can't fetch comments. """ client = APIClient() non_translator_user = UserFactory.create() client.force_authenticate(user=non_translator_user) msgid = u"String 1" msghash = get_hash_from_msgid_context(msgid, '') url = "{}?{}".format( reverse('mobetta:api:messagecomment-list'), urlencode({ 'translation_file': self.transfile.pk, 'msghash': msghash, })) response = client.get(url, format='json') self.assertEqual(response.status_code, 403)
def test_post_comment_unauthorised(self): """ Try to post a comment as a user without translation permissions. """ client = APIClient() non_translator_user = UserFactory.create() client.force_authenticate(user=non_translator_user) msgid = u"String 1" msghash = get_hash_from_msgid_context(msgid, '') postdata = { 'msghash': msghash, 'translation_file': self.transfile.pk, 'body': u"", } response = client.post(reverse('mobetta:api:messagecomment-list'), postdata, format='json') self.assertEqual(response.status_code, 403)