Best Python code snippet using localstack_python
test_apigw_domain_name.py
Source:test_apigw_domain_name.py
1#!/usr/bin/python2# TODO: License goes here3import library.apigw_domain_name as apigw_domain_name4from library.apigw_domain_name import ApiGwDomainName5import mock6from mock import patch7from mock import create_autospec8from mock import ANY9import unittest10import boto11from botocore.exceptions import BotoCoreError, ClientError12import copy13class TestApiGwDomainName(unittest.TestCase):14 def setUp(self):15 self.module = mock.MagicMock()16 self.module.check_mode = False17 self.module.exit_json = mock.MagicMock()18 self.module.fail_json = mock.MagicMock()19 self.domain_name = ApiGwDomainName(self.module)20 self.domain_name.client = mock.MagicMock()21 self.domain_name.module.params = {22 'name': 'testify',23 'cert_name': 'cert-name',24 'cert_body': 'cert-body',25 'cert_private_key': 'cert-private-key',26 'cert_chain': 'cert-chain',27 'state': 'present',28 }29 reload(apigw_domain_name)30 def test_boto_module_not_found(self):31 # Setup Mock Import Function32 import __builtin__ as builtins33 real_import = builtins.__import__34 def mock_import(name, *args):35 if name == 'boto': raise ImportError36 return real_import(name, *args)37 with mock.patch('__builtin__.__import__', side_effect=mock_import):38 reload(apigw_domain_name)39 ApiGwDomainName(self.module)40 self.module.fail_json.assert_called_with(msg='boto and boto3 are required for this module')41 def test_boto3_module_not_found(self):42 # Setup Mock Import Function43 import __builtin__ as builtins44 real_import = builtins.__import__45 def mock_import(name, *args):46 if name == 'boto3': raise ImportError47 return real_import(name, *args)48 with mock.patch('__builtin__.__import__', side_effect=mock_import):49 reload(apigw_domain_name)50 ApiGwDomainName(self.module)51 self.module.fail_json.assert_called_with(msg='boto and boto3 are required for this module')52 @patch.object(apigw_domain_name, 'boto3')53 def test_boto3_client_properly_instantiated(self, mock_boto):54 ApiGwDomainName(self.module)55 mock_boto.client.assert_called_once_with('apigateway')56 @patch.object(ApiGwDomainName, '_update_domain_name', return_value=('hi', 'mom'))57 def test_process_request_calls_get_domain_name_and_stores_result_when_invoked(self, m):58 self.domain_name.client.get_domain_name = mock.MagicMock(return_value='found it!')59 self.domain_name.process_request()60 self.assertEqual('found it!', self.domain_name.me)61 self.domain_name.client.get_domain_name.assert_called_once_with(domainName='testify')62 def test_process_request_stores_None_result_when_not_found_in_get_domain_name_result(self):63 self.domain_name.client.get_domain_name = mock.MagicMock(side_effect=ClientError({'Error': {'Code': 'x NotFoundException x'}}, 'xxx'))64 self.domain_name.process_request()65 self.assertEqual(None, self.domain_name.me)66 self.domain_name.client.get_domain_name.assert_called_once_with(domainName='testify')67 def test_process_request_calls_fail_json_when_get_domain_name_raises_exception(self):68 self.domain_name.client.get_domain_name = mock.MagicMock(side_effect=BotoCoreError())69 self.domain_name.process_request()70 self.domain_name.client.get_domain_name.assert_called_once_with(domainName='testify')71 self.domain_name.module.fail_json.assert_called_once_with(72 msg='Error when getting domain_name from boto3: An unspecified error occurred'73 )74 @patch.object(ApiGwDomainName, '_delete_domain_name', return_value='Mitchell!')75 @patch.object(ApiGwDomainName, '_retrieve_domain_name', return_value={'id': 'found'})76 def test_process_request_calls_exit_json_with_expected_value_after_successful_delete(self, mr, md):77 self.domain_name.module.params = {78 'name': 'testify',79 'state': 'absent',80 }81 self.domain_name.process_request()82 self.domain_name.module.exit_json.assert_called_once_with(changed='Mitchell!', domain_name=None)83 @patch.object(ApiGwDomainName, '_retrieve_domain_name', return_value={'id': 'found'})84 def test_process_request_calls_delete_domain_name_when_state_absent_and_domain_name_found(self, m):85 self.domain_name.module.params = {86 'name': 'testify',87 'state': 'absent',88 }89 self.domain_name.process_request()90 self.domain_name.client.delete_domain_name.assert_called_once_with(domainName='testify')91 @patch.object(ApiGwDomainName, '_retrieve_domain_name', return_value={'id': 'found'})92 def test_process_request_skips_delete_and_calls_exit_json_with_true_when_check_mode_set_and_auth_found(self, m):93 self.domain_name.module.params = {94 'name': 'testify',95 'state': 'absent',96 }97 self.domain_name.module.check_mode = True98 self.domain_name.process_request()99 self.assertEqual(0, self.domain_name.client.delete_domain_name.call_count)100 self.domain_name.module.exit_json.assert_called_once_with(changed=True, domain_name=None)101 @patch.object(ApiGwDomainName, '_retrieve_domain_name', return_value={'id': 'found'})102 def test_process_request_calls_fail_json_when_delete_domain_name_raises_error(self, m):103 self.domain_name.module.params = {104 'name': 'testify',105 'state': 'absent',106 }107 self.domain_name.client.delete_domain_name = mock.MagicMock(side_effect=BotoCoreError)108 self.domain_name.process_request()109 self.domain_name.client.delete_domain_name.assert_called_once_with(domainName='testify')110 self.domain_name.module.fail_json.assert_called_once_with(111 msg='Error when deleting domain_name via boto3: An unspecified error occurred'112 )113 @patch.object(ApiGwDomainName, '_retrieve_domain_name', return_value=None)114 def test_process_request_skips_delete_when_domain_name_not_found(self, m):115 self.domain_name.module.params = {116 'name': 'testify',117 'state': 'absent',118 }119 self.domain_name.process_request()120 self.assertEqual(0, self.domain_name.client.delete_domain_name.call_count)121 @patch.object(ApiGwDomainName, '_create_domain_name', return_value=('veins', 'clogging'))122 @patch.object(ApiGwDomainName, '_retrieve_domain_name', return_value=None)123 def test_process_request_calls_exit_json_with_expected_value_after_successful_create(self, mra, mca):124 self.domain_name.process_request()125 self.domain_name.module.exit_json.assert_called_once_with(changed='veins', domain_name='clogging')126 @patch.object(ApiGwDomainName, '_retrieve_domain_name', return_value=None)127 def test_process_request_returns_create_domain_name_result_when_create_succeeds(self, m):128 self.domain_name.client.create_domain_name = mock.MagicMock(return_value='woot')129 self.domain_name.process_request()130 self.domain_name.module.exit_json.assert_called_once_with(changed=True, domain_name='woot')131 @patch.object(ApiGwDomainName, '_retrieve_domain_name', return_value=None)132 def test_process_request_calls_create_domain_name_when_state_present_and_domain_name_not_found(self, m):133 self.domain_name.process_request()134 self.domain_name.client.create_domain_name.assert_called_once_with(135 domainName='testify',136 certificateName='cert-name',137 certificateBody='cert-body',138 certificatePrivateKey='cert-private-key',139 certificateChain='cert-chain',140 )141 @patch.object(ApiGwDomainName, '_retrieve_domain_name', return_value=None)142 def test_process_request_calls_fail_json_when_create_domain_name_raises_exception(self, m):143 self.domain_name.client.create_domain_name = mock.MagicMock(side_effect=BotoCoreError())144 self.domain_name.process_request()145 self.domain_name.client.create_domain_name.assert_called_once_with(146 domainName='testify',147 certificateName='cert-name',148 certificateBody='cert-body',149 certificatePrivateKey='cert-private-key',150 certificateChain='cert-chain',151 )152 self.domain_name.module.fail_json.assert_called_once_with(153 msg='Error when creating domain_name via boto3: An unspecified error occurred'154 )155 @patch.object(ApiGwDomainName, '_retrieve_domain_name', return_value=None)156 def test_process_request_validates_required_params_when_state_is_present(self, m):157 orig = copy.deepcopy(self.domain_name.module.params)158 for p in ['cert_name', 'cert_body', 'cert_private_key', 'cert_chain']:159 self.domain_name.module.params = copy.deepcopy(orig)160 self.domain_name.module.params.pop(p)161 self.domain_name.process_request()162 self.assertEqual(0, self.domain_name.client.create_domain_name.call_count)163 self.domain_name.module.fail_json.assert_called_with(msg='All certificate parameters are required to create a domain name')164 @patch.object(ApiGwDomainName, '_retrieve_domain_name', return_value=None)165 def test_process_request_skips_create_call_and_returns_changed_True_when_check_mode(self, m):166 self.domain_name.module.check_mode = True167 self.domain_name.process_request()168 self.assertEqual(0, self.domain_name.client.create_domain_name.call_count)169 self.domain_name.module.exit_json.assert_called_once_with(changed=True, domain_name=None)170 @patch.object(ApiGwDomainName, '_retrieve_domain_name', return_value={'certificateName': 'old'})171 def test_process_request_calls_update_domain_name_when_state_present_and_domain_name_changed(self, m):172 expected_patches = [173 {'op': 'replace', 'path': '/certificateName', 'value': 'cert-name'},174 ]175 self.domain_name.process_request()176 self.domain_name.client.update_domain_name.assert_called_once_with(177 domainName='testify',178 patchOperations=expected_patches179 )180 @patch.object(ApiGwDomainName, '_retrieve_domain_name', return_value={'certificateName': 'cert-name'})181 def test_process_request_skips_update_domain_name_and_replies_false_when_no_changes(self, m):182 self.domain_name.process_request()183 self.assertEqual(0, self.domain_name.client.update_domain_name.call_count)184 self.domain_name.module.exit_json.assert_called_once_with(changed=False, domain_name={'certificateName': 'cert-name'})185 @patch.object(ApiGwDomainName, '_retrieve_domain_name', return_value={'certificateName': 'cert-namexxx'})186 def test_process_request_calls_fail_json_when_update_domain_name_raises_exception(self, m):187 self.domain_name.client.update_domain_name = mock.MagicMock(side_effect=BotoCoreError())188 self.domain_name.process_request()189 self.domain_name.client.update_domain_name.assert_called_once_with(190 domainName='testify',191 patchOperations=[{'op': 'replace', 'path': '/certificateName', 'value': 'cert-name'}]192 )193 self.domain_name.module.fail_json.assert_called_once_with(194 msg='Error when updating domain_name via boto3: An unspecified error occurred'195 )196 @patch.object(ApiGwDomainName, '_retrieve_domain_name', side_effect=[{'certificateName': 'hi'}, 'second call'])197 def test_process_request_returns_result_of_find_when_update_is_successful(self, m):198 self.domain_name.process_request()199 self.domain_name.client.update_domain_name.assert_called_once_with(200 domainName='testify',201 patchOperations=[{'op': 'replace', 'path': '/certificateName', 'value': 'cert-name'}]202 )203 self.domain_name.module.exit_json.assert_called_once_with(changed=True, domain_name='second call')204 @patch.object(ApiGwDomainName, '_retrieve_domain_name', return_value={'certificateName': 'here'})205 def test_process_request_skips_update_domain_name_and_replies_true_when_check_mode(self, m):206 self.domain_name.module.check_mode = True207 self.domain_name.process_request()208 self.assertEqual(0, self.domain_name.client.update_domain_name.call_count)209 self.domain_name.module.exit_json.assert_called_once_with(changed=True, domain_name={'certificateName': 'here'})210 def test_define_argument_spec(self):211 result = ApiGwDomainName._define_module_argument_spec()212 self.assertIsInstance(result, dict)213 self.assertEqual(result, dict(214 name=dict(required=True, aliases=['domain_name']),215 cert_name=dict(required=False),216 cert_body=dict(required=False),217 cert_private_key=dict(required=False),218 cert_chain=dict(required=False),219 state=dict(default='present', choices=['present', 'absent']),220 ))221 @patch.object(apigw_domain_name, 'AnsibleModule')222 @patch.object(apigw_domain_name, 'ApiGwDomainName')223 def test_main(self, mock_ApiGwDomainName, mock_AnsibleModule):224 mock_ApiGwDomainName_instance = mock.MagicMock()225 mock_AnsibleModule_instance = mock.MagicMock()226 mock_ApiGwDomainName.return_value = mock_ApiGwDomainName_instance227 mock_AnsibleModule.return_value = mock_AnsibleModule_instance228 apigw_domain_name.main()229 mock_ApiGwDomainName.assert_called_once_with(mock_AnsibleModule_instance)230 assert mock_ApiGwDomainName_instance.process_request.call_count == 1231if __name__ == '__main__':...
tests.py
Source:tests.py
1"""2As this is a django internal template, we disable pylint3"""4# pylint: disable=W06115from django.test import TestCase6from django.core.exceptions import ObjectDoesNotExist7from slam_domain.models import Domain, DomainEntry8DOMAIN_OPTIONS_EMPTY = {9 'dns_master': '127.0.0.1'10}11DOMAIN_OPTIONS_FULL = {12 'dns_master': '127.0.0.1',13 'description': 'This is a test',14 'contact': 'ns-master@full.com',15 'creation_date': '2020-01-01'16}17EMPTY_DOMAIN_NAME = 'empty.com'18FULL_DOMAIN_NAME = 'full.com'19DELETE_DOMAIN_NAME = 'delete.com'20UPDATE_DOMAIN_NAME = 'update.com'21class DomainTestCase(TestCase):22 def setUp(self) -> None:23 Domain.create(name=EMPTY_DOMAIN_NAME, args=DOMAIN_OPTIONS_EMPTY)24 Domain.create(name=FULL_DOMAIN_NAME, args=DOMAIN_OPTIONS_FULL)25 Domain.create(name=DELETE_DOMAIN_NAME, args=DOMAIN_OPTIONS_EMPTY)26 Domain.create(name=UPDATE_DOMAIN_NAME, args=DOMAIN_OPTIONS_EMPTY)27 def test_domain_get(self):28 empty_com = Domain.objects.get(name=EMPTY_DOMAIN_NAME)29 full_com = Domain.objects.get(name=FULL_DOMAIN_NAME)30 self.assertEqual(empty_com.dns_master, DOMAIN_OPTIONS_EMPTY['dns_master'])31 self.assertIsNone(empty_com.description)32 self.assertIsNone(empty_com.contact)33 self.assertIsNotNone(empty_com.creation_date)34 self.assertEqual(full_com.dns_master, DOMAIN_OPTIONS_FULL['dns_master'])35 self.assertEqual(full_com.description, DOMAIN_OPTIONS_FULL['description'])36 self.assertEqual(full_com.contact, DOMAIN_OPTIONS_FULL['contact'])37 self.assertIsNotNone(full_com.creation_date)38 def test_domain_delete(self):39 Domain.remove(name=DELETE_DOMAIN_NAME)40 with self.assertRaisesMessage(ObjectDoesNotExist, 'Domain matching query does not exist.'):41 Domain.objects.get(name=DELETE_DOMAIN_NAME)42 def test_domain_update(self):43 Domain.update(name=UPDATE_DOMAIN_NAME, args=DOMAIN_OPTIONS_FULL)44 update_com = Domain.objects.get(name=UPDATE_DOMAIN_NAME)45 self.assertEqual(update_com.dns_master, DOMAIN_OPTIONS_FULL['dns_master'])46 self.assertEqual(update_com.description, DOMAIN_OPTIONS_FULL['description'])...
DeleteTestScenarios.py
Source:DeleteTestScenarios.py
...27 system_page._save_domain()28 system_page._save_settings()29 self.take_s2_snapshot()30 system_page.click_enterprise_domain()31 system_page.delete_domain_name(system_page.gmail_delete_icon)32 system_page.delete_domain_name(system_page.yahoo_co_in_delete_icon)33 system_page.delete_domain_name(system_page.arubathena_com_delete_icon)34 system_page._save_settings()35 system_page.click_enterprise_domain()36 system_page.assert_domain_name(system_page.gmail_delete_icon)37 system_page.assert_domain_name(system_page.yahoo_co_in_delete_icon)38 system_page.assert_domain_name(system_page.arubathena_com_delete_icon)39 system_page.delete_domain_name(system_page.google_delete_icon)40 system_page.delete_domain_name(system_page.yahoo_com_delete_icon)41 system_page._save_settings()42 self.take_s3_snapshot()43 self.assert_s1_s2_diff(0)44 self.assert_s1_s3_diff()45 self.clear()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!