浏览代码

Merge pull request #16 from lebouquetin/feature/improve-mail-notifications

Tracim 10 年前
父节点
当前提交
c34e9484d6
共有 3 个文件被更改,包括 362 次插入106 次删除
  1. 4 2
      tracim/tracim/lib/content.py
  2. 9 1
      tracim/tracim/lib/user.py
  3. 349 103
      tracim/tracim/tests/library/test_content_api.py

+ 4 - 2
tracim/tracim/lib/content.py 查看文件

66
 
66
 
67
     def __init__(self, current_user: User, show_archived=False, show_deleted=False, all_content_in_treeview=True):
67
     def __init__(self, current_user: User, show_archived=False, show_deleted=False, all_content_in_treeview=True):
68
         self._user = current_user
68
         self._user = current_user
69
+        self._user_id = current_user.user_id if current_user else None
69
         self._show_archived = show_archived
70
         self._show_archived = show_archived
70
         self._show_deleted = show_deleted
71
         self._show_deleted = show_deleted
71
         self._show_all_type_of_contents_in_treeview = all_content_in_treeview
72
         self._show_all_type_of_contents_in_treeview = all_content_in_treeview
134
             result = result.filter(Content.workspace_id==workspace.workspace_id)
135
             result = result.filter(Content.workspace_id==workspace.workspace_id)
135
 
136
 
136
         if self._user:
137
         if self._user:
138
+            user = DBSession.query(User).get(self._user_id)
137
             # Filter according to user workspaces
139
             # Filter according to user workspaces
138
-            workspace_ids = [r.workspace_id for r in self._user.roles \
140
+            workspace_ids = [r.workspace_id for r in user.roles \
139
                              if r.role>=UserRoleInWorkspace.READER]
141
                              if r.role>=UserRoleInWorkspace.READER]
140
             result = result.filter(Content.workspace_id.in_(workspace_ids))
142
             result = result.filter(Content.workspace_id.in_(workspace_ids))
141
 
143
 
222
 
224
 
223
         return result
225
         return result
224
 
226
 
225
-    def create(self, content_type: str, workspace: Workspace=None, parent: Content=None, label:str ='', do_save=False) -> Content:
227
+    def create(self, content_type: str, workspace: Workspace, parent: Content=None, label:str ='', do_save=False) -> Content:
226
         assert content_type in ContentType.allowed_types()
228
         assert content_type in ContentType.allowed_types()
227
         content = Content()
229
         content = Content()
228
         content.owner = self._user
230
         content.owner = self._user

+ 9 - 1
tracim/tracim/lib/user.py 查看文件

44
         except:
44
         except:
45
             return False
45
             return False
46
 
46
 
47
-    def create_user(self, save_now=False) -> User:
47
+    def create_user(self, email=None, groups=[], save_now=False) -> User:
48
         user = User()
48
         user = User()
49
+
50
+        if email:
51
+            user.email = email
52
+
53
+        for group in groups:
54
+            user.groups.append(group)
55
+
49
         DBSession.add(user)
56
         DBSession.add(user)
57
+
50
         if save_now:
58
         if save_now:
51
             DBSession.flush()
59
             DBSession.flush()
52
 
60
 

+ 349 - 103
tracim/tracim/tests/library/test_content_api.py 查看文件

2
 
2
 
3
 from nose.tools import eq_
3
 from nose.tools import eq_
4
 from nose.tools import raises
4
 from nose.tools import raises
5
+from tracim.lib.group import GroupApi
5
 
6
 
6
 import transaction
7
 import transaction
7
 
8
 
8
-from tracim.lib.base import logger
9
 from tracim.lib.content import compare_content_for_sorting_by_type_and_name
9
 from tracim.lib.content import compare_content_for_sorting_by_type_and_name
10
 from tracim.lib.content import ContentApi
10
 from tracim.lib.content import ContentApi
11
+from tracim.lib.group import GroupApi
11
 from tracim.lib.user import UserApi
12
 from tracim.lib.user import UserApi
12
-from tracim.lib.user import UserStaticApi
13
+from tracim.lib.workspace import RoleApi
14
+from tracim.lib.workspace import WorkspaceApi
15
+
16
+from tracim.model.auth import Group
13
 
17
 
14
-from tracim.model.auth import User
15
 from tracim.model.data import ActionDescription
18
 from tracim.model.data import ActionDescription
16
 from tracim.model.data import Content
19
 from tracim.model.data import Content
17
 from tracim.model.data import ContentType
20
 from tracim.model.data import ContentType
18
-from tracim.model.data import ContentStatus
21
+from tracim.model.data import UserRoleInWorkspace
19
 
22
 
20
 from tracim.tests import TestStandard
23
 from tracim.tests import TestStandard
21
 
24
 
85
         items = [c1, c2]
88
         items = [c1, c2]
86
         sorteds = ContentApi.sort_content(items)
89
         sorteds = ContentApi.sort_content(items)
87
 
90
 
88
-        eq_(sorteds[0], c2, 'value is {} instead of {}'.format(sorteds[0].content_id, c2.content_id))
89
-        eq_(sorteds[1], c1, 'value is {} instead of {}'.format(sorteds[1].content_id, c1.content_id))
91
+        eq_(sorteds[0], c2,
92
+            'value is {} instead of {}'.format(sorteds[0].content_id,
93
+                                               c2.content_id))
94
+        eq_(sorteds[1], c1,
95
+            'value is {} instead of {}'.format(sorteds[1].content_id,
96
+                                               c1.content_id))
90
 
