Completed
Push — master ( 78de89...99647d )
by
unknown
02:14 queued 58s
created

AuditTests.test_audit_put()   A

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 8
loc 8
rs 9.4285
1
# -*- coding: utf-8 -*-
2
import unittest
3
from oe_utils.audit import audit
4
from oe_utils.data.data_managers import AuditManager
5
from oe_utils.data.models import Wijziging
6
from oe_utils.data.db_audit_manager import includeme
7
from oe_utils.data.db_audit_manager import audit_manager
8
9
try:
10
    from unittest.mock import Mock, MagicMock
11
except ImportError:
12
    from mock import Mock, MagicMock, call  # pragma: no cover
13
try:
14
    import configparser
15
except ImportError:
16
    import ConfigParser as configparser
17
import os
18
from sqlalchemy import engine_from_config
19
from sqlalchemy.orm import sessionmaker
20
from zope.sqlalchemy import ZopeTransactionExtension
21
from oe_utils.data.models import Base
22
from jsonpublish import register_adapter
23
24
25
def init_test_db(engine):
26
    Base.metadata.drop_all(engine)
27
    Base.metadata.create_all(engine)
28
    session_maker = sessionmaker(
29
        bind=engine
30
    )
31
    session = session_maker()
32
33
    session.commit()
34
    session.close()
35
36
37
class Resource:
38
    def __init__(self):
39
        self.id = None,
40
        self.naam = None
41
        self.voornaam = None
42
43
44
@register_adapter(Resource)
45
def resource_adapter(obj):
46
    return {
47
        'id': obj.id,
48
        'naam': obj.naam,
49
        'voornaam': obj.voornaam
50
}
51
52
53
class DummyParent(object):
54
55
    def __init__(self):
56
        self.request = MagicMock()
57
58
    @audit()
59
    def dummy(self):
60
        resource = Resource()
61
        resource.id = 123
62
        resource.naam = 'crash test'
63
        resource.voornaam = 'dummy'
64
        return resource
65
66
    @audit()
67
    def dummy_none(self):
68
        return 'test'
69
70
    @audit(actie='bijlage bewerken')
71
    def dummy_bijlage(self):
72
        return None
73
74
75
class AuditTests(unittest.TestCase):
76
77 View Code Duplication
    @classmethod
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
78
    def setUpClass(cls):
79
        config = configparser.ConfigParser()
80
        config.read(os.path.join(os.path.dirname(__file__), 'test.ini'))
81
        settings = config.items('app:oe_utils')
82
        settings = dict((s[0], s[1]) for s in settings)
83
        cls.engine = engine_from_config(settings, prefix='sqlalchemy.')
84
        cls.session_maker = sessionmaker(
85
            bind=cls.engine,
86
            extension=ZopeTransactionExtension()
87
        )
88
89
    def setUp(self):
90
        init_test_db(self.engine)
91
        self.session = self.session_maker()
92
        self.dummy_parent = DummyParent()
93
        self.audit_manager = AuditManager(self.session)
94
        self.dummy_parent.request.audit_manager = self.audit_manager
95
96
    def tearDown(self):
97
        self.session.close()
98
99 View Code Duplication
    def test_audit_get(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
100
        self.dummy_parent.request.user = {'actor': {'uri': 'uri', 'omschrijving': 'omschrijving'}}
101
        self.dummy_parent.request.method = 'GET'
102
        self.dummy_parent.request.matchdict = {'id': 123}
103
        self.dummy_parent.dummy()
104
        wijziging = self.session.query(Wijziging).order_by(Wijziging.updated_at.desc()).first()
105
        self.assertEqual('opvragen', wijziging.actie)
106
        self.assertEqual('omschrijving', wijziging.updated_by_omschrijving)
107
        self.assertEqual('crash test', wijziging.resource_object_json['naam'])
108
109 View Code Duplication
    def test_audit_put(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
110
        self.dummy_parent.request.method = 'PUT'
111
        self.dummy_parent.request.matchdict = {'id': 123}
112
        self.dummy_parent.dummy()
113
        wijziging = self.session.query(Wijziging).order_by(Wijziging.updated_at.desc()).first()
114
        self.assertEqual('bewerken', wijziging.actie)
115
        self.assertEqual('crash test', wijziging.resource_object_json['naam'])
116
        self.assertEqual('publiek', wijziging.updated_by_omschrijving)
117
118
    def test_audit_post(self):
119
        self.dummy_parent.request.method = 'POST'
120
        self.dummy_parent.dummy()
121
        wijziging = self.session.query(Wijziging).order_by(Wijziging.updated_at.desc()).first()
122
        self.assertEqual('aanmaken', wijziging.actie)
123
        self.assertEqual('crash test', wijziging.resource_object_json['naam'])
124
        self.assertEqual('crash test', wijziging.resource_object_json['naam'])
125
126 View Code Duplication
    def test_audit_delete(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
127
        self.dummy_parent.request.method = 'DELETE'
128
        self.dummy_parent.request.matchdict = {'id': 123}
129
        self.dummy_parent.dummy_none()
130
        wijziging = self.session.query(Wijziging).order_by(Wijziging.updated_at.desc()).first()
131
        self.assertEqual('verwijderen', wijziging.actie)
132
        self.assertEqual(123, wijziging.resource_object_id)
133
        self.assertIsNone(wijziging.resource_object_json)
134
135 View Code Duplication
    def test_audit_other(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
136
        self.dummy_parent.request.method = 'HEAD'
137
        self.dummy_parent.request.matchdict = {'id': 123}
138
        self.dummy_parent.dummy_none()
139
        wijziging = self.session.query(Wijziging).order_by(Wijziging.updated_at.desc()).first()
140
        self.assertEqual('onbekend', wijziging.actie)
141
        self.assertEqual(123, wijziging.resource_object_id)
142
        self.assertIsNone(wijziging.resource_object_json)
143
144 View Code Duplication
    def test_audit_bijlage(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
145
        self.dummy_parent.request.method = 'PUT'
146
        self.dummy_parent.request.matchdict = {'id': 123}
147
        self.dummy_parent.dummy_bijlage()
148
        wijziging = self.session.query(Wijziging).order_by(Wijziging.updated_at.desc()).first()
149
        self.assertEqual('bijlage bewerken', wijziging.actie)
150
        self.assertEqual(123, wijziging.resource_object_id)
151
        self.assertIsNone(wijziging.resource_object_json)
152
153
    def test_db_audit_manager(self):
154
        config = Mock()
155
        includeme(config)
156
        request = MagicMock()
157
        request.db = self.session
158
        manager = audit_manager(request)
159
        self.assertIsInstance(manager, AuditManager)
160
161
162