def testDislike(self): self.makeClient("127.0.0.1").post("/new?uri=test", data=json.dumps({"text": "..."})) rv = self.makeClient("1.2.3.4").post('/id/1/dislike') self.assertEqual(rv.status_code, 200) self.assertEqual(loads(rv.data)['likes'], 0) self.assertEqual(loads(rv.data)['dislikes'], 1)
def testDislike(self): self.makeClient("127.0.0.1").post("/new?uri=test", data=json.dumps({"text": "..."})) rv = self.makeClient("1.2.3.4").post("/id/1/dislike") assert rv.status_code == 200 assert loads(rv.data)["likes"] == 0 assert loads(rv.data)["dislikes"] == 1
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.put('/id/1', data=json.dumps({"text": "Typo"})) assert loads(self.get('/id/1').data)["text"] == "<p>Typo</p>"
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': '...'})) assert loads(a.data)["id"] == 1 assert loads(b.data)["id"] == 2 assert loads(c.data)["id"] == 3
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': '...'})) self.assertEqual(loads(a.data)["id"], 1) self.assertEqual(loads(b.data)["id"], 2) self.assertEqual(loads(c.data)["id"], 3)
def testTooManyLikes(self): self.makeClient("127.0.0.1").post("/new?uri=test", data=json.dumps({"text": "..."})) for num in range(256): rv = self.makeClient("1.2.%i.0" % num).post("/id/1/like") assert rv.status_code == 200 if num >= 142: assert loads(rv.data)["likes"] == 142 else: assert loads(rv.data)["likes"] == num + 1
def testTooManyLikes(self): self.makeClient("127.0.0.1").post("/new?uri=test", data=json.dumps({"text": "..."})) for num in range(256): rv = self.makeClient("1.2.%i.0" % num).post('/id/1/like') self.assertEqual(rv.status_code, 200) if num >= 142: self.assertEqual(loads(rv.data)["likes"], 142) else: self.assertEqual(loads(rv.data)["likes"], num + 1)
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": "..."})) a = loads(a.data) b = loads(b.data) c = loads(c.data) self.assertNotEqual(a['hash'], '192.168.1.1') self.assertEqual(a['hash'], b['hash']) self.assertNotEqual(a['hash'], c['hash'])
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 a = loads(a.data) b = loads(b.data) c = loads(c.data) assert a['hash'] != '192.168.1.1' assert a['hash'] == b['hash'] assert a['hash'] != c['hash']
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
def testMultipleLikes(self): self.makeClient("127.0.0.1").post("/new?uri=test", data=json.dumps({"text": "..."})) for num in range(15): rv = self.makeClient("1.2.%i.0" % num).post('/id/1/like') self.assertEqual(rv.status_code, 200) self.assertEqual(loads(rv.data)["likes"], num + 1)
def testSingleLike(self): self.makeClient("127.0.0.1").post("/new?uri=test", data=json.dumps({"text": "..."})) rv = self.makeClient("0.0.0.0").post("/id/1/like") self.assertEqual(rv.status_code, 200) self.assertEqual(loads(rv.data)["likes"], 1)
def testMultipleLikes(self): self.makeClient("127.0.0.1").post("/new?uri=test", data=json.dumps({"text": "..."})) for num in range(15): rv = self.makeClient("1.2.%i.0" % num).post("/id/1/like") assert rv.status_code == 200 assert loads(rv.data)["likes"] == num + 1
def testDelete(self): self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Lorem ipsum ...'})) r = self.delete('/id/1') self.assertEqual(r.status_code, 200) self.assertEqual(loads(r.data), None) self.assertEqual(self.get('/id/1').status_code, 404)
def testCreateInvalidParent(self): self.post('/new?uri=test', data=json.dumps({'text': '...'})) self.post('/new?uri=test', data=json.dumps({'text': '...', 'parent': 1})) invalid = self.post('/new?uri=test', data=json.dumps({'text': '...', 'parent': 2})) self.assertEqual(loads(invalid.data)["parent"], 1)
def testSelfLike(self): bob = self.makeClient("127.0.0.1") bob.post("/new?uri=test", data=json.dumps({"text": "..."})) rv = bob.post("/id/1/like") assert rv.status_code == 200 assert loads(rv.data)["likes"] == 0
def testPreview(self): response = self.post( '/preview', data=json.dumps({'text': 'This is **mark***down*'})) self.assertEqual(response.status_code, 200) rv = loads(response.data) self.assertEqual( rv["text"], '<p>This is <strong>mark</strong><em>down</em></p>')
def testSelfLike(self): bob = self.makeClient("127.0.0.1") bob.post("/new?uri=test", data=json.dumps({"text": "..."})) rv = bob.post('/id/1/like') self.assertEqual(rv.status_code, 200) self.assertEqual(loads(rv.data)["likes"], 0)
def textCreateWithNonAsciiText(self): rv = self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Здравствуй, мир!'})) self.assertEqual(rv.status_code, 201) rv = loads(rv.data) self.assertEqual(rv["mode"], 1) self.assertEqual(rv["text"], '<p>Здравствуй, мир!</p>')
def testGet(self): self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Lorem ipsum ...'})) r = self.get('/id/1') self.assertEqual(r.status_code, 200) rv = loads(r.data) self.assertEqual(rv['id'], 1) self.assertEqual(rv['text'], '<p>Lorem ipsum ...</p>')
def testMultipleCounts(self): expected = {'a': 1, 'b': 2, 'c': 0} for uri, count in iteritems(expected): for _ in range(count): self.post('/new?uri=%s' % uri, data=json.dumps({"text": "..."})) rv = self.post('/count', data=json.dumps(list(expected.keys()))) self.assertEqual(loads(rv.data), list(expected.values()))
def testGetLimited(self): for i in range(20): self.post('/new?uri=test', data=json.dumps({'text': '...'})) r = self.get('/?uri=test&limit=10') self.assertEqual(r.status_code, 200) rv = loads(r.data) self.assertEqual(len(rv['replies']), 10)
def testGetNested(self): self.post('/new?uri=test', data=json.dumps({'text': '...'})) self.post('/new?uri=test', data=json.dumps({'text': '...', 'parent': 1})) r = self.get('/?uri=test&parent=1') self.assertEqual(r.status_code, 200) rv = loads(r.data) self.assertEqual(len(rv['replies']), 1)
def testCreateAndGetMultiple(self): for i in range(20): self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Spam'})) r = self.get('/?uri=%2Fpath%2F') self.assertEqual(r.status_code, 200) rv = loads(r.data) self.assertEqual(len(rv['replies']), 20)
def testCreateAndGetMultiple(self): for i in range(20): self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Spam'})) r = self.get('/?uri=%2Fpath%2F') assert r.status_code == 200 rv = loads(r.data) assert len(rv) == 20
def testGet(self): self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Lorem ipsum ...'})) r = self.get('/id/1') assert r.status_code == 200 rv = loads(r.data) assert rv['id'] == 1 assert 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)) rv = loads(rv.data) assert rv["mode"] == 1 assert rv["text"] == '<p>Здравствуй, мир!</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)) rv = loads(rv.data) assert rv["mode"] == 1 assert rv["text"] == '<p>Lorem ipsum ...</p>'
def testVisibleFields(self): rv = self.post('/new?uri=%2Fpath%2F', data=json.dumps({"text": "..."})) assert rv.status_code == 201 rv = loads(rv.data) for key in comments.API.FIELDS: rv.pop(key) assert not any(rv.keys())
def testCreate(self): rv = self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Lorem ipsum ...'})) self.assertEqual(rv.status_code, 201) self.assertIn("Set-Cookie", rv.headers) rv = loads(rv.data) self.assertEqual(rv["mode"], 1) self.assertEqual(rv["text"], '<p>Lorem ipsum ...</p>')
def testUpdate(self): self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Lorem ipsum ...'})) self.put('/id/1', data=json.dumps({ 'text': 'Hello World', 'author': 'me', 'website': 'http://example.com/' })) r = self.get('/id/1?plain=1') self.assertEqual(r.status_code, 200) rv = loads(r.data) self.assertEqual(rv['text'], 'Hello World') self.assertEqual(rv['author'], 'me') self.assertEqual(rv['website'], 'http://example.com/') self.assertIn('modified', rv)
def testLatestOk(self): # load some comments in a mix of posts saved = [] for idx, post_id in enumerate( [1, 2, 2, 1, 2, 1, 3, 1, 4, 2, 3, 4, 1, 2]): text = 'text-{}'.format(idx) post_uri = 'test-{}'.format(post_id) self.post('/new?uri=' + post_uri, data=json.dumps({'text': text})) saved.append((post_uri, text)) response = self.get('/latest?limit=5') self.assertEqual(response.status_code, 200) body = loads(response.data) expected_items = saved[-5:] # latest 5 for reply, expected in zip(body, expected_items): expected_uri, expected_text = expected self.assertIn(expected_text, reply['text']) self.assertEqual(expected_uri, reply['uri'])
def testDeleteWithReference(self): client = JSONClient(self.app, Response) client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'First'})) client.post('/new?uri=%2Fpath%2F', data=json.dumps({ 'text': 'First', 'parent': 1 })) r = client.delete('/id/1') self.assertEqual(r.status_code, 200) self.assertEqual(loads(r.data)['mode'], 4) self.assertIn('/path/', self.app.db.threads) 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') self.assertEqual(self.get('/?uri=%2Fpath%2F').status_code, 404) self.assertNotIn('/path/', self.app.db.threads)
def testZeroLikes(self): rv = self.makeClient("127.0.0.1").post( "/new?uri=test", data=json.dumps({"text": "..."})) self.assertEqual(loads(rv.data)['likes'], 0) self.assertEqual(loads(rv.data)['dislikes'], 0)
def testPreview(self): response = self.post('/preview', data=json.dumps({'text': 'This is **mark***down*'})) self.assertEqual(response.status_code, 200) rv = loads(response.data) self.assertEqual(rv["text"], '<p>This is <strong>mark</strong><em>down</em></p>')
def testVoteOnNonexistentComment(self): rv = self.makeClient("1.2.3.4").post('/id/1/like') self.assertEqual(rv.status_code, 200) self.assertEqual(loads(rv.data), None)