From feb1a745ef4022fa7b17f30d941ecbddd5a2eb07 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Fri, 13 Mar 2020 16:41:55 +0100
Subject: [PATCH] python3: encoding correction on hobo_deploy.py (#40726)

---
 .../management/commands/hobo_deploy.py        |  5 +-
 tests/test_hobo_deploy.py                     | 48 ++++++++++++++++++-
 2 files changed, 49 insertions(+), 4 deletions(-)

diff --git a/bijoe/hobo_agent/management/commands/hobo_deploy.py b/bijoe/hobo_agent/management/commands/hobo_deploy.py
index 285605d..a01922b 100644
--- a/bijoe/hobo_agent/management/commands/hobo_deploy.py
+++ b/bijoe/hobo_agent/management/commands/hobo_deploy.py
@@ -17,16 +17,17 @@
 import os
 import urlparse
 import ConfigParser
 
 from tenant_schemas.utils import tenant_context
 from hobo.agent.common.management.commands import hobo_deploy
 from hobo.multitenant.settings_loaders import KnownServices
 
+from django.utils.encoding import force_str
 from django.conf import settings
 
 
 def pg_dsn_quote(value):
     return "'%s'" % str(value).replace('\\', '\\\\').replace('\'', '\\\'')
 
 
 def config_parser_quote(value):
@@ -83,13 +84,13 @@ class Command(hobo_deploy.Command):
                 key = KnownServices.shared_secret(our_key, their_key)
 
                 if config.has_section(base_url):
                     config.remove_section(base_url)
                 config.add_section(base_url)
                 config.set(base_url, 'orig', orig)
                 config.set(base_url, 'key', key)
                 config.set(base_url, 'schema', schema)
-                config.set(base_url, 'cubes_label', service.get('title').encode('utf-8'))
+                config.set(base_url, 'cubes_label', force_str(service.get('title')))
                 if 'slug' in service:
                     config.set(base_url, 'cubes_slug', service['slug'])
             with open(ini_file, 'w') as f:
-                config.write(f)	
+                config.write(f)
diff --git a/tests/test_hobo_deploy.py b/tests/test_hobo_deploy.py
index c661541..341621c 100644
--- a/tests/test_hobo_deploy.py
+++ b/tests/test_hobo_deploy.py
@@ -1,8 +1,9 @@
+# -*- coding: utf-8 -*-
 # bijoe - BI dashboard
 # Copyright (C) 2015  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.
 #
@@ -10,20 +11,20 @@
 # 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 contextlib import contextmanager
-import ConfigParser
-
 from psycopg2.extensions import parse_dsn
 
+from django.utils.six.moves import configparser as ConfigParser
+
 from bijoe.hobo_agent.management.commands import hobo_deploy
 
 
 @contextmanager
 def donothing(tenant):
     yield
 
 
@@ -71,8 +72,51 @@ def test_deploy_specifics(tmpdir, settings, monkeypatch):
         config.readfp(fd)
         pg_dsn = config.get('wcs-olap', 'pg_dsn')
         parsed_pg_dsn = parse_dsn(pg_dsn)
         assert parsed_pg_dsn['dbname'] == 'coucou'
         assert parsed_pg_dsn['host'] == 'hostname.zob.org'
         assert parsed_pg_dsn['user'] == 'hep'
         assert parsed_pg_dsn['password'] == 'a \'%fc'
         assert parsed_pg_dsn['port'] == '1234'
+
+    bijoe_base_url = 'https://bijoe.example.net'
+    wcs_base_url = 'https://wcs.example.net'
+    hobo_environment = {
+        'services': [
+            {
+                'this': True,
+                'secret_key': 'xx',
+                'service-id': 'bijoe',
+                'base_url': bijoe_base_url,
+            },
+            {
+                'this': False,
+                'secret_key': 'yy',
+                'service-id': 'wcs',
+                'base_url': wcs_base_url,
+                'title': 'my läâàlel',
+                'slug': 'my_slug',
+            },
+            {
+                'this': False,
+                'secret_key': 'zz',
+                'service-id': 'wcs',
+                'base_url': 'https://wcs-2.example.net',
+                'secondary': True,
+            },
+        ],
+    }
+    command.deploy_specifics(hobo_environment, tenant)
+    with wcs_olap_ini_path.open() as fd:
+        config = ConfigParser.SafeConfigParser()
+        config.readfp(fd)
+        assert config.get(wcs_base_url, 'orig') == 'bijoe.example.net'
+        assert config.get(wcs_base_url, 'schema') == 'wcs_example_net'
+        assert config.get(wcs_base_url, 'cubes_label') == 'my läâàlel'
+        assert config.get(wcs_base_url, 'cubes_slug') == 'my_slug'
+
+    hobo_environment['services'][0]['base_url'] = 'https://new-bijoe.example.net'
+    command.deploy_specifics(hobo_environment, tenant)
+    with wcs_olap_ini_path.open() as fd:
+        config = ConfigParser.SafeConfigParser()
+        config.readfp(fd)
+        assert config.get(wcs_base_url, 'orig') == 'new-bijoe.example.net'
-- 
2.25.1