97
 
91
     def test_delete(self):
98
     def test_delete(self):
92
-        api = ContentApi(None)
93
-        item = api.create(ContentType.Folder, None, None, 'not_deleted', True)
94
-        item2 = api.create(ContentType.Folder, None, None, 'to_delete', True)
99
+        uapi = UserApi(None)
100
+        groups = [GroupApi(None).get_one(Group.TIM_USER),
101
+                  GroupApi(None).get_one(Group.TIM_MANAGER),
102
+                  GroupApi(None).get_one(Group.TIM_ADMIN)]
103
+
104
+        user = uapi.create_user(email='this.is@user',
105
+                                groups=groups, save_now=True)
106
+        workspace = WorkspaceApi(user).create_workspace('test workspace',
107
+                                                        save_now=True)
108
+
109
+        api = ContentApi(user)
110
+        item = api.create(ContentType.Folder, workspace, None,
111
+                          'not_deleted', True)
112
+        item2 = api.create(ContentType.Folder, workspace, None,
113
+                           'to_delete', True)
114
+        uid = user.user_id
115
+        wid = workspace.workspace_id
95
         transaction.commit()
116
         transaction.commit()
96
 
117
 
97
-        items = api.get_all(None, ContentType.Any, None)
118
+        # Refresh instances after commit
119
+        user = uapi.get_one(uid)
120
+        workspace = WorkspaceApi(user).get_one(wid)
121
+        api = ContentApi(user)
122
+        items = api.get_all(None, ContentType.Any, workspace)
98
         eq_(2, len(items))
123
         eq_(2, len(items))
99
 
124
 
100
-        items = api.get_all(None, ContentType.Any, None)
125
+        items = api.get_all(None, ContentType.Any, workspace)
101
         api.delete(items[0])
126
         api.delete(items[0])
102
         transaction.commit()
127
         transaction.commit()
103
 
128
 
104
-        items = api.get_all(None, ContentType.Any, None)
129
+        # Refresh instances after commit
130
+        user = uapi.get_one(uid)
131
+        workspace = WorkspaceApi(user).get_one(wid)
132
+        api = ContentApi(user)
133
+        items = api.get_all(None, ContentType.Any, workspace)
105
         eq_(1, len(items))
134
         eq_(1, len(items))
106
         transaction.commit()
135
         transaction.commit()
107
 
136
 
108
         # Test that the item is still available if "show deleted" is activated
137
         # Test that the item is still available if "show deleted" is activated
109
-        api = ContentApi(None, show_deleted=True)
110
-        items = api.get_all(None, ContentType.Any, None)
138
+        # Refresh instances after commit
139
+        user = uapi.get_one(uid)
140
+        workspace = WorkspaceApi(user).get_one(wid)
141
+        api = ContentApi(user, show_deleted=True)
142
+        items = api.get_all(None, ContentType.Any, workspace)
111
         eq_(2, len(items))
143
         eq_(2, len(items))
112
 
144
 
113
 
145
 
114
     def test_archive(self):
146
     def test_archive(self):
115
-        api = ContentApi(None)
116
-        item = api.create(ContentType.Folder, None, None, 'not_archived', True)
117
-        item2 = api.create(ContentType.Folder, None, None, 'to_archive', True)
147
+        uapi = UserApi(None)
148
+        groups = [GroupApi(None).get_one(Group.TIM_USER),
149
+                  GroupApi(None).get_one(Group.TIM_MANAGER),
150
+                  GroupApi(None).get_one(Group.TIM_ADMIN)]
151
+
152
+        user = uapi.create_user(email='this.is@user',
153
+                                groups=groups, save_now=True)
154
+        workspace = WorkspaceApi(user).create_workspace('test workspace',
155
+                                                        save_now=True)
156
+
157
+        api = ContentApi(user)
158
+        item = api.create(ContentType.Folder, workspace, None,
159
+                          'not_archived', True)
160
+        item2 = api.create(ContentType.Folder, workspace, None,
161
+                           'to_archive', True)
162
+        uid = user.user_id
163
+        wid = workspace.workspace_id
118
         transaction.commit()
164
         transaction.commit()
119
 
165
 
120
-        items = api.get_all(None, ContentType.Any, None)
166
+        # Refresh instances after commit
167
+        user = uapi.get_one(uid)
168
+        workspace = WorkspaceApi(user).get_one(wid)
169
+        api = ContentApi(user)
170
+
171
+        items = api.get_all(None, ContentType.Any, workspace)
121
         eq_(2, len(items))
172
         eq_(2, len(items))
122
 
173
 
123
-        items = api.get_all(None, ContentType.Any, None)
174
+        items = api.get_all(None, ContentType.Any, workspace)
124
         api.archive(items[0])
175
         api.archive(items[0])
125
         transaction.commit()
176
         transaction.commit()
126
 
177
 
127
-        items = api.get_all(None, ContentType.Any, None)
178
+        # Refresh instances after commit
179
+        user = uapi.get_one(uid)
180
+        workspace = WorkspaceApi(user).get_one(wid)
181
+        api = ContentApi(user)
182
+
183
+        items = api.get_all(None, ContentType.Any, workspace)
128
         eq_(1, len(items))
184
         eq_(1, len(items))
129
         transaction.commit()
185
         transaction.commit()
130
 
186
 
