From 9aba91d8045d1161e5305cdbf0a76883c1496ce4 Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Jul 24 2018 10:34:51 +0000
Subject: [PATCH 1/2] Move from a context_processor to jinja filters


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

---

diff --git a/pagure/flask_app.py b/pagure/flask_app.py
index 8352c7c..8a053d4 100644
--- a/pagure/flask_app.py
+++ b/pagure/flask_app.py
@@ -107,36 +107,6 @@ def create_app(config=None):
             os.path.join(app.root_path, 'static'),
         ]
 
-    @app.context_processor
-    def context_processor():
-        def user_can_clone_ssh(username):
-            ssh_keys = ''
-            if flask.g.authenticated:
-                ssh_keys = pagure.lib.search_user(
-                    flask.g.session,
-                    username=flask.g.fas_user.username
-                ).public_ssh_key or ''
-            if not (pagure_config.get('ALWAYS_RENDER_SSH_CLONE_URL')
-                    or ssh_keys.strip()):
-                return False
-            return True
-
-        def get_git_url_ssh():
-            """ Return the GIT SSH URL to be displayed in the UI based on the
-            content of the configuration file.
-            """
-            git_url_ssh = pagure_config.get('GIT_URL_SSH')
-            if flask.g.authenticated and git_url_ssh:
-                try:
-                    git_url_ssh = git_url_ssh.format(
-                        username=flask.g.fas_user.username)
-                except (KeyError, IndexError):
-                    pass
-            return git_url_ssh
-
-        return {'user_can_clone_ssh': user_can_clone_ssh,
-                'git_url_ssh': get_git_url_ssh()}
-
     auth = pagure_config.get('PAGURE_AUTH', None)
     if auth in ['fas', 'openid']:
         # Only import and set flask_fas_openid if it is needed
diff --git a/pagure/templates/repo_master.html b/pagure/templates/repo_master.html
index f71b2cc..44a4457 100644
--- a/pagure/templates/repo_master.html
+++ b/pagure/templates/repo_master.html
@@ -1,7 +1,7 @@
 {% extends "master.html" %}
 
-{% macro print_ssh_url(repo, git_url_ssh, current_user) %}
-  {% if not user_can_clone_ssh(current_user) %}
+{% macro print_ssh_url(repo, end_url_ssh, current_user) %}
+  {% if not (current_user |user_can_clone_ssh) %}
     <a href="{{ url_for('ui_ns.user_settings') + '#nav-ssh-tab' }}">
       You need to upload SSH key to be able to clone over SSH
     </a>
@@ -13,7 +13,7 @@
       <div class="input-group input-group-sm">
         <div class="input-group-addon">SSH</div>
         <input class="form-control bg-white" type="text" value="{{
-          git_url_ssh }}{{ repo.fullname }}.git" readonly>
+          end_url_ssh | git_url_ssh }}{{ repo.fullname }}.git" readonly>
       </div>
     </div>
   {% endif %}
@@ -171,7 +171,7 @@
                 <h5><strong>Source Code</strong></h5>
                 <div>
                   {% if g.authenticated and g.repo_committer %}
-                    {{ print_ssh_url(repo, git_url_ssh, g.fas_user.username) }}
+                    {{ print_ssh_url(repo, g.fas_user.username) }}
                   {% endif %}
                   <div class="form-group">
                     <div class="input-group input-group-sm">
@@ -186,7 +186,7 @@
                         and repo.settings.get('project_documentation', True) %}
                       <h5><strong>Documentation</strong></h5>
                       {% if g.authenticated and g.repo_committer %}
-                        {{ print_ssh_url(repo, git_url_ssh + "docs/", g.fas_user.username) }}
+                        {{ print_ssh_url(repo, "docs/", g.fas_user.username) }}
                       {% endif %}
                       <div class="form-group">
                         <div class="input-group input-group-sm">
@@ -200,10 +200,10 @@
                       {% if config.get('ENABLE_TICKETS', True)
                          and repo.settings.get('issue_tracker', True) %}
                         <h5><strong>Issues</strong></h5>
-                        {{ print_ssh_url(repo, git_url_ssh + "tickets/", g.fas_user.username) }}
+                        {{ print_ssh_url(repo, "tickets/", g.fas_user.username) }}
                       {% endif %}
                       <h5><strong>Pull Requests</strong></h5>
-                      {{ print_ssh_url(repo, git_url_ssh + "requests/", g.fas_user.username) }}
+                      {{ print_ssh_url(repo, "requests/", g.fas_user.username) }}
                     {% endif %}
                   </div>
                 </div>
diff --git a/pagure/ui/filters.py b/pagure/ui/filters.py
index db89e8f..b9840ac 100644
--- a/pagure/ui/filters.py
+++ b/pagure/ui/filters.py
@@ -685,3 +685,32 @@ def join_prefix(values, num):
     if len(values) <= num:
         return ", ".join(values[:-1]) + " and " + values[-1]
     return "%s and %d others" % (", ".join(values[:num]), len(values) - num)
+
+
+@UI_NS.app_template_filter('user_can_clone_ssh')
+def user_can_clone_ssh(username):
+    ssh_keys = ''
+    if flask.g.authenticated:
+        ssh_keys = pagure.lib.search_user(
+            flask.g.session,
+            username=flask.g.fas_user.username
+        ).public_ssh_key or ''
+    if not (pagure_config.get('ALWAYS_RENDER_SSH_CLONE_URL')
+            or ssh_keys.strip()):
+        return False
+    return True
+
+
+@UI_NS.app_template_filter('git_url_ssh')
+def get_git_url_ssh(complement=''):
+    """ Return the GIT SSH URL to be displayed in the UI based on the
+    content of the configuration file.
+    """
+    git_url_ssh = pagure_config.get('GIT_URL_SSH')
+    if flask.g.authenticated and git_url_ssh:
+        try:
+            git_url_ssh = git_url_ssh.format(
+                username=flask.g.fas_user.username)
+        except (KeyError, IndexError):
+            pass
+    return git_url_ssh + complement

From 674045b9cd010c7946b1ab397a64e48e8a683e47 Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Jul 24 2018 10:34:51 +0000
Subject: [PATCH 2/2] Add tests checking that the API doc shows properly


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

---

diff --git a/tests/test_pagure_flask_api.py b/tests/test_pagure_flask_api.py
index dc54ce5..18fd82c 100644
--- a/tests/test_pagure_flask_api.py
+++ b/tests/test_pagure_flask_api.py
@@ -33,6 +33,26 @@ import tests
 class PagureFlaskApitests(tests.SimplePagureTest):
     """ Tests for flask API controller of pagure """
 
+    def test_api_doc(self):
+        """ Test the API documentation page. """
+        output = self.app.get('/api/0/')
+        output_text = output.get_data(as_text=True)
+        self.assertIn(
+            '<title> API | pagure  - Pagure</title>\n', output_text)
+        self.assertIn(
+            '&nbsp; Pagure API Reference\n        </h2>\n', output_text)
+
+    def test_api_doc_authenticated(self):
+        """ Test the API documentation page. """
+        user = tests.FakeUser(username='foo')
+        with tests.user_set(self.app.application, user):
+            output = self.app.get('/api/0/')
+            output_text = output.get_data(as_text=True)
+            self.assertIn(
+                '<title> API | pagure  - Pagure</title>\n', output_text)
+            self.assertIn(
+                '&nbsp; Pagure API Reference\n        </h2>\n', output_text)
+
     def test_api_get_request_data(self):
         data = {'foo': 'bar'}
         # test_request_context doesn't set flask.g, but some teardown

