From 792ae4cbdb5029cec652f16fa7c1eb90a047c399 Mon Sep 17 00:00:00 2001 From: pierre-24 Date: Sun, 11 Oct 2015 12:06:20 +0200 Subject: [PATCH] Corrige les erreurs sur `get_commit_author()` --- .../management/commands/migrate_to_zep12.py | 4 +- zds/tutorialv2/tests/tests_utils.py | 40 ++++++++++++++++++- zds/tutorialv2/utils.py | 15 +++++-- 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/zds/tutorialv2/management/commands/migrate_to_zep12.py b/zds/tutorialv2/management/commands/migrate_to_zep12.py index 137e5fe027..5b3b2368da 100644 --- a/zds/tutorialv2/management/commands/migrate_to_zep12.py +++ b/zds/tutorialv2/management/commands/migrate_to_zep12.py @@ -22,7 +22,7 @@ from zds.tutorialv2.models.models_database import PublishableContent, ContentReaction, ContentRead, PublishedContent,\ Validation -from zds.tutorialv2.utils import publish_content +from zds.tutorialv2.utils import publish_content, get_commit_author from django.core.management.base import BaseCommand from django.db import transaction from zds.gallery.models import Gallery, UserGallery, Image @@ -218,7 +218,7 @@ def copy_and_clean_repo(path_from, path_to): if len(to_delete) != 0: old_repo.index.remove(to_delete) - sha = old_repo.index.commit('Nettoyage pré-migratoire') + sha = old_repo.index.commit('Nettoyage pré-migratoire', **get_commit_author()) # then clone it to new repo old_repo.clone(path_to) diff --git a/zds/tutorialv2/tests/tests_utils.py b/zds/tutorialv2/tests/tests_utils.py index b115b7f6df..2d8a7db350 100644 --- a/zds/tutorialv2/tests/tests_utils.py +++ b/zds/tutorialv2/tests/tests_utils.py @@ -10,6 +10,7 @@ from django.test.utils import override_settings from zds.forum.models import Topic from zds.settings import BASE_DIR +from django.core.urlresolvers import reverse from zds.member.factories import ProfileFactory, StaffProfileFactory from zds.tutorialv2.factories import PublishableContentFactory, ContainerFactory, LicenceFactory, ExtractFactory, \ @@ -18,7 +19,7 @@ from zds.tutorialv2.models.models_versioned import Container from zds.tutorialv2.utils import get_target_tagged_tree_for_container, publish_content, unpublish_content, \ get_target_tagged_tree_for_extract, retrieve_and_update_images_links, last_participation_is_old, \ - InvalidSlugError, BadManifestError, get_content_from_json + InvalidSlugError, BadManifestError, get_content_from_json, get_commit_author from zds.tutorialv2.models.models_database import PublishableContent, PublishedContent, ContentReaction, ContentRead from django.core.management import call_command from zds.tutorial.factories import BigTutorialFactory, MiniTutorialFactory, PublishedMiniTutorial, NoteFactory, \ @@ -636,6 +637,43 @@ def testParseBadManifest(self): get_content_from_json, dictionary, None, '', max_title_len=PublishableContent._meta.get_field('title').max_length) + def test_get_commit_author(self): + """Ensure the behavior of `get_commit_author()` : + - `git.Actor` use the pk of the bot account when no one is connected + - `git.Actor` use the pk (and the email) of the connected account when available + + (Implementation of `git.Actor` is there : + https://github.com/gitpython-developers/GitPython/blob/master/git/util.py#L312) + """ + + # 1. With user connected + self.assertEqual( + self.client.login( + username=self.user_author.username, + password='hostel77'), + True) + + # go to whatever page, if not, `get_current_user()` does not work at all + result = self.client.get(reverse('zds.pages.views.index')) + self.assertEqual(result.status_code, 200) + + actor = get_commit_author() + self.assertEqual(actor['committer'].name, str(self.user_author.pk)) + self.assertEqual(actor['author'].name, str(self.user_author.pk)) + self.assertEqual(actor['committer'].email, self.user_author.email) + self.assertEqual(actor['author'].email, self.user_author.email) + + # 2. Without connected user + self.client.logout() + + # as above ... + result = self.client.get(reverse('zds.pages.views.index')) + self.assertEqual(result.status_code, 200) + + actor = get_commit_author() + self.assertEqual(actor['committer'].name, str(self.mas.pk)) + self.assertEqual(actor['author'].name, str(self.mas.pk)) + def tearDown(self): if os.path.isdir(settings.ZDS_APP['content']['repo_private_path']): shutil.rmtree(settings.ZDS_APP['content']['repo_private_path']) diff --git a/zds/tutorialv2/utils.py b/zds/tutorialv2/utils.py index abff86bc5b..2fe7e2dab1 100644 --- a/zds/tutorialv2/utils.py +++ b/zds/tutorialv2/utils.py @@ -29,6 +29,7 @@ from zds.utils import slugify as old_slugify from zds.utils.models import Licence from zds.utils.templatetags.emarkdown import emarkdown +from zds.member.models import User def all_is_string_appart_from_children(dict_representation): @@ -1014,14 +1015,15 @@ def init_new_repo(db_object, introduction_text, conclusion_text, commit_message= def get_commit_author(): - """get a dictionary that represent the commit author with ``author`` and ``comitter`` key + """get a dictionary that represent the commit author with ``author`` and ``comitter`` key. If there is no users, + bot account pk is used. :return: correctly formatted commit author for ``repo.index.commit()`` :rtype: dict """ user = get_current_user() - if user: + if user and user.is_authenticated(): aut_user = str(user.pk) aut_email = None @@ -1029,11 +1031,16 @@ def get_commit_author(): aut_email = user.email else: - aut_user = ZDS_APP['member']['bot_account'] + try: + aut_user = str(User.objects.filter(username=settings.ZDS_APP['member']['bot_account']).first().pk) + except AttributeError: + aut_user = '0' + aut_email = None if aut_email is None or aut_email.strip() == "": - aut_email = "inconnu@{}".format(settings.ZDS_APP['site']['dns']) + aut_email = _(u"inconnu@{}").format(settings.ZDS_APP['site']['dns']) + return {'author': Actor(aut_user, aut_email), 'committer': Actor(aut_user, aut_email)}