187
+        # Refresh instances after commit
188
+        user = uapi.get_one(uid)
189
+        workspace = WorkspaceApi(user).get_one(wid)
190
+        api = ContentApi(user)
191
+
131
         # Test that the item is still available if "show deleted" is activated
192
         # Test that the item is still available if "show deleted" is activated
132
         api = ContentApi(None, show_archived=True)
193
         api = ContentApi(None, show_archived=True)
133
-        items = api.get_all(None, ContentType.Any, None)
194
+        items = api.get_all(None, ContentType.Any, workspace)
134
         eq_(2, len(items))
195
         eq_(2, len(items))
135
 
196
 
136
     def test_get_all_with_filter(self):
197
     def test_get_all_with_filter(self):
137
-        api = ContentApi(None)
138
-        item = api.create(ContentType.Folder, None, None, 'thefolder', True)
139
-        item2 = api.create(ContentType.File, None, None, 'thefile', True)
198
+        uapi = UserApi(None)
199
+        groups = [GroupApi(None).get_one(Group.TIM_USER),
200
+                  GroupApi(None).get_one(Group.TIM_MANAGER),
201
+                  GroupApi(None).get_one(Group.TIM_ADMIN)]
202
+
203
+        user = uapi.create_user(email='this.is@user',
204
+                                groups=groups, save_now=True)
205
+        workspace = WorkspaceApi(user).create_workspace('test workspace',
206
+                                                        save_now=True)
207
+
208
+        api = ContentApi(user)
209
+        item = api.create(ContentType.Folder, workspace, None,
210
+                          'thefolder', True)
211
+        item2 = api.create(ContentType.File, workspace, None, 'thefile', True)
212
+        uid = user.user_id
213
+        wid = workspace.workspace_id
140
         transaction.commit()
214
         transaction.commit()
141
 
215
 
142
-        items = api.get_all(None, ContentType.Any, None)
216
+        # Refresh instances after commit
217
+        user = uapi.get_one(uid)
218
+        workspace = WorkspaceApi(user).get_one(wid)
219
+        api = ContentApi(user)
220
+
221
+        items = api.get_all(None, ContentType.Any, workspace)
143
         eq_(2, len(items))
222
         eq_(2, len(items))
144
 
223
 
145
-        items2 = api.get_all(None, ContentType.File, None)
224
+        items2 = api.get_all(None, ContentType.File, workspace)
146
         eq_(1, len(items2))
225
         eq_(1, len(items2))
147
         eq_('thefile', items2[0].label)
226
         eq_('thefile', items2[0].label)
148
 
227
 
149
-        items3 = api.get_all(None, ContentType.Folder, None)
228
+        items3 = api.get_all(None, ContentType.Folder, workspace)
150
         eq_(1, len(items3))
229
         eq_(1, len(items3))
151
         eq_('thefolder', items3[0].label)
230
         eq_('thefolder', items3[0].label)
152
 
231
 
153
     def test_get_all_with_parent_id(self):
232
     def test_get_all_with_parent_id(self):
154
-        api = ContentApi(None)
155
-        item = api.create(ContentType.Folder, None, None, 'parent', True)
156
-        item2 = api.create(ContentType.File, None, item, 'file1', True)
157
-        item3 = api.create(ContentType.File, None, None, 'file2', True)
233
+        uapi = UserApi(None)
234
+        groups = [GroupApi(None).get_one(Group.TIM_USER),
235
+                  GroupApi(None).get_one(Group.TIM_MANAGER),
236
+                  GroupApi(None).get_one(Group.TIM_ADMIN)]
237
+
238
+        user = uapi.create_user(email='this.is@user',
239
+                                groups=groups, save_now=True)
240
+        workspace = WorkspaceApi(user).create_workspace('test workspace',
241
+                                                        save_now=True)
242
+
243
+        api = ContentApi(user)
244
+        item = api.create(ContentType.Folder, workspace, None, 'parent', True)
245
+        item2 = api.create(ContentType.File, workspace, item, 'file1', True)
246
+        item3 = api.create(ContentType.File, workspace, None, 'file2', True)
158
         parent_id = item.content_id
247
         parent_id = item.content_id
159
         child_id = item2.content_id
248
         child_id = item2.content_id
249
+        uid = user.user_id
250
+        wid = workspace.workspace_id
160
         transaction.commit()
251
         transaction.commit()
161
 
252
 
162
-        items = api.get_all(None, ContentType.Any, None)
253
+        # Refresh instances after commit
254
+        user = uapi.get_one(uid)
255
+        workspace = WorkspaceApi(user).get_one(wid)
256
+        api = ContentApi(user)
257
+
258
+        items = api.get_all(None, ContentType.Any, workspace)
163
         eq_(3, len(items))
259
         eq_(3, len(items))
164
 
260
 
165
-        items2 = api.get_all(parent_id, ContentType.File, None)
261
+        items2 = api.get_all(parent_id, ContentType.File, workspace)
166
         eq_(1, len(items2))
262
         eq_(1, len(items2))
167
         eq_(child_id, items2[0].content_id)
263
         eq_(child_id, items2[0].content_id)
168
 
264
 
169
     @raises(ValueError)
265
     @raises(ValueError)
170
     def test_set_status_unknown_status(self):
266
     def test_set_status_unknown_status(self):
