Completed
Branch master (e3faea)
by Koen
01:21
created

ProtectedTests   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %
Metric Value
wmc 4
dl 0
loc 22
rs 10
1
# -*- coding: utf-8 -*-
2
import unittest
3
from atramhasis.protected_resources import ProtectedResourceEvent, ProtectedResourceException, protected_operation
4
try:
5
    from unittest.mock import Mock, MagicMock
6
except ImportError:
7
    from mock import Mock, MagicMock, call  # pragma: no cover
8
9
10
class DummyParent(object):
11
12
    def __init__(self):
13
        self.request = MagicMock()
14
        self.request.path_url = 'http://localhost/conceptschemes/GEOGRAPHY/c/9'
15
16
    @protected_operation
17
    def protected_dummy(self):
18
        return 'dummy ok'
19
20
21
class ProtectedTests(unittest.TestCase):
22
23
    def setUp(self):
24
        pass
25
26
    def test_protected_resource_event(self):
27
        event = ProtectedResourceEvent('urn:test')
28
        self.assertEqual('urn:test', event.uri)
29
30
    def test_protected_resource_exception(self):
31
        referenced_in = ['urn:someobject', 'http://test.test.org/object/2']
32
        error = ProtectedResourceException('test_msg', referenced_in)
33
        self.assertIsNotNone(error)
34
        self.assertEqual("'test_msg'", str(error))
35
36
    def test_protected_event(self):
37
        dummy = DummyParent()
38
        notify_mock = Mock()
39
        dummy.request.registry.notify = notify_mock
40
        dummy.protected_dummy()
41
        notify_call = notify_mock.mock_calls[0]
42
        self.assertEqual('http://localhost/conceptschemes/GEOGRAPHY/c/9', notify_call[1][0].uri)