[d34684]: / ForgeDiscussion / forgediscussion / tests / functional / test_rest.py  Maximize  Restore  History

Download this file

286 lines (262 with data), 12.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from allura.lib import helpers as h
from allura.tests import decorators as td
from allura import model as M
from alluratest.controller import TestRestApiBase
from forgediscussion.model import ForumThread
from ming.odm import ThreadLocalODMSession
class TestDiscussionApiBase(TestRestApiBase):
def setup_method(self, method):
super().setup_method(method)
self.setup_with_tools()
@td.with_discussion
def setup_with_tools(self):
h.set_context('test', 'discussion', neighborhood='Projects')
self.create_forum('héllo', 'Say Héllo', 'Say héllo here')
self.create_topic('general', 'Let\'s talk', '1st post')
self.create_topic('general', 'Hi guys', 'Hi boys and girls')
def create_forum(self, shortname, name, description):
r = self.app.get('/admin/discussion/forums')
form = r.forms['add-forum']
form['add_forum.shortname'] = 'héllo'
form['add_forum.name'] = 'Say Héllo'
form['add_forum.description'] = 'Say héllo here'
form.submit()
def create_topic(self, forum, subject, text):
r = self.app.get('/discussion/create_topic/')
f = r.html.find(
'form', {'action': '/p/test/discussion/save_new_topic'})
params = dict()
inputs = f.find_all('input')
for field in inputs:
if field.has_attr('name'):
params[field['name']] = field.get('value') or ''
params[f.find('textarea')['name']] = text
params[f.find('select')['name']] = forum
params[f.find('input', {'style': 'width: 90%'})['name']] = subject
r = self.app.post('/discussion/save_new_topic', params=params)
class TestRootRestController(TestDiscussionApiBase):
def test_forum_list(self):
forums = self.api_get('/rest/p/test/discussion/')
forums = forums.json['forums']
assert len(forums) == 2
forums = sorted(forums, key=lambda x: x['name'])
assert forums[0]['name'] == 'General Discussion'
assert (
forums[0]['description'] == 'Forum about anything you want to talk about.')
assert forums[0]['num_topics'] == 2
assert (
forums[0]['url'] == 'http://localhost/rest/p/test/discussion/general/')
assert forums[0]['last_post']['subject'] == 'Hi guys'
assert forums[0]['last_post']['author'] == 'test-admin'
assert forums[0]['last_post']['text'] == 'Hi boys and girls'
assert forums[1]['name'] == 'Say Héllo'
assert forums[1]['description'] == 'Say héllo here'
assert forums[1]['num_topics'] == 0
assert (
forums[1]['url'] == 'http://localhost/rest/p/test/discussion/h%C3%A9llo/')
assert forums[1]['last_post'] is None
def test_forum(self):
forum = self.api_get('/rest/p/test/discussion/general/')
forum = forum.json['forum']
assert forum['name'] == 'General Discussion'
assert (
forum['description'] == 'Forum about anything you want to talk about.')
topics = forum['topics']
assert len(topics) == 2
assert topics[0]['subject'] == 'Hi guys'
assert topics[0]['num_views'] == 0
assert topics[0]['num_replies'] == 1
assert topics[0]['last_post']['author'] == 'test-admin'
assert topics[0]['last_post']['text'] == 'Hi boys and girls'
t = ForumThread.query.find({'subject': 'Hi guys'}).first()
url = 'http://localhost/rest/p/test/discussion/general/thread/%s/' % t._id
assert topics[0]['url'] == url
assert topics[1]['subject'] == 'Let\'s talk'
assert topics[1]['num_views'] == 0
assert topics[1]['num_replies'] == 1
assert topics[1]['last_post']['author'] == 'test-admin'
assert topics[1]['last_post']['text'] == '1st post'
t = ForumThread.query.find({'subject': 'Let\'s talk'}).first()
url = 'http://localhost/rest/p/test/discussion/general/thread/%s/' % t._id
assert topics[1]['url'] == url
def test_forum_show_ok_topics(self):
forum = self.api_get('/rest/p/test/discussion/general/')
forum = forum.json['forum']
assert forum['name'] == 'General Discussion'
topics = forum['topics']
assert len(topics) == 2
self.create_topic('general', 'Hi again', 'It should not be shown')
t = ForumThread.query.find({'subject': 'Hi again'}).first()
first_post = t.first_post
first_post.status = 'pending'
first_post.commit()
forum = self.api_get('/rest/p/test/discussion/general/')
forum = forum.json['forum']
assert forum['name'] == 'General Discussion'
topics = forum['topics']
assert len(topics) == 2
def test_topic(self):
forum = self.api_get('/rest/p/test/discussion/general/')
forum = forum.json['forum']
assert forum['name'] == 'General Discussion'
assert (
forum['description'] == 'Forum about anything you want to talk about.')
topics = forum['topics']
topic = self.api_get(topics[0]['url'][len('http://localhost'):])
topic = topic.json['topic']
assert len(topic['posts']) == 1
assert topic['subject'] == 'Hi guys'
assert topic['posts'][0]['text'] == 'Hi boys and girls'
assert topic['posts'][0]['subject'] == 'Hi guys'
assert 'timestamp' in topic['posts'][0]
assert 'last_edited' in topic['posts'][0]
def test_forum_list_pagination(self):
resp = self.app.get('/rest/p/test/discussion/?limit=1')
forums = resp.json['forums']
assert len(forums) == 1
assert forums[0]['name'] == 'General Discussion'
assert resp.json['count'] == 2
assert resp.json['page'] == 0
assert resp.json['limit'] == 1
resp = self.app.get('/rest/p/test/discussion/?limit=1&page=1')
forums = resp.json['forums']
assert len(forums) == 1
assert forums[0]['name'] == 'Say Héllo'
assert resp.json['count'] == 2
assert resp.json['page'] == 1
assert resp.json['limit'] == 1
def test_forum_pagination(self):
resp = self.app.get('/rest/p/test/discussion/general/?limit=1')
topics = resp.json['forum']['topics']
assert len(topics) == 1
assert topics[0]['subject'] == 'Hi guys'
assert resp.json['count'] == 2
assert resp.json['page'] == 0
assert resp.json['limit'] == 1
resp = self.app.get('/rest/p/test/discussion/general/?limit=1&page=1')
topics = resp.json['forum']['topics']
assert len(topics) == 1
assert topics[0]['subject'] == 'Let\'s talk'
assert resp.json['count'] == 2
assert resp.json['page'] == 1
assert resp.json['limit'] == 1
def test_topic_pagination(self):
thread = ForumThread.query.find({'subject': 'Hi guys'}).first()
thread.post('Hi guy', 'I am second post')
url = '/rest/p/test/discussion/general/thread/%s/' % thread._id
resp = self.app.get(url + '?limit=1')
posts = resp.json['topic']['posts']
assert len(posts) == 1
assert posts[0]['text'] == 'Hi boys and girls'
assert resp.json['count'] == 2
assert resp.json['page'] == 0
assert resp.json['limit'] == 1
resp = self.app.get(url + '?limit=1&page=1')
posts = resp.json['topic']['posts']
assert len(posts) == 1
assert posts[0]['text'] == 'I am second post'
assert resp.json['count'] == 2
assert resp.json['page'] == 1
assert resp.json['limit'] == 1
def test_topic_show_ok_only(self):
thread = ForumThread.query.find({'subject': 'Hi guys'}).first()
url = '/rest/p/test/discussion/general/thread/%s/' % thread._id
resp = self.app.get(url)
posts = resp.json['topic']['posts']
assert len(posts) == 1
thread.post('Hello', 'I am not ok post')
last_post = thread.last_post
last_post.status = 'pending'
last_post.commit()
ThreadLocalODMSession.flush_all()
resp = self.app.get(url)
posts = resp.json['topic']['posts']
assert len(posts) == 1
def test_security(self):
p = M.Project.query.get(shortname='test')
acl = p.app_instance('discussion').config.acl
anon = M.ProjectRole.by_name('*anonymous')._id
auth = M.ProjectRole.by_name('*authenticated')._id
anon_read = M.ACE.allow(anon, 'read')
auth_read = M.ACE.allow(auth, 'read')
acl.remove(anon_read)
acl.append(auth_read)
self.api_get('/rest/p/test/discussion/')
self.app.get('/rest/p/test/discussion/',
extra_environ={'username': '*anonymous'},
status=401)
self.api_get('/rest/p/test/discussion/general/')
self.app.get('/rest/p/test/discussion/general/',
extra_environ={'username': '*anonymous'},
status=401)
t = ForumThread.query.find({'subject': 'Hi guys'}).first()
self.api_get('/rest/p/test/discussion/general/thread/%s/' % t._id)
self.app.get('/rest/p/test/discussion/general/thread/%s/' % t._id,
extra_environ={'username': '*anonymous'},
status=401)
def test_private_forums(self):
r = self.app.get('/p/test/admin/discussion/forums')
form = r.forms['edit-forums']
if form['forum-0.shortname'].value == 'héllo':
form['forum-0.members_only'] = True
else:
form['forum-1.members_only'] = True
form.submit()
r = self.api_get('/rest/p/test/discussion/')
assert len(r.json['forums']) == 2
r = self.app.get('/rest/p/test/discussion/',
extra_environ={'username': '*anonymous'})
assert len(r.json['forums']) == 1
assert r.json['forums'][0]['shortname'] == 'general'
def test_has_access_no_params(self):
self.api_get('/rest/p/test/discussion/has_access', status=404)
self.api_get('/rest/p/test/discussion/has_access?user=root', status=404)
self.api_get('/rest/p/test/discussion/has_access?perm=read', status=404)
def test_has_access_unknown_params(self):
"""Unknown user and/or permission always False for has_access API"""
r = self.api_get(
'/rest/p/test/discussion/has_access?user=babadook&perm=read',
user='root')
assert r.status_int == 200
assert r.json['result'] is False
r = self.api_get(
'/rest/p/test/discussion/has_access?user=test-user&perm=jump',
user='root')
assert r.status_int == 200
assert r.json['result'] is False
def test_has_access_not_admin(self):
"""
User which has no 'admin' permission on neighborhood can't use
has_access API
"""
self.api_get(
'/rest/p/test/discussion/has_access?user=test-admin&perm=admin',
user='test-user',
status=403)
def test_has_access(self):
r = self.api_get(
'/rest/p/test/discussion/has_access?user=test-admin&perm=post',
user='root')
assert r.status_int == 200
assert r.json['result'] is True
r = self.api_get(
'/rest/p/test/discussion/has_access?user=*anonymous&perm=admin',
user='root')
assert r.status_int == 200
assert r.json['result'] is False