171
-        api = ContentApi(None)
172
-        c = api.create(ContentType.Folder, None, None, 'parent', True)
267
+        uapi = UserApi(None)
268
+        groups = [GroupApi(None).get_one(Group.TIM_USER),
269
+                  GroupApi(None).get_one(Group.TIM_MANAGER),
270
+                  GroupApi(None).get_one(Group.TIM_ADMIN)]
271
+
272
+        user = uapi.create_user(email='this.is@user',
273
+                                groups=groups, save_now=True)
274
+
275
+        workspace = WorkspaceApi(user).create_workspace('test workspace',
276
+                                                        save_now=True)
277
+        api = ContentApi(user)
278
+        c = api.create(ContentType.Folder, workspace, None, 'parent', True)
173
         api.set_status(c, 'unknown-status')
279
         api.set_status(c, 'unknown-status')
174
 
280
 
175
     def test_set_status_ok(self):
281
     def test_set_status_ok(self):
176
-        api = ContentApi(None)
177
-        c = api.create(ContentType.Folder, None, None, 'parent', True)
178
-        for new_status in ['open', 'closed-validated', 'closed-unvalidated', 'closed-deprecated']:
282
+        uapi = UserApi(None)
283
+        groups = [GroupApi(None).get_one(Group.TIM_USER),
284
+                  GroupApi(None).get_one(Group.TIM_MANAGER),
285
+                  GroupApi(None).get_one(Group.TIM_ADMIN)]
286
+
287
+        user = uapi.create_user(email='this.is@user',
288
+                                groups=groups, save_now=True)
289
+
290
+        workspace = WorkspaceApi(user).create_workspace('test workspace',
291
+                                                        save_now=True)
292
+        api = ContentApi(user)
293
+        c = api.create(ContentType.Folder, workspace, None, 'parent', True)
294
+        for new_status in ['open', 'closed-validated', 'closed-unvalidated',
295
+                           'closed-deprecated']:
179
             api.set_status(c, new_status)
296
             api.set_status(c, new_status)
180
             eq_(new_status, c.status)
297
             eq_(new_status, c.status)
181
             eq_(ActionDescription.STATUS_UPDATE, c.revision_type)
298
             eq_(ActionDescription.STATUS_UPDATE, c.revision_type)
182
 
299
 
183
     def test_create_comment_ok(self):
300
     def test_create_comment_ok(self):
184
         uapi = UserApi(None)
301
         uapi = UserApi(None)
185
-        user = uapi.create_user()
186
-        user.email = 'this.is@user'
187
-        uapi.save(user)
302
+        groups = [GroupApi(None).get_one(Group.TIM_USER),
303
+                  GroupApi(None).get_one(Group.TIM_MANAGER),
304
+                  GroupApi(None).get_one(Group.TIM_ADMIN)]
305
+
306
+        user = uapi.create_user(email='this.is@user',
307
+                                groups=groups, save_now=True)
308
+
309
+        workspace = WorkspaceApi(user).create_workspace('test workspace',
310
+                                                        save_now=True)
188
 
311
 
189
         api = ContentApi(user)
312
         api = ContentApi(user)
190
-        p = api.create(ContentType.Page, None, None, 'this_is_a_page')
191
-        c = api.create_comment(None, p, 'this is the comment', True)
313
+        p = api.create(ContentType.Page, workspace, None, 'this_is_a_page')
314
+        c = api.create_comment(workspace, p, 'this is the comment', True)
192
 
315
 
193
         eq_(Content, c.__class__)
316
         eq_(Content, c.__class__)
194
         eq_(p.content_id, c.parent_id)
317
         eq_(p.content_id, c.parent_id)
195
         eq_(user, c.owner)
318
         eq_(user, c.owner)
196
-        eq_(None, c.workspace)
319
+        eq_(workspace, c.workspace)
197
         eq_(ContentType.Comment, c.type)
320
         eq_(ContentType.Comment, c.type)
198
         eq_('this is the comment', c.description)
321
         eq_('this is the comment', c.description)
199
         eq_('', c.label)
322
         eq_('', c.label)
202
 
325
 
203
     def test_update(self):
326
     def test_update(self):
204
         uapi = UserApi(None)
327
         uapi = UserApi(None)
328
+        groups = [GroupApi(None).get_one(Group.TIM_USER),
329
+                  GroupApi(None).get_one(Group.TIM_MANAGER),
330
+                  GroupApi(None).get_one(Group.TIM_ADMIN)]
205
 
331
 
206
-        user1 = uapi.create_user()
207
-        user1.email = 'this.is@user'
208
-        uapi.save(user1)
332
+        user1 = uapi.create_user(email='this.is@user',
333
+                                groups=groups, save_now=True)
334
+
335
+        workspace = WorkspaceApi(user1).create_workspace('test workspace',
336
+                                                        save_now=True)
337
+        wid = workspace.workspace_id
209
 
338
 
210
         user2 = uapi.create_user()
339
         user2 = uapi.create_user()
211
         user2.email = 'this.is@another.user'
340
         user2.email = 'this.is@another.user'
212
         uapi.save(user2)
341
         uapi.save(user2)
213
 
342
 
343
+        RoleApi(user1).create_one(user2, workspace,
344
+                                  UserRoleInWorkspace.CONTENT_MANAGER,
345
+                                  flush=True)
346
+
347
+        # Test starts here
348
+
214
         api = ContentApi(user1)
349
         api = ContentApi(user1)
215
-        p = api.create(ContentType.Page, None, None, 'this_is_a_page', True)
350
+        p = api.create(ContentType.Page, workspace, None,
351
+                       'this_is_a_page', True)
216
 
