2012-10-16 20:49:43 +00:00
|
|
|
|
2013-09-02 12:44:53 +00:00
|
|
|
import json
|
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
|
|
|
|
|
2013-09-02 12:44:53 +00:00
|
|
|
from isso import Isso
|
2012-10-16 20:49:43 +00:00
|
|
|
from isso.models import Comment
|
|
|
|
|
|
|
|
|
|
|
|
def comment(**kw):
|
2013-09-02 12:44:53 +00:00
|
|
|
return Comment.fromjson(Isso.dumps(kw))
|
2012-10-16 20:49:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestComments(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
fd, self.path = tempfile.mkstemp()
|
2013-09-02 12:44:53 +00:00
|
|
|
self.app = Isso(self.path, '...', '...', 15*60)
|
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):
|
|
|
|
|
2013-09-02 12:44:53 +00:00
|
|
|
self.post('/new?uri=%2Fpath%2F', data=Isso.dumps(comment(text='Lorem ipsum ...')))
|
|
|
|
r = self.get('/?uri=%2Fpath%2F&id=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):
|
|
|
|
|
2013-09-02 12:44:53 +00:00
|
|
|
rv = self.post('/new?uri=%2Fpath%2F', data=Isso.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
|
2013-09-02 12:44:53 +00:00
|
|
|
assert c.text == '<p>Lorem ipsum ...</p>\n'
|
2012-10-16 20:49:43 +00:00
|
|
|
|
|
|
|
def testCreateAndGetMultiple(self):
|
|
|
|
|
2012-10-21 20:37:39 +00:00
|
|
|
for i in range(20):
|
2013-09-02 12:44:53 +00:00
|
|
|
self.post('/new?uri=%2Fpath%2F', data=Isso.dumps(comment(text='Spam')))
|
2012-10-16 20:49:43 +00:00
|
|
|
|
2013-09-02 12:44:53 +00:00
|
|
|
r = self.get('/?uri=%2Fpath%2F')
|
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):
|
|
|
|
|
2013-09-02 12:44:53 +00:00
|
|
|
assert self.get('/?uri=%2Fpath%2F&id=123').status_code == 404
|
|
|
|
assert self.get('/?uri=%2Fpath%2Fspam%2F&id=123').status_code == 404
|
|
|
|
assert self.get('/?uri=?uri=%foo%2F').status_code == 404
|
2012-10-17 09:06:26 +00:00
|
|
|
|
|
|
|
def testUpdate(self):
|
|
|
|
|
2013-09-02 12:44:53 +00:00
|
|
|
self.post('/new?uri=%2Fpath%2F', data=Isso.dumps(comment(text='Lorem ipsum ...')))
|
|
|
|
self.put('/?uri=%2Fpath%2F&id=1', data=Isso.dumps(comment(
|
2012-10-17 09:06:26 +00:00
|
|
|
text='Hello World', author='me', website='http://example.com/')))
|
|
|
|
|
2013-09-02 12:44:53 +00:00
|
|
|
r = self.get('/?uri=%2Fpath%2F&id=1&plain=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):
|
|
|
|
|
2013-09-02 12:44:53 +00:00
|
|
|
self.post('/new?uri=%2Fpath%2F', data=Isso.dumps(comment(text='Lorem ipsum ...')))
|
|
|
|
r = self.delete('/?uri=%2Fpath%2F&id=1')
|
2012-10-17 10:22:52 +00:00
|
|
|
assert r.status_code == 200
|
|
|
|
assert json.loads(r.data) == None
|
2013-09-02 12:44:53 +00:00
|
|
|
assert self.get('/?uri=%2Fpath%2F&id=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)
|
2013-09-02 12:44:53 +00:00
|
|
|
client.post('/new?uri=%2Fpath%2F', data=Isso.dumps(comment(text='First')))
|
|
|
|
client.post('/new?uri=%2Fpath%2F', data=Isso.dumps(comment(text='First', parent=1)))
|
2012-10-17 09:06:26 +00:00
|
|
|
|
2013-09-02 12:44:53 +00:00
|
|
|
r = client.delete('/?uri=%2Fpath%2F&id=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
|
|
|
|
2013-09-02 12:44:53 +00:00
|
|
|
assert self.get('/?uri=%2Fpath%2F&id=1').status_code == 200
|
|
|
|
assert self.get('/?uri=%2Fpath%2F&id=2').status_code == 200
|
2012-10-19 13:51:51 +00:00
|
|
|
|
|
|
|
def testPathVariations(self):
|
|
|
|
|
2013-09-02 12:44:53 +00:00
|
|
|
paths = ['/sub/path/', '/path.html', '/sub/path.html', 'path', '/']
|
2012-10-19 13:51:51 +00:00
|
|
|
|
|
|
|
for path in paths:
|
2013-09-02 12:44:53 +00:00
|
|
|
assert self.post('/new?' + urllib.urlencode({'uri': path}),
|
|
|
|
data=Isso.dumps(comment(text='...'))).status_code == 201
|
2012-10-19 13:51:51 +00:00
|
|
|
|
|
|
|
for path in paths:
|
2013-09-02 12:44:53 +00:00
|
|
|
assert self.get('/?' + urllib.urlencode({'uri': path})).status_code == 200
|
|
|
|
assert self.get('/?' + urllib.urlencode({'uri': path, id: 1})).status_code == 200
|
2012-10-23 14:12:36 +00:00
|
|
|
|
|
|
|
def testDeleteAndCreateByDifferentUsersButSamePostId(self):
|
|
|
|
|
|
|
|
mallory = Client(self.app, Response)
|
2013-09-02 12:44:53 +00:00
|
|
|
mallory.post('/new?uri=%2Fpath%2F', data=Isso.dumps(comment(text='Foo')))
|
|
|
|
mallory.delete('/?uri=%2Fpath%2F&id=1')
|
2012-10-23 14:12:36 +00:00
|
|
|
|
|
|
|
bob = Client(self.app, Response)
|
2013-09-02 12:44:53 +00:00
|
|
|
bob.post('/new?uri=%2Fpath%2F', data=Isso.dumps(comment(text='Bar')))
|
2012-10-23 14:12:36 +00:00
|
|
|
|
2013-09-02 12:44:53 +00:00
|
|
|
assert mallory.delete('/?uri=%2Fpath%2F&id=1').status_code == 403
|
|
|
|
assert bob.delete('/?uri=%2Fpath%2F&id=1').status_code == 200
|