def test_strict_user_check_missing_role(self, monkeypatch, module_args):
        """
        Test that the module fails when in strict user check mode
        and the user doesn't have the 'docs' role.
        """
        monkeypatch.setenv('ANSIBLE_STRICT_USER_CHECK_MODE', 'True')

        module_args['default_docs_reviewer'] = '*****@*****.**'
        module_args['_ansible_check_mode'] = True
        set_module_args(module_args)

        mock_ensure = Mock()
        mock_ensure.return_value = {'changed': True}
        monkeypatch.setattr(errata_tool_product, 'ensure_product', mock_ensure)

        mock_get_user = Mock()
        mock_get_user.return_value = {'roles': ['qa']}
        monkeypatch.setattr(common_errata_tool, 'get_user', mock_get_user)

        with pytest.raises(AnsibleFailJson) as ex:
            main()

        result = ex.value.args[0]
        assert result['changed'] is False

        expected = "User [email protected] does not have 'docs' role in ET"
        assert result['msg'] == expected
    def test_strict_user_check_missing_user(self, monkeypatch, module_args):
        """
        Test that the module fails when in strict user check mode
        and the user doesn't exist.
        """
        monkeypatch.setenv('ANSIBLE_STRICT_USER_CHECK_MODE', 'True')

        module_args['default_docs_reviewer'] = '*****@*****.**'
        module_args['_ansible_check_mode'] = True
        set_module_args(module_args)

        mock_ensure = Mock()
        mock_ensure.return_value = {'changed': True}
        monkeypatch.setattr(errata_tool_product, 'ensure_product', mock_ensure)

        mock_get_user = Mock()
        mock_get_user.side_effect = UserNotFoundError('*****@*****.**')
        monkeypatch.setattr(common_errata_tool, 'get_user', mock_get_user)

        with pytest.raises(AnsibleFailJson) as ex:
            main()

        result = ex.value.args[0]
        assert result['changed'] is False

        expected = 'default_docs_reviewer [email protected] account not found'
        assert result['msg'] == expected
 def test_simple(self, monkeypatch, module_args):
     """ Create a simple minimal product. """
     mock_ensure = Mock()
     mock_ensure.return_value = {'changed': True}
     monkeypatch.setattr(errata_tool_product, 'ensure_product', mock_ensure)
     set_module_args(module_args)
     with pytest.raises(AnsibleExitJson) as ex:
         main()
     result = ex.value.args[0]
     assert result['changed'] is True
 def test_invalid_input(self, monkeypatch, module_args):
     """
     When validate_params() raises InvalidInputError, the
     Ansible module exits with the proper error message.
     """
     module_args['valid_bug_states'] = ['BOGUS_STATE_LOL']
     set_module_args(module_args)
     with pytest.raises(AnsibleFailJson) as ex:
         main()
     result = ex.value.args[0]
     assert result['changed'] is False
     expected = 'invalid valid_bug_states value "BOGUS_STATE_LOL"'
     assert result['msg'] == expected
 def test_missing_doc_reviewer(self, monkeypatch, module_args):
     """
     When ensure_product() raises DocsReviewerNotFoundError, the
     Ansible module exits with the proper error message.
     """
     mock_ensure = Mock()
     mock_ensure.side_effect = \
         DocsReviewerNotFoundError('*****@*****.**')
     monkeypatch.setattr(errata_tool_product, 'ensure_product', mock_ensure)
     module_args['default_docs_reviewer'] = '*****@*****.**',
     set_module_args(module_args)
     with pytest.raises(AnsibleFailJson) as ex:
         main()
     result = ex.value.args[0]
     assert result['changed'] is False
     expected = 'default_docs_reviewer [email protected] account not found'
     assert result['msg'] == expected