From ae0b93dfe1bdb846425c5d734b3f159b6f29db93 Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Oct 09 2018 12:35:22 +0000
Subject: [PATCH 1/5] Fix adding and removing user's ssh keys


Signed-off-by: Pierre-Yves Chibon <pingou@pingoured.fr>

---

diff --git a/pagure/ui/app.py b/pagure/ui/app.py
index d07ad6c..eb7b506 100644
--- a/pagure/ui/app.py
+++ b/pagure/ui/app.py
@@ -1173,17 +1173,18 @@ def add_user_sshkey():
     form = pagure.forms.AddSSHKeyForm()
 
     if form.validate_on_submit():
+        user = _get_user(username=flask.g.fas_user.username)
         try:
             msg = pagure.lib.add_sshkey_to_project_or_user(
                 flask.g.session,
                 ssh_key=form.ssh_key.data,
                 pushaccess=True,
-                creator=flask.g.fas_user,
-                user=flask.g.fas_user,
+                creator=user,
+                user=user,
             )
             flask.g.session.commit()
             pagure.lib.create_user_ssh_keys_on_disk(
-                flask.g.fas_user, pagure_config.get("GITOLITE_KEYDIR", None)
+                user, pagure_config.get("GITOLITE_KEYDIR", None)
             )
             pagure.lib.tasks.gitolite_post_compile_only.delay()
             flask.flash(msg)
@@ -1217,8 +1218,8 @@ def remove_user_sshkey(keyid):
         )
     form = pagure.forms.ConfirmationForm()
     if form.validate_on_submit():
+        user = _get_user(username=flask.g.fas_user.username)
         found = False
-        user = pagure.lib.get_user(flask.g.session, flask.g.fas_user.username)
         for key in user.sshkeys:
             if key.id == keyid:
                 flask.g.session.delete(key)
@@ -1234,7 +1235,7 @@ def remove_user_sshkey(keyid):
         try:
             flask.g.session.commit()
             pagure.lib.create_user_ssh_keys_on_disk(
-                flask.g.fas_user, pagure_config.get("GITOLITE_KEYDIR", None)
+                user, pagure_config.get("GITOLITE_KEYDIR", None)
             )
             pagure.lib.tasks.gitolite_post_compile_only.delay()
             flask.flash("SSH key removed")

From 11ad7c9cef992af27b4bef614e4dae0357259990 Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Oct 09 2018 12:56:51 +0000
Subject: [PATCH 2/5] Don't add an id to the FakeUser objects by default


This broke adding/removing ssh keys in the UI.
Cf: https://pagure.io/pagure/pull-request/3873

Signed-off-by: Pierre-Yves Chibon <pingou@pingoured.fr>

---

diff --git a/tests/__init__.py b/tests/__init__.py
index 0908eb4..762c76f 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -553,7 +553,7 @@ class FakeGroup(object):    # pylint: disable=too-few-public-methods
 class FakeUser(object):     # pylint: disable=too-few-public-methods
     """ Fake user used to test the fedocallib library. """
 
