2013-11-05 11:35:17 +00:00
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
|
2013-09-08 11:02:25 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-09-06 13:56:43 +00:00
|
|
|
import os
|
2013-09-02 12:44:53 +00:00
|
|
|
import json
|
2012-10-16 20:49:43 +00:00
|
|
|
import tempfile
|
|
|
|
import unittest
|
|
|
|
|
2013-10-09 14:28:54 +00:00
|
|
|
try:
|
|
|
|
from urllib.parse import urlencode
|
|
|
|
except ImportError:
|
|
|
|
from urllib import urlencode
|
|
|
|
|
2012-10-16 20:49:43 +00:00
|
|
|
from werkzeug.test import Client
|
|
|
|
from werkzeug.wrappers import Response
|
|
|
|
|
2013-11-09 14:18:21 +00:00
|
|
|
from isso import Isso, core
|
2013-10-24 12:34:45 +00:00
|
|
|
from isso.utils import http
|
2013-11-08 17:47:30 +00:00
|
|
|
from isso.views import comments
|
2012-10-16 20:49:43 +00:00
|
|
|
|
2013-11-13 13:20:58 +00:00
|
|
|
from fixtures import curl, loads, FakeIP
|
|
|
|
http.curl = curl
|
2012-10-16 20:49:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestComments(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
fd, self.path = tempfile.mkstemp()
|
2013-10-06 16:37:05 +00:00
|
|
|
conf = core.Config.load(None)
|
|
|
|
conf.set("general", "dbpath", self.path)
|
|
|
|
conf.set("guard", "enabled", "off")
|
2013-09-05 21:09:56 +00:00
|
|
|
|
2013-10-06 16:37:05 +00:00
|
|
|
class App(Isso, core.Mixin):
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.app = App(conf)
|
2013-11-13 13:20:58 +00:00
|
|
|
self.app.wsgi_app = FakeIP(self.app.wsgi_app, "192.168.1.1")
|
2012-10-16 20:49:43 +00:00
|
|
|
|
2012-10-17 20:20:02 +00:00
|
|
|
self.client = Client(self.app, Response)
|
2013-09-05 21:09:56 +00:00
|
|
|
self.get = self.client.get
|
|
|
|
self.put = self.client.put
|
|
|
|
self.post = self.client.post
|
|
|
|
self.delete = self.client.delete
|
2012-10-17 20:20:02 +00:00
|
|
|
|
2013-09-06 13:56:43 +00:00
|
|
|
def tearDown(self):
|
|
|
|
os.unlink(self.path)
|
|
|
|
|
2012-10-16 20:49:43 +00:00
|
|
|
def testGet(self):
|
|
|
|
|
2013-09-05 21:09:56 +00:00
|
|
|
self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Lorem ipsum ...'}))
|
2013-09-19 16:30:46 +00:00
|
|
|
r = self.get('/id/1')
|
2012-10-16 20:49:43 +00:00
|
|
|
assert r.status_code == 200
|
|
|
|
|
2013-10-09 14:28:54 +00:00
|
|
|
rv = loads(r.data)
|
2012-10-16 20:49:43 +00:00
|
|
|
|
|
|
|
assert rv['id'] == 1
|
2013-09-09 12:52:32 +00:00
|
|
|
assert rv['text'] == '<p>Lorem ipsum ...</p>\n'
|
2012-10-16 20:49:43 +00:00
|
|
|
|
|
|
|
def testCreate(self):
|
|
|
|
|
2013-09-05 21:09:56 +00:00
|
|
|
rv = self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Lorem ipsum ...'}))
|
2012-10-16 20:49:43 +00:00
|
|
|
|
|
|
|
assert rv.status_code == 201
|
2013-10-09 14:28:54 +00:00
|
|
|
assert any(filter(lambda header: header[0] == 'Set-Cookie', rv.headers))
|
2012-10-16 20:49:43 +00:00
|
|
|
|
2013-10-09 14:28:54 +00:00
|
|
|
rv = loads(rv.data)
|
2012-10-16 20:49:43 +00:00
|
|
|
|
2013-09-08 11:02:25 +00:00
|
|
|
assert rv["mode"] == 1
|
|
|
|
assert rv["text"] == '<p>Lorem ipsum ...</p>\n'
|
2012-10-16 20:49:43 +00:00
|
|
|
|
2013-11-05 11:35:17 +00:00
|
|
|
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))
|
|
|
|
|
|
|
|
rv = loads(rv.data)
|
|
|
|
|
|
|
|
assert rv["mode"] == 1
|
|
|
|
assert rv["text"] == '<p>Здравствуй, мир!</p>\n'
|
|
|
|
|
2013-09-19 16:30:46 +00:00
|
|
|
def testCreateMultiple(self):
|
|
|
|
|
|
|
|
a = self.post('/new?uri=test', data=json.dumps({'text': '...'}))
|
|
|
|
b = self.post('/new?uri=test', data=json.dumps({'text': '...'}))
|
|
|
|
c = self.post('/new?uri=test', data=json.dumps({'text': '...'}))
|
|
|
|
|
2013-10-09 14:28:54 +00:00
|
|
|
assert loads(a.data)["id"] == 1
|
|
|
|
assert loads(b.data)["id"] == 2
|
|
|
|
assert loads(c.data)["id"] == 3
|
2013-09-19 16:30:46 +00:00
|
|
|
|
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-05 21:09:56 +00:00
|
|
|
self.post('/new?uri=%2Fpath%2F', data=json.dumps({'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
|
|
|
|
|
2013-10-09 14:28:54 +00:00
|
|
|
rv = loads(r.data)
|
2012-10-16 20:49:43 +00:00
|
|
|
assert len(rv) == 20
|
2012-10-16 20:56:21 +00:00
|
|
|
|
2013-12-03 23:35:09 +00:00
|
|
|
def testCreateBlank(self):
|
|
|
|
rv = self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': ''}))
|
|
|
|
assert rv.status_code == 400
|
|
|
|
rv = self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': "\n\n\n"}))
|
|
|
|
assert rv.status_code == 400
|
|
|
|
|
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-05 21:09:56 +00:00
|
|
|
self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Lorem ipsum ...'}))
|
2013-09-19 16:30:46 +00:00
|
|
|
self.put('/id/1', data=json.dumps({
|
2013-09-05 21:09:56 +00:00
|
|
|
'text': 'Hello World', 'author': 'me', 'website': 'http://example.com/'}))
|
2012-10-17 09:06:26 +00:00
|
|
|
|
2013-09-19 16:30:46 +00:00
|
|
|
r = self.get('/id/1?plain=1')
|
2012-10-17 09:06:26 +00:00
|
|
|
assert r.status_code == 200
|
|
|
|
|
2013-10-09 14:28:54 +00:00
|
|
|
rv = loads(r.data)
|
2012-10-17 09:06:26 +00:00
|
|
|
assert rv['text'] == 'Hello World'
|
|
|
|
assert rv['author'] == 'me'
|
|
|
|
assert rv['website'] == 'http://example.com/'
|
|
|
|
assert 'modified' in rv
|
|
|
|
|
|
|
|
def testDelete(self):
|
|
|
|
|
2013-09-05 21:09:56 +00:00
|
|
|
self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Lorem ipsum ...'}))
|
2013-09-19 16:30:46 +00:00
|
|
|
r = self.delete('/id/1')
|
2012-10-17 10:22:52 +00:00
|
|
|
assert r.status_code == 200
|
2013-10-09 14:28:54 +00:00
|
|
|
assert loads(r.data) == None
|
2013-09-19 16:30:46 +00:00
|
|
|
assert self.get('/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-05 21:09:56 +00:00
|
|
|
client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'First'}))
|
|
|
|
client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'First', 'parent': 1}))
|
2012-10-17 09:06:26 +00:00
|
|
|
|
2013-09-19 16:30:46 +00:00
|
|
|
r = client.delete('/id/1')
|
2012-10-17 10:00:11 +00:00
|
|
|
assert r.status_code == 200
|
2013-10-09 14:28:54 +00:00
|
|
|
assert loads(r.data)['mode'] == 4
|
2013-10-31 10:08:00 +00:00
|
|
|
assert '/path/' in self.app.db.threads
|
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
|
|
|
|
2013-09-19 16:30:46 +00:00
|
|
|
r = client.delete('/id/2')
|
2013-09-08 15:21:33 +00:00
|
|
|
assert self.get('/?uri=%2Fpath%2F').status_code == 404
|
2013-10-31 10:08:00 +00:00
|
|
|
assert '/path/' not in self.app.db.threads
|
2013-09-08 15:21:33 +00:00
|
|
|
|
|
|
|
def testDeleteWithMultipleReferences(self):
|
|
|
|
"""
|
|
|
|
[ comment 1 ]
|
|
|
|
|
|
|
|
|
--- [ comment 2, ref 1 ]
|
|
|
|
|
|
|
|
|
--- [ comment 3, ref 2 ]
|
|
|
|
|
|
|
|
|
--- [ comment 4, ref 2 ]
|
|
|
|
[ comment 5 ]
|
|
|
|
"""
|
|
|
|
client = Client(self.app, Response)
|
|
|
|
|
|
|
|
client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'First'}))
|
|
|
|
client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Second', 'parent': 1}))
|
|
|
|
client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Third 1', 'parent': 2}))
|
|
|
|
client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Third 2', 'parent': 2}))
|
|
|
|
client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': '...'}))
|
|
|
|
|
2013-09-19 16:30:46 +00:00
|
|
|
client.delete('/id/1')
|
2013-09-08 15:21:33 +00:00
|
|
|
assert self.get('/?uri=%2Fpath%2F').status_code == 200
|
2013-09-19 16:30:46 +00:00
|
|
|
client.delete('/id/2')
|
2013-09-08 15:21:33 +00:00
|
|
|
assert self.get('/?uri=%2Fpath%2F').status_code == 200
|
2013-09-19 16:30:46 +00:00
|
|
|
client.delete('/id/3')
|
2013-09-08 15:21:33 +00:00
|
|
|
assert self.get('/?uri=%2Fpath%2F').status_code == 200
|
2013-09-19 16:30:46 +00:00
|
|
|
client.delete('/id/4')
|
2013-09-08 15:21:33 +00:00
|
|
|
assert self.get('/?uri=%2Fpath%2F').status_code == 200
|
2013-09-19 16:30:46 +00:00
|
|
|
client.delete('/id/5')
|
2013-09-08 15:21:33 +00:00
|
|
|
assert self.get('/?uri=%2Fpath%2F').status_code == 404
|
|
|
|
|
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-10-09 14:28:54 +00:00
|
|
|
assert self.post('/new?' + urlencode({'uri': path}),
|
2013-09-05 21:09:56 +00:00
|
|
|
data=json.dumps({'text': '...'})).status_code == 201
|
2012-10-19 13:51:51 +00:00
|
|
|
|
2013-09-19 16:30:46 +00:00
|
|
|
for i, path in enumerate(paths):
|
2013-10-09 14:28:54 +00:00
|
|
|
assert self.get('/?' + urlencode({'uri': path})).status_code == 200
|
2013-09-19 16:30:46 +00:00
|
|
|
assert self.get('/id/%i' % (i + 1)).status_code == 200
|
2012-10-23 14:12:36 +00:00
|
|
|
|
|
|
|
def testDeleteAndCreateByDifferentUsersButSamePostId(self):
|
|
|
|
|
|
|
|
mallory = Client(self.app, Response)
|
2013-09-05 21:09:56 +00:00
|
|
|
mallory.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Foo'}))
|
2013-09-19 16:30:46 +00:00
|
|
|
mallory.delete('/id/1')
|
2012-10-23 14:12:36 +00:00
|
|
|
|
|
|
|
bob = Client(self.app, Response)
|
2013-09-05 21:09:56 +00:00
|
|
|
bob.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Bar'}))
|
2012-10-23 14:12:36 +00:00
|
|
|
|
2013-09-19 16:30:46 +00:00
|
|
|
assert mallory.delete('/id/1').status_code == 403
|
|
|
|
assert bob.delete('/id/1').status_code == 200
|
2013-09-05 21:09:56 +00:00
|
|
|
|
|
|
|
def testHash(self):
|
|
|
|
|
|
|
|
a = self.post('/new?uri=%2Fpath%2F', data=json.dumps({"text": "Aaa"}))
|
|
|
|
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
|
2013-10-09 14:28:54 +00:00
|
|
|
a = loads(a.data)
|
|
|
|
b = loads(b.data)
|
|
|
|
c = loads(c.data)
|
2013-09-05 21:09:56 +00:00
|
|
|
|
|
|
|
assert a['hash'] != '192.168.1.1'
|
|
|
|
assert a['hash'] == b['hash']
|
|
|
|
assert a['hash'] != c['hash']
|
2013-09-08 11:02:25 +00:00
|
|
|
|
|
|
|
def testVisibleFields(self):
|
|
|
|
|
|
|
|
rv = self.post('/new?uri=%2Fpath%2F', data=json.dumps({"text": "..."}))
|
|
|
|
assert rv.status_code == 201
|
|
|
|
|
2013-10-09 14:28:54 +00:00
|
|
|
rv = loads(rv.data)
|
2013-09-08 11:02:25 +00:00
|
|
|
|
2013-11-09 14:18:21 +00:00
|
|
|
for key in comments.API.FIELDS:
|
2013-09-08 11:02:25 +00:00
|
|
|
rv.pop(key)
|
|
|
|
|
2013-10-09 14:28:54 +00:00
|
|
|
assert not any(rv.keys())
|
2013-09-08 11:02:25 +00:00
|
|
|
|
2013-09-12 15:19:04 +00:00
|
|
|
def testCounts(self):
|
|
|
|
|
|
|
|
assert 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
|
2013-10-09 14:28:54 +00:00
|
|
|
assert loads(rv.data) == 1
|
2013-09-12 15:19:04 +00:00
|
|
|
|
|
|
|
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
|
2013-10-09 14:28:54 +00:00
|
|
|
assert loads(rv.data) == 4
|
2013-09-12 15:19:04 +00:00
|
|
|
|
|
|
|
for x in range(4):
|
2013-09-19 16:30:46 +00:00
|
|
|
self.delete('/id/%i' % (x + 1))
|
2013-09-12 15:19:04 +00:00
|
|
|
|
|
|
|
rv = self.get('/count?uri=%2Fpath%2F')
|
|
|
|
assert rv.status_code == 404
|
2013-10-03 16:12:59 +00:00
|
|
|
|
|
|
|
def testModify(self):
|
|
|
|
self.post('/new?uri=test', data=json.dumps({"text": "Tpyo"}))
|
|
|
|
|
|
|
|
self.put('/id/1', data=json.dumps({"text": "Tyop"}))
|
2013-10-09 14:28:54 +00:00
|
|
|
assert loads(self.get('/id/1').data)["text"] == "<p>Tyop</p>\n"
|
2013-10-03 16:12:59 +00:00
|
|
|
|
|
|
|
self.put('/id/1', data=json.dumps({"text": "Typo"}))
|
2013-10-09 14:28:54 +00:00
|
|
|
assert loads(self.get('/id/1').data)["text"] == "<p>Typo</p>\n"
|
2013-10-13 13:00:54 +00:00
|
|
|
|
2013-10-31 10:08:00 +00:00
|
|
|
def testDeleteCommentRemovesThread(self):
|
|
|
|
|
|
|
|
rv = self.client.post('/new?uri=%2F', data=json.dumps({"text": "..."}))
|
|
|
|
assert '/' in self.app.db.threads
|
|
|
|
self.client.delete('/id/1')
|
|
|
|
assert '/' not in self.app.db.threads
|
|
|
|
|
2013-10-13 13:00:54 +00:00
|
|
|
|
|
|
|
class TestModeratedComments(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
fd, self.path = tempfile.mkstemp()
|
|
|
|
conf = core.Config.load(None)
|
|
|
|
conf.set("general", "dbpath", self.path)
|
2013-10-24 12:16:14 +00:00
|
|
|
conf.set("moderation", "enabled", "true")
|
2013-10-13 13:00:54 +00:00
|
|
|
conf.set("guard", "enabled", "off")
|
|
|
|
|
|
|
|
class App(Isso, core.Mixin):
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.app = App(conf)
|
2013-11-13 13:20:58 +00:00
|
|
|
self.app.wsgi_app = FakeIP(self.app.wsgi_app, "192.168.1.1")
|
2013-10-13 13:00:54 +00:00
|
|
|
self.client = Client(self.app, Response)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
os.unlink(self.path)
|
|
|
|
|
|
|
|
def testAddComment(self):
|
|
|
|
|
|
|
|
rv = self.client.post('/new?uri=test', data=json.dumps({"text": "..."}))
|
|
|
|
assert rv.status_code == 202
|
|
|
|
|
|
|
|
assert self.client.get('/id/1').status_code == 200
|
|
|
|
assert self.client.get('/?uri=test').status_code == 404
|
|
|
|
|
|
|
|
self.app.db.comments.activate(1)
|
|
|
|
assert self.client.get('/?uri=test').status_code == 200
|
2013-10-24 12:16:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestPurgeComments(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
fd, self.path = tempfile.mkstemp()
|
|
|
|
conf = core.Config.load(None)
|
|
|
|
conf.set("general", "dbpath", self.path)
|
|
|
|
conf.set("moderation", "enabled", "true")
|
|
|
|
conf.set("guard", "enabled", "off")
|
|
|
|
|
|
|
|
class App(Isso, core.Mixin):
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.app = App(conf)
|
2013-11-13 13:20:58 +00:00
|
|
|
self.app.wsgi_app = FakeIP(self.app.wsgi_app, "192.168.1.1")
|
2013-10-24 12:16:14 +00:00
|
|
|
self.client = Client(self.app, Response)
|
|
|
|
|
|
|
|
def testPurgeDoesNoHarm(self):
|
|
|
|
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
|
|
|
|
|
|
|
|
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.client.post('/new?uri=test', data=json.dumps({"text": "..."}))
|
|
|
|
self.app.db.comments.purge(3600)
|
|
|
|
assert self.client.get('/id/1').status_code == 200
|