From c533f0902672694d6c0fe909badd27bff08a1801 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Tue, 7 Apr 2020 18:38:16 +0200
Subject: [PATCH 1/2] seo: add middleware for /robots.txt url (#20263)

---
 hobo/middleware/seo.py | 29 +++++++++++++++++++++++++++++
 hobo/settings.py       |  1 +
 tests/test_seo.py      | 40 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 70 insertions(+)
 create mode 100644 hobo/middleware/seo.py
 create mode 100644 tests/test_seo.py

diff --git a/hobo/middleware/seo.py b/hobo/middleware/seo.py
new file mode 100644
index 0000000..8de46cf
--- /dev/null
+++ b/hobo/middleware/seo.py
@@ -0,0 +1,29 @@
+# hobo - portal to configure and deploy applications
+# Copyright (C) 2020  Entr'ouvert
+#
+# This program is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Affero General Public License as published
+# by the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+from django.http import HttpResponse
+from django.utils.deprecation import MiddlewareMixin
+
+from hobo.environment.utils import get_variable
+
+
+class RobotMiddleware(MiddlewareMixin):
+    def process_request(self, request):
+        if request.method == 'GET' and request.path == '/robots.txt':
+            return HttpResponse(
+                get_variable('robots_txt').value,
+                content_type='text/plain')
+        return None
diff --git a/hobo/settings.py b/hobo/settings.py
index 7373ca8..deeeaf2 100644
--- a/hobo/settings.py
+++ b/hobo/settings.py
@@ -47,16 +47,17 @@ INSTALLED_APPS = (
     'hobo.matomo',
     'hobo.profile',
     'hobo.theme',
     'hobo.emails',
     'hobo.deploy',
 )
 
 MIDDLEWARE_CLASSES = (
+    'hobo.middleware.seo.RobotMiddleware',
     'hobo.middleware.xforwardedfor.XForwardedForMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.locale.LocaleMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware',
diff --git a/tests/test_seo.py b/tests/test_seo.py
new file mode 100644
index 0000000..f976834
--- /dev/null
+++ b/tests/test_seo.py
@@ -0,0 +1,40 @@
+# hobo - portal to configure and deploy applications
+# Copyright (C) 2020  Entr'ouvert
+#
+# This program is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Affero General Public License as published
+# by the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.import pytest
+import pytest
+
+from hobo.environment.utils import get_variable
+from hobo.seo.views import ALLOW, DISALLOW
+
+
+pytestmark = pytest.mark.django_db
+
+def test_middleware(app):
+    variable = get_variable('robots_txt')
+    variable.value = DISALLOW
+    variable.save()
+    resp = app.get('/robots.txt', status=200)
+    assert resp.text == DISALLOW
+
+    variable = get_variable('robots_txt')
+    variable.value = ALLOW
+    variable.save()
+    resp = app.get('/robots.txt', status=200)
+    assert resp.text == ALLOW
+
+    variable = get_variable('robots_txt')
+    variable.delete()
+    resp = app.get('/robots.txt', status=200)
+    assert resp.text == ALLOW
-- 
2.25.1

