Browse Source

Clean test code

Bastien Sevajol (Algoo) 8 years ago
parent
commit
1448193ca3
2 changed files with 59 additions and 57 deletions
  1. 27 5
      tracim/tracim/tests/__init__.py
  2. 32 52
      tracim/tracim/tests/functional/test_calendar.py

+ 27 - 5
tracim/tracim/tests/__init__.py View File

@@ -13,6 +13,7 @@ import transaction
13 13
 from gearbox.commands.setup_app import SetupAppCommand
14 14
 from ldap_test import LdapServer
15 15
 from nose.tools import eq_
16
+from nose.tools import make_decorator
16 17
 from nose.tools import ok_
17 18
 from paste.deploy import loadapp
18 19
 from sqlalchemy.engine import reflection
@@ -31,15 +32,13 @@ from who_ldap import make_connection
31 32
 from tracim.fixtures import FixturesLoader
32 33
 from tracim.fixtures.users_and_groups import Base as BaseFixture
33 34
 from tracim.fixtures.users_and_groups import Test as TestFixture
34
-from tracim.config.app_cfg import daemons, start_daemons
35
+from tracim.config.app_cfg import daemons
35 36
 from tracim.lib.base import logger
36 37
 from tracim.lib.content import ContentApi
37 38
 from tracim.lib.workspace import WorkspaceApi
38 39
 from tracim.model import DBSession, Content
39
-from tracim.model.data import Workspace, ContentType, ContentRevisionRO
40
-from tracim.lib.calendar import CALENDAR_BASE_URL_TEMPLATE
41
-from tracim.lib.calendar import CALENDAR_WORKSPACE_URL_TEMPLATE
42
-from tracim.lib.calendar import CALENDAR_USER_URL_TEMPLATE
40
+from tracim.model.data import Workspace
41
+from tracim.model.data import ContentType
43 42
 
44 43
 __all__ = ['setup_app', 'setup_db', 'teardown_db', 'TestController']
45 44
 
@@ -371,3 +370,26 @@ class TestCalendar(TestController):
371 370
                 shutil.rmtree('{0}/{1}'.format(radicale_fs_path, file))
372 371
         except FileNotFoundError:
373 372
             pass  # Dir not exists yet, no need to clear it
373
+
374
+
375
+def not_raises(*exceptions):
376
+    """
377
+    Test must not raise one of expected exceptions to pass.
378
+    """
379
+    valid = ' or '.join([e.__name__ for e in exceptions])
380
+
381
+    def decorate(func):
382
+        name = func.__name__
383
+
384
+        def newfunc(*arg, **kw):
385
+            try:
386
+                func(*arg, **kw)
387
+            except exceptions as exc:
388
+                message = '{0} raise {1} exception and should be not'\
389
+                    .format(name, exc)
390
+                raise AssertionError(message)
391
+            except:
392
+                raise
393
+        newfunc = make_decorator(func)(newfunc)
394
+        return newfunc
395
+    return decorate

+ 32 - 52
tracim/tracim/tests/functional/test_calendar.py View File

@@ -3,7 +3,9 @@ import time
3 3
 import caldav
4 4
 import transaction
5 5
 from caldav.lib.error import AuthorizationError
6
-from nose.tools import eq_, ok_
6
+from nose.tools import eq_
7
+from nose.tools import ok_
8
+from nose.tools import raises
7 9
 import requests
8 10
 from requests.exceptions import ConnectionError
9 11
 from sqlalchemy.orm.exc import NoResultFound
@@ -13,6 +15,7 @@ from tracim.lib.calendar import CalendarManager
13 15
 from tracim.lib.workspace import WorkspaceApi
14 16
 from tracim.model import DBSession
15 17
 from tracim.tests import TestCalendar as BaseTestCalendar
18
+from tracim.tests import not_raises
16 19
 from tracim.model.auth import User
17 20
 from tracim.model.data import Content
18 21
 
@@ -34,6 +37,7 @@ class TestCalendar(BaseTestCalendar):
34 37
         except ConnectionError as exc:
35 38
             ok_(False, 'Unable to contact radicale on HTTP: {0}'.format(exc))
36 39
 
40
+    @not_raises(AuthorizationError)
37 41
     def test_func__radicale_auth__ok__as_lawrence(self):
38 42
         radicale_base_url = CalendarManager.get_base_url()
39 43
         client = caldav.DAVClient(
@@ -41,12 +45,9 @@ class TestCalendar(BaseTestCalendar):
41 45
             username='lawrence-not-real-email@fsf.local',
42 46
             password='foobarbaz'
43 47
         )
44
-        try:
45
-            client.propfind()
46
-            ok_(True, 'No auth error when communicate with radicale server')
47
-        except AuthorizationError:
48
-            ok_(False, 'AuthorizationError when communicate with radicale')
48
+        client.propfind()
49 49
 
