|
@@ -2,13 +2,245 @@
|
2
|
2
|
"""
|
3
|
3
|
Tests for /api/v2/users subpath endpoints.
|
4
|
4
|
"""
|
|
5
|
+from time import sleep
|
|
6
|
+
|
|
7
|
+import transaction
|
|
8
|
+
|
|
9
|
+from tracim import models
|
|
10
|
+from tracim.lib.core.content import ContentApi
|
|
11
|
+from tracim.lib.core.user import UserApi
|
|
12
|
+from tracim.lib.core.workspace import WorkspaceApi
|
|
13
|
+from tracim.models import get_tm_session
|
|
14
|
+from tracim.models.contents import ContentTypeLegacy as ContentType
|
|
15
|
+from tracim.models.revision_protection import new_revision
|
5
|
16
|
from tracim.tests import FunctionalTest
|
6
|
17
|
from tracim.fixtures.content import Content as ContentFixtures
|
7
|
18
|
from tracim.fixtures.users_and_groups import Base as BaseFixture
|
8
|
19
|
|
9
|
20
|
|
|
21
|
+class TestUserRecentlyActiveContentEndpoint(FunctionalTest):
|
|
22
|
+ """
|
|
23
|
+ Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/contents/recently_active # nopep8
|
|
24
|
+ """
|
|
25
|
+ fixtures = [BaseFixture]
|
|
26
|
+
|
|
27
|
+ def test_api__get_recently_active_content__ok__200__nominal_case(self):
|
|
28
|
+
|
|
29
|
+ # init DB
|
|
30
|
+ dbsession = get_tm_session(self.session_factory, transaction.manager)
|
|
31
|
+ admin = dbsession.query(models.User) \
|
|
32
|
+ .filter(models.User.email == 'admin@admin.admin') \
|
|
33
|
+ .one()
|
|
34
|
+ workspace_api = WorkspaceApi(
|
|
35
|
+ current_user=admin,
|
|
36
|
+ session=dbsession,
|
|
37
|
+ config=self.app_config
|
|
38
|
+
|
|
39
|
+ )
|
|
40
|
+ workspace = WorkspaceApi(
|
|
41
|
+ current_user=admin,
|
|
42
|
+ session=dbsession,
|
|
43
|
+ config=self.app_config,
|
|
44
|
+ ).create_workspace(
|
|
45
|
+ 'test workspace',
|
|
46
|
+ save_now=True
|
|
47
|
+ )
|
|
48
|
+ workspace2 = WorkspaceApi(
|
|
49
|
+ current_user=admin,
|
|
50
|
+ session=dbsession,
|
|
51
|
+ config=self.app_config,
|
|
52
|
+ ).create_workspace(
|
|
53
|
+ 'test workspace2',
|
|
54
|
+ save_now=True
|
|
55
|
+ )
|
|
56
|
+
|
|
57
|
+ api = ContentApi(
|
|
58
|
+ current_user=admin,
|
|
59
|
+ session=dbsession,
|
|
60
|
+ config=self.app_config,
|
|
61
|
+ )
|
|
62
|
+ main_folder_workspace2 = api.create(ContentType.Folder, workspace2, None, 'Hepla', '', True) # nopep8
|
|
63
|
+ main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
|
|
64
|
+ # creation order test
|
|
65
|
+ firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
|
|
66
|
+ secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
|
|
67
|
+ # update order test
|
|
68
|
+ firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True) # nopep8
|
|
69
|
+ secondly_created_but_not_updated = api.create(ContentType.Page, workspace, main_folder, 'another update_order_test', '', True) # nopep8
|
|
70
|
+ with new_revision(
|
|
71
|
+ session=dbsession,
|
|
72
|
+ tm=transaction.manager,
|
|
73
|
+ content=firstly_created_but_recently_updated,
|
|
74
|
+ ):
|
|
75
|
+ firstly_created_but_recently_updated.description = 'Just an update'
|
|
76
|
+ api.save(firstly_created_but_recently_updated)
|
|
77
|
+ # comment change order
|
|
78
|
+ firstly_created_but_recently_commented = api.create(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
|
|
79
|
+ secondly_created_but_not_commented = api.create(ContentType.Page, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
|
|
80
|
+ comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
|
|
81
|
+ content_workspace_2 = api.create(ContentType.Page, workspace2,main_folder_workspace2, 'content_workspace_2', '',True) # nopep8
|
|
82
|
+ dbsession.flush()
|
|
83
|
+ transaction.commit()
|
|
84
|
+
|
|
85
|
+ self.testapp.authorization = (
|
|
86
|
+ 'Basic',
|
|
87
|
+ (
|
|
88
|
+ 'admin@admin.admin',
|
|
89
|
+ 'admin@admin.admin'
|
|
90
|
+ )
|
|
91
|
+ )
|
|
92
|
+ res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/recently_active'.format(workspace.workspace_id), status=200) # nopep8
|
|
93
|
+ res = res.json_body
|
|
94
|
+ assert len(res) == 7
|
|
95
|
+ for elem in res:
|
|
96
|
+ assert isinstance(elem['content_id'], int)
|
|
97
|
+ assert isinstance(elem['content_type'], str)
|
|
98
|
+ assert elem['content_type'] != 'comments'
|
|
99
|
+ assert isinstance(elem['is_archived'], bool)
|
|
100
|
+ assert isinstance(elem['is_deleted'], bool)
|
|
101
|
+ assert isinstance(elem['label'], str)
|
|
102
|
+ assert isinstance(elem['parent_id'], int) or elem['parent_id'] is None
|
|
103
|
+ assert isinstance(elem['show_in_ui'], bool)
|
|
104
|
+ assert isinstance(elem['slug'], str)
|
|
105
|
+ assert isinstance(elem['status'], str)
|
|
106
|
+ assert isinstance(elem['sub_content_types'], list)
|
|
107
|
+ for sub_content_type in elem['sub_content_types']:
|
|
108
|
+ assert isinstance(sub_content_type, str)
|
|
109
|
+ assert isinstance(elem['workspace_id'], int)
|
|
110
|
+ # comment is newest than page2
|
|
111
|
+ assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
|
|
112
|
+ assert res[1]['content_id'] == secondly_created_but_not_commented.content_id
|
|
113
|
+ # last updated content is newer than other one despite creation
|
|
114
|
+ # of the other is more recent
|
|
115
|
+ assert res[2]['content_id'] == firstly_created_but_recently_updated.content_id
|
|
116
|
+ assert res[3]['content_id'] == secondly_created_but_not_updated.content_id
|
|
117
|
+ # creation order is inverted here as last created is last active
|
|
118
|
+ assert res[4]['content_id'] == secondly_created.content_id
|
|
119
|
+ assert res[5]['content_id'] == firstly_created.content_id
|
|
120
|
+ # folder subcontent modification does not change folder order
|
|
121
|
+ assert res[6]['content_id'] == main_folder.content_id
|
|
122
|
+
|
|
123
|
+ def test_api__get_recently_active_content__ok__200__limit_2_multiple(self):
|
|
124
|
+ # TODO - G.M - 2018-07-20 - Better fix for this test, do not use sleep()
|
|
125
|
+ # anymore to fix datetime lack of precision.
|
|
126
|
+
|
|
127
|
+ # init DB
|
|
128
|
+ dbsession = get_tm_session(self.session_factory, transaction.manager)
|
|
129
|
+ admin = dbsession.query(models.User) \
|
|
130
|
+ .filter(models.User.email == 'admin@admin.admin') \
|
|
131
|
+ .one()
|
|
132
|
+ workspace_api = WorkspaceApi(
|
|
133
|
+ current_user=admin,
|
|
134
|
+ session=dbsession,
|
|
135
|
+ config=self.app_config
|
|
136
|
+
|
|
137
|
+ )
|
|
138
|
+ workspace = WorkspaceApi(
|
|
139
|
+ current_user=admin,
|
|
140
|
+ session=dbsession,
|
|
141
|
+ config=self.app_config,
|
|
142
|
+ ).create_workspace(
|
|
143
|
+ 'test workspace',
|
|
144
|
+ save_now=True
|
|
145
|
+ )
|
|
146
|
+ workspace2 = WorkspaceApi(
|
|
147
|
+ current_user=admin,
|
|
148
|
+ session=dbsession,
|
|
149
|
+ config=self.app_config,
|
|
150
|
+ ).create_workspace(
|
|
151
|
+ 'test workspace2',
|
|
152
|
+ save_now=True
|
|
153
|
+ )
|
|
154
|
+
|
|
155
|
+ api = ContentApi(
|
|
156
|
+ current_user=admin,
|
|
157
|
+ session=dbsession,
|
|
158
|
+ config=self.app_config,
|
|
159
|
+ )
|
|
160
|
+ main_folder_workspace2 = api.create(ContentType.Folder, workspace2, None, 'Hepla', '', True) # nopep8
|
|
161
|
+ sleep(1)
|
|
162
|
+ main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
|
|
163
|
+ # creation order test
|
|
164
|
+ firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
|
|
165
|
+ sleep(1)
|
|
166
|
+ secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
|
|
167
|
+ # update order test
|
|
168
|
+ firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True) # nopep8
|
|
169
|
+ sleep(1)
|
|
170
|
+ secondly_created_but_not_updated = api.create(ContentType.Page, workspace, main_folder, 'another update_order_test', '', True) # nopep8
|
|
171
|
+ sleep(1)
|
|
172
|
+ with new_revision(
|
|
173
|
+ session=dbsession,
|
|
174
|
+ tm=transaction.manager,
|
|
175
|
+ content=firstly_created_but_recently_updated,
|
|
176
|
+ ):
|
|
177
|
+ firstly_created_but_recently_updated.description = 'Just an update'
|
|
178
|
+ api.save(firstly_created_but_recently_updated)
|
|
179
|
+ # comment change order
|
|
180
|
+ firstly_created_but_recently_commented = api.create(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
|
|
181
|
+ sleep(1)
|
|
182
|
+ secondly_created_but_not_commented = api.create(ContentType.Page, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
|
|
183
|
+ sleep(1)
|
|
184
|
+ comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
|
|
185
|
+ sleep(1)
|
|
186
|
+ content_workspace_2 = api.create(ContentType.Page, workspace2,main_folder_workspace2, 'content_workspace_2', '',True) # nopep8
|
|
187
|
+ dbsession.flush()
|
|
188
|
+ transaction.commit()
|
|
189
|
+
|
|
190
|
+ self.testapp.authorization = (
|
|
191
|
+ 'Basic',
|
|
192
|
+ (
|
|
193
|
+ 'admin@admin.admin',
|
|
194
|
+ 'admin@admin.admin'
|
|
195
|
+ )
|
|
196
|
+ )
|
|
197
|
+ params = {
|
|
198
|
+ 'limit': 2,
|
|
199
|
+ }
|
|
200
|
+ res = self.testapp.get(
|
|
201
|
+ '/api/v2/users/1/workspaces/{}/contents/recently_active'.format(workspace.workspace_id), # nopep8
|
|
202
|
+ status=200,
|
|
203
|
+ params=params
|
|
204
|
+ ) # nopep8
|
|
205
|
+ res = res.json_body
|
|
206
|
+ assert len(res) == 2
|
|
207
|
+ for elem in res:
|
|
208
|
+ assert isinstance(elem['content_id'], int)
|
|
209
|
+ assert isinstance(elem['content_type'], str)
|
|
210
|
+ assert elem['content_type'] != 'comments'
|
|
211
|
+ assert isinstance(elem['is_archived'], bool)
|
|
212
|
+ assert isinstance(elem['is_deleted'], bool)
|
|
213
|
+ assert isinstance(elem['label'], str)
|
|
214
|
+ assert isinstance(elem['parent_id'], int) or elem['parent_id'] is None
|
|
215
|
+ assert isinstance(elem['show_in_ui'], bool)
|
|
216
|
+ assert isinstance(elem['slug'], str)
|
|
217
|
+ assert isinstance(elem['status'], str)
|
|
218
|
+ assert isinstance(elem['sub_content_types'], list)
|
|
219
|
+ for sub_content_type in elem['sub_content_types']:
|
|
220
|
+ assert isinstance(sub_content_type, str)
|
|
221
|
+ assert isinstance(elem['workspace_id'], int)
|
|
222
|
+ # comment is newest than page2
|
|
223
|
+ assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
|
|
224
|
+ assert res[1]['content_id'] == secondly_created_but_not_commented.content_id
|
|
225
|
+
|
|
226
|
+ params = {
|
|
227
|
+ 'limit': 2,
|
|
228
|
+ 'before_datetime': secondly_created_but_not_commented.get_last_activity_date().strftime('%Y-%m-%dT%H:%M:%SZ'), # nopep8
|
|
229
|
+ }
|
|
230
|
+ res = self.testapp.get(
|
|
231
|
+ '/api/v2/users/1/workspaces/{}/contents/recently_active'.format(workspace.workspace_id), # nopep8
|
|
232
|
+ status=200,
|
|
233
|
+ params=params
|
|
234
|
+ )
|
|
235
|
+ res = res.json_body
|
|
236
|
+ assert len(res) == 2
|
|
237
|
+ # last updated content is newer than other one despite creation
|
|
238
|
+ # of the other is more recent
|
|
239
|
+ assert res[0]['content_id'] == firstly_created_but_recently_updated.content_id
|
|
240
|
+ assert res[1]['content_id'] == secondly_created_but_not_updated.content_id
|
|
241
|
+
|
|
242
|
+
|
10
|
243
|
class TestUserWorkspaceEndpoint(FunctionalTest):
|
11
|
|
- # -*- coding: utf-8 -*-
|
12
|
244
|
"""
|
13
|
245
|
Tests for /api/v2/users/{user_id}/workspaces
|
14
|
246
|
"""
|