浏览代码

Clean code and fix tests

Bastien Sevajol (Algoo) 9 年前
父节点
当前提交
a0a20c8215
共有 4 个文件被更改,包括 66 次插入52 次删除
  1. 4 4
      tracim/test.ini
  2. 46 1
      tracim/tracim/lib/calendar.py
  3. 0 34
      tracim/tracim/tests/__init__.py
  4. 16 13
      tracim/tracim/tests/functional/test_calendar.py

+ 4 - 4
tracim/test.ini 查看文件

11
 error_email_from = turbogears@localhost
11
 error_email_from = turbogears@localhost
12
 radicale.server.port = 15232
12
 radicale.server.port = 15232
13
 radicale.client.port = 15232
13
 radicale.client.port = 15232
14
+radicale.server.filesystem.folder = /tmp/tracim_tests_radicale_fs
14
 
15
 
15
 [server:main]
16
 [server:main]
16
 use = egg:gearbox#wsgiref
17
 use = egg:gearbox#wsgiref
18
 port = 8080
19
 port = 8080
19
 
20
 
20
 [app:main]
21
 [app:main]
21
-sqlalchemy.url = postgresql://postgres:dummy@127.0.0.1:5432/tracim_test?client_encoding=utf8
22
+sqlalchemy.url = mysql+oursql://tracim:tracim@localhost/tracim_test
22
 use = config:development.ini
23
 use = config:development.ini
23
 
24
 
24
 [app:main_without_authn]
25
 [app:main_without_authn]
26
 skip_authentication = True
27
 skip_authentication = True
27
 
28
 
28
 [app:ldap]
29
 [app:ldap]
29
-sqlalchemy.url = postgresql://postgres:dummy@127.0.0.1:5432/tracim_test?client_encoding=utf8
30
+sqlalchemy.url = mysql+oursql://tracim:tracim@localhost/tracim_test
30
 auth_type = ldap
31
 auth_type = ldap
31
 ldap_url = ldap://localhost:3333
32
 ldap_url = ldap://localhost:3333
32
 ldap_base_dn = dc=directory,dc=fsf,dc=org
33
 ldap_base_dn = dc=directory,dc=fsf,dc=org
39
 use = config:development.ini
40
 use = config:development.ini
40
 
41
 
41
 [app:radicale]
42
 [app:radicale]
42
-sqlalchemy.url = postgresql://postgres:dummy@127.0.0.1:5432/tracim_test?client_encoding=utf8
43
-radicale.server.filesystem.folder = /tmp/tracim_tests_radicale_fs
43
+sqlalchemy.url = mysql+oursql://tracim:tracim@localhost/tracim_test
44
 use = config:development.ini
44
 use = config:development.ini
45
 
45
 
46
 # Add additional test specific configuration options as necessary.
46
 # Add additional test specific configuration options as necessary.

+ 46 - 1
tracim/tracim/lib/calendar.py 查看文件

32
 
32
 
33
 
33
 
34
 class CalendarManager(object):
34
 class CalendarManager(object):
35
+    @staticmethod
36
+    def get_base_url():
37
+        from tracim.config.app_cfg import CFG
38
+        cfg = CFG.get_instance()
39
+
40
+        return CALENDAR_BASE_URL_TEMPLATE.format(
41
+            proto='https' if cfg.RADICALE_CLIENT_SSL else 'http',
42
+            domain=cfg.RADICALE_CLIENT_HOST or '127.0.0.1',
43
+            port=str(cfg.RADICALE_CLIENT_PORT)
44
+        )
45
+
46
+    @staticmethod
47
+    def get_user_calendar_url(user_id: int, extra: str=''):
48
+        from tracim.config.app_cfg import CFG
49
+        cfg = CFG.get_instance()
50
+
51
+        return CALENDAR_USER_URL_TEMPLATE.format(
52
+            proto='https' if cfg.RADICALE_CLIENT_SSL else 'http',
53
+            domain=cfg.RADICALE_CLIENT_HOST or '127.0.0.1',
54
+            port=str(cfg.RADICALE_CLIENT_PORT),
55
+            id=str(user_id),
56
+            extra=extra,
57
+        )
58
+
59
+    @staticmethod
60
+    def get_workspace_calendar_url(workspace_id: int, extra: str=''):
61
+        from tracim.config.app_cfg import CFG
62
+        cfg = CFG.get_instance()
63
+
64
+        return CALENDAR_WORKSPACE_URL_TEMPLATE.format(
65
+            proto='https' if cfg.RADICALE_CLIENT_SSL else 'http',
66
+            domain=cfg.RADICALE_CLIENT_HOST or '127.0.0.1',
67
+            port=str(cfg.RADICALE_CLIENT_PORT),
68
+            id=str(workspace_id),
69
+            extra=extra,
70
+        )
71
+
35
     def __init__(self, user: User):
