Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_dnsimple_info.py

raw
1 # Copyright (c) Ansible project
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import annotations
6
7 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
8 AnsibleExitJson,
9 AnsibleFailJson,
10 ModuleTestCase,
11 set_module_args,
12 )
13 from httmock import response, urlmatch, with_httmock
14
15 from ansible_collections.community.general.plugins.modules import dnsimple_info
16
17
18 @urlmatch(netloc="(.)*dnsimple.com(.)*", path="/v2/[0-9]*/zones/")
19 def zones_resp(url, request):
20 """return domains"""
21 headers = {"content-type": "application/json"}
22 data_content = {
23 "data": [
24 {
25 "account_id": "1234",
26 },
27 ],
28 "pagination": {"total_pages": 1},
29 }
30 content = data_content
31 return response(200, content, headers, None, 5, request)
32
33
34 @urlmatch(netloc="(.)*dnsimple.com(.)*", path="/v2/[0-9]*/zones/(.)*/records(.*)")
35 def records_resp(url, request):
36 """return record(s)"""
37 headers = {"content-type": "application/json"}
38 data_content = {"data": [{"content": "example", "name": "example.com"}], "pagination": {"total_pages": 1}}
39 content = data_content
40 return response(200, content, headers, None, 5, request)
41
42
43 class TestDNSimple_Info(ModuleTestCase):
44 """Main class for testing dnsimple module."""
45
46 def setUp(self):
47 """Setup."""
48 super().setUp()
49 self.module = dnsimple_info
50
51 def tearDown(self):
52 """Teardown."""
53 super().tearDown()
54
55 def test_with_no_parameters(self):
56 """Failure must occurs when all parameters are missing"""
57 with self.assertRaises(AnsibleFailJson):
58 with set_module_args({}):
59 self.module.main()
60
61 @with_httmock(zones_resp)
62 def test_only_key_and_account(self):
63 """key and account will pass, returns domains"""
64 account_id = "1234"
65 with self.assertRaises(AnsibleExitJson) as exc_info:
66 with set_module_args({"api_key": "abcd1324", "account_id": account_id}):
67 self.module.main()
68 result = exc_info.exception.args[0]
69 # nothing should change
70 self.assertFalse(result["changed"])
71 # we should return at least one item with the matching account ID
72 assert result["dnsimple_domain_info"][0]["account_id"] == account_id
73
74 @with_httmock(records_resp)
75 def test_only_name_without_record(self):
76 """name and no record should not fail, returns the record"""
77 name = "example.com"
78 with self.assertRaises(AnsibleExitJson) as exc_info:
79 with set_module_args({"api_key": "abcd1324", "name": "example.com", "account_id": "1234"}):
80 self.module.main()
81 result = exc_info.exception.args[0]
82 # nothing should change
83 self.assertFalse(result["changed"])
84 # we should return at least one item with matching domain
85 assert result["dnsimple_records_info"][0]["name"] == name
86
87 @with_httmock(records_resp)
88 def test_name_and_record(self):
89 """name and record should not fail, returns the record"""
90 record = "example"
91 with self.assertRaises(AnsibleExitJson) as exc_info:
92 with set_module_args(
93 {"api_key": "abcd1324", "account_id": "1234", "name": "example.com", "record": "example"}
94 ):
95 self.module.main()
96 result = exc_info.exception.args[0]
97 # nothing should change
98 self.assertFalse(result["changed"])
99 # we should return at least one item and content should match
100 assert result["dnsimple_record_info"][0]["content"] == record