2012-10-16 20:49:43 +00:00
|
|
|
|
2012-10-19 13:51:51 +00:00
|
|
|
import urllib
|
2012-10-16 20:49:43 +00:00
|
|
|
import tempfile
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from werkzeug.test import Client
|
|
|
|
from werkzeug.wrappers import Response
|
|
|
|
|
|
|
|
from isso import Isso, json
|
|
|
|
from isso.models import Comment
|
|
|
|
|
|
|
|
|
|
|
|
def comment(**kw):
|
|
|
|
return Comment.fromjson(json.dumps(kw))
|
|
|
|
|
|
|
|
|
|
|
|
class TestComments(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
fd, self.path = tempfile.mkstemp()
|
2012-10-21 07:51:32 +00:00
|
|
|
self.app = Isso({'SQLITE': self.path, 'PRODUCTION': False,
|
|
|
|
'MARKUP': 'isso.markup.Markup'})
|
2012-10-16 20:49:43 +00:00
|
|
|
|
2012-10-17 20:20:02 +00:00
|
|
|
self.client = Client(self.app, Response)
|
|
|
|
self.get = lambda *x, **z: self.client.get(*x, **z)
|
|
|
|
self.put = lambda *x, **z: self.client.put(*x, **z)
|
|
|
|
self.post = lambda *x, **z: self.client.post(*x, **z)
|
|
|
|
self.delete = lambda *x, **z: self.client.delete(*x, **z)
|
|
|
|
|
2012-10-16 20:49:43 +00:00
|
|
|
def testGet(self):
|
|
|
|
|
2012-10-23 19:09:51 +00:00
|
|
|
self.post('/1.0/path/new', data=json.dumps(comment(text='Lorem ipsum ...')))
|
|
|
|
r = self.get('/1.0/path/1')
|
2012-10-16 20:49:43 +00:00
|
|
|
assert r.status_code == 200
|
|
|
|
|
|
|
|
rv = json.loads(r.data)
|
|
|
|
|
|
|
|
assert rv['id'] == 1
|
|
|
|
assert rv['text'] == 'Lorem ipsum ...'
|
|
|
|
|
|
|
|
def testCreate(self):
|
|
|
|
|
2012-10-23 19:09:51 +00:00
|
|
|
rv = self.post('/1.0/path/new', data=json.dumps(comment(text='Lorem ipsum ...')))
|
2012-10-16 20:49:43 +00:00
|
|
|
|
|
|
|
assert rv.status_code == 201
|
2012-10-17 20:20:02 +00:00
|
|
|
assert len(filter(lambda header: header[0] == 'Set-Cookie', rv.headers)) == 1
|
2012-10-16 20:49:43 +00:00
|
|
|
|
|
|
|
c = Comment.fromjson(rv.data)
|
|
|
|
|
|
|
|
assert not c.pending
|
|
|
|
assert not c.deleted
|
|
|
|
assert c.text == 'Lorem ipsum ...'
|
|
|
|
|
|
|
|
def testCreateAndGetMultiple(self):
|
|
|
|
|
2012-10-21 20:37:39 +00:00
|
|
|
for i in range(20):
|
2012-10-23 19:09:51 +00:00
|
|
|
self.post('/1.0/path/new', data=json.dumps(comment(text='Spam')))
|
2012-10-16 20:49:43 +00:00
|
|
|
|
2012-10-23 19:09:51 +00:00
|
|
|
r = self.get('/1.0/path/')
|
2012-10-16 20:49:43 +00:00
|
|
|
assert r.status_code == 200
|
|
|
|
|
|
|
|
rv = json.loads(r.data)
|
|
|
|
assert len(rv) == 20
|
2012-10-16 20:56:21 +00:00
|
|
|
|
|
|
|
def testGetInvalid(self):
|
|
|
|
|
2012-10-23 19:09:51 +00:00
|
|
|
assert self.get('/1.0/path/123').status_code == 404
|
|
|
|
assert self.get('/1.0/path/spam/123').status_code == 404
|
|
|
|
assert self.get('/1.0/foo/').status_code == 404
|
2012-10-17 09:06:26 +00:00
|
|
|
|
|
|
|
def testUpdate(self):
|
|
|
|
|
2012-10-23 19:09:51 +00:00
|
|
|
self.post('/1.0/path/new', data=json.dumps(comment(text='Lorem ipsum ...')))
|
|
|
|
self.put('/1.0/path/1', data=json.dumps(comment(
|
2012-10-17 09:06:26 +00:00
|
|
|
text='Hello World', author='me', website='http://example.com/')))
|
|
|
|
|
2012-10-23 19:09:51 +00:00
|
|
|
r = self.get('/1.0/path/1')
|
2012-10-17 09:06:26 +00:00
|
|
|
assert r.status_code == 200
|
|
|
|
|
|
|
|
rv = json.loads(r.data)
|
|
|
|
assert rv['text'] == 'Hello World'
|
|
|
|
assert rv['author'] == 'me'
|
|
|
|
assert rv['website'] == 'http://example.com/'
|
|
|
|
assert 'modified' in rv
|
|
|
|
|
|
|
|
def testDelete(self):
|
|
|
|
|
2012-10-23 19:09:51 +00:00
|
|
|
self.post('/1.0/path/new', data=json.dumps(comment(text='Lorem ipsum ...')))
|
|
|
|
r = self.delete('/1.0/path/1')
|
2012-10-17 10:22:52 +00:00
|
|
|
assert r.status_code == 200
|
|
|
|
assert json.loads(r.data) == None
|
2012-10-23 19:09:51 +00:00
|
|
|
assert self.get('/1.0/path/1').status_code == 404
|
2012-10-17 09:06:26 +00:00
|
|
|
|
2012-10-17 10:00:11 +00:00
|
|
|
def testDeleteWithReference(self):
|
|
|
|
|
2012-10-17 20:20:02 +00:00
|
|
|
client = Client(self.app, Response)
|
2012-10-23 19:09:51 +00:00
|
|
|
resp = client.post('/1.0/path/new', data=json.dumps(comment(text='First')))
|
|
|
|
self.post('/1.0/path/new', data=json.dumps(comment(text='Second', parent=1)))
|
2012-10-17 09:06:26 +00:00
|
|
|
|
2012-10-23 19:09:51 +00:00
|
|
|
r = client.delete('/1.0/path/1')
|
2012-10-17 10:00:11 +00:00
|
|
|
assert r.status_code == 200
|
|
|
|
assert Comment(**json.loads(r.data)).deleted
|
2012-10-17 10:22:52 +00:00
|
|
|
|
2012-10-23 19:09:51 +00:00
|
|
|
assert self.get('/1.0/path/1').status_code == 200
|
|
|
|
assert self.get('/1.0/path/2').status_code == 200
|
2012-10-19 13:51:51 +00:00
|
|
|
|
|
|
|
def testPathVariations(self):
|
|
|
|
|
2012-10-23 19:09:51 +00:00
|
|
|
paths = ['/sub/path/', '/path.html', '/sub/path.html', '%2Fpath/%2F', '/']
|
2012-10-19 13:51:51 +00:00
|
|
|
|
|
|
|
for path in paths:
|
2012-10-23 19:09:51 +00:00
|
|
|
assert self.post('/1.0/' + path + '/new',
|
2012-10-19 13:51:51 +00:00
|
|
|
data=json.dumps(comment(text='...'))).status_code == 201
|
|
|
|
|
|
|
|
for path in paths:
|
2012-10-23 19:09:51 +00:00
|
|
|
assert self.get('/1.0/' + path)
|
|
|
|
assert self.get('/1.0/' + path + '/1')
|
2012-10-23 14:12:36 +00:00
|
|
|
|
|
|
|
def testDeleteAndCreateByDifferentUsersButSamePostId(self):
|
|
|
|
|
|
|
|
mallory = Client(self.app, Response)
|
2012-10-23 19:09:51 +00:00
|
|
|
mallory.post('/1.0/path/new', data=json.dumps(comment(text='Foo')))
|
|
|
|
mallory.delete('/1.0/path/1')
|
2012-10-23 14:12:36 +00:00
|
|
|
|
|
|
|
bob = Client(self.app, Response)
|
2012-10-23 19:09:51 +00:00
|
|
|
bob.post('/1.0/path/new', data=json.dumps(comment(text='Bar')))
|
2012-10-23 14:12:36 +00:00
|
|
|
|
2012-10-23 19:09:51 +00:00
|
|
|
assert mallory.delete('/1.0/path/1').status_code == 403
|
|
|
|
assert bob.delete('/1.0/path/1').status_code == 200
|