50
+    @raises(AuthorizationError)
50 51
     def test_func__radicale_auth__fail__as_john_doe(self):
51 52
         radicale_base_url = CalendarManager.get_base_url()
52 53
         client = caldav.DAVClient(
@@ -54,13 +55,9 @@ class TestCalendar(BaseTestCalendar):
54 55
             username='john.doe@foo.local',
55 56
             password='nopasswd'
56 57
         )
57
-        try:
58
-            client.propfind()
59
-            ok_(False, 'Auth with unknown user should be raise'
60
-                       ' AuthorizationError !')
61
-        except AuthorizationError:
62
-            ok_(True, 'AuthorizationError thrown correctly')
58
+        client.propfind()
63 59
 
60
+    @not_raises(AuthorizationError)
64 61
     def test_func__rights_read_user_calendar__ok__as_lawrence(self):
65 62
         radicale_base_url = CalendarManager.get_base_url()
66 63
         client = caldav.DAVClient(
@@ -72,17 +69,13 @@ class TestCalendar(BaseTestCalendar):
72 69
             User.email == 'lawrence-not-real-email@fsf.local'
73 70
         ).one()
74 71
         user_calendar_url = CalendarManager.get_user_calendar_url(user.user_id)
75
-        try:
76
-            caldav.Calendar(
77
-                parent=client,
78
-                client=client,
79
-                url=user_calendar_url
80
-            ).events()
81
-
82
-            ok_(True, 'User can access it\'s own calendar')
83
-        except AuthorizationError:
84
-            ok_(False, 'User should not access that')
72
+        caldav.Calendar(
73
+            parent=client,
74
+            client=client,
75
+            url=user_calendar_url
76
+        ).events()
85 77
 
78
+    @raises(AuthorizationError)
86 79
     def test_func__rights_read_user_calendar__fail__as_john_doe(self):
87 80
         radicale_base_url = CalendarManager.get_base_url()
88 81
         client = caldav.DAVClient(
@@ -94,17 +87,13 @@ class TestCalendar(BaseTestCalendar):
94 87
             User.email == 'admin@admin.admin'
95 88
         ).one()
96 89
         user_calendar_url = CalendarManager.get_user_calendar_url(other_user.user_id)
97
-        try:
98
-            caldav.Calendar(
99
-                parent=client,
100
-                client=client,
101
-                url=user_calendar_url
102
-            ).events()
103
-
104
-            ok_(False, 'User can\'t acces other user calendar')
105
-        except AuthorizationError:
106
-            ok_(True, 'User should not acces other user calendar')
90
+        caldav.Calendar(
91
+            parent=client,
92
+            client=client,
93
+            url=user_calendar_url
94
+        ).events()
107 95
 
96
+    @not_raises(AuthorizationError)
108 97
     def test_func__rights_read_workspace_calendar__ok__as_owner(self):
109 98
         lawrence = DBSession.query(User).filter(
110 99
             User.email == 'lawrence-not-real-email@fsf.local'
@@ -128,17 +117,13 @@ class TestCalendar(BaseTestCalendar):
128 117
             username='lawrence-not-real-email@fsf.local',
129 118
             password='foobarbaz'
130 119
         )
131
-        try:
132
-            caldav.Calendar(
133
-                parent=client,
134
-                client=client,
135
-                url=workspace_calendar_url
136
-            ).events()
137
-
138
-            ok_(True, 'User can acces own workspace calendar')
139
-        except AuthorizationError:
140
-            ok_(False, 'User should not acces own workspace calendar')
120
+        caldav.Calendar(
121
+            parent=client,
122
+            client=client,
123
+            url=workspace_calendar_url
124
+        ).events()
141 125
 
126
+    @raises(AuthorizationError)
142 127
     def test_func__rights_read_workspace_calendar__fail__as_unauthorized(self):
143 128
         lawrence = DBSession.query(User).filter(
144 129
             User.email == 'lawrence-not-real-email@fsf.local'
@@ -162,16 +147,11 @@ class TestCalendar(BaseTestCalendar):
162 147
             username='bob@fsf.local',
163 148
             password='foobarbaz'
164 149
         )
165
-        try:
166
-            caldav.Calendar(
167
-                parent=client,
168
-                client=client,
169
-                url=workspace_calendar_url
170
-            ).events()
171
-
172
-            ok_(False, 'User can\'t access unright workspace calendar')
173
-        except AuthorizationError:
174
-            ok_(True, 'User should not access unright workspace calendar')
150
+        caldav.Calendar(
151
+            parent=client,
152
+            client=client,
153
+            url=workspace_calendar_url
154
+        ).events()
175 155
 
176 156
     def test_func__event_create__ok__nominal_case(self):
177 157
         lawrence = DBSession.query(User).filter(