Parent: [af5c8e] (diff)

Child: [93d12e] (diff)

Download this file

test_rest.py    192 lines (174 with data), 8.5 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
# coding: utf-8
# 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 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 forgeblog import model as BM
class TestBlogApi(TestRestApiBase):
def setUp(self):
super(TestBlogApi, self).setUp()
self.setup_with_tools()
@td.with_tool('test', 'Blog', 'blog')
def setup_with_tools(self):
h.set_context('test', 'blog', neighborhood='Projects')
def test_create_post(self):
data = {
'title': 'test',
'text': 'test text',
'state': 'published',
'labels': 'label1, label2'
}
r = self.api_post('/rest/p/test/blog/', **data)
assert_equal(r.status_int, 201)
url = '/rest' + BM.BlogPost.query.find().first().url()
r = self.api_get('/rest/p/test/blog/')
assert_equal(r.json['posts'][0]['title'], 'test')
assert_equal(r.json['posts'][0]['url'], h.absurl(url))
r = self.api_get(url)
assert_equal(r.json['title'], data['title'])
assert_equal(r.json['text'], data['text'])
assert_equal(r.json['author'], 'test-admin')
assert_equal(r.json['state'], data['state'])
assert_equal(r.json['labels'], data['labels'].split(','))
def test_update_post(self):
data = {
'title': 'test',
'text': 'test text',
'state': 'published',
'labels': 'label1, label2'
}
r = self.api_post('/rest/p/test/blog/', **data)
assert_equal(r.status_int, 201)
url = '/rest' + BM.BlogPost.query.find().first().url()
data = {
'text': 'test text2',
'state': 'draft',
'labels': 'label3'
}
self.api_post(url, **data)
r = self.api_get(url)
assert_equal(r.json['title'], 'test')
assert_equal(r.json['text'], data['text'])
assert_equal(r.json['state'], data['state'])
assert_equal(r.json['labels'], data['labels'].split(','))
def test_delete_post(self):
data = {
'title': 'test',
'state': 'published',
'labels': 'label1, label2'
}
r = self.api_post('/rest/p/test/blog/', **data)
assert_equal(r.status_int, 201)
url = '/rest' + BM.BlogPost.query.find().first().url()
self.api_post(url, delete='')
r = self.api_get(url)
assert_equal(r.status_int, 404)
def test_post_does_not_exist(self):
r = self.api_get('/rest/p/test/blog/2013/07/fake/')
assert_equal(r.status_int, 404)
def test_read_permissons(self):
self.api_post('/rest/p/test/blog/', title='test', text='test text', state='published')
self.app.get('/rest/p/test/blog/', extra_environ={'username': '*anonymous'}, status=200)
p = M.Project.query.get(shortname='test')
acl = p.app_instance('blog').config.acl
anon = M.ProjectRole.by_name('*anonymous')._id
anon_read = M.ACE.allow(anon, 'read')
acl.remove(anon_read)
self.app.get('/rest/p/test/blog/',
extra_environ={'username': '*anonymous'},
status=401)
def test_new_post_permissons(self):
self.app.post('/rest/p/test/blog/',
params=dict(title='test', text='test text', state='published'),
extra_environ={'username': '*anonymous'},
status=401)
p = M.Project.query.get(shortname='test')
acl = p.app_instance('blog').config.acl
anon = M.ProjectRole.by_name('*anonymous')._id
anon_write = M.ACE.allow(anon, 'write')
acl.append(anon_write)
self.app.post('/rest/p/test/blog/',
params=dict(title='test', text='test text', state='published'),
extra_environ={'username': '*anonymous'},
status=201)
def test_update_post_permissons(self):
self.api_post('/rest/p/test/blog/', title='test', text='test text', state='published')
url = '/rest' + BM.BlogPost.query.find().first().url()
self.app.post(url.encode('utf-8'),
params=dict(title='test2', text='test text2', state='published'),
extra_environ={'username': '*anonymous'},
status=401)
p = M.Project.query.get(shortname='test')
acl = p.app_instance('blog').config.acl
anon = M.ProjectRole.by_name('*anonymous')._id
anon_write = M.ACE.allow(anon, 'write')
acl.append(anon_write)
self.app.post(url.encode('utf-8'),
params=dict(title='test2', text='test text2', state='published'),
extra_environ={'username': '*anonymous'},
status=200)
r = self.api_get(url)
assert_equal(r.json['title'], 'test2')
assert_equal(r.json['text'], 'test text2')
assert_equal(r.json['state'], 'published')
def test_permission_draft_post(self):
self.api_post('/rest/p/test/blog/', title='test', text='test text', state='draft')
r = self.app.get('/rest/p/test/blog/', extra_environ={'username': '*anonymous'})
assert_equal(r.json['posts'], [])
url = '/rest' + BM.BlogPost.query.find().first().url()
self.app.post(url.encode('utf-8'),
params=dict(title='test2', text='test text2', state='published'),
extra_environ={'username': '*anonymous'},
status=401)
p = M.Project.query.get(shortname='test')
acl = p.app_instance('blog').config.acl
anon = M.ProjectRole.by_name('*anonymous')._id
anon_write = M.ACE.allow(anon, 'write')
acl.append(anon_write)
r = self.app.get('/rest/p/test/blog/', extra_environ={'username': '*anonymous'})
assert_equal(r.json['posts'][0]['title'], 'test')
def test_draft_post(self):
self.api_post('/rest/p/test/blog/', title='test', text='test text', state='draft')
r = self.app.get('/rest/p/test/blog/', extra_environ={'username': '*anonymous'})
assert_equal(r.json['posts'], [])
url = '/rest' + BM.BlogPost.query.find().first().url()
self.api_post(url, state='published')
r = self.app.get('/rest/p/test/blog/', extra_environ={'username': '*anonymous'})
assert_equal(r.json['posts'][0]['title'], 'test')
def test_pagination(self):
self.api_post('/rest/p/test/blog/', title='test1', text='test text1', state='published')
self.api_post('/rest/p/test/blog/', title='test2', text='test text2', state='published')
self.api_post('/rest/p/test/blog/', title='test3', text='test text3', state='published')
r = self.api_get('/rest/p/test/blog/', limit='1', page='0')
assert_equal(r.json['posts'][0]['title'], 'test3')
assert_equal(r.json['count'], 3)
assert_equal(r.json['limit'], 1)
assert_equal(r.json['page'], 0)
r = self.api_get('/rest/p/test/blog/', limit='2', page='0')
assert_equal(r.json['posts'][0]['title'], 'test3')
assert_equal(r.json['posts'][1]['title'], 'test2')
assert_equal(r.json['count'], 3)
assert_equal(r.json['limit'], 2)
assert_equal(r.json['page'], 0)
r = self.api_get('/rest/p/test/blog/', limit='1', page='2')
assert_equal(r.json['posts'][0]['title'], 'test1')
assert_equal(r.json['count'], 3)
assert_equal(r.json['limit'], 1)
assert_equal(r.json['page'], 2)