Parent: [44ad38] (diff)

Child: [57067c] (diff)

Download this file

test_rest.py    190 lines (172 with data), 8.8 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
# coding: utf-8
from nose.tools import assert_equal
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
class TestDiscussionApiBase(TestRestApiBase):
def setUp(self):
super(TestDiscussionApiBase, self).setUp()
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')
r.forms[1]['add_forum.shortname'] = 'héllo'
r.forms[1]['add_forum.name'] = 'Say Héllo'
r.forms[1]['add_forum.description'] = 'Say héllo here'
r.forms[1].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.findAll('input')
for field in inputs:
if field.has_key('name'):
params[field['name']] = field.has_key('value') and field['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_equal(len(forums), 2)
forums = sorted(forums, key=lambda x: x['name'])
assert_equal(forums[0]['name'], 'General Discussion')
assert_equal(forums[0]['description'], 'Forum about anything you want to talk about.')
assert_equal(forums[0]['num_topics'], 2)
assert_equal(forums[0]['url'], 'http://localhost:80/rest/p/test/discussion/general/')
assert_equal(forums[0]['last_post']['subject'], 'Hi guys')
assert_equal(forums[0]['last_post']['author'], 'test-admin')
assert_equal(forums[0]['last_post']['text'], 'Hi boys and girls')
assert_equal(forums[1]['name'], u'Say Héllo')
assert_equal(forums[1]['description'], u'Say héllo here')
assert_equal(forums[1]['num_topics'], 0)
assert_equal(forums[1]['url'], 'http://localhost:80/rest/p/test/discussion/h%C3%A9llo/')
assert_equal(forums[1]['last_post'], None)
def test_forum(self):
forum = self.api_get('/rest/p/test/discussion/general/')
forum = forum.json['forum']
assert_equal(forum['name'], 'General Discussion')
assert_equal(forum['description'], 'Forum about anything you want to talk about.')
topics = forum['topics']
assert_equal(len(topics), 2)
assert_equal(topics[0]['subject'], 'Hi guys')
assert_equal(topics[0]['num_views'], 0)
assert_equal(topics[0]['num_replies'], 1)
assert_equal(topics[0]['last_post']['author'], 'test-admin')
assert_equal(topics[0]['last_post']['text'], 'Hi boys and girls')
t = ForumThread.query.find({'subject': 'Hi guys'}).first()
url = 'http://localhost:80/rest/p/test/discussion/general/thread/%s/' % t._id
assert_equal(topics[0]['url'], url)
assert_equal(topics[1]['subject'], 'Let\'s talk')
assert_equal(topics[1]['num_views'], 0)
assert_equal(topics[1]['num_replies'], 1)
assert_equal(topics[1]['last_post']['author'], 'test-admin')
assert_equal(topics[1]['last_post']['text'], '1st post')
t = ForumThread.query.find({'subject': 'Let\'s talk'}).first()
url = 'http://localhost:80/rest/p/test/discussion/general/thread/%s/' % t._id
assert_equal(topics[1]['url'], url)
def test_topic(self):
forum = self.api_get('/rest/p/test/discussion/general/')
forum = forum.json['forum']
assert_equal(forum['name'], 'General Discussion')
assert_equal(forum['description'], 'Forum about anything you want to talk about.')
topics = forum['topics']
topic = self.api_get(topics[0]['url'][len('http://localhost:80'):])
topic = topic.json['topic']
assert_equal(len(topic['posts']), 1)
assert_equal(topic['subject'], 'Hi guys')
assert_equal(topic['posts'][0]['text'], 'Hi boys and girls')
assert_equal(topic['posts'][0]['subject'], 'Hi guys')
def test_forum_list_pagination(self):
resp = self.app.get('/rest/p/test/discussion/?limit=1')
forums = resp.json['forums']
assert_equal(len(forums), 1)
assert_equal(forums[0]['name'], 'General Discussion')
assert_equal(resp.json['count'], 2)
assert_equal(resp.json['page'], 0)
assert_equal(resp.json['limit'], 1)
resp = self.app.get('/rest/p/test/discussion/?limit=1&page=1')
forums = resp.json['forums']
assert_equal(len(forums), 1)
assert_equal(forums[0]['name'], u'Say Héllo')
assert_equal(resp.json['count'], 2)
assert_equal(resp.json['page'], 1)
assert_equal(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_equal(len(topics), 1)
assert_equal(topics[0]['subject'], 'Hi guys')
assert_equal(resp.json['count'], 2)
assert_equal(resp.json['page'], 0)
assert_equal(resp.json['limit'], 1)
resp = self.app.get('/rest/p/test/discussion/general/?limit=1&page=1')
topics = resp.json['forum']['topics']
assert_equal(len(topics), 1)
assert_equal(topics[0]['subject'], 'Let\'s talk')
assert_equal(resp.json['count'], 2)
assert_equal(resp.json['page'], 1)
assert_equal(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_equal(len(posts), 1)
assert_equal(posts[0]['text'], 'Hi boys and girls')
assert_equal(resp.json['count'], 2)
assert_equal(resp.json['page'], 0)
assert_equal(resp.json['limit'], 1)
resp = self.app.get(url + '?limit=1&page=1')
posts = resp.json['topic']['posts']
assert_equal(len(posts), 1)
assert_equal(posts[0]['text'], 'I am second post')
assert_equal(resp.json['count'], 2)
assert_equal(resp.json['page'], 1)
assert_equal(resp.json['limit'], 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[0]
if form['forum-0.shortname'].value == u'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_equal(len(r.json['forums']), 2)
r = self.app.get('/rest/p/test/discussion/',
extra_environ={'username': '*anonymous'})
assert_equal(len(r.json['forums']), 1)
assert_equal(r.json['forums'][0]['shortname'], 'general')