72
     def __init__(self, user: User):
36
         self._user = user
73
         self._user = user
37
 
74
 
224
             content: Content,
261
             content: Content,
225
             event: iCalendarEvent,
262
             event: iCalendarEvent,
226
             event_name: str,
263
             event_name: str,
227
-    ) -> Content:
264
+    ) -> None:
265
+        """
266
+        Populate Content content instance from iCalendarEvent event attributes.
267
+        :param content: content to populate
268
+        :param event: event with data to insert in content
269
+        :param event_name: Event name (ID) like
270
+        20160602T083511Z-18100-1001-1-71_Bastien-20160602T083516Z.ics
271
+        :return: given content
272
+        """
228
         content.label = event.get('summary')
273
         content.label = event.get('summary')
229
         content.description = event.get('description')
274
         content.description = event.get('description')
230
         content.properties = {
275
         content.properties = {

+ 0 - 34
tracim/tracim/tests/__init__.py 查看文件

371
                 shutil.rmtree('{0}/{1}'.format(radicale_fs_path, file))
371
                 shutil.rmtree('{0}/{1}'.format(radicale_fs_path, file))
372
         except FileNotFoundError:
372
         except FileNotFoundError:
373
             pass  # Dir not exists yet, no need to clear it
373
             pass  # Dir not exists yet, no need to clear it
374
-
375
-    def _get_base_url(self):
376
-        from tracim.config.app_cfg import CFG
377
-        cfg = CFG.get_instance()
378
-
379
-        return CALENDAR_BASE_URL_TEMPLATE.format(
380
-            proto='https' if cfg.RADICALE_CLIENT_SSL else 'http',
381
-            domain=cfg.RADICALE_CLIENT_HOST or '127.0.0.1',
382
-            port=str(cfg.RADICALE_CLIENT_PORT)
383
-        )
384
-
385
-    def _get_user_calendar_url(self, user_id):
386
-        from tracim.config.app_cfg import CFG
387
-        cfg = CFG.get_instance()
388
-
389
-        return CALENDAR_USER_URL_TEMPLATE.format(
390
-            proto='https' if cfg.RADICALE_CLIENT_SSL else 'http',
391
-            domain=cfg.RADICALE_CLIENT_HOST or '127.0.0.1',
392
-            port=str(cfg.RADICALE_CLIENT_PORT),
393
-            id=user_id,
394
-            extra='',
395
-        )
396
-
397
-    def _get_workspace_calendar_url(self, workspace_id):
398
-        from tracim.config.app_cfg import CFG
399
-        cfg = CFG.get_instance()
400
-
401
-        return CALENDAR_WORKSPACE_URL_TEMPLATE.format(
402
-            proto='https' if cfg.RADICALE_CLIENT_SSL else 'http',
403
-            domain=cfg.RADICALE_CLIENT_HOST or '127.0.0.1',
404
-            port=str(cfg.RADICALE_CLIENT_PORT),
405
-            id=workspace_id,
406
-            extra='',
407
-        )

+ 16 - 13
tracim/tracim/tests/functional/test_calendar.py 查看文件

9
 from sqlalchemy.orm.exc import NoResultFound
9
 from sqlalchemy.orm.exc import NoResultFound
10
 
10
 
11
 from tracim.config.app_cfg import daemons
11
 from tracim.config.app_cfg import daemons
12
+from tracim.lib.calendar import CalendarManager
12
 from tracim.lib.workspace import WorkspaceApi
13
 from tracim.lib.workspace import WorkspaceApi
13
 from tracim.model import DBSession
14
 from tracim.model import DBSession
14
 from tracim.tests import TestCalendar as BaseTestCalendar
15
 from tracim.tests import TestCalendar as BaseTestCalendar
23
         # ... radicale daemon started. We should lock something somewhere !
24
         # ... radicale daemon started. We should lock something somewhere !
24
 
25
 
25
     def test_func__radicale_connectivity__ok__nominal_case(self):
26
     def test_func__radicale_connectivity__ok__nominal_case(self):
26
-        radicale_base_url = self._get_base_url()
27
+        radicale_base_url = CalendarManager.get_base_url()
27
 
28
 
28
         try:
29
         try:
29
             response = requests.get(radicale_base_url)
30
             response = requests.get(radicale_base_url)
32
             ok_(False, 'Unable to contact radicale on HTTP')
33
             ok_(False, 'Unable to contact radicale on HTTP')
33
 
34
 
34
     def test_func__radicale_auth__ok__as_lawrence(self):
35
     def test_func__radicale_auth__ok__as_lawrence(self):
35
-        radicale_base_url = self._get_base_url()
36
+        radicale_base_url = CalendarManager.get_base_url()
36
         client = caldav.DAVClient(
37
         client = caldav.DAVClient(
37
             radicale_base_url,
38
             radicale_base_url,
38
             username='lawrence-not-real-email@fsf.local',
39
             username='lawrence-not-real-email@fsf.local',
45
             ok_(False, 'AuthorizationError when communicate with radicale')
46
             ok_(False, 'AuthorizationError when communicate with radicale')
46
 
47
 
47
     def test_func__radicale_auth__fail__as_john_doe(self):
48
     def test_func__radicale_auth__fail__as_john_doe(self):
48
-        radicale_base_url = self._get_base_url()
49
+        radicale_base_url = CalendarManager.get_base_url()
49
         client = caldav.DAVClient(
50
         client = caldav.DAVClient(
50
             radicale_base_url,
51
             radicale_base_url,
51
             username='john.doe@foo.local',
52
             username='john.doe@foo.local',
59
             ok_(True, 'AuthorizationError thrown correctly')
60
             ok_(True, 'AuthorizationError thrown correctly')
60
 
61
 
61
     def test_func__rights_read_user_calendar__ok__as_lawrence(self):
62
     def test_func__rights_read_user_calendar__ok__as_lawrence(self):
62
-        radicale_base_url = self._get_base_url()
63
+        radicale_base_url = CalendarManager.get_base_url()
63
         client = caldav.DAVClient(
64
         client = caldav.DAVClient(
64
             radicale_base_url,
65
             radicale_base_url,
65
             username='lawrence-not-real-email@fsf.local',
66
             username='lawrence-not-real-email@fsf.local',
68
         user = DBSession.query(User).filter(
69
         user = DBSession.query(User).filter(
69
             User.email == 'lawrence-not-real-email@fsf.local'
70
             User.email == 'lawrence-not-real-email@fsf.local'
70
         ).one()
71
         ).one()
71
-        user_calendar_url = self._get_user_calendar_url(user.user_id)
72
+        user_calendar_url = CalendarManager.get_user_calendar_url(user.user_id)
72
         try:
73
         try:
73
             caldav.Calendar(
74
             caldav.Calendar(
74
                 parent=client,
75
                 parent=client,
81
             ok_(False, 'User should not access that')
82
             ok_(False, 'User should not access that')
82
 
83
 
83
     def test_func__rights_read_user_calendar__fail__as_john_doe(self):
84
     def test_func__rights_read_user_calendar__fail__as_john_doe(self):
84
-        radicale_base_url = self._get_base_url()
85
+        radicale_base_url = CalendarManager.get_base_url()
85
         client = caldav.DAVClient(
86
         client = caldav.DAVClient(
86
             radicale_base_url,
87
             radicale_base_url,
87
             username='john.doe@foo.local',
88
             username='john.doe@foo.local',
90
         other_user = DBSession.query(User).filter(
91
         other_user = DBSession.query(User).filter(
91
             User.email == 'admin@admin.admin'
92
             User.email == 'admin@admin.admin'
92
         ).one()
93
         ).one()
93
-        user_calendar_url = self._get_user_calendar_url(other_user.user_id)
94
+        user_calendar_url = CalendarManager.get_user_calendar_url(other_user.user_id)
94
         try:
95
         try:
95
             caldav.Calendar(
96
             caldav.Calendar(
96
                 parent=client,
97
                 parent=client,
113
         workspace.calendar_enabled = True
114
         workspace.calendar_enabled = True
114
         DBSession.flush()
115
         DBSession.flush()
115
 
116
 
116
-        workspace_calendar_url = self._get_workspace_calendar_url(
117
+        workspace_calendar_url = CalendarManager.get_workspace_calendar_url(
117
             workspace.workspace_id
118
             workspace.workspace_id
118
         )
119
         )
119
 
120
 
120
         transaction.commit()
121
         transaction.commit()
121
 
122
 
122
-        radicale_base_url = self._get_base_url()
123
+        radicale_base_url = CalendarManager.get_base_url()
123
         client = caldav.DAVClient(
124
         client = caldav.DAVClient(
124
             radicale_base_url,
125
             radicale_base_url,
125
             username='lawrence-not-real-email@fsf.local',
126
             username='lawrence-not-real-email@fsf.local',
147
         workspace.calendar_enabled = True
148
         workspace.calendar_enabled = True
148
         DBSession.flush()
149
         DBSession.flush()
149
 
150
 
150
-        workspace_calendar_url = self._get_workspace_calendar_url(
151
+        workspace_calendar_url = CalendarManager.get_workspace_calendar_url(
151
             workspace.workspace_id
152
             workspace.workspace_id
152
         )
153
         )
153
 
154
 
154
         transaction.commit()
155
         transaction.commit()
155
 
156
 
156
-        radicale_base_url = self._get_base_url()
157
+        radicale_base_url = CalendarManager.get_base_url()
157
         client = caldav.DAVClient(
158
         client = caldav.DAVClient(
158
             radicale_base_url,
159
             radicale_base_url,
159
             username='bob@fsf.local',
160
             username='bob@fsf.local',
174
         lawrence = DBSession.query(User).filter(
175
         lawrence = DBSession.query(User).filter(
175
             User.email == 'lawrence-not-real-email@fsf.local'
176
             User.email == 'lawrence-not-real-email@fsf.local'
176
         ).one()
177
         ).one()
177
-        radicale_base_url = self._get_base_url()
178
+        radicale_base_url = CalendarManager.get_base_url()
178
         client = caldav.DAVClient(
179
         client = caldav.DAVClient(
179
             radicale_base_url,
180
             radicale_base_url,
180
             username='lawrence-not-real-email@fsf.local',
181
             username='lawrence-not-real-email@fsf.local',
181
             password='foobarbaz'
182
             password='foobarbaz'
182
         )
183
         )
183
-        user_calendar_url = self._get_user_calendar_url(lawrence.user_id)
184
+        user_calendar_url = CalendarManager.get_user_calendar_url(
185
+            lawrence.user_id
186
+        )
184
         user_calendar = caldav.Calendar(
187
         user_calendar = caldav.Calendar(
185
             parent=client,
188
             parent=client,
186
             client=client,
189
             client=client,