|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
import unittest |
|
3
|
|
|
from atramhasis.audit import audit, _origin_from_response |
|
4
|
|
|
from pyramid.response import Response |
|
5
|
|
|
import logging |
|
6
|
|
|
from testfixtures import LogCapture |
|
7
|
|
|
|
|
8
|
|
|
try: |
|
9
|
|
|
from unittest.mock import Mock, MagicMock |
|
10
|
|
|
except ImportError: |
|
11
|
|
|
from mock import Mock, MagicMock, call # pragma: no cover |
|
12
|
|
|
|
|
13
|
|
|
log = logging.getLogger('') |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
class RecordingManager(object): |
|
17
|
|
|
|
|
18
|
|
|
def __init__(self): |
|
19
|
|
|
self.saved_objects = [] |
|
20
|
|
|
|
|
21
|
|
|
def save(self, object): |
|
22
|
|
|
self.saved_objects.append(object) |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
class DummyParent(object): |
|
26
|
|
|
|
|
27
|
|
|
def __init__(self): |
|
28
|
|
|
self.request = MagicMock() |
|
29
|
|
|
|
|
30
|
|
|
@audit |
|
31
|
|
|
def dummy(self): |
|
32
|
|
|
return None |
|
33
|
|
|
|
|
34
|
|
|
@audit |
|
35
|
|
|
def dummy_with_response(self): |
|
36
|
|
|
return Response(content_type='application/rdf+xml') |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
class AuditTests(unittest.TestCase): |
|
41
|
|
|
|
|
42
|
|
|
def setUp(self): |
|
43
|
|
|
self.audit_manager = RecordingManager() |
|
44
|
|
|
self.dummy_parent = DummyParent() |
|
45
|
|
|
self.dummy_parent.request.data_managers = { |
|
46
|
|
|
'audit_manager': self.audit_manager |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
def tearDown(self): |
|
50
|
|
|
pass |
|
51
|
|
|
|
|
52
|
|
|
def _check(self, nr, origin, type_id_list): |
|
53
|
|
|
self.assertEqual(nr, len(self.audit_manager.saved_objects)) |
|
54
|
|
|
self.assertEqual(origin, self.audit_manager.saved_objects[nr-1].origin) |
|
55
|
|
|
for type_id in type_id_list: |
|
56
|
|
|
self.assertEqual('1', getattr(self.audit_manager.saved_objects[nr-1], type_id)) |
|
57
|
|
|
|
|
58
|
|
|
def test_audit_rest(self): |
|
59
|
|
|
self.dummy_parent.request.url = "http://host/conceptschemes/STYLES" |
|
60
|
|
|
self.dummy_parent.request.accept = ['application/json'] |
|
61
|
|
|
self.dummy_parent.request.matchdict = {'scheme_id': '1'} |
|
62
|
|
|
self.dummy_parent.dummy() |
|
63
|
|
|
self._check(1, 'REST', ['conceptscheme_id']) |
|
64
|
|
|
self.dummy_parent.request.matchdict = {'scheme_id': '1', 'c_id': '1'} |
|
65
|
|
|
self.dummy_parent.dummy() |
|
66
|
|
|
self._check(2, 'REST', ['conceptscheme_id', 'concept_id']) |
|
67
|
|
|
|
|
68
|
|
|
def test_audit_html(self): |
|
69
|
|
|
self.dummy_parent.request.url = "http://host/conceptschemes/STYLES" |
|
70
|
|
|
self.dummy_parent.request.accept = ['text/html'] |
|
71
|
|
|
self.dummy_parent.request.matchdict = {'scheme_id': '1'} |
|
72
|
|
|
self.dummy_parent.dummy() |
|
73
|
|
|
self._check(1, 'HTML', ['conceptscheme_id']) |
|
74
|
|
|
self.dummy_parent.request.matchdict = {'scheme_id': '1', 'c_id': '1'} |
|
75
|
|
|
self.dummy_parent.dummy() |
|
76
|
|
|
self._check(2, 'HTML', ['conceptscheme_id', 'concept_id']) |
|
77
|
|
|
|
|
78
|
|
|
def test_audit_rdf_xml(self): |
|
79
|
|
|
self.dummy_parent.request.matchdict = {'scheme_id': '1'} |
|
80
|
|
|
self.dummy_parent.dummy_with_response() |
|
81
|
|
|
self._check(1, 'RDF', ['conceptscheme_id']) |
|
82
|
|
|
self.dummy_parent.request.matchdict = {'scheme_id': '1', 'c_id': '1'} |
|
83
|
|
|
self.dummy_parent.dummy_with_response() |
|
84
|
|
|
self._check(2, 'RDF', ['conceptscheme_id', 'concept_id']) |
|
85
|
|
|
|
|
86
|
|
|
def test_audit_csv(self): |
|
87
|
|
|
self.dummy_parent.request.url = "http://host/conceptschemes/STYLES.csv" |
|
88
|
|
|
self.dummy_parent.request.accept = "text/csv" |
|
89
|
|
|
self.dummy_parent.request.matchdict = {'scheme_id': '1'} |
|
90
|
|
|
self.dummy_parent.dummy() |
|
91
|
|
|
self._check(1, 'CSV', ['conceptscheme_id']) |
|
92
|
|
|
self.dummy_parent.request.matchdict = {'scheme_id': '1', 'c_id': '1'} |
|
93
|
|
|
self.dummy_parent.dummy() |
|
94
|
|
|
self._check(2, 'CSV', ['conceptscheme_id', 'concept_id']) |
|
95
|
|
|
|
|
96
|
|
|
def test_audit_other(self): |
|
97
|
|
|
self.dummy_parent.request.url = "http://host/conceptschemes/STYLES" |
|
98
|
|
|
self.dummy_parent.request.accept = ['application/octet-stream'] |
|
99
|
|
|
self.dummy_parent.request.matchdict = {'scheme_id': '1'} |
|
100
|
|
|
self.dummy_parent.dummy() |
|
101
|
|
|
self._check(1, None, ['conceptscheme_id']) |
|
102
|
|
|
self.dummy_parent.request.matchdict = {'scheme_id': '1', 'c_id': '1'} |
|
103
|
|
|
self.dummy_parent.dummy() |
|
104
|
|
|
self._check(2, None, ['conceptscheme_id', 'concept_id']) |
|
105
|
|
|
|
|
106
|
|
|
def test_invalid_use(self): |
|
107
|
|
|
with LogCapture() as logs: |
|
108
|
|
|
self.dummy_parent.request.url = "http://host/conceptschemes/STYLES" |
|
109
|
|
|
self.dummy_parent.request.accept = ['application/json'] |
|
110
|
|
|
self.dummy_parent.request.matchdict = {'invalid_parameter_id': '1'} |
|
111
|
|
|
self.dummy_parent.dummy() |
|
112
|
|
|
self.assertIn('Misuse of the audit decorator. The url must at least contain a {scheme_id} parameter', str(logs)) |
|
113
|
|
|
|
|
114
|
|
|
def test_origin_from_response_None(self): |
|
115
|
|
|
res = Response(content_type='application/octet-stream') |
|
116
|
|
|
self.assertIsNone(_origin_from_response(res)) |
|
117
|
|
|
|
|
118
|
|
|
|