summaryrefslogtreecommitdiff
path: root/ironic/tests/unit/api/test_audit.py
blob: 40dd0bf81709a826776b87e58994e3e2a488a090 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
"""
Tests to assert that audit middleware works as expected.
"""

from keystonemiddleware import audit
import mock
from oslo_config import cfg

from ironic.common import exception
from ironic.tests.unit.api import base


CONF = cfg.CONF


class TestAuditMiddleware(base.BaseApiTest):
    """Provide a basic smoke test to ensure audit middleware is active.

    The tests below provide minimal confirmation that the audit middleware
    is called, and may be configured. For comprehensive tests, please consult
    the test suite in keystone audit_middleware.
    """

    @mock.patch.object(audit, 'AuditMiddleware')
    def test_enable_audit_request(self, mock_audit):
        CONF.set_override('enabled', True, 'audit')
        self._make_app()
        mock_audit.assert_called_once_with(
            mock.ANY,
            audit_map_file=CONF.audit.audit_map_file,
            ignore_req_list=CONF.audit.ignore_req_list)

    @mock.patch.object(audit, 'AuditMiddleware')
    def test_enable_audit_request_error(self, mock_audit):
        CONF.set_override('enabled', True, 'audit')
        mock_audit.side_effect = IOError("file access error")
        self.assertRaises(exception.InputFileError,
                          self._make_app)

    @mock.patch.object(audit, 'AuditMiddleware')
    def test_disable_audit_request(self, mock_audit):
        CONF.set_override('enabled', False, 'audit')
        self._make_app()
        self.assertFalse(mock_audit.called)