def test_requires_existing_resource_for_set_password(self): self.resource.get_by.return_value = [] self.mock_ansible_module.params = PARAMS_FOR_SET_PASSWORD UserModule().run() self.mock_ansible_module.fail_json.assert_called_once_with(exception=mock.ANY, msg='The specified user does not exist.')
def test_should_remove_user(self): self.resource.get_by.return_value = [DEFAULT_PARAMS] self.mock_ansible_module.params = PARAMS_FOR_ABSENT UserModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=UserModule.MSG_DELETED)
def test_should_do_nothing_when_user_not_exist(self): self.resource.get_by.return_value = [] self.mock_ansible_module.params = PARAMS_FOR_ABSENT UserModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=False, msg=UserModule.MSG_ALREADY_ABSENT)
def test_requires_password_for_set_password(self): self.resource.get_by.return_value = DEFAULT_PARAMS data_for_comparison = deepcopy(PARAMS_FOR_SET_PASSWORD) data_for_comparison['data'].pop('password') self.mock_ansible_module.params = data_for_comparison UserModule().run() self.mock_ansible_module.fail_json.assert_called_once_with(exception=mock.ANY, msg='This state requires a password to be declared.')
def test_missing_filed_username(self): self.mock_ansible_module.params = PARAMS_MISSING_USERNAME UserModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=False, failed=True, msg=UserModule.MSG_USERNAME_MISSING )
def test_delete_multiple_user(self): self.resource.delete_multiple_user.return_value = True self.mock_ansible_module.params = PARAMS_DELETE_MULTIPLE_USER UserModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=UserModule.MSG_MULTIPLE_USER_DELETED, ansible_facts=dict(user=True))
def test_requires_existing_resource_for_set_password(self): self.resource.get_by_userName.return_value = None self.mock_ansible_module.params = PARAMS_FOR_SET_PASSWORD UserModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=False, failed=True, msg=UserModule.MSG_USERNAME_DOES_NOT_EXIST)
def test_should_create_new_user(self): self.resource.get_by.side_effect = HPOneViewException('FAKE_MSG_ERROR') self.resource.create.return_value = DEFAULT_PARAMS self.mock_ansible_module.params = PARAMS_FOR_PRESENT UserModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=UserModule.MSG_CREATED, ansible_facts=dict(user=DEFAULT_PARAMS))
def test_set_password_to_a_user(self): self.resource.get_by.return_value = DEFAULT_PARAMS self.resource.update.return_value = DEFAULT_PARAMS self.mock_ansible_module.params = PARAMS_FOR_SET_PASSWORD UserModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=UserModule.MSG_PASSWORD_UPDATED, ansible_facts=dict(user=DEFAULT_PARAMS))
def test_validate_full_name_with_missing_field(self): removed_data = deepcopy(PARAMS_VALIDATE_FULLNAME) removed_data['data'].pop('fullName') self.mock_ansible_module.params = removed_data UserModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=False, failed=True, msg=UserModule.MSG_FULLNAME_MISSING)
def test_validate_full_name(self): self.resource.data = True self.resource.validate_full_name.return_value = self.resource self.mock_ansible_module.params = PARAMS_VALIDATE_FULLNAME UserModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=UserModule.MSG_VALIDATED_FULLNAME, ansible_facts=dict(user=True))
def test_delete_multiple_user_with_missing_field(self): removed_data = deepcopy(PARAMS_DELETE_MULTIPLE_USER) removed_data['data'].pop('users_list') self.mock_ansible_module.params = removed_data UserModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=False, failed=True, msg=UserModule.MSG_USERLIST_MISSING)
def test_requires_password_for_set_password(self): self.resource.get_by_userName.return_value = self.resource data_for_comparison = deepcopy(PARAMS_FOR_SET_PASSWORD) data_for_comparison['data'].pop('oldPassword') self.mock_ansible_module.params = data_for_comparison UserModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=False, failed=True, msg=UserModule.MSG_PASSWORD_MISSING)
def test_update_role_to_userName(self): self.resource.get_by_userName.return_value = self.resource self.resource.update_role_to_userName.return_value = DEFAULT_ROLE self.mock_ansible_module.params = PARAMS_UPDATE_ROLE UserModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=UserModule.MSG_UPDATED_ROLE, ansible_facts=dict(user=DEFAULT_ROLE) )
def test_remove_role_from_username(self): self.resource.get_by_userName.return_value = self.resource self.resource.remove_role_from_username.return_value = True self.mock_ansible_module.params = PARAMS_REMOVE_ROLE UserModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=UserModule.MSG_DELETED_ROLE, ansible_facts=dict(user=True) )
def test_should_not_update_when_data_is_equals(self): data_for_comparison = deepcopy(DEFAULT_PARAMS) data_for_comparison.pop('password') self.resource.get_by.return_value = data_for_comparison self.mock_ansible_module.params = PARAMS_FOR_PRESENT UserModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=False, msg=UserModule.MSG_ALREADY_PRESENT, ansible_facts=dict(user=data_for_comparison))
def test_should_create_new_user(self): self.resource.get_by_userName.return_value = None self.resource.data = DEFAULT_PARAMS self.resource.create.return_value = self.resource self.mock_ansible_module.params = PARAMS_FOR_PRESENT UserModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=UserModule.MSG_CREATED, ansible_facts=dict(user=DEFAULT_PARAMS) )
def test_add_multiple_users(self): data1 = deepcopy(DEFAULT_PARAMS) data2 = deepcopy(DEFAULT_PARAMS) self.resource.data = [data1, data2] self.resource.create_multiple_user.return_value = self.resource self.mock_ansible_module.params = PARAMS_ADD_MULTIPLE_USER UserModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=UserModule.MSG_MULTIPLE_USER_CREATED, ansible_facts=dict(user=[data1, data2]))
def test_update_when_data_has_modified_attributes(self): data_merged = DEFAULT_PARAMS.copy() self.resource.get_by.return_value = DEFAULT_PARAMS self.resource.update.return_value = data_merged self.mock_ansible_module.params = PARAMS_WITH_CHANGES UserModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=UserModule.MSG_UPDATED, ansible_facts=dict(user=data_merged))
def test_missing_field_role_list(self): removed_data = deepcopy(PARAMS_ADD_ROLE) removed_data['data'].pop('role_list') self.resource.get_by_userName.return_value = self.resource self.mock_ansible_module.params = removed_data UserModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=False, failed=True, msg=UserModule.MSG_ROLELIST_MISSING )