From 7fec0b69a082aaeec72fee0612766aa42f6b1b4d Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Thu, 18 Apr 2024 07:52:35 +0200 Subject: Merging upstream version 9.4.0+dfsg. Signed-off-by: Daniel Baumann --- .../grafana_datasource/test_grafana_datasource.py | 283 +++++----- .../grafana/grafana_plugin/test_grafana_plugin.py | 35 +- .../grafana/grafana_team/test_grafana_team.py | 572 +++++++++++++-------- .../grafana/grafana_user/test_grafana_user.py | 239 +++++---- 4 files changed, 664 insertions(+), 465 deletions(-) (limited to 'ansible_collections/community/grafana/tests/unit/modules') diff --git a/ansible_collections/community/grafana/tests/unit/modules/grafana/grafana_datasource/test_grafana_datasource.py b/ansible_collections/community/grafana/tests/unit/modules/grafana/grafana_datasource/test_grafana_datasource.py index d2fba0fe1..ff8e4f242 100644 --- a/ansible_collections/community/grafana/tests/unit/modules/grafana/grafana_datasource/test_grafana_datasource.py +++ b/ansible_collections/community/grafana/tests/unit/modules/grafana/grafana_datasource/test_grafana_datasource.py @@ -1,7 +1,7 @@ -from __future__ import (absolute_import, division, print_function) +from __future__ import absolute_import, division, print_function from unittest import TestCase -from unittest.mock import call, patch, MagicMock +from unittest.mock import patch from ansible_collections.community.grafana.plugins.modules import grafana_datasource from ansible.module_utils._text import to_bytes from ansible.module_utils import basic @@ -13,200 +13,207 @@ __metaclass__ = type def set_module_args(args): """prepare arguments so that they will be picked up during module creation""" - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) + args = json.dumps({"ANSIBLE_MODULE_ARGS": args}) basic._ANSIBLE_ARGS = to_bytes(args) def exit_json(*args, **kwargs): """function to patch over exit_json; package return data into an exception""" - if 'changed' not in kwargs: - kwargs['changed'] = False + if "changed" not in kwargs: + kwargs["changed"] = False raise AnsibleExitJson(kwargs) def fail_json(*args, **kwargs): """function to patch over fail_json; package return data into an exception""" - kwargs['failed'] = True + kwargs["failed"] = True raise AnsibleFailJson(kwargs) class AnsibleExitJson(Exception): """Exception class to be raised by module.exit_json and caught by the test case""" + pass class AnsibleFailJson(Exception): """Exception class to be raised by module.fail_json and caught by the test case""" + pass class GrafanaDatasource(TestCase): - def setUp(self): self.authorization = basic_auth_header("admin", "admin") - self.mock_module_helper = patch.multiple(basic.AnsibleModule, - exit_json=exit_json, - fail_json=fail_json) + self.mock_module_helper = patch.multiple( + basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json + ) self.mock_module_helper.start() self.addCleanup(self.mock_module_helper.stop) def test_payload_prometheus(self): expected_payload = { - 'access': 'proxy', - 'basicAuth': False, - 'database': '', - 'isDefault': False, - 'jsonData': { - 'tlsAuth': False, - 'tlsAuthWithCACert': False, - 'tlsSkipVerify': True + "access": "proxy", + "basicAuth": False, + "database": "", + "isDefault": False, + "jsonData": { + "tlsAuth": False, + "tlsAuthWithCACert": False, + "tlsSkipVerify": True, }, - 'name': 'openshift_prometheus', - 'uid': 'xyz123', - 'orgId': 1, - 'secureJsonData': {}, - 'type': 'prometheus', - 'url': 'https://openshift-monitoring.company.com', - 'user': '', - 'withCredentials': False + "name": "openshift_prometheus", + "uid": "xyz123", + "orgId": 1, + "secureJsonData": {}, + "type": "prometheus", + "url": "https://openshift-monitoring.company.com", + "user": "", + "withCredentials": False, } - set_module_args({ - 'url': 'https://grafana.example.com', - 'url_username': 'admin', - 'url_password': 'admin', - 'name': 'openshift_prometheus', - 'uid': 'xyz123', - 'ds_type': 'prometheus', - 'ds_url': 'https://openshift-monitoring.company.com', - 'access': 'proxy', - 'tls_skip_verify': 'true', - }) + set_module_args( + { + "url": "https://grafana.example.com", + "url_username": "admin", + "url_password": "admin", + "name": "openshift_prometheus", + "uid": "xyz123", + "ds_type": "prometheus", + "ds_url": "https://openshift-monitoring.company.com", + "access": "proxy", + "tls_skip_verify": "true", + } + ) module = grafana_datasource.setup_module_object() payload = grafana_datasource.get_datasource_payload(module.params) self.assertEqual(payload, expected_payload) def test_payload_prometheus_with_basic_auth(self): expected_payload = { - 'access': 'proxy', - 'basicAuth': True, - 'basicAuthUser': 'admin', - 'database': '', - 'isDefault': False, - 'jsonData': { - 'tlsAuth': False, - 'tlsAuthWithCACert': False, - 'tlsSkipVerify': True + "access": "proxy", + "basicAuth": True, + "basicAuthUser": "admin", + "database": "", + "isDefault": False, + "jsonData": { + "tlsAuth": False, + "tlsAuthWithCACert": False, + "tlsSkipVerify": True, }, - 'name': 'openshift_prometheus', - 'uid': 'xyz123', - 'orgId': 1, - 'secureJsonData': {'basicAuthPassword': 'admin'}, - 'type': 'prometheus', - 'url': 'https://openshift-monitoring.company.com', - 'user': '', - 'withCredentials': False + "name": "openshift_prometheus", + "uid": "xyz123", + "orgId": 1, + "secureJsonData": {"basicAuthPassword": "admin"}, + "type": "prometheus", + "url": "https://openshift-monitoring.company.com", + "user": "", + "withCredentials": False, } - set_module_args({ - 'url': 'https://grafana.example.com', - 'url_username': 'admin', - 'url_password': 'admin', - 'name': 'openshift_prometheus', - 'uid': 'xyz123', - 'ds_type': 'prometheus', - 'ds_url': 'https://openshift-monitoring.company.com', - 'access': 'proxy', - 'basic_auth_user': 'admin', - 'basic_auth_password': 'admin', - 'tls_skip_verify': 'true', - }) + set_module_args( + { + "url": "https://grafana.example.com", + "url_username": "admin", + "url_password": "admin", + "name": "openshift_prometheus", + "uid": "xyz123", + "ds_type": "prometheus", + "ds_url": "https://openshift-monitoring.company.com", + "access": "proxy", + "basic_auth_user": "admin", + "basic_auth_password": "admin", + "tls_skip_verify": "true", + } + ) module = grafana_datasource.setup_module_object() payload = grafana_datasource.get_datasource_payload(module.params) self.assertEqual(payload, expected_payload) def test_payload_influxdb(self): expected_payload = { - 'access': 'proxy', - 'basicAuth': False, - 'database': 'telegraf', - 'isDefault': False, - 'jsonData': { - 'timeInterval': '>10s', - 'tlsAuth': False, - 'tlsAuthWithCACert': True - }, - 'name': 'datasource-influxdb', - 'uid': 'xyz123', - 'orgId': 1, - 'secureJsonData': { - 'tlsCACert': '/etc/ssl/certs/ca.pem' + "access": "proxy", + "basicAuth": False, + "database": "telegraf", + "isDefault": False, + "jsonData": { + "timeInterval": ">10s", + "tlsAuth": False, + "tlsAuthWithCACert": True, }, - 'type': 'influxdb', - 'url': 'https://influx.company.com:8086', - 'user': '', - 'withCredentials': False + "name": "datasource-influxdb", + "uid": "xyz123", + "orgId": 1, + "secureJsonData": {"tlsCACert": "/etc/ssl/certs/ca.pem"}, + "type": "influxdb", + "url": "https://influx.company.com:8086", + "user": "", + "withCredentials": False, } - set_module_args({ - 'url': 'https://grafana.example.com', - 'url_username': 'admin', - 'url_password': 'admin', - 'name': 'datasource-influxdb', - 'uid': 'xyz123', - 'ds_type': 'influxdb', - 'ds_url': 'https://influx.company.com:8086', - 'database': 'telegraf', - 'time_interval': '>10s', - 'tls_ca_cert': '/etc/ssl/certs/ca.pem' - }) + set_module_args( + { + "url": "https://grafana.example.com", + "url_username": "admin", + "url_password": "admin", + "name": "datasource-influxdb", + "uid": "xyz123", + "ds_type": "influxdb", + "ds_url": "https://influx.company.com:8086", + "database": "telegraf", + "time_interval": ">10s", + "tls_ca_cert": "/etc/ssl/certs/ca.pem", + } + ) module = grafana_datasource.setup_module_object() payload = grafana_datasource.get_datasource_payload(module.params) self.assertEqual(payload, expected_payload) def test_payload_elastic(self): expected_payload = { - 'access': 'proxy', - 'basicAuth': True, - 'basicAuthUser': 'grafana', - 'database': '[logstash_]YYYY.MM.DD', - 'isDefault': False, - 'jsonData': { - 'esVersion': 56, - 'interval': 'Daily', - 'maxConcurrentShardRequests': 42, - 'timeField': '@timestamp', - 'timeInterval': '1m', - 'tlsAuth': False, - 'tlsAuthWithCACert': True + "access": "proxy", + "basicAuth": True, + "basicAuthUser": "grafana", + "database": "[logstash_]YYYY.MM.DD", + "isDefault": False, + "jsonData": { + "esVersion": 56, + "interval": "Daily", + "maxConcurrentShardRequests": 42, + "timeField": "@timestamp", + "timeInterval": "1m", + "tlsAuth": False, + "tlsAuthWithCACert": True, }, - 'name': 'datasource-elastic', - 'uid': 'xyz123', - 'orgId': 1, - 'secureJsonData': { - 'basicAuthPassword': 'grafana', - 'tlsCACert': '/etc/ssl/certs/ca.pem' + "name": "datasource-elastic", + "uid": "xyz123", + "orgId": 1, + "secureJsonData": { + "basicAuthPassword": "grafana", + "tlsCACert": "/etc/ssl/certs/ca.pem", }, - 'type': 'elasticsearch', - 'url': 'https://elastic.company.com:9200', - 'user': '', - 'withCredentials': False + "type": "elasticsearch", + "url": "https://elastic.company.com:9200", + "user": "", + "withCredentials": False, } - set_module_args({ - 'url': 'https://grafana.example.com', - 'url_username': 'admin', - 'url_password': 'admin', - 'name': 'datasource-elastic', - 'uid': 'xyz123', - 'ds_type': 'elasticsearch', - 'ds_url': 'https://elastic.company.com:9200', - 'database': '[logstash_]YYYY.MM.DD', - 'basic_auth_user': 'grafana', - 'basic_auth_password': 'grafana', - 'time_field': '@timestamp', - 'time_interval': '1m', - 'interval': 'Daily', - 'es_version': 56, - 'max_concurrent_shard_requests': 42, - 'tls_ca_cert': '/etc/ssl/certs/ca.pem' - }) + set_module_args( + { + "url": "https://grafana.example.com", + "url_username": "admin", + "url_password": "admin", + "name": "datasource-elastic", + "uid": "xyz123", + "ds_type": "elasticsearch", + "ds_url": "https://elastic.company.com:9200", + "database": "[logstash_]YYYY.MM.DD", + "basic_auth_user": "grafana", + "basic_auth_password": "grafana", + "time_field": "@timestamp", + "time_interval": "1m", + "interval": "Daily", + "es_version": 56, + "max_concurrent_shard_requests": 42, + "tls_ca_cert": "/etc/ssl/certs/ca.pem", + } + ) module = grafana_datasource.setup_module_object() payload = grafana_datasource.get_datasource_payload(module.params) self.assertEqual(payload, expected_payload) diff --git a/ansible_collections/community/grafana/tests/unit/modules/grafana/grafana_plugin/test_grafana_plugin.py b/ansible_collections/community/grafana/tests/unit/modules/grafana/grafana_plugin/test_grafana_plugin.py index b3b025c4e..ec691a467 100644 --- a/ansible_collections/community/grafana/tests/unit/modules/grafana/grafana_plugin/test_grafana_plugin.py +++ b/ansible_collections/community/grafana/tests/unit/modules/grafana/grafana_plugin/test_grafana_plugin.py @@ -1,4 +1,4 @@ -from __future__ import (absolute_import, division, print_function) +from __future__ import absolute_import, division, print_function from unittest import TestCase from unittest.mock import patch, MagicMock @@ -53,14 +53,13 @@ Error: ✗ plugin does not exist class GrafanaPlugin(TestCase): - - @patch('ansible_collections.community.grafana.plugins.modules.grafana_plugin.grafana_cli_bin') + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_plugin.grafana_cli_bin" + ) def test_plugin_install_zip(self, mock_grafana_cli_bin): mock_grafana_cli_bin.return_value = "grafana-cli plugins" - params = { - "name": "alexanderzobnin-zabbix-app" - } + params = {"name": "alexanderzobnin-zabbix-app"} module = MagicMock() module.run_command.return_value = run_command_install_zip() @@ -68,13 +67,13 @@ class GrafanaPlugin(TestCase): result = grafana_plugin.get_grafana_plugin_version(module, params) self.assertEqual(result, None) - @patch('ansible_collections.community.grafana.plugins.modules.grafana_plugin.grafana_cli_bin') + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_plugin.grafana_cli_bin" + ) def test_plugin_ls(self, mock_grafana_cli_bin): mock_grafana_cli_bin.return_value = "grafana-cli plugins" - params = { - "name": "alexanderzobnin-zabbix-app" - } + params = {"name": "alexanderzobnin-zabbix-app"} module = MagicMock() module.run_command.return_value = run_command_ls() @@ -82,13 +81,13 @@ class GrafanaPlugin(TestCase): result = grafana_plugin.get_grafana_plugin_version(module, params) self.assertEqual(result, "3.10.5") - @patch('ansible_collections.community.grafana.plugins.modules.grafana_plugin.grafana_cli_bin') + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_plugin.grafana_cli_bin" + ) def test_plugin_uninstall(self, mock_grafana_cli_bin): mock_grafana_cli_bin.return_value = "grafana-cli plugins" - params = { - "name": "alexanderzobnin-zabbix-app" - } + params = {"name": "alexanderzobnin-zabbix-app"} module = MagicMock() module.run_command.return_value = run_command_uninstall() @@ -96,13 +95,13 @@ class GrafanaPlugin(TestCase): result = grafana_plugin.get_grafana_plugin_version(module, params) self.assertEqual(result, None) - @patch('ansible_collections.community.grafana.plugins.modules.grafana_plugin.grafana_cli_bin') + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_plugin.grafana_cli_bin" + ) def test_plugin_uninstall_again(self, mock_grafana_cli_bin): mock_grafana_cli_bin.return_value = "grafana-cli plugins" - params = { - "name": "alexanderzobnin-zabbix-app" - } + params = {"name": "alexanderzobnin-zabbix-app"} module = MagicMock() module.run_command.return_value = run_command_uninstall_again() diff --git a/ansible_collections/community/grafana/tests/unit/modules/grafana/grafana_team/test_grafana_team.py b/ansible_collections/community/grafana/tests/unit/modules/grafana/grafana_team/test_grafana_team.py index c59953afa..8efba998b 100644 --- a/ansible_collections/community/grafana/tests/unit/modules/grafana/grafana_team/test_grafana_team.py +++ b/ansible_collections/community/grafana/tests/unit/modules/grafana/grafana_team/test_grafana_team.py @@ -1,7 +1,7 @@ -from __future__ import (absolute_import, division, print_function) +from __future__ import absolute_import, division, print_function from unittest import TestCase -from unittest.mock import patch, MagicMock +from unittest.mock import patch from ansible_collections.community.grafana.plugins.modules import grafana_team from ansible.module_utils._text import to_bytes from ansible.module_utils import basic @@ -21,30 +21,32 @@ class MockedReponse(object): def exit_json(*args, **kwargs): """function to patch over exit_json; package return data into an exception""" - if 'changed' not in kwargs: - kwargs['changed'] = False + if "changed" not in kwargs: + kwargs["changed"] = False raise AnsibleExitJson(kwargs) def fail_json(*args, **kwargs): """function to patch over fail_json; package return data into an exception""" - kwargs['failed'] = True + kwargs["failed"] = True raise AnsibleFailJson(kwargs) class AnsibleExitJson(Exception): """Exception class to be raised by module.exit_json and caught by the test case""" + pass class AnsibleFailJson(Exception): """Exception class to be raised by module.fail_json and caught by the test case""" + pass def set_module_args(args): """prepare arguments so that they will be picked up during module creation""" - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) + args = json.dumps({"ANSIBLE_MODULE_ARGS": args}) basic._ANSIBLE_ARGS = to_bytes(args) @@ -65,7 +67,10 @@ def get_low_version_resp(): def team_exists_resp(): - server_response = json.dumps({"totalCount": 1, "teams": [{"name": "MyTestTeam", "email": "email@test.com"}]}, sort_keys=True) + server_response = json.dumps( + {"totalCount": 1, "teams": [{"name": "MyTestTeam", "email": "email@test.com"}]}, + sort_keys=True, + ) return (MockedReponse(server_response), {"status": 200}) @@ -90,21 +95,26 @@ def team_deleted_resp(): def team_members_resp(): - server_response = json.dumps([{ - "orgId": 1, - "teamId": 2, - "userId": 3, - "email": "user1@email.com", - "login": "user1", - "avatarUrl": r"\/avatar\/1b3c32f6386b0185c40d359cdc733a79" - }, { - "orgId": 1, - "teamId": 2, - "userId": 2, - "email": "user2@email.com", - "login": "user2", - "avatarUrl": r"\/avatar\/cad3c68da76e45d10269e8ef02f8e73e" - }]) + server_response = json.dumps( + [ + { + "orgId": 1, + "teamId": 2, + "userId": 3, + "email": "user1@email.com", + "login": "user1", + "avatarUrl": r"\/avatar\/1b3c32f6386b0185c40d359cdc733a79", + }, + { + "orgId": 1, + "teamId": 2, + "userId": 2, + "email": "user2@email.com", + "login": "user2", + "avatarUrl": r"\/avatar\/cad3c68da76e45d10269e8ef02f8e73e", + }, + ] + ) return (MockedReponse(server_response), {"status": 200}) @@ -124,12 +134,11 @@ def delete_team_member_resp(): class GrafanaTeamsTest(TestCase): - def setUp(self): self.authorization = basic_auth_header("admin", "admin") - self.mock_module_helper = patch.multiple(basic.AnsibleModule, - exit_json=exit_json, - fail_json=fail_json) + self.mock_module_helper = patch.multiple( + basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json + ) self.mock_module_helper.start() self.addCleanup(self.mock_module_helper.stop) @@ -139,112 +148,148 @@ class GrafanaTeamsTest(TestCase): with self.assertRaises(AnsibleFailJson) as result: grafana_team.main() - err, arg_list = result.exception.args[0]['msg'].split(':') - missing_args = [item.strip() for item in arg_list.split(',')] - self.assertEqual(err, 'missing required arguments') + err, arg_list = result.exception.args[0]["msg"].split(":") + self.assertEqual(err, "missing required arguments") self.assertEqual(arg_list, ["name", "email", "url"]) def test_module_setup_fails_without_name(self): - set_module_args({ - 'email': 'email@test.com', - 'url': 'http://grafana.example.com' - }) + set_module_args( + {"email": "email@test.com", "url": "http://grafana.example.com"} + ) with self.assertRaises(AnsibleFailJson) as result: grafana_team.main() - self.assertEqual(result.exception.args[0]['msg'], 'missing required arguments: name') + self.assertEqual( + result.exception.args[0]["msg"], "missing required arguments: name" + ) def test_module_setup_fails_without_email(self): - set_module_args({ - 'name': 'MyTestTeam', - 'url': 'http://grafana.example.com' - }) + set_module_args({"name": "MyTestTeam", "url": "http://grafana.example.com"}) with self.assertRaises(AnsibleFailJson) as result: grafana_team.main() - self.assertEqual(result.exception.args[0]['msg'], 'missing required arguments: email') + self.assertEqual( + result.exception.args[0]["msg"], "missing required arguments: email" + ) def test_module_setup_fails_without_url(self): - set_module_args({ - 'name': 'MyTestTeam', - 'email': 'email@test.com', - }) + set_module_args( + { + "name": "MyTestTeam", + "email": "email@test.com", + } + ) with self.assertRaises(AnsibleFailJson) as result: grafana_team.main() - self.assertEqual(result.exception.args[0]['msg'], 'missing required arguments: url') + self.assertEqual( + result.exception.args[0]["msg"], "missing required arguments: url" + ) def test_module_setup_fails_with_mutually_exclusive_auth_methods(self): - set_module_args({ - 'name': 'MyTestTeam', - 'email': 'email@test.com', - 'url': 'http://grafana.example.com', - 'grafana_user': 'admin', - 'grafana_api_key': 'random_api_key', - }) + set_module_args( + { + "name": "MyTestTeam", + "email": "email@test.com", + "url": "http://grafana.example.com", + "grafana_user": "admin", + "grafana_api_key": "random_api_key", + } + ) with self.assertRaises(AnsibleFailJson) as result: grafana_team.main() - self.assertEqual(result.exception.args[0]['msg'], 'parameters are mutually exclusive: url_username|grafana_api_key') - - @patch('ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version') + self.assertEqual( + result.exception.args[0]["msg"], + "parameters are mutually exclusive: url_username|grafana_api_key", + ) + + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version" + ) def test_module_fails_with_low_grafana_version(self, mock_get_version): - set_module_args({ - 'name': 'MyTestTeam', - 'email': 'email@test.com', - 'url': 'http://grafana.example.com', - 'grafana_user': 'admin', - 'grafana_password': 'admin', - }) + set_module_args( + { + "name": "MyTestTeam", + "email": "email@test.com", + "url": "http://grafana.example.com", + "grafana_user": "admin", + "grafana_password": "admin", + } + ) - module = grafana_team.setup_module_object() mock_get_version.return_value = get_low_version_resp() with self.assertRaises(AnsibleFailJson) as result: grafana_team.main() - self.assertEqual(result.exception.args[0]['msg'], 'Teams API is available starting Grafana v5') - - @patch('ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version') - @patch('ansible_collections.community.grafana.plugins.modules.grafana_team.fetch_url') - def test_module_failure_with_unauthorized_resp(self, mock_fetch_url, mock_get_version): - set_module_args({ - 'name': 'MyTestTeam', - 'email': 'email@test.com', - 'url': 'http://grafana.example.com', - }) - module = grafana_team.setup_module_object() + self.assertEqual( + result.exception.args[0]["msg"], + "Teams API is available starting Grafana v5", + ) + + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version" + ) + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_team.fetch_url" + ) + def test_module_failure_with_unauthorized_resp( + self, mock_fetch_url, mock_get_version + ): + set_module_args( + { + "name": "MyTestTeam", + "email": "email@test.com", + "url": "http://grafana.example.com", + } + ) mock_fetch_url.return_value = unauthorized_resp() mock_get_version.return_value = get_version_resp() with self.assertRaises(AnsibleFailJson) as result: grafana_team.main() - self.assertTrue(result.exception.args[0]['msg'].startswith('Unauthorized to perform action')) - - @patch('ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version') - @patch('ansible_collections.community.grafana.plugins.modules.grafana_team.fetch_url') - def test_module_failure_with_permission_denied_resp(self, mock_fetch_url, mock_get_version): - set_module_args({ - 'name': 'MyTestTeam', - 'email': 'email@test.com', - 'url': 'http://grafana.example.com', - }) - module = grafana_team.setup_module_object() + self.assertTrue( + result.exception.args[0]["msg"].startswith("Unauthorized to perform action") + ) + + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version" + ) + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_team.fetch_url" + ) + def test_module_failure_with_permission_denied_resp( + self, mock_fetch_url, mock_get_version + ): + set_module_args( + { + "name": "MyTestTeam", + "email": "email@test.com", + "url": "http://grafana.example.com", + } + ) mock_fetch_url.return_value = permission_denied_resp() mock_get_version.return_value = get_version_resp() with self.assertRaises(AnsibleFailJson) as result: grafana_team.main() - self.assertTrue(result.exception.args[0]['msg'].startswith('Permission Denied')) - - @patch('ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version') - @patch('ansible_collections.community.grafana.plugins.modules.grafana_team.fetch_url') + self.assertTrue(result.exception.args[0]["msg"].startswith("Permission Denied")) + + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version" + ) + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_team.fetch_url" + ) def test_get_team_method_with_existing_team(self, mock_fetch_url, mock_get_version): - set_module_args({ - 'state': 'present', - 'name': 'MyTestTeam', - 'email': 'email@test.com', - 'url': 'http://grafana.example.com' - }) + set_module_args( + { + "state": "present", + "name": "MyTestTeam", + "email": "email@test.com", + "url": "http://grafana.example.com", + } + ) module = grafana_team.setup_module_object() mock_fetch_url.return_value = team_exists_resp() mock_get_version.return_value = get_version_resp() @@ -252,21 +297,34 @@ class GrafanaTeamsTest(TestCase): grafana_iface = grafana_team.GrafanaTeamInterface(module) res = grafana_iface.get_team("MyTestTeam") mock_fetch_url.assert_called_once_with( - module, 'http://grafana.example.com/api/teams/search?name=MyTestTeam', + module, + "http://grafana.example.com/api/teams/search?name=MyTestTeam", data=None, - headers={'Content-Type': 'application/json', 'Authorization': self.authorization}, - method='GET') - self.assertEquals(res, {"email": "email@test.com", "name": "MyTestTeam"}) - - @patch('ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version') - @patch('ansible_collections.community.grafana.plugins.modules.grafana_team.fetch_url') - def test_get_team_method_with_non_existing_team(self, mock_fetch_url, mock_get_version): - set_module_args({ - 'state': 'present', - 'name': 'MyTestTeam', - 'email': 'email@test.com', - 'url': 'http://grafana.example.com' - }) + headers={ + "Content-Type": "application/json", + "Authorization": self.authorization, + }, + method="GET", + ) + self.assertEqual(res, {"email": "email@test.com", "name": "MyTestTeam"}) + + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version" + ) + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_team.fetch_url" + ) + def test_get_team_method_with_non_existing_team( + self, mock_fetch_url, mock_get_version + ): + set_module_args( + { + "state": "present", + "name": "MyTestTeam", + "email": "email@test.com", + "url": "http://grafana.example.com", + } + ) module = grafana_team.setup_module_object() mock_fetch_url.return_value = team_not_found_resp() mock_get_version.return_value = get_version_resp() @@ -274,21 +332,32 @@ class GrafanaTeamsTest(TestCase): grafana_iface = grafana_team.GrafanaTeamInterface(module) res = grafana_iface.get_team("MyTestTeam") mock_fetch_url.assert_called_once_with( - module, 'http://grafana.example.com/api/teams/search?name=MyTestTeam', + module, + "http://grafana.example.com/api/teams/search?name=MyTestTeam", data=None, - headers={'Content-Type': 'application/json', 'Authorization': self.authorization}, - method='GET') - self.assertEquals(res, None) - - @patch('ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version') - @patch('ansible_collections.community.grafana.plugins.modules.grafana_team.fetch_url') + headers={ + "Content-Type": "application/json", + "Authorization": self.authorization, + }, + method="GET", + ) + self.assertEqual(res, None) + + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version" + ) + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_team.fetch_url" + ) def test_create_team_method(self, mock_fetch_url, mock_get_version): - set_module_args({ - 'state': 'present', - 'name': 'MyTestTeam', - 'email': 'email@test.com', - 'url': 'http://grafana.example.com' - }) + set_module_args( + { + "state": "present", + "name": "MyTestTeam", + "email": "email@test.com", + "url": "http://grafana.example.com", + } + ) module = grafana_team.setup_module_object() mock_fetch_url.return_value = team_created_resp() mock_get_version.return_value = get_version_resp() @@ -297,21 +366,34 @@ class GrafanaTeamsTest(TestCase): res = grafana_iface.create_team("MyTestTeam", "email@test.com") mock_fetch_url.assert_called_once_with( - module, 'http://grafana.example.com/api/teams', - data=json.dumps({"email": "email@test.com", "name": "MyTestTeam"}, sort_keys=True), - headers={'Content-Type': 'application/json', 'Authorization': self.authorization}, - method='POST') - self.assertEquals(res, {"message": "Team created", "teamId": 2}) - - @patch('ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version') - @patch('ansible_collections.community.grafana.plugins.modules.grafana_team.fetch_url') + module, + "http://grafana.example.com/api/teams", + data=json.dumps( + {"email": "email@test.com", "name": "MyTestTeam"}, sort_keys=True + ), + headers={ + "Content-Type": "application/json", + "Authorization": self.authorization, + }, + method="POST", + ) + self.assertEqual(res, {"message": "Team created", "teamId": 2}) + + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version" + ) + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_team.fetch_url" + ) def test_update_team_method(self, mock_fetch_url, mock_get_version): - set_module_args({ - 'state': 'present', - 'name': 'MyTestTeam', - 'email': 'email@test.com', - 'url': 'http://grafana.example.com' - }) + set_module_args( + { + "state": "present", + "name": "MyTestTeam", + "email": "email@test.com", + "url": "http://grafana.example.com", + } + ) module = grafana_team.setup_module_object() mock_fetch_url.return_value = team_updated_resp() mock_get_version.return_value = get_version_resp() @@ -319,21 +401,34 @@ class GrafanaTeamsTest(TestCase): grafana_iface = grafana_team.GrafanaTeamInterface(module) res = grafana_iface.update_team(2, "MyTestTeam", "email@test.com") mock_fetch_url.assert_called_once_with( - module, 'http://grafana.example.com/api/teams/2', - data=json.dumps({"email": "email@test.com", "name": "MyTestTeam"}, sort_keys=True), - headers={'Content-Type': 'application/json', 'Authorization': self.authorization}, - method='PUT') - self.assertEquals(res, {"message": "Team updated"}) - - @patch('ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version') - @patch('ansible_collections.community.grafana.plugins.modules.grafana_team.fetch_url') + module, + "http://grafana.example.com/api/teams/2", + data=json.dumps( + {"email": "email@test.com", "name": "MyTestTeam"}, sort_keys=True + ), + headers={ + "Content-Type": "application/json", + "Authorization": self.authorization, + }, + method="PUT", + ) + self.assertEqual(res, {"message": "Team updated"}) + + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version" + ) + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_team.fetch_url" + ) def test_delete_team_method(self, mock_fetch_url, mock_get_version): - set_module_args({ - 'state': 'absent', - 'name': 'MyTestTeam', - 'email': 'email@test.com', - 'url': 'http://grafana.example.com' - }) + set_module_args( + { + "state": "absent", + "name": "MyTestTeam", + "email": "email@test.com", + "url": "http://grafana.example.com", + } + ) module = grafana_team.setup_module_object() mock_fetch_url.return_value = team_deleted_resp() mock_get_version.return_value = get_version_resp() @@ -341,21 +436,32 @@ class GrafanaTeamsTest(TestCase): grafana_iface = grafana_team.GrafanaTeamInterface(module) res = grafana_iface.delete_team(2) mock_fetch_url.assert_called_once_with( - module, 'http://grafana.example.com/api/teams/2', + module, + "http://grafana.example.com/api/teams/2", data=None, - headers={'Content-Type': 'application/json', 'Authorization': self.authorization}, - method='DELETE') - self.assertEquals(res, {"message": "Team deleted"}) - - @patch('ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version') - @patch('ansible_collections.community.grafana.plugins.modules.grafana_team.fetch_url') + headers={ + "Content-Type": "application/json", + "Authorization": self.authorization, + }, + method="DELETE", + ) + self.assertEqual(res, {"message": "Team deleted"}) + + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version" + ) + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_team.fetch_url" + ) def test_get_team_members_method(self, mock_fetch_url, mock_get_version): - set_module_args({ - 'state': 'present', - 'name': 'MyTestTeam', - 'email': 'email@test.com', - 'url': 'http://grafana.example.com' - }) + set_module_args( + { + "state": "present", + "name": "MyTestTeam", + "email": "email@test.com", + "url": "http://grafana.example.com", + } + ) module = grafana_team.setup_module_object() mock_fetch_url.return_value = team_members_resp() mock_get_version.return_value = get_version_resp() @@ -363,21 +469,34 @@ class GrafanaTeamsTest(TestCase): grafana_iface = grafana_team.GrafanaTeamInterface(module) res = grafana_iface.get_team_members(2) mock_fetch_url.assert_called_once_with( - module, 'http://grafana.example.com/api/teams/2/members', + module, + "http://grafana.example.com/api/teams/2/members", data=None, - headers={'Content-Type': 'application/json', 'Authorization': self.authorization}, - method='GET') - self.assertEquals(res, ["user1@email.com", "user2@email.com"]) - - @patch('ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version') - @patch('ansible_collections.community.grafana.plugins.modules.grafana_team.fetch_url') - def test_get_team_members_method_no_members_returned(self, mock_fetch_url, mock_get_version): - set_module_args({ - 'state': 'present', - 'name': 'MyTestTeam', - 'email': 'email@test.com', - 'url': 'http://grafana.example.com' - }) + headers={ + "Content-Type": "application/json", + "Authorization": self.authorization, + }, + method="GET", + ) + self.assertEqual(res, ["user1@email.com", "user2@email.com"]) + + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version" + ) + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_team.fetch_url" + ) + def test_get_team_members_method_no_members_returned( + self, mock_fetch_url, mock_get_version + ): + set_module_args( + { + "state": "present", + "name": "MyTestTeam", + "email": "email@test.com", + "url": "http://grafana.example.com", + } + ) module = grafana_team.setup_module_object() mock_fetch_url.return_value = team_members_no_members_resp() mock_get_version.return_value = get_version_resp() @@ -385,63 +504,96 @@ class GrafanaTeamsTest(TestCase): grafana_iface = grafana_team.GrafanaTeamInterface(module) res = grafana_iface.get_team_members(2) mock_fetch_url.assert_called_once_with( - module, 'http://grafana.example.com/api/teams/2/members', + module, + "http://grafana.example.com/api/teams/2/members", data=None, - headers={'Content-Type': 'application/json', 'Authorization': self.authorization}, - method='GET') - self.assertEquals(res, []) - - @patch('ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version') - @patch('ansible_collections.community.grafana.plugins.modules.grafana_team.fetch_url') + headers={ + "Content-Type": "application/json", + "Authorization": self.authorization, + }, + method="GET", + ) + self.assertEqual(res, []) + + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version" + ) + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_team.fetch_url" + ) def test_add_team_member_method(self, mock_fetch_url, mock_get_version): - set_module_args({ - 'state': 'present', - 'name': 'MyTestTeam', - 'email': 'email@test.com', - 'url': 'http://grafana.example.com' - }) + set_module_args( + { + "state": "present", + "name": "MyTestTeam", + "email": "email@test.com", + "url": "http://grafana.example.com", + } + ) module = grafana_team.setup_module_object() mock_fetch_url.return_value = add_team_member_resp() mock_get_version.return_value = get_version_resp() grafana_iface = grafana_team.GrafanaTeamInterface(module) - with patch.object(grafana_team.GrafanaTeamInterface, 'get_user_id_from_mail') as mock_get_user_id_from_mail: + with patch.object( + grafana_team.GrafanaTeamInterface, "get_user_id_from_mail" + ) as mock_get_user_id_from_mail: mock_get_user_id_from_mail.return_value = 42 res = grafana_iface.add_team_member(2, "another@test.com") mock_fetch_url.assert_called_once_with( - module, 'http://grafana.example.com/api/teams/2/members', - data=json.dumps({'userId': 42}), - headers={'Content-Type': 'application/json', 'Authorization': self.authorization}, - method='POST') - self.assertEquals(res, None) - - @patch('ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version') - @patch('ansible_collections.community.grafana.plugins.modules.grafana_team.fetch_url') + module, + "http://grafana.example.com/api/teams/2/members", + data=json.dumps({"userId": 42}), + headers={ + "Content-Type": "application/json", + "Authorization": self.authorization, + }, + method="POST", + ) + self.assertEqual(res, None) + + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_team.GrafanaTeamInterface.get_version" + ) + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_team.fetch_url" + ) def test_delete_team_member_method(self, mock_fetch_url, mock_get_version): - set_module_args({ - 'state': 'present', - 'name': 'MyTestTeam', - 'email': 'email@test.com', - 'url': 'http://grafana.example.com' - }) + set_module_args( + { + "state": "present", + "name": "MyTestTeam", + "email": "email@test.com", + "url": "http://grafana.example.com", + } + ) module = grafana_team.setup_module_object() mock_fetch_url.return_value = delete_team_member_resp() mock_get_version.return_value = get_version_resp() grafana_iface = grafana_team.GrafanaTeamInterface(module) - with patch.object(grafana_team.GrafanaTeamInterface, 'get_user_id_from_mail') as mock_get_user_id_from_mail: + with patch.object( + grafana_team.GrafanaTeamInterface, "get_user_id_from_mail" + ) as mock_get_user_id_from_mail: mock_get_user_id_from_mail.return_value = 42 res = grafana_iface.delete_team_member(2, "another@test.com") mock_fetch_url.assert_called_once_with( - module, 'http://grafana.example.com/api/teams/2/members/42', + module, + "http://grafana.example.com/api/teams/2/members/42", data=None, - headers={'Content-Type': 'application/json', 'Authorization': self.authorization}, - method='DELETE') - self.assertEquals(res, None) + headers={ + "Content-Type": "application/json", + "Authorization": self.authorization, + }, + method="DELETE", + ) + self.assertEqual(res, None) def test_diff_members_function(self): list1 = ["foo@example.com", "bar@example.com"] list2 = ["bar@example.com", "random@example.com"] res = grafana_team.diff_members(list1, list2) - self.assertEquals(res, {"to_del": ["random@example.com"], "to_add": ["foo@example.com"]}) + self.assertEqual( + res, {"to_del": ["random@example.com"], "to_add": ["foo@example.com"]} + ) diff --git a/ansible_collections/community/grafana/tests/unit/modules/grafana/grafana_user/test_grafana_user.py b/ansible_collections/community/grafana/tests/unit/modules/grafana/grafana_user/test_grafana_user.py index 925c01655..09d0dce05 100644 --- a/ansible_collections/community/grafana/tests/unit/modules/grafana/grafana_user/test_grafana_user.py +++ b/ansible_collections/community/grafana/tests/unit/modules/grafana/grafana_user/test_grafana_user.py @@ -1,7 +1,7 @@ -from __future__ import (absolute_import, division, print_function) +from __future__ import absolute_import, division, print_function from unittest import TestCase -from unittest.mock import call, patch, MagicMock +from unittest.mock import call, patch from ansible_collections.community.grafana.plugins.modules import grafana_user from ansible.module_utils._text import to_bytes from ansible.module_utils import basic @@ -21,30 +21,32 @@ class MockedReponse(object): def exit_json(*args, **kwargs): """function to patch over exit_json; package return data into an exception""" - if 'changed' not in kwargs: - kwargs['changed'] = False + if "changed" not in kwargs: + kwargs["changed"] = False raise AnsibleExitJson(kwargs) def fail_json(*args, **kwargs): """function to patch over fail_json; package return data into an exception""" - kwargs['failed'] = True + kwargs["failed"] = True raise AnsibleFailJson(kwargs) class AnsibleExitJson(Exception): """Exception class to be raised by module.exit_json and caught by the test case""" + pass class AnsibleFailJson(Exception): """Exception class to be raised by module.fail_json and caught by the test case""" + pass def set_module_args(args): """prepare arguments so that they will be picked up during module creation""" - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) + args = json.dumps({"ANSIBLE_MODULE_ARGS": args}) basic._ANSIBLE_ARGS = to_bytes(args) @@ -59,75 +61,94 @@ def user_already_exists_resp(): def user_created_resp(): - server_response = json.dumps({ - "id": 2, - "email": "robin@gotham.com", - "name": "Robin", - "login": "adrobinmin", - "theme": "light", - "orgId": 1, - "isGrafanaAdmin": False, - "isDisabled": False, - "isExternal": False, - "authLabels": None, - "updatedAt": "2019-09-25T14:44:37+01:00", - "createdAt": "2019-09-25T14:44:37+01:00" - }, sort_keys=True) + server_response = json.dumps( + { + "id": 2, + "email": "robin@gotham.com", + "name": "Robin", + "login": "adrobinmin", + "theme": "light", + "orgId": 1, + "isGrafanaAdmin": False, + "isDisabled": False, + "isExternal": False, + "authLabels": None, + "updatedAt": "2019-09-25T14:44:37+01:00", + "createdAt": "2019-09-25T14:44:37+01:00", + }, + sort_keys=True, + ) return (MockedReponse(server_response), {"status": 200}) class GrafanaUserTest(TestCase): - def setUp(self): self.authorization = basic_auth_header("admin", "changeme") - self.mock_module_helper = patch.multiple(basic.AnsibleModule, - exit_json=exit_json, - fail_json=fail_json) + self.mock_module_helper = patch.multiple( + basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json + ) self.mock_module_helper.start() self.addCleanup(self.mock_module_helper.stop) # create an already existing user - @patch('ansible_collections.community.grafana.plugins.modules.grafana_user.fetch_url') + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_user.fetch_url" + ) def test_create_user_existing_user(self, mock_fetch_url): - set_module_args({ - 'url': 'https://grafana.example.com', - 'url_username': 'admin', - 'url_password': 'changeme', - 'name': 'Joker', - 'email': 'joker@gotham.com', - 'login': 'joker', - 'password': 'oups', - 'state': 'present' - }) + set_module_args( + { + "url": "https://grafana.example.com", + "url_username": "admin", + "url_password": "changeme", + "name": "Joker", + "email": "joker@gotham.com", + "login": "joker", + "password": "oups", + "state": "present", + } + ) module = grafana_user.setup_module_object() mock_fetch_url.return_value = user_already_exists_resp() grafana_iface = grafana_user.GrafanaUserInterface(module) with self.assertRaises(AnsibleFailJson): - grafana_iface.create_user( - 'Joker', 'joker@gotham.com', 'joker', 'oups') + grafana_iface.create_user("Joker", "joker@gotham.com", "joker", "oups") mock_fetch_url.assert_called_once_with( module, - 'https://grafana.example.com/api/admin/users', - data=json.dumps({'name': 'Joker', 'email': 'joker@gotham.com', - 'login': 'joker', 'password': 'oups'}, sort_keys=True), - headers={'Content-Type': 'application/json', - 'Authorization': self.authorization}, - method='POST') + "https://grafana.example.com/api/admin/users", + data=json.dumps( + { + "name": "Joker", + "email": "joker@gotham.com", + "login": "joker", + "password": "oups", + }, + sort_keys=True, + ), + headers={ + "Content-Type": "application/json", + "Authorization": self.authorization, + }, + method="POST", + ) # create a new user - @patch('ansible_collections.community.grafana.plugins.modules.grafana_user.fetch_url') + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_user.fetch_url" + ) def test_create_user_new_user(self, mock_fetch_url): - set_module_args({ - 'url': 'https://grafana.example.com', - 'url_username': 'admin', - 'url_password': 'changeme', - 'name': 'Robin', - 'email': 'robin@gotham.com', - 'login': 'robin', - 'password': 'oups', - 'state': 'present' - }) + set_module_args( + { + "url": "https://grafana.example.com", + "url_username": "admin", + "url_password": "changeme", + "name": "Robin", + "email": "robin@gotham.com", + "login": "robin", + "password": "oups", + "state": "present", + } + ) module = grafana_user.setup_module_object() mock_fetch_url.return_value = user_created_resp() @@ -136,54 +157,71 @@ class GrafanaUserTest(TestCase): # first call to create user call( module, - 'https://grafana.example.com/api/admin/users', - data=json.dumps({'name': 'Robin', 'email': 'robin@gotham.com', - 'login': 'robin', 'password': 'oups'}, sort_keys=True), - headers={'Content-Type': 'application/json', - 'Authorization': self.authorization}, - method='POST'), - + "https://grafana.example.com/api/admin/users", + data=json.dumps( + { + "name": "Robin", + "email": "robin@gotham.com", + "login": "robin", + "password": "oups", + }, + sort_keys=True, + ), + headers={ + "Content-Type": "application/json", + "Authorization": self.authorization, + }, + method="POST", + ), # second call to return created user call( module, - 'https://grafana.example.com/api/users/lookup?loginOrEmail=robin', + "https://grafana.example.com/api/users/lookup?loginOrEmail=robin", data=None, - headers={'Content-Type': 'application/json', - 'Authorization': self.authorization}, - method='GET'), + headers={ + "Content-Type": "application/json", + "Authorization": self.authorization, + }, + method="GET", + ), ] grafana_iface = grafana_user.GrafanaUserInterface(module) - result = grafana_iface.create_user( - 'Robin', 'robin@gotham.com', 'robin', 'oups') - - mock_fetch_url.assert_has_calls( - expected_fetch_url_calls, any_order=False) - - self.assertEquals(result, { - "id": 2, - "email": "robin@gotham.com", - "name": "Robin", - "login": "adrobinmin", - "theme": "light", - "orgId": 1, - "isGrafanaAdmin": False, - "isDisabled": False, - "isExternal": False, - "authLabels": None, - "updatedAt": "2019-09-25T14:44:37+01:00", - "createdAt": "2019-09-25T14:44:37+01:00" - }) - - @patch('ansible_collections.community.grafana.plugins.modules.grafana_user.fetch_url') + result = grafana_iface.create_user("Robin", "robin@gotham.com", "robin", "oups") + + mock_fetch_url.assert_has_calls(expected_fetch_url_calls, any_order=False) + + self.assertEqual( + result, + { + "id": 2, + "email": "robin@gotham.com", + "name": "Robin", + "login": "adrobinmin", + "theme": "light", + "orgId": 1, + "isGrafanaAdmin": False, + "isDisabled": False, + "isExternal": False, + "authLabels": None, + "updatedAt": "2019-09-25T14:44:37+01:00", + "createdAt": "2019-09-25T14:44:37+01:00", + }, + ) + + @patch( + "ansible_collections.community.grafana.plugins.modules.grafana_user.fetch_url" + ) def test_delete_user(self, mock_fetch_url): - set_module_args({ - 'url': 'https://grafana.example.com', - 'url_username': 'admin', - 'url_password': 'changeme', - 'login': 'batman', - 'state': 'absent' - }) + set_module_args( + { + "url": "https://grafana.example.com", + "url_username": "admin", + "url_password": "changeme", + "login": "batman", + "state": "absent", + } + ) module = grafana_user.setup_module_object() mock_fetch_url.return_value = user_deleted_resp() @@ -192,9 +230,12 @@ class GrafanaUserTest(TestCase): result = grafana_iface.delete_user(user_id) mock_fetch_url.assert_called_once_with( module, - 'https://grafana.example.com/api/admin/users/42', + "https://grafana.example.com/api/admin/users/42", data=None, - headers={'Content-Type': 'application/json', - 'Authorization': self.authorization}, - method='DELETE') - self.assertEquals(result, {"message": "User deleted"}) + headers={ + "Content-Type": "application/json", + "Authorization": self.authorization, + }, + method="DELETE", + ) + self.assertEqual(result, {"message": "User deleted"}) -- cgit v1.2.3