|
|
|
@ -5,7 +5,11 @@ from __future__ import unicode_literals
|
|
|
|
|
import os
|
|
|
|
|
import json
|
|
|
|
|
import tempfile
|
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
import unittest2 as unittest
|
|
|
|
|
except ImportError:
|
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
from urllib.parse import urlencode
|
|
|
|
@ -49,36 +53,36 @@ class TestComments(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Lorem ipsum ...'}))
|
|
|
|
|
r = self.get('/id/1')
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
|
|
rv = loads(r.data)
|
|
|
|
|
|
|
|
|
|
assert rv['id'] == 1
|
|
|
|
|
assert rv['text'] == '<p>Lorem ipsum ...</p>'
|
|
|
|
|
self.assertEqual(rv['id'], 1)
|
|
|
|
|
self.assertEqual(rv['text'], '<p>Lorem ipsum ...</p>')
|
|
|
|
|
|
|
|
|
|
def testCreate(self):
|
|
|
|
|
|
|
|
|
|
rv = self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Lorem ipsum ...'}))
|
|
|
|
|
|
|
|
|
|
assert rv.status_code == 201
|
|
|
|
|
assert any(filter(lambda header: header[0] == 'Set-Cookie', rv.headers))
|
|
|
|
|
self.assertEqual(rv.status_code, 201)
|
|
|
|
|
self.assertIn("Set-Cookie", rv.headers)
|
|
|
|
|
|
|
|
|
|
rv = loads(rv.data)
|
|
|
|
|
|
|
|
|
|
assert rv["mode"] == 1
|
|
|
|
|
assert rv["text"] == '<p>Lorem ipsum ...</p>'
|
|
|
|
|
self.assertEqual(rv["mode"], 1)
|
|
|
|
|
self.assertEqual(rv["text"], '<p>Lorem ipsum ...</p>')
|
|
|
|
|
|
|
|
|
|
def textCreateWithNonAsciiText(self):
|
|
|
|
|
|
|
|
|
|
rv = self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Здравствуй, мир!'}))
|
|
|
|
|
|
|
|
|
|
assert rv.status_code == 201
|
|
|
|
|
assert any(filter(lambda header: header[0] == 'Set-Cookie', rv.headers))
|
|
|
|
|
self.assertEqual(rv.status_code, 201)
|
|
|
|
|
self.assertEqual(any(filter(lambda header: header[0], 'Set-Cookie', rv.headers)))
|
|
|
|
|
|
|
|
|
|
rv = loads(rv.data)
|
|
|
|
|
|
|
|
|
|
assert rv["mode"] == 1
|
|
|
|
|
assert rv["text"] == '<p>Здравствуй, мир!</p>'
|
|
|
|
|
self.assertEqual(rv["mode"], 1)
|
|
|
|
|
self.assertEqual(rv["text"], '<p>Здравствуй, мир!</p>')
|
|
|
|
|
|
|
|
|
|
def testCreateMultiple(self):
|
|
|
|
|
|
|
|
|
@ -86,9 +90,9 @@ class TestComments(unittest.TestCase):
|
|
|
|
|
b = self.post('/new?uri=test', data=json.dumps({'text': '...'}))
|
|
|
|
|
c = self.post('/new?uri=test', data=json.dumps({'text': '...'}))
|
|
|
|
|
|
|
|
|
|
assert loads(a.data)["id"] == 1
|
|
|
|
|
assert loads(b.data)["id"] == 2
|
|
|
|
|
assert loads(c.data)["id"] == 3
|
|
|
|
|
self.assertEqual(loads(a.data)["id"], 1)
|
|
|
|
|
self.assertEqual(loads(b.data)["id"], 2)
|
|
|
|
|
self.assertEqual(loads(c.data)["id"], 3)
|
|
|
|
|
|
|
|
|
|
def testCreateAndGetMultiple(self):
|
|
|
|
|
|
|
|
|
@ -96,22 +100,23 @@ class TestComments(unittest.TestCase):
|
|
|
|
|
self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Spam'}))
|
|
|
|
|
|
|
|
|
|
r = self.get('/?uri=%2Fpath%2F')
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
|
|
rv = loads(r.data)
|
|
|
|
|
assert len(rv) == 20
|
|
|
|
|
self.assertEqual(len(rv), 20)
|
|
|
|
|
|
|
|
|
|
def testVerifyFields(self):
|
|
|
|
|
|
|
|
|
|
def testCreateBlank(self):
|
|
|
|
|
rv = self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': ''}))
|
|
|
|
|
assert rv.status_code == 400
|
|
|
|
|
self.assertEqual(rv.status_code, 400)
|
|
|
|
|
rv = self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': "\n\n\n"}))
|
|
|
|
|
assert rv.status_code == 400
|
|
|
|
|
self.assertEqual(rv.status_code, 400)
|
|
|
|
|
|
|
|
|
|
def testGetInvalid(self):
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
self.assertEqual(self.get('/?uri=%2Fpath%2F&id=123').status_code, 404)
|
|
|
|
|
self.assertEqual(self.get('/?uri=%2Fpath%2Fspam%2F&id=123').status_code, 404)
|
|
|
|
|
self.assertEqual(self.get('/?uri=?uri=%foo%2F').status_code, 404)
|
|
|
|
|
|
|
|
|
|
def testUpdate(self):
|
|
|
|
|
|
|
|
|
@ -120,21 +125,21 @@ class TestComments(unittest.TestCase):
|
|
|
|
|
'text': 'Hello World', 'author': 'me', 'website': 'http://example.com/'}))
|
|
|
|
|
|
|
|
|
|
r = self.get('/id/1?plain=1')
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
|
|
rv = loads(r.data)
|
|
|
|
|
assert rv['text'] == 'Hello World'
|
|
|
|
|
assert rv['author'] == 'me'
|
|
|
|
|
assert rv['website'] == 'http://example.com/'
|
|
|
|
|
assert 'modified' in rv
|
|
|
|
|
self.assertEqual(rv['text'], 'Hello World')
|
|
|
|
|
self.assertEqual(rv['author'], 'me')
|
|
|
|
|
self.assertEqual(rv['website'], 'http://example.com/')
|
|
|
|
|
self.assertIn('modified', rv)
|
|
|
|
|
|
|
|
|
|
def testDelete(self):
|
|
|
|
|
|
|
|
|
|
self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Lorem ipsum ...'}))
|
|
|
|
|
r = self.delete('/id/1')
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
|
assert loads(r.data) == None
|
|
|
|
|
assert self.get('/id/1').status_code == 404
|
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
self.assertEqual(loads(r.data), None)
|
|
|
|
|
self.assertEqual(self.get('/id/1').status_code, 404)
|
|
|
|
|
|
|
|
|
|
def testDeleteWithReference(self):
|
|
|
|
|
|
|
|
|
@ -143,16 +148,16 @@ class TestComments(unittest.TestCase):
|
|
|
|
|
client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'First', 'parent': 1}))
|
|
|
|
|
|
|
|
|
|
r = client.delete('/id/1')
|
|
|
|
|
assert r.status_code == 200
|
|
|
|
|
assert loads(r.data)['mode'] == 4
|
|
|
|
|
assert '/path/' in self.app.db.threads
|
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
self.assertEqual(loads(r.data)['mode'], 4)
|
|
|
|
|
self.assertIn('/path/', self.app.db.threads)
|
|
|
|
|
|
|
|
|
|
assert self.get('/?uri=%2Fpath%2F&id=1').status_code == 200
|
|
|
|
|
assert self.get('/?uri=%2Fpath%2F&id=2').status_code == 200
|
|
|
|
|
self.assertEqual(self.get('/?uri=%2Fpath%2F&id=1').status_code, 200)
|
|
|
|
|
self.assertEqual(self.get('/?uri=%2Fpath%2F&id=2').status_code, 200)
|
|
|
|
|
|
|
|
|
|
r = client.delete('/id/2')
|
|
|
|
|
assert self.get('/?uri=%2Fpath%2F').status_code == 404
|
|
|
|
|
assert '/path/' not in self.app.db.threads
|
|
|
|
|
self.assertEqual(self.get('/?uri=%2Fpath%2F').status_code, 404)
|
|
|
|
|
self.assertNotIn('/path/', self.app.db.threads)
|
|
|
|
|
|
|
|
|
|
def testDeleteWithMultipleReferences(self):
|
|
|
|
|
"""
|
|
|
|
@ -174,27 +179,27 @@ class TestComments(unittest.TestCase):
|
|
|
|
|
client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': '...'}))
|
|
|
|
|
|
|
|
|
|
client.delete('/id/1')
|
|
|
|
|
assert self.get('/?uri=%2Fpath%2F').status_code == 200
|
|
|
|
|
self.assertEqual(self.get('/?uri=%2Fpath%2F').status_code, 200)
|
|
|
|
|
client.delete('/id/2')
|
|
|
|
|
assert self.get('/?uri=%2Fpath%2F').status_code == 200
|
|
|
|
|
self.assertEqual(self.get('/?uri=%2Fpath%2F').status_code, 200)
|
|
|
|
|
client.delete('/id/3')
|
|
|
|
|
assert self.get('/?uri=%2Fpath%2F').status_code == 200
|
|
|
|
|
self.assertEqual(self.get('/?uri=%2Fpath%2F').status_code, 200)
|
|
|
|
|
client.delete('/id/4')
|
|
|
|
|
assert self.get('/?uri=%2Fpath%2F').status_code == 200
|
|
|
|
|
self.assertEqual(self.get('/?uri=%2Fpath%2F').status_code, 200)
|
|
|
|
|
client.delete('/id/5')
|
|
|
|
|
assert self.get('/?uri=%2Fpath%2F').status_code == 404
|
|
|
|
|
self.assertEqual(self.get('/?uri=%2Fpath%2F').status_code, 404)
|
|
|
|
|
|
|
|
|
|
def testPathVariations(self):
|
|
|
|
|
|
|
|
|
|
paths = ['/sub/path/', '/path.html', '/sub/path.html', 'path', '/']
|
|
|
|
|
|
|
|
|
|
for path in paths:
|
|
|
|
|
assert self.post('/new?' + urlencode({'uri': path}),
|
|
|
|
|
data=json.dumps({'text': '...'})).status_code == 201
|
|
|
|
|
self.assertEqual(self.post('/new?' + urlencode({'uri': path}),
|
|
|
|
|
data=json.dumps({'text': '...'})).status_code, 201)
|
|
|
|
|
|
|
|
|
|
for i, path in enumerate(paths):
|
|
|
|
|
assert self.get('/?' + urlencode({'uri': path})).status_code == 200
|
|
|
|
|
assert self.get('/id/%i' % (i + 1)).status_code == 200
|
|
|
|
|
self.assertEqual(self.get('/?' + urlencode({'uri': path})).status_code, 200)
|
|
|
|
|
self.assertEqual(self.get('/id/%i' % (i + 1)).status_code, 200)
|
|
|
|
|
|
|
|
|
|
def testDeleteAndCreateByDifferentUsersButSamePostId(self):
|
|
|
|
|
|
|
|
|
@ -205,8 +210,8 @@ class TestComments(unittest.TestCase):
|
|
|
|
|
bob = JSONClient(self.app, Response)
|
|
|
|
|
bob.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Bar'}))
|
|
|
|
|
|
|
|
|
|
assert mallory.delete('/id/1').status_code == 403
|
|
|
|
|
assert bob.delete('/id/1').status_code == 200
|
|
|
|
|
self.assertEqual(mallory.delete('/id/1').status_code, 403)
|
|
|
|
|
self.assertEqual(bob.delete('/id/1').status_code, 200)
|
|
|
|
|
|
|
|
|
|
def testHash(self):
|
|
|
|
|
|
|
|
|
@ -214,65 +219,64 @@ class TestComments(unittest.TestCase):
|
|
|
|
|
b = self.post('/new?uri=%2Fpath%2F', data=json.dumps({"text": "Bbb"}))
|
|
|
|
|
c = self.post('/new?uri=%2Fpath%2F', data=json.dumps({"text": "Ccc", "email": "..."}))
|
|
|
|
|
|
|
|
|
|
assert a.status_code == b.status_code == c.status_code == 201
|
|
|
|
|
a = loads(a.data)
|
|
|
|
|
b = loads(b.data)
|
|
|
|
|
c = loads(c.data)
|
|
|
|
|
|
|
|
|
|
assert isinstance(int(a['hash'], 16), int)
|
|
|
|
|
assert a['hash'] != '192.168.1.1'
|
|
|
|
|
assert a['hash'] == b['hash']
|
|
|
|
|
assert a['hash'] != c['hash']
|
|
|
|
|
self.assertIsInstance(int(a['hash'], 16), int)
|
|
|
|
|
self.assertNotEqual(a['hash'], '192.168.1.1')
|
|
|
|
|
self.assertEqual(a['hash'], b['hash'])
|
|
|
|
|
self.assertNotEqual(a['hash'], c['hash'])
|
|
|
|
|
|
|
|
|
|
def testVisibleFields(self):
|
|
|
|
|
|
|
|
|
|
rv = self.post('/new?uri=%2Fpath%2F', data=json.dumps({"text": "..."}))
|
|
|
|
|
assert rv.status_code == 201
|
|
|
|
|
self.assertEqual(rv.status_code, 201)
|
|
|
|
|
|
|
|
|
|
rv = loads(rv.data)
|
|
|
|
|
|
|
|
|
|
for key in comments.API.FIELDS:
|
|
|
|
|
rv.pop(key)
|
|
|
|
|
|
|
|
|
|
assert not any(rv.keys())
|
|
|
|
|
self.assertListEqual(list(rv.keys()), [])
|
|
|
|
|
|
|
|
|
|
def testCounts(self):
|
|
|
|
|
|
|
|
|
|
assert self.get('/count?uri=%2Fpath%2F').status_code == 404
|
|
|
|
|
self.assertEqual(self.get('/count?uri=%2Fpath%2F').status_code, 404)
|
|
|
|
|
self.post('/new?uri=%2Fpath%2F', data=json.dumps({"text": "..."}))
|
|
|
|
|
|
|
|
|
|
rv = self.get('/count?uri=%2Fpath%2F')
|
|
|
|
|
assert rv.status_code == 200
|
|
|
|
|
assert loads(rv.data) == 1
|
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
|
self.assertEqual(loads(rv.data), 1)
|
|
|
|
|
|
|
|
|
|
for x in range(3):
|
|
|
|
|
self.post('/new?uri=%2Fpath%2F', data=json.dumps({"text": "..."}))
|
|
|
|
|
|
|
|
|
|
rv = self.get('/count?uri=%2Fpath%2F')
|
|
|
|
|
assert rv.status_code == 200
|
|
|
|
|
assert loads(rv.data) == 4
|
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
|
self.assertEqual(loads(rv.data), 4)
|
|
|
|
|
|
|
|
|
|
for x in range(4):
|
|
|
|
|
self.delete('/id/%i' % (x + 1))
|
|
|
|
|
|
|
|
|
|
rv = self.get('/count?uri=%2Fpath%2F')
|
|
|
|
|
assert rv.status_code == 404
|
|
|
|
|
self.assertEqual(rv.status_code, 404)
|
|
|
|
|
|
|
|
|
|
def testModify(self):
|
|
|
|
|
self.post('/new?uri=test', data=json.dumps({"text": "Tpyo"}))
|
|
|
|
|
|
|
|
|
|
self.put('/id/1', data=json.dumps({"text": "Tyop"}))
|
|
|
|
|
assert loads(self.get('/id/1').data)["text"] == "<p>Tyop</p>"
|
|
|
|
|
self.assertEqual(loads(self.get('/id/1').data)["text"], "<p>Tyop</p>")
|
|
|
|
|
|
|
|
|
|
self.put('/id/1', data=json.dumps({"text": "Typo"}))
|
|
|
|
|
assert loads(self.get('/id/1').data)["text"] == "<p>Typo</p>"
|
|
|
|
|
self.assertEqual(loads(self.get('/id/1').data)["text"], "<p>Typo</p>")
|
|
|
|
|
|
|
|
|
|
def testDeleteCommentRemovesThread(self):
|
|
|
|
|
|
|
|
|
|
rv = self.client.post('/new?uri=%2F', data=json.dumps({"text": "..."}))
|
|
|
|
|
assert '/' in self.app.db.threads
|
|
|
|
|
self.assertIn('/', self.app.db.threads)
|
|
|
|
|
self.client.delete('/id/1')
|
|
|
|
|
assert '/' not in self.app.db.threads
|
|
|
|
|
self.assertNotIn('/', self.app.db.threads)
|
|
|
|
|
|
|
|
|
|
def testCSRF(self):
|
|
|
|
|
|
|
|
|
@ -282,17 +286,17 @@ class TestComments(unittest.TestCase):
|
|
|
|
|
self.post('/new?uri=%2F', data=json.dumps({"text": "..."}))
|
|
|
|
|
|
|
|
|
|
# no header is fine (default for XHR)
|
|
|
|
|
assert self.post('/id/1/dislike', content_type="").status_code == 200
|
|
|
|
|
self.assertEqual(self.post('/id/1/dislike', content_type="").status_code, 200)
|
|
|
|
|
|
|
|
|
|
# x-www-form-urlencoded is definitely not RESTful
|
|
|
|
|
assert self.post('/id/1/dislike', content_type=form).status_code == 403
|
|
|
|
|
assert self.post('/new?uri=%2F', data=json.dumps({"text": "..."}),
|
|
|
|
|
content_type=form).status_code == 403
|
|
|
|
|
self.assertEqual(self.post('/id/1/dislike', content_type=form).status_code, 403)
|
|
|
|
|
self.assertEqual(self.post('/new?uri=%2F', data=json.dumps({"text": "..."}),
|
|
|
|
|
content_type=form).status_code, 403)
|
|
|
|
|
# just for the record
|
|
|
|
|
assert self.post('/id/1/dislike', content_type=js).status_code == 200
|
|
|
|
|
self.assertEqual(self.post('/id/1/dislike', content_type=js).status_code, 200)
|
|
|
|
|
|
|
|
|
|
def testCheckIP(self):
|
|
|
|
|
assert self.get('/check-ip').data.decode("utf-8") == '192.168.1.0'
|
|
|
|
|
self.assertEqual(self.get('/check-ip').data.decode("utf-8"), '192.168.1.0')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestModeratedComments(unittest.TestCase):
|
|
|
|
@ -317,13 +321,13 @@ class TestModeratedComments(unittest.TestCase):
|
|
|
|
|
def testAddComment(self):
|
|
|
|
|
|
|
|
|
|
rv = self.client.post('/new?uri=test', data=json.dumps({"text": "..."}))
|
|
|
|
|
assert rv.status_code == 202
|
|
|
|
|
self.assertEqual(rv.status_code, 202)
|
|
|
|
|
|
|
|
|
|
assert self.client.get('/id/1').status_code == 200
|
|
|
|
|
assert self.client.get('/?uri=test').status_code == 404
|
|
|
|
|
self.assertEqual(self.client.get('/id/1').status_code, 200)
|
|
|
|
|
self.assertEqual(self.client.get('/?uri=test').status_code, 404)
|
|
|
|
|
|
|
|
|
|
self.app.db.comments.activate(1)
|
|
|
|
|
assert self.client.get('/?uri=test').status_code == 200
|
|
|
|
|
self.assertEqual(self.client.get('/?uri=test').status_code, 200)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestPurgeComments(unittest.TestCase):
|
|
|
|
@ -346,13 +350,13 @@ class TestPurgeComments(unittest.TestCase):
|
|
|
|
|
self.client.post('/new?uri=test', data=json.dumps({"text": "..."}))
|
|
|
|
|
self.app.db.comments.activate(1)
|
|
|
|
|
self.app.db.comments.purge(0)
|
|
|
|
|
assert self.client.get('/?uri=test').status_code == 200
|
|
|
|
|
self.assertEqual(self.client.get('/?uri=test').status_code, 200)
|
|
|
|
|
|
|
|
|
|
def testPurgeWorks(self):
|
|
|
|
|
self.client.post('/new?uri=test', data=json.dumps({"text": "..."}))
|
|
|
|
|
self.app.db.comments.purge(0)
|
|
|
|
|
assert self.client.get('/id/1').status_code == 404
|
|
|
|
|
self.assertEqual(self.client.get('/id/1').status_code, 404)
|
|
|
|
|
|
|
|
|
|
self.client.post('/new?uri=test', data=json.dumps({"text": "..."}))
|
|
|
|
|
self.app.db.comments.purge(3600)
|
|
|
|
|
assert self.client.get('/id/1').status_code == 200
|
|
|
|
|
self.assertEqual(self.client.get('/id/1').status_code, 200)
|
|
|
|
|