352
 
217
         u1id = user1.user_id
353
         u1id = user1.user_id
218
         u2id = user2.user_id
354
         u2id = user2.user_id
221
 
357
 
222
         transaction.commit()
358
         transaction.commit()
223
 
359
 
224
-        content = api.get_one(pcid, ContentType.Any, None)
360
+        # Refresh instances after commit
361
+        user1 = uapi.get_one(u1id)
362
+        workspace = WorkspaceApi(user1).get_one(wid)
363
+        api = ContentApi(user1)
364
+
365
+        content = api.get_one(pcid, ContentType.Any, workspace)
225
         eq_(u1id, content.owner_id)
366
         eq_(u1id, content.owner_id)
226
         eq_(poid, content.owner_id)
367
         eq_(poid, content.owner_id)
227
 
368
 
228
         u2 = UserApi(None).get_one(u2id)
369
         u2 = UserApi(None).get_one(u2id)
229
         api2 = ContentApi(u2)
370
         api2 = ContentApi(u2)
230
-        content2 = api2.get_one(pcid, ContentType.Any, None)
371
+        content2 = api2.get_one(pcid, ContentType.Any, workspace)
231
         api2.update_content(content2, 'this is an updated page', 'new content')
372
         api2.update_content(content2, 'this is an updated page', 'new content')
232
         api2.save(content2)
373
         api2.save(content2)
233
         transaction.commit()
374
         transaction.commit()
234
 
375
 
235
-        updated = api.get_one(pcid, ContentType.Any, None)
236
-        eq_(u2id, updated.owner_id, 'the owner id should be {} (found {})'.format(u2id, updated.owner_id))
376
+        # Refresh instances after commit
377
+        user1 = uapi.get_one(u1id)
378
+        workspace = WorkspaceApi(user1).get_one(wid)
379
+        api = ContentApi(user1)
380
+
381
+        updated = api.get_one(pcid, ContentType.Any, workspace)
382
+        eq_(u2id, updated.owner_id,
383
+            'the owner id should be {} (found {})'.format(u2id,
384
+                                                          updated.owner_id))
237
         eq_('this is an updated page', updated.label)
385
         eq_('this is an updated page', updated.label)
238
         eq_('new content', updated.description)
386
         eq_('new content', updated.description)
239
         eq_(ActionDescription.EDITION, updated.revision_type)
387
         eq_(ActionDescription.EDITION, updated.revision_type)
240
 
388
 
241
-
242
     def test_update_file_data(self):
389
     def test_update_file_data(self):
243
         uapi = UserApi(None)
390
         uapi = UserApi(None)
391
+        groups = [GroupApi(None).get_one(Group.TIM_USER),
392
+                  GroupApi(None).get_one(Group.TIM_MANAGER),
393
+                  GroupApi(None).get_one(Group.TIM_ADMIN)]
244
 
394
 
245
-        user1 = uapi.create_user()
246
-        user1.email = 'this.is@user'
247
-        uapi.save(user1)
395
+        user1 = uapi.create_user(email='this.is@user',
396
+                                groups=groups, save_now=True)
397
+
398
+        workspace = WorkspaceApi(user1).create_workspace('test workspace',
399
+                                                        save_now=True)
400
+        wid = workspace.workspace_id
248
 
401
 
249
         user2 = uapi.create_user()
402
         user2 = uapi.create_user()
250
         user2.email = 'this.is@another.user'
403
         user2.email = 'this.is@another.user'
251
         uapi.save(user2)
404
         uapi.save(user2)
252
 
405
 
406
+        RoleApi(user1).create_one(user2, workspace,
407
+                                  UserRoleInWorkspace.CONTENT_MANAGER,
408
+                                  flush=True)
409
+
410
+        # Test starts here
411
+
253
         api = ContentApi(user1)
412
         api = ContentApi(user1)
254
-        p = api.create(ContentType.File, None, None, 'this_is_a_page', True)
413
+        p = api.create(ContentType.File, workspace, None,
414
+                       'this_is_a_page', True)
255
 
415
 
256
         u1id = user1.user_id
416
         u1id = user1.user_id
257
         u2id = user2.user_id
417
         u2id = user2.user_id
258
         pcid = p.content_id
418
         pcid = p.content_id
259
         poid = p.owner_id
419
         poid = p.owner_id
260
 
420
 
421
+        api.save(p)
261
         transaction.commit()
422
         transaction.commit()
262
 
423
 
263
-        content = api.get_one(pcid, ContentType.Any, None)
424
+        # Refresh instances after commit
425
+        user1 = uapi.get_one(u1id)
426
+        workspace = WorkspaceApi(user1).get_one(wid)
427
+        api = ContentApi(user1)
428
+
429
+        content = api.get_one(pcid, ContentType.Any, workspace)
264
         eq_(u1id, content.owner_id)
430
         eq_(u1id, content.owner_id)
265
         eq_(poid, content.owner_id)
431
         eq_(poid, content.owner_id)
266
 
432
 
267
         u2 = UserApi(None).get_one(u2id)
433
         u2 = UserApi(None).get_one(u2id)
268
         api2 = ContentApi(u2)
434
         api2 = ContentApi(u2)
269
-        content2 = api2.get_one(pcid, ContentType.Any, None)
270
-        api2.update_file_data(content2, 'index.html', 'text/html', b'<html>hello world</html>')
435
+        content2 = api2.get_one(pcid, ContentType.Any, workspace)
436
+        api2.update_file_data(content2, 'index.html', 'text/html',
437
+                              b'<html>hello world</html>')
271
         api2.save(content2)
