From d6ddc41f73aa03b4aedf4a6d39b41387af8a1c2e Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Dec 01 2016 08:19:00 +0000 Subject: Fix retrieving user activity when there was only 1 commit that day We had a bug where retrieving the user's activity when there was only one commit on that day would crash. This commit fixes it and add unit-tests confirming the fix. --- diff --git a/pagure/api/user.py b/pagure/api/user.py index 7786aaa..4113585 100644 --- a/pagure/api/user.py +++ b/pagure/api/user.py @@ -312,7 +312,10 @@ def api_view_user_activity_date(username, date): acts.append(activity) for project in commits: if len(commits[project]) == 1: - tmp = commits[project] + tmp = dict( + description_mk=pagure.lib.text2markdown( + str(commits[project][0])) + ) else: tmp = dict( description_mk=pagure.lib.text2markdown( @@ -323,6 +326,7 @@ def api_view_user_activity_date(username, date): ) js_act.append(tmp) activities = acts + for act in activities: activity = act.to_json(public=True) activity['description_mk'] = pagure.lib.text2markdown(str(act)) diff --git a/tests/test_pagure_flask_api_user.py b/tests/test_pagure_flask_api_user.py index 3b171a9..81ef7e9 100644 --- a/tests/test_pagure_flask_api_user.py +++ b/tests/test_pagure_flask_api_user.py @@ -25,6 +25,7 @@ sys.path.insert(0, os.path.join(os.path.dirname( import pagure.api import pagure.lib +import pagure.lib.model as model import tests @@ -242,6 +243,44 @@ class PagureFlaskApiUSertests(tests.Modeltests): self.assertEqual(data, exp) + @patch('pagure.lib.notify.send_email') + def test_api_view_user_activity_date_1_activity(self, mockemail): + """ Test the api_view_user_activity_date method of the flask user + api when the user only did one action. """ + + tests.create_projects(self.session) + repo = pagure.lib.get_project(self.session, 'test') + + now = datetime.datetime.utcnow() + date = now.date().strftime('%Y-%m-%d') + # Create a single commit log + log = model.PagureLog( + user_id=1, + user_email='foo@bar.com', + project_id=1, + log_type='committed', + ref_id='githash', + date=now.date(), + date_created=now + ) + self.session.add(log) + self.session.commit() + + # Retrieve the user's logs for today + output = self.app.get( + '/api/0/user/pingou/activity/%s?grouped=1' % date) + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + exp = { + "activities": [ + { + "description_mk": "

pingou committed on test#githash

" + } + ], + "date": date, + } + self.assertEqual(data, exp) + if __name__ == '__main__': SUITE = unittest.TestLoader().loadTestsFromTestCase(