From 5c642ddaf672abd17239135538fb16fa5d428a38 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Tue, 7 May 2019 19:31:39 +0200
Subject: [PATCH 1/3] cook: add unit tests for cook.py (#32886)

---
 tests/test_cook.py | 377 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 369 insertions(+), 8 deletions(-)

diff --git a/tests/test_cook.py b/tests/test_cook.py
index 1f46a35..240592e 100644
--- a/tests/test_cook.py
+++ b/tests/test_cook.py
@@ -1,11 +1,16 @@
 import pytest
-import mock
+import json
 import StringIO
+from mock import call, mock_open, patch, Mock
 
+from django.contrib.auth.models import User
+from django.contrib.contenttypes.models import ContentType
 from django.core.management.base import CommandError
 
-from hobo.environment.models import ServiceBase
+from hobo.environment.models import (Authentic, BiJoe, Chrono, Combo, Corbo, Fargo,
+    Hobo, Passerelle, ServiceBase, Variable, Wcs, Welco)
 from hobo.environment.management.commands.cook import Command
+from hobo.profile.models import AttributeDefinition
 
 
 def test_check_action(monkeypatch):
@@ -55,9 +60,48 @@ def test_check_action_invalid_certificat(monkeypatch):
         command.check_action(action, action_args)
     assert 'has no valid certificate' in str(e_info.value)
 
-@mock.patch('hobo.environment.management.commands.cook.open')
+def test_handle():
+    kwargs = {'verbosity': 'verbosity value',
+              'timeout': 'timeout value',
+              'permissive': 'permissive value'}
+    command = Command()
+    command.run_cook = Mock()
+
+    command.handle('recipe_me.json', **kwargs)
+    assert command.verbosity == 'verbosity value'
+    assert command.timeout == 'timeout value'
+    assert command.permissive == 'permissive value'
+    assert command.run_cook.mock_calls == [call('recipe_me.json')]
+
+@patch('hobo.environment.management.commands.cook.open')
+@patch('hobo.environment.management.commands.cook.notify_agents')
+def test_run_cook(mocked_notify_agents, mocked_open):
+    receipe = """{
+  "steps": [
+    {"create-hobo": {
+      "url": "https://entrouvert.org/"
+    }}
+  ]
+}"""
+    command = Command()
+    command.permissive = False
+    command.must_notify = True
+    command.timeout = 42
+    command.check_action = Mock()
+    command.create_hobo = Mock()
+    mocked_notify_agents = Mock()
+    command.wait_operationals = Mock()
+
+    mocked_open.side_effect = [StringIO.StringIO(receipe)]
+    command.run_cook('recipe_me.json')
+    assert command.check_action.mock_calls == [
+        call(u'create-hobo', {u'url': u'https://entrouvert.org/'})]
+    assert command.create_hobo.mock_calls == [call(url=u'https://entrouvert.org/')]
+    assert mocked_notify_agents.mock_calls == []
+    assert command.wait_operationals.mock_calls == [call(timeout=42)]
+
+@patch('hobo.environment.management.commands.cook.open')
 def test_having_several_action_args(mocked_open):
-    """load variables from a file"""
     receipe = """{
   "steps": [
     {"create-authentic": {
@@ -66,12 +110,329 @@ def test_having_several_action_args(mocked_open):
     }}
   ]
 }"""
-    expected_a2 = "[call(title=u'Connexion', url=u'https://entrouvert.org/')]"
-
     command = Command()
     command.permissive = True
-    command.create_authentic = mock.MagicMock()
+    command.create_authentic = Mock()
 
     mocked_open.side_effect = [StringIO.StringIO(receipe)]
     command.run_cook('recipe_me.json')
-    assert str(command.create_authentic.mock_calls) == expected_a2
+    assert command.create_authentic.mock_calls == [
+        call(title=u'Connexion', url=u'https://entrouvert.org/')]
+
+@patch('hobo.environment.management.commands.cook.open')
+def test_load_variables_from(mocked_open):
+    """load variables from a file"""
+    receipe = """{
+  "load-variables-from": "variables.json",
+  "variables": {
+    "domain": "entrouvert.org",
+    "bar": "bar2"
+  },
+  "steps": [
+    {"create-hobo": {
+      "url": "https://${domain}/"
+    }},
+    {"create-authentic": {
+      "url": "https://${foo}/"
+    }},
+    {"create-combo": {
+      "url": "https://${bar}/",
+      "not_a_string": []
+    }}
+  ]
+}"""
+    variables = """{
+  "foo": "foo1",
+  "bar": "bar1"
+}"""
+    command = Command()
+    command.permissive = True
+    command.create_hobo = Mock()
+    command.create_authentic = Mock()
+    command.create_combo = Mock()
+
+    mocked_open.side_effect = [StringIO.StringIO(receipe),
+                               StringIO.StringIO(variables)]
+    command.run_cook('recipe_me.json')
+    assert command.create_hobo.mock_calls == [call(url=u'https://entrouvert.org/')]
+    assert command.create_authentic.mock_calls == [call(url=u'https://foo1/')]
+    assert command.create_combo.mock_calls == [
+        call(not_a_string=[], url=u'https://bar2/')]
+
+def test_wait_operationals(db, monkeypatch):
+    service1 = Mock()
+    service2 = Mock()
+    obj1 = Mock()
+    obj2 = Mock()
+
+    def do_nothing(*args, **kwargs):
+        pass
+
+    obj1.check_operational = do_nothing
+    obj2.check_operational = do_nothing
+    obj1.base_url = 'url1'
+    obj2.base_url = 'url2'
+
+    service1.objects.all = Mock(return_value=[obj1])
+    service2.objects.all = Mock(return_value=[obj2])
+    monkeypatch.setattr(
+        'hobo.environment.management.commands.cook.AVAILABLE_SERVICES',
+        [service1, service2])
+    command = Command()
+
+    # already operational
+    obj1.last_operational_success_timestamp = 'some date'
+    obj2.last_operational_success_timestamp = 'some date'
+    command.wait_operationals(2)
+    assert True
+
+    # not operational
+    obj2.last_operational_success_timestamp = None
+    with pytest.raises(CommandError) as e_info:
+        command.wait_operationals(.6)
+    assert str(e_info).find('CommandError: timeout waiting for url2') != -1
+
+def test_set_variable(db):
+    command = Command()
+
+    command.set_variable('foo', 'bar')
+    value = Variable.objects.get(name='foo').value
+    label = Variable.objects.get(name='foo').label
+    assert value == 'bar'
+    assert label == 'foo'
+
+    command.set_variable('foo', 'bar', label='FOO')
+    value = Variable.objects.get(name='foo').value
+    label = Variable.objects.get(name='foo').label
+    assert value == 'bar'
+    assert label == 'FOO'
+
+    command.set_variable('foo', ['bar', 'baz'])
+    value = Variable.objects.get(name='foo').value
+    assert value == '["bar", "baz"]'
+
+    command.set_variable('foo', {'key1': 'bar', 'key2': 'baz'})
+    value = Variable.objects.get(name='foo').value
+    ordered_dump = json.dumps(json.loads(value), sort_keys=True)
+    assert  ordered_dump == '{"key1": "bar", "key2": "baz"}'
+
+def test_set_attribute(db):
+    command = Command()
+    command.set_attribute('foo_name', 'foo_label')
+    values = AttributeDefinition.objects.filter(name='foo_name')
+    assert values.count() == 1
+    assert values[0].label == 'foo_label'
+    assert values[0].disabled is False
+
+    command.set_attribute('foo_name', 'foo_label', disabled=True)
+    values = AttributeDefinition.objects.filter(name='foo_name')
+    assert values.count() == 1
+    assert values[0].label == 'foo_label'
+    assert values[0].disabled is True
+
+def test_disable_attribute(db):
+    command = Command()
+    command.set_attribute('foo_name', 'foo_label')
+    values = AttributeDefinition.objects.filter(name='foo_name')
+    assert values.count() == 1
+    assert values[0].label == 'foo_label'
+    assert values[0].disabled is False
+
+    command.disable_attribute('foo_name')
+    values = AttributeDefinition.objects.filter(name='foo_name')
+    assert values[0].disabled is True
+
+    command.disable_attribute('not_defined')
+    values = AttributeDefinition.objects.filter(name='not_defined')
+    assert values.count() == 0
+
+def test_enable_attribute(db):
+    command = Command()
+    command.set_attribute('foo_name', 'foo_label', disabled=True)
+    values = AttributeDefinition.objects.filter(name='foo_name')
+    assert values.count() == 1
+    assert values[0].label == 'foo_label'
+    assert values[0].disabled is True
+
+    command.enable_attribute('foo_name')
+    values = AttributeDefinition.objects.filter(name='foo_name')
+    assert values[0].disabled is False
+
+    command.enable_attribute('not_defined')
+    values = AttributeDefinition.objects.filter(name='not_defined')
+    assert values.count() == 0
+
+def test_create_superuser(db):
+    command = Command()
+    command.create_superuser()
+    assert User.objects.count() == 1
+    user = User.objects.all()[0]
+    assert user.username == 'admin'
+    assert user.is_superuser is True
+
+def test_create_site(db):
+    """
+    code redondant ?
+      service_type=obj_type,
+      service_pk=obj.id,
+    """
+    command = Command()
+    base_url = 'http://entrouvert.org'
+    title = 'site title'
+    slug = None
+    template_name = ''
+    variables = {'foo': {'label': 'FOO', 'value': {'key': 'bar'}}}
+    command.create_site(Combo, base_url, title, slug, template_name,
+                        variables)
+
+    # Combo object
+    assert Combo.objects.count() == 1
+    combo = Combo.objects.all()[0]
+    assert combo.title == title
+    assert combo.base_url == base_url + '/'
+    assert combo.template_name == ''
+
+    # ContentType object
+    obj_type = ContentType.objects.get_for_model(Combo)
+
+    # Variables
+    variable = Variable.objects.get(name='foo')
+    assert variable.label == 'FOO'
+    assert variable.value == '{"key": "bar"}'
+    assert variable.service_type == obj_type
+    assert variable.service_pk == combo.id
+
+    with pytest.raises(CommandError) as e_info:
+        command.create_site(Combo, 'unvalid_url', 'site title', 'a slug', '', '')
+    assert 'Enter a valid URL.' in str(e_info.value)
+
+@patch('hobo.environment.management.commands.cook.connection')
+@patch('hobo.environment.management.commands.cook.call_command')
+@patch('hobo.environment.management.commands.cook.TenantMiddleware')
+def test_create_hobo_primary(mocked_TenantMiddleware, mocked_call_command,
+                             mocked_connection):
+    command = Command()
+    command.create_site = Mock()
+    tenant = Mock()
+    tenant.schema_name = 'public'
+    tenant.get_directory = Mock(return_value='/foo')
+    mocked_connection.get_tenant = Mock(return_value=tenant)
+    mocked_connection.set_tenant = Mock()
+    mocked_TenantMiddleware.get_tenant_by_hostname = Mock(return_value=tenant)
+    mocked_call_command.side_effect = CommandError
+
+    mocked_open = mock_open()
+    with patch('hobo.environment.management.commands.cook.open', mocked_open,
+               create=True):
+        command.create_hobo('http://entrouvert.org/and_much')
+    assert command.create_site.mock_calls == []
+    assert mocked_call_command.mock_calls == [
+        call('create_hobo_tenant', 'entrouvert.org')]
+    assert len(mocked_connection.set_tenant.mock_calls) == 1
+    assert mocked_open.mock_calls == [
+        call('/foo/base_url', 'w'),
+        call().write('http://entrouvert.org/and_much'),
+        call().close()]
+
+@patch('hobo.environment.management.commands.cook.connection')
+@patch('hobo.environment.management.commands.cook.call_command')
+@patch('hobo.environment.management.commands.cook.TenantMiddleware')
+def test_create_hobo_not_primary(mocked_TenantMiddleware, mocked_call_command,
+                                 mocked_connection):
+    command = Command()
+    command.create_site = Mock()
+    tenant = Mock()
+    tenant.schema_name = 'other than public'
+    tenant.get_directory = Mock(return_value='/foo')
+    mocked_connection.get_tenant = Mock(return_value=tenant)
+    mocked_connection.set_tenant = Mock()
+    mocked_TenantMiddleware.get_tenant_by_hostname = Mock(return_value=tenant)
+
+    mocked_open = mock_open()
+    with patch('hobo.environment.management.commands.cook.open', mocked_open,
+               create=True):
+        command.create_hobo('http://entrouvert.org/and_much')
+    assert command.create_site.mock_calls == [
+        call(Hobo, 'http://entrouvert.org/and_much', None, u'hobo-none',
+             template_name='', variables=None)]
+    assert mocked_call_command.mock_calls == []
+    assert len(mocked_connection.set_tenant.mock_calls) == 1
+    assert mocked_open.mock_calls == []
+
+def test_create_services():
+    command = Command()
+    command.create_site = Mock()
+    command.create_authentic('url', 'title')
+    command.create_combo('url', 'title')
+    command.create_wcs('url', 'title')
+    command.create_passerelle('url', 'title')
+    command.create_fargo('url', 'title')
+    command.create_welco('url', 'title')
+    command.create_chrono('url', 'title')
+    command.create_corbo('url', 'title')
+    command.create_bijoe('url', 'title')
+
+    assert len(command.create_site.mock_calls) == 9
+    assert command.create_site.mock_calls == [
+        call(Authentic, 'url', 'title', None, '', None),
+        call(Combo, 'url', 'title', None, '', None),
+        call(Wcs, 'url', 'title', None, '', None),
+        call(Passerelle, 'url', 'title', None, '', None),
+        call(Fargo, 'url', 'title', None, '', None),
+        call(Welco, 'url', 'title', None, '', None),
+        call(Chrono, 'url', 'title', None, '', None),
+        call(Corbo, 'url', 'title', None, '', None),
+        call(BiJoe, 'url', 'title', None, '', None)]
+
+def test_set_idp(db):
+    command = Command()
+
+    # exceptions maybe we should handle into cook.py ?
+    with pytest.raises(Authentic.DoesNotExist,
+                       match='Authentic matching query does not exist'):
+        command.set_idp('url')
+    with pytest.raises(IndexError, match='list index out of range'):
+        command.set_idp()
+
+    # objects sorted on title: [obj1, obj2]
+    obj1, ignored = Authentic.objects.get_or_create(
+        slug='slug1', defaults={'title': 'bar'})
+    obj2, ignored = Authentic.objects.get_or_create(
+        slug='slug2', defaults={'title': 'foo', 'base_url': 'http://example.org'})
+    assert obj1.use_as_idp_for_self is False
+    assert obj2.use_as_idp_for_self is False
+
+    # set first
+    command.set_idp('')
+    obj = Authentic.objects.get(title='bar')
+    assert obj.use_as_idp_for_self is True
+
+    # set using url
+    command.set_idp('http://example.org/')
+    obj = Authentic.objects.get(title='foo')
+    assert obj.use_as_idp_for_self is True
+
+@patch('hobo.environment.management.commands.cook.set_theme')
+@patch('hobo.environment.management.commands.cook.connection')
+@patch('hobo.agent.common.management.commands.hobo_deploy.Command.configure_theme')
+def test_set_theme(mocked_configure_theme, mocked_connection, mocked_set_theme):
+    mocked_connection.get_tenant = Mock(return_value='the tenant')
+    command = Command()
+    command.set_theme('the theme')
+
+    assert mocked_set_theme.mock_calls == [call('the theme')]
+    assert len(mocked_connection.get_tenant.mock_calls) == 1
+    assert mocked_configure_theme.mock_calls == [
+        call({'variables': {'theme': 'the theme'}}, 'the tenant')]
+
+@patch('hobo.environment.management.commands.cook.connection')
+def test_cook(mocked_connection):
+    mocked_connection.get_tenant = Mock(return_value='the tenant')
+    mocked_connection.set_tenant = Mock()
+    command = Command()
+    command.run_cook = Mock()
+    command.cook('a-recipe-file.json')
+
+    assert len(mocked_connection.get_tenant.mock_calls) == 1
+    assert command.run_cook.mock_calls == [call('a-recipe-file.json')]
+    assert mocked_connection.set_tenant.mock_calls == [call('the tenant')]
-- 
2.20.1