438
         api2.save(content2)
272
         transaction.commit()
439
         transaction.commit()
273
 
440
 
274
-        updated = api.get_one(pcid, ContentType.Any, None)
275
-        eq_(u2id, updated.owner_id, 'the owner id should be {} (found {})'.format(u2id, updated.owner_id))
441
+        # Refresh instances after commit
442
+        user1 = uapi.get_one(u1id)
443
+        workspace = WorkspaceApi(user1).get_one(wid)
444
+
445
+        updated = api.get_one(pcid, ContentType.Any, workspace)
446
+        eq_(u2id, updated.owner_id,
447
+            'the owner id should be {} (found {})'.format(u2id,
448
+                                                          updated.owner_id))
276
         eq_('index.html', updated.file_name)
449
         eq_('index.html', updated.file_name)
277
         eq_('text/html', updated.file_mimetype)
450
         eq_('text/html', updated.file_mimetype)
278
         eq_(b'<html>hello world</html>', updated.file_content)
451
         eq_(b'<html>hello world</html>', updated.file_content)
279
         eq_(ActionDescription.REVISION, updated.revision_type)
452
         eq_(ActionDescription.REVISION, updated.revision_type)
280
 
453
 
281
-
282
     def test_archive_unarchive(self):
454
     def test_archive_unarchive(self):
283
         uapi = UserApi(None)
455
         uapi = UserApi(None)
456
+        groups = [GroupApi(None).get_one(Group.TIM_USER),
457
+                  GroupApi(None).get_one(Group.TIM_MANAGER),
458
+                  GroupApi(None).get_one(Group.TIM_ADMIN)]
459
+
460
+        user1 = uapi.create_user(email='this.is@user',
461
+                                groups=groups, save_now=True)
462
+        u1id = user1.user_id
284
 
463
 
285
-        user1 = uapi.create_user()
286
-        user1.email = 'this.is@user'
287
-        uapi.save(user1)
464
+        workspace = WorkspaceApi(user1).create_workspace('test workspace',
465
+                                                        save_now=True)
466
+        wid = workspace.workspace_id
288
 
467
 
289
         user2 = uapi.create_user()
468
         user2 = uapi.create_user()
290
         user2.email = 'this.is@another.user'
469
         user2.email = 'this.is@another.user'
291
         uapi.save(user2)
470
         uapi.save(user2)
292
 
471
 
293
-        api = ContentApi(user1, show_archived=True) # show archived is used at the top end of the test
294
-        p = api.create(ContentType.File, None, None, 'this_is_a_page', True)
472
+        RoleApi(user1).create_one(user2, workspace,
473
+                                  UserRoleInWorkspace.CONTENT_MANAGER,
474
+                                  flush=True)
475
+
476
+        # show archived is used at the top end of the test
477
+        api = ContentApi(user1, show_archived=True)
478
+        p = api.create(ContentType.File, workspace, None,
479
+                       'this_is_a_page', True)
295
 
480
 
296
         u1id = user1.user_id
481
         u1id = user1.user_id
297
         u2id = user2.user_id
482
         u2id = user2.user_id
302
 
487
 
303
         ####
488
         ####
304
 
489
 
305
-        content = api.get_one(pcid, ContentType.Any, None)
490
+        # refresh after commit
491
+        user1 = UserApi(None).get_one(u1id)
492
+        workspace = WorkspaceApi(user1).get_one(wid)
493
+
494
+        content = api.get_one(pcid, ContentType.Any, workspace)
306
         eq_(u1id, content.owner_id)
495
         eq_(u1id, content.owner_id)
307
         eq_(poid, content.owner_id)
496
         eq_(poid, content.owner_id)
308
 
497
 
309
         u2 = UserApi(None).get_one(u2id)
498
         u2 = UserApi(None).get_one(u2id)
310
         api2 = ContentApi(u2, show_archived=True)
499
         api2 = ContentApi(u2, show_archived=True)
311
-        content2 = api2.get_one(pcid, ContentType.Any, None)
500
+        content2 = api2.get_one(pcid, ContentType.Any, workspace)
312
         api2.archive(content2)
501
         api2.archive(content2)
313
         api2.save(content2)
502
         api2.save(content2)
314
         transaction.commit()
503
         transaction.commit()
315
 
504
 
316
-        updated = api2.get_one(pcid, ContentType.Any, None)
317
-        eq_(u2id, updated.owner_id, 'the owner id should be {} (found {})'.format(u2id, updated.owner_id))
505
+        # refresh after commit
506
+        user1 = UserApi(None).get_one(u1id)
507
+        workspace = WorkspaceApi(user1).get_one(wid)
508
+        u2 = UserApi(None).get_one(u2id)
509
+        api = ContentApi(user1, show_archived=True)
510
+        api2 = ContentApi(u2, show_archived=True)
511
+
512
+        updated = api2.get_one(pcid, ContentType.Any, workspace)
513
+        eq_(u2id, updated.owner_id,
514
+            'the owner id should be {} (found {})'.format(u2id,
515
+                                                          updated.owner_id))
318
         eq_(True, updated.is_archived)
516
         eq_(True, updated.is_archived)
319
         eq_(ActionDescription.ARCHIVING, updated.revision_type)
517
         eq_(ActionDescription.ARCHIVING, updated.revision_type)