-    def __init__(self, groups=None, username='username', cla_done=True, id=1):
+    def __init__(self, groups=None, username='username', cla_done=True, id=None):
         """ Constructor.
         :arg groups: list of the groups in which this fake user is
             supposed to be.

From f1a842646590d74d161ff25a7b62cb1debb5101d Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Oct 09 2018 12:57:41 +0000
Subject: [PATCH 3/5] Adjust tests' name to fit their function name so git grep works


Signed-off-by: Pierre-Yves Chibon <pingou@pingoured.fr>

---

diff --git a/tests/test_pagure_flask_ui_app.py b/tests/test_pagure_flask_ui_app.py
index 8048ac2..8f3b4fa 100644
--- a/tests/test_pagure_flask_ui_app.py
+++ b/tests/test_pagure_flask_ui_app.py
@@ -721,8 +721,8 @@ class PagureFlaskApptests(tests.Modeltests):
             self.assertEqual(output.status_code, 302)
 
     @patch('pagure.decorators.admin_session_timedout')
-    def test_add_sshkey(self, ast):
-        """ Test the add_sshkey endpoint. """
+    def test_add_user_sshkey(self, ast):
+        """ Test the add_user_sshkey endpoint. """
         ast.return_value = False
 
         # User not logged in
@@ -798,7 +798,7 @@ class PagureFlaskApptests(tests.Modeltests):
             self.assertIn('SSH key added', output_text)
 
     @patch('pagure.decorators.admin_session_timedout')
-    def test_remove_sshkey(self, ast):
+    def test_remove_user_sshkey(self, ast):
         """ Test the remove_sshkey endpoint. """
         ast.return_value = False
 
@@ -826,7 +826,7 @@ class PagureFlaskApptests(tests.Modeltests):
             ssh_key='ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAzBMSIlvPRaEiLOTVInErkRIw9CzQQcnslDekAn1jFnGf+SNa1acvbTiATbCX71AA03giKrPxPH79dxcC7aDXerc6zRcKjJs6MAL9PrCjnbyxCKXRNNZU5U9X/DLaaL1b3caB+WD6OoorhS3LTEtKPX8xyjOzhf3OQSzNjhJp5Q==',
             user=pingou,
             pushaccess=True,
-            creator=user,
+            creator=pingou,
         )
         self.session.commit()
         self.assertEqual(msg, 'SSH key added')

From 9efbd585e4f524f79a331dd9245a283209281f52 Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Oct 09 2018 13:09:39 +0000
Subject: [PATCH 4/5] Pass along the user object not the fas_user and fix unit-tests accordingly


Signed-off-by: Pierre-Yves Chibon <pingou@pingoured.fr>

---

diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py
index b667069..8b03049 100644
--- a/pagure/ui/repo.py
+++ b/pagure/ui/repo.py
@@ -47,6 +47,7 @@ import pagure.lib.tasks
 import pagure.forms
 import pagure.ui.plugins
 from pagure.config import config as pagure_config
+from pagure.flask_app import _get_user
 from pagure.lib import encoding_utils
 from pagure.ui import UI_NS
 from pagure.utils import (
@@ -1859,11 +1860,12 @@ def add_deploykey(repo, username=None, namespace=None):
     form = pagure.forms.AddDeployKeyForm()
 
     if form.validate_on_submit():
+        user = _get_user(username=flask.g.fas_user.username)
         try:
             msg = pagure.lib.add_sshkey_to_project_or_user(
                 flask.g.session,
                 ssh_key=form.ssh_key.data,
-                creator=flask.g.fas_user,
+                creator=user,
                 project=repo,
                 pushaccess=form.pushaccess.data,
             )
diff --git a/tests/test_pagure_flask_ui_repo.py b/tests/test_pagure_flask_ui_repo.py
index ae79058..e8d957d 100644
--- a/tests/test_pagure_flask_ui_repo.py
+++ b/tests/test_pagure_flask_ui_repo.py
@@ -616,12 +616,13 @@ class PagureFlaskRepotests(tests.Modeltests):
 
         # Add a deploy key to a project
         repo = pagure.lib.get_authorized_project(self.session, 'test')
+        pingou = pagure.lib.get_user(self.session, 'pingou')
         msg = pagure.lib.add_sshkey_to_project_or_user(
             session=self.session,
             project=repo,
             ssh_key='ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAzBMSIlvPRaEiLOTVInErkRIw9CzQQcnslDekAn1jFnGf+SNa1acvbTiATbCX71AA03giKrPxPH79dxcC7aDXerc6zRcKjJs6MAL9PrCjnbyxCKXRNNZU5U9X/DLaaL1b3caB+WD6OoorhS3LTEtKPX8xyjOzhf3OQSzNjhJp5Q==',
             pushaccess=True,
-            creator=user,
+            creator=pingou,
         )
         self.session.commit()
         self.assertEqual(msg, 'SSH key added')

From 8e493dae04749b49cdab64d3461a02d9d3be5d9d Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Oct 09 2018 13:13:33 +0000
Subject: [PATCH 5/5] Run black on the sources


Signed-off-by: Pierre-Yves Chibon <pingou@pingoured.fr>

---

diff --git a/pagure/api/__init__.py b/pagure/api/__init__.py
index 217c2bf..80a2e56 100644
--- a/pagure/api/__init__.py
+++ b/pagure/api/__init__.py
@@ -599,8 +599,7 @@ def api():
 
     api_pull_request_views_doc = load_doc(fork.api_pull_request_views)
     api_pull_request_view_doc = load_doc(fork.api_pull_request_view)
-    api_pull_request_diffstats_doc = load_doc(
-        fork.api_pull_request_diffstats)
+    api_pull_request_diffstats_doc = load_doc(fork.api_pull_request_diffstats)
     api_pull_request_by_uid_view_doc = load_doc(
         fork.api_pull_request_by_uid_view
     )

