From c5adbf447113506c0d52a57c3dfdd0f25f5ed605 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Sat, 22 Feb 2020 22:09:03 +0100
Subject: [PATCH 2/3] tests: add test to environment views and command (#40098)

---
 tests/test_environment.py | 176 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 175 insertions(+), 1 deletion(-)

diff --git a/tests/test_environment.py b/tests/test_environment.py
index fec226f..d0efec5 100644
--- a/tests/test_environment.py
+++ b/tests/test_environment.py
@@ -1,15 +1,16 @@
 # -*- coding: utf-8 -*-
 import pytest
 
 from django.core.exceptions import ValidationError
+from django.core.management import call_command
 from django.utils import timezone
 
-from hobo.environment.models import AVAILABLE_SERVICES, Combo, Passerelle, ServiceBase
+from hobo.environment.models import AVAILABLE_SERVICES, Combo, Passerelle, ServiceBase, Variable
 
 from test_manager import login
 
 pytestmark = pytest.mark.django_db
 
 
 def test_service_id():
     for service in AVAILABLE_SERVICES:
@@ -117,8 +118,181 @@ def test_service_creation_url_validation(app, admin_user, monkeypatch):
     response = form.submit()
     assert 'no valid certificate' in response
 
     assert not Combo.objects.exists()
     monkeypatch.setattr(ServiceBase, 'has_valid_certificate', lambda x: True)
     form = response.form
     response = form.submit()
     assert Combo.objects.exists()
+
+
+def test_home_view(app, admin_user):
+    app = login(app)
+    Combo.objects.create(base_url='https://combo.agglo.love',
+                         template_name='...portal-user...',
+                         slug='portal')
+    response = app.get('/sites').follow()
+    assert 'Add new service:' in response.text
+    for service in AVAILABLE_SERVICES:
+        if service.is_enabled():
+            assert str(service._meta.verbose_name) in response.text
+    assert response.html.find('span', {'class': 'slug'}).a.text == 'https://combo.agglo.love/'
+
+
+def test_variables_view(app, admin_user):
+    app = login(app)
+    Variable.objects.create(name='foo', value='bar')
+    response = app.get('/sites/variables')
+    assert response.html.find('label').text == 'foo'
+    assert response.html.find('input')['value'] == 'bar'
+    assert 'Add new variable' in response.text
+
+
+def test_new_variable_view(app, admin_user):
+    app = login(app)
+    response = app.get('/sites/new-variable')
+    response.form['name'] = 'foo'
+    response.form['label'] = 'bar'
+    response.form['value'] = 'barbar'
+    response = response.form.submit()
+    assert response.location == '/sites/variables'
+    assert Variable.objects.all()[0].name == 'foo'
+    assert Variable.objects.all()[0].label == 'bar'
+    assert Variable.objects.all()[0].value == 'barbar'
+    response = app.get('/sites/new-variable')
+    response.form['name'] = 'foo'
+    response.form['label'] = 'bar'
+    response.form['value'] = 'foofoo'
+    response = response.form.submit()
+    assert response.location == '/sites/variables'
+    assert Variable.objects.all()[0].value == 'foofoo'
+
+
+def test_new_variable_service_view(app, admin_user):
+    app = login(app)
+    Combo.objects.create(base_url='https://combo.agglo.love',
+                         template_name='...portal-user...',
+                         slug='portal')
+    response = app.get('/sites/new-variable-combo/portal')
+    response.form['name'] = 'foo'
+    response.form['label'] = 'bar'
+    response.form['value'] = 'barbar'
+    response = response.form.submit()
+    assert response.location == '/sites/'
+    assert Variable.objects.all()[0].name == 'foo'
+    assert Variable.objects.all()[0].label == 'bar'
+    assert Variable.objects.all()[0].value == 'barbar'
+
+
+def test_variable_update_view(app, admin_user):
+    app = login(app)
+    Variable.objects.create(name='foo', value='bar')
+    response = app.get('/sites/update-variable/1')
+    assert response.html.find('input', {'name': 'name'})['value'] == 'foo'
+    assert response.html.find('textarea').text == '\nbar'
+    response.form['value'] = 'barbar'
+    response = response.form.submit()
+    assert response.location == '/sites/variables'
+    assert Variable.objects.all()[0].value == 'barbar'
+
+
+def test_variable_delete_view(app, admin_user):
+    app = login(app)
+    Variable.objects.create(name='foo', value='bar')
+    response = app.get('/sites/delete-variable/1')
+    assert response.html.find('h2').text == 'Removal of "foo"'
+    response = response.form.submit()
+    assert response.location == '/sites/variables'
+    assert Variable.objects.count() == 0
+
+
+def test_service_update_view(app, admin_user):
+    app = login(app)
+    Combo.objects.create(base_url='https://combo.agglo.love',
+                         template_name='...portal-user...',
+                         slug='portal')
+    response = app.get('/sites/save-combo/portal')
+    response.form['title'] = 'foobar'
+    response = response.form.submit()
+    assert response.location == '/sites/'
+    assert Combo.objects.all()[0].title == 'foobar'
+
+
+def test_service_save_extra_variables(app, admin_user, settings):
+    settings.SERVICE_EXTRA_VARIABLES = {
+        'passerelle': ['legal_url', 'commune_url', 'domain_key'],
+        'combo': [{'name': 'theme', 'label': 'Theme'}],
+    }
+    app = login(app)
+    Combo.objects.create(base_url='https://combo.agglo.love',
+                         template_name='...portal-user...',
+                         slug='portal')
+    Passerelle.objects.create(base_url='https://passerelle.agglo.love',
+                         slug='passerelle')
+    app.get('/sites/save-combo/portal')
+    Variable.objects.all()[0].name = 'theme'
+    Variable.objects.all()[0].label = 'Theme'
+    app.get('/sites/save-passerelle/passerelle')
+    Variable.objects.all()[0].name = 'legal_url'
+    Variable.objects.all()[1].name = 'commune_url'
+    Variable.objects.all()[2].name = 'domain_key'
+
+
+def test_service_delete_view(app, admin_user):
+    app = login(app)
+    Combo.objects.create(base_url='https://combo.agglo.love',
+                         template_name='...portal-user...',
+                         title='foo',
+                         slug='portal')
+    response = app.get('/sites/delete-combo/portal')
+    assert response.html.find('h2').text == 'Removal of "foo"'
+    response = response.form.submit()
+    assert response.location == '/sites/'
+    assert Combo.objects.count() == 0
+
+
+def test_check_operational_view(app, admin_user, monkeypatch):
+    app = login(app)
+    Combo.objects.create(base_url='https://combo.agglo.love',
+                         template_name='...portal-user...',
+                         slug='portal')
+    response = app.get('/sites/check_operational/combo/portal')
+    assert response.json['operational'] == False
+
+    monkeypatch.setattr(ServiceBase, 'is_operational', lambda x: True)
+    response = app.get('/sites/check_operational/combo/portal')
+    assert response.json['operational'] == True
+
+    response = app.get('/sites/check_operational/foo/bar', status=404)
+
+
+def test_debug_json_view(app, admin_user):
+    app = login(app)
+    Variable.objects.create(name='foo', value='bar')
+    Combo.objects.create(base_url='https://combo.agglo.love',
+                         template_name='...portal-user...',
+                         slug='portal')
+    response = app.get('/sites/debug-json')
+    assert response.json[0]['variables']['foo'] == 'bar'
+    assert response.json[0]['services'][0]['slug'] == 'hobo'
+    assert response.json[0]['services'][1]['slug'] == 'portal'
+
+
+def test_check_operational_command(monkeypatch, capsys):
+    combo = Combo.objects.create(base_url='https://combo.agglo.love',
+                                 template_name='...portal-user...',
+                                 title='foo',
+                                 slug='portal')
+    monkeypatch.setattr(ServiceBase, 'is_operational', lambda x: True)
+    call_command('check_operational', '-v2')
+    captured = capsys.readouterr()
+    assert captured.out == 'foo is operational\n'
+
+    monkeypatch.setattr(ServiceBase, 'is_operational', lambda x: False)
+    combo.last_operational_success_timestamp = '2022-2-22'
+    combo.save()
+    call_command('check_operational', '-v2')
+    captured = capsys.readouterr()
+    assert captured.out.split('\n')[:-1] == [
+        'foo is NOT operational',
+        '  last operational success: 2022-02-22 00:00:00+00:00'
+    ]
-- 
2.25.0