320
 
518
 
321
         ####
519
         ####
322
 
520
 
323
-        updated2 = api.get_one(pcid, ContentType.Any, None)
521
+        updated2 = api.get_one(pcid, ContentType.Any, workspace)
324
         api.unarchive(updated)
522
         api.unarchive(updated)
325
         api.save(updated2)
523
         api.save(updated2)
326
         eq_(False, updated2.is_archived)
524
         eq_(False, updated2.is_archived)
327
         eq_(ActionDescription.UNARCHIVING, updated2.revision_type)
525
         eq_(ActionDescription.UNARCHIVING, updated2.revision_type)
328
         eq_(u1id, updated2.owner_id)
526
         eq_(u1id, updated2.owner_id)
329
 
527
 
330
-
331
     def test_delete_undelete(self):
528
     def test_delete_undelete(self):
332
         uapi = UserApi(None)
529
         uapi = UserApi(None)
530
+        groups = [GroupApi(None).get_one(Group.TIM_USER),
531
+                  GroupApi(None).get_one(Group.TIM_MANAGER),
532
+                  GroupApi(None).get_one(Group.TIM_ADMIN)]
533
+
534
+        user1 = uapi.create_user(email='this.is@user',
535
+                                groups=groups, save_now=True)
536
+        u1id = user1.user_id
333
 
537
 
334
-        user1 = uapi.create_user()
335
-        user1.email = 'this.is@user'
336
-        uapi.save(user1)
538
+        workspace = WorkspaceApi(user1).create_workspace('test workspace',
539
+                                                        save_now=True)
540
+        wid = workspace.workspace_id
337
 
541
 
338
         user2 = uapi.create_user()
542
         user2 = uapi.create_user()
339
         user2.email = 'this.is@another.user'
543
         user2.email = 'this.is@another.user'
340
         uapi.save(user2)
544
         uapi.save(user2)
341
 
545
 
342
-        api = ContentApi(user1, show_deleted=True) # show archived is used at the top end of the test
343
-        p = api.create(ContentType.File, None, None, 'this_is_a_page', True)
546
+        RoleApi(user1).create_one(user2, workspace,
547
+                                  UserRoleInWorkspace.CONTENT_MANAGER,
548
+                                  flush=True)
549
+
550
+        # show archived is used at the top end of the test
551
+        api = ContentApi(user1, show_deleted=True)
552
+        p = api.create(ContentType.File, workspace, None,
553
+                       'this_is_a_page', True)
344
 
554
 
345
         u1id = user1.user_id
555
         u1id = user1.user_id
346
         u2id = user2.user_id
556
         u2id = user2.user_id
350
         transaction.commit()
560
         transaction.commit()
351
 
561
 
352
         ####
562
         ####
563
+        user1 = UserApi(None).get_one(u1id)
564
+        workspace = WorkspaceApi(user1).get_one(wid)
353
 
565
 
354
-        content = api.get_one(pcid, ContentType.Any, None)
566
+        content = api.get_one(pcid, ContentType.Any, workspace)
355
         eq_(u1id, content.owner_id)
567
         eq_(u1id, content.owner_id)
356
         eq_(poid, content.owner_id)
568
         eq_(poid, content.owner_id)
357
 
569
 
358
         u2 = UserApi(None).get_one(u2id)
570
         u2 = UserApi(None).get_one(u2id)
359
         api2 = ContentApi(u2, show_deleted=True)
571
         api2 = ContentApi(u2, show_deleted=True)
360
-        content2 = api2.get_one(pcid, ContentType.Any, None)
572
+        content2 = api2.get_one(pcid, ContentType.Any, workspace)
361
         api2.delete(content2)
573
         api2.delete(content2)
362
         api2.save(content2)
574
         api2.save(content2)
363
         transaction.commit()
575
         transaction.commit()
364
 
576
 
365
-        updated = api2.get_one(pcid, ContentType.Any, None)
366
-        eq_(u2id, updated.owner_id, 'the owner id should be {} (found {})'.format(u2id, updated.owner_id))
577
+        ####
578
+
579
+        user1 = UserApi(None).get_one(u1id)
580
+        workspace = WorkspaceApi(user1).get_one(wid)
581
+        # show archived is used at the top end of the test
582
+        api = ContentApi(user1, show_deleted=True)
583
+        u2 = UserApi(None).get_one(u2id)
584
+        api2 = ContentApi(u2, show_deleted=True)
585
+
586
+        updated = api2.get_one(pcid, ContentType.Any, workspace)
587
+        eq_(u2id, updated.owner_id,
588
+            'the owner id should be {} (found {})'.format(u2id,
589
+                                                          updated.owner_id))
367
         eq_(True, updated.is_deleted)
590
         eq_(True, updated.is_deleted)
368
         eq_(ActionDescription.DELETION, updated.revision_type)
591
         eq_(ActionDescription.DELETION, updated.revision_type)
369
 
592
 
370
         ####
593
         ####
371
 
594
 
372
-        updated2 = api.get_one(pcid, ContentType.Any, None)
595
+        updated2 = api.get_one(pcid, ContentType.Any, workspace)
373
         api.undelete(updated)
596
         api.undelete(updated)
374
         api.save(updated2)
597
         api.save(updated2)
375
         eq_(False, updated2.is_deleted)
598
         eq_(False, updated2.is_deleted)
381
         # This test is based on a bug which does NOT return results found
604
         # This test is based on a bug which does NOT return results found
382
         # at root of a workspace (eg a folder)
605
         # at root of a workspace (eg a folder)
383
         uapi = UserApi(None)
606
         uapi = UserApi(None)
384
-        user = uapi.create_user()
385
-        user.email = 'this.is@user'
386
-        uapi.save(user)
607
+        groups = [GroupApi(None).get_one(Group.TIM_USER),
608
+                  GroupApi(None).get_one(Group.TIM_MANAGER),
609
+                  GroupApi(None).get_one(Group.TIM_ADMIN)]
387
 
610
 
388
-        api = ContentApi(user)
611
+        user = uapi.create_user(email='this.is@user',
612
+                                groups=groups, save_now=True)
389
 
613
 
390
-        a = api.create(ContentType.Folder, None, None, 'this is randomized folder', True)
391
-        p = api.create(ContentType.Page, None, a, 'this is randomized label content', True)
614
+        workspace = WorkspaceApi(user).create_workspace('test workspace',
615
+                                                        save_now=True)
616
+
617
+        api = ContentApi(user)
618
+        a = api.create(ContentType.Folder, workspace, None,
619
+                       'this is randomized folder', True)
620
+        p = api.create(ContentType.Page, workspace, a,
621
+                       'this is randomized label content', True)
392
         p.description = 'This is some amazing test'
622
         p.description = 'This is some amazing test'
393
         api.save(p)
623
         api.save(p)
394
         original_id = p.content_id
624
         original_id = p.content_id
402
         # HACK - D.A. - 2015-03-09
632
         # HACK - D.A. - 2015-03-09
403
         # This test is based on a bug which does NOT return results found
633
         # This test is based on a bug which does NOT return results found
404
         # at root of a workspace (eg a folder)
634
         # at root of a workspace (eg a folder)
635
+
405
         uapi = UserApi(None)
636
         uapi = UserApi(None)
406
-        user = uapi.create_user()
407
-        user.email = 'this.is@user'
408
-        uapi.save(user)
637
+        groups = [GroupApi(None).get_one(Group.TIM_USER),
638
+                  GroupApi(None).get_one(Group.TIM_MANAGER),
639
+                  GroupApi(None).get_one(Group.TIM_ADMIN)]
409
 
640
 
410
-        api = ContentApi(user)
641
+        user = uapi.create_user(email='this.is@user',
642
+                                groups=groups, save_now=True)
411
 
643
 
412
-        a = api.create(ContentType.Folder, None, None, 'this is randomized folder', True)
644
+        workspace = WorkspaceApi(user).create_workspace('test workspace',
645
+                                                        save_now=True)
413
 
646
 
414
-        p = api.create(ContentType.Page, None, a, 'this is dummy label content', True)
647
+        api = ContentApi(user)
648
+        a = api.create(ContentType.Folder, workspace, None,
649
+                       'this is randomized folder', True)
650
+        p = api.create(ContentType.Page, workspace, a,
651
+                       'this is dummy label content', True)
415
         p.description = 'This is some amazing test'
652
         p.description = 'This is some amazing test'
416
         api.save(p)
653
         api.save(p)
417
         original_id = p.content_id
654
         original_id = p.content_id
421
         item = res.all()[0]
658
         item = res.all()[0]
422
         eq_(original_id, item.content_id)
659
         eq_(original_id, item.content_id)
423
 
660
 
424
-
425
     def test_search_in_label_or_description(self):
661
     def test_search_in_label_or_description(self):
426
         # HACK - D.A. - 2015-03-09
662
         # HACK - D.A. - 2015-03-09
427
         # This test is based on a bug which does NOT return results found
663
         # This test is based on a bug which does NOT return results found
428
         # at root of a workspace (eg a folder)
664
         # at root of a workspace (eg a folder)
665
+
429
         uapi = UserApi(None)
666
         uapi = UserApi(None)
430
-        user = uapi.create_user()
431
-        user.email = 'this.is@user'
432
-        uapi.save(user)
667
+        groups = [GroupApi(None).get_one(Group.TIM_USER),
668
+                  GroupApi(None).get_one(Group.TIM_MANAGER),
669
+                  GroupApi(None).get_one(Group.TIM_ADMIN)]
670
+
671
+        user = uapi.create_user(email='this.is@user',
672
+                                groups=groups, save_now=True)
673
+
674
+        workspace = WorkspaceApi(user).create_workspace('test workspace',
675
+                                                        save_now=True)
676
+
433
 
677
 
434
         api = ContentApi(user)
678
         api = ContentApi(user)
435
 
679
 
436
-        a = api.create(ContentType.Folder, None, None, 'this is randomized folder', True)
437
-        p1 = api.create(ContentType.Page, None, a, 'this is dummy label content', True)
680
+        a = api.create(ContentType.Folder, workspace, None,
681
+                       'this is randomized folder', True)
682
+        p1 = api.create(ContentType.Page, workspace, a,
683
+                        'this is dummy label content', True)
438
         p1.description = 'This is some amazing test'
684
         p1.description = 'This is some amazing test'
439
-        p2 = api.create(ContentType.Page, None, a, 'Hey ! Jon !', True)
685
+        p2 = api.create(ContentType.Page, workspace, a, 'Hey ! Jon !', True)
440
         p2.description = 'What\'s up ?'
686
         p2.description = 'What\'s up ?'
441
         api.save(p1)
687
         api.save(p1)
442
         api.save(p2)
688
         api.save(p2)