2013-09-06 15:19:25 +00:00
|
|
|
|
2013-09-08 11:02:25 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-09-06 15:19:25 +00:00
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import tempfile
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
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-29 11:01:45 +00:00
|
|
|
from isso.utils import http
|
2013-09-13 14:12:26 +00:00
|
|
|
|
2013-11-13 13:20:58 +00:00
|
|
|
from fixtures import curl, loads, FakeIP
|
|
|
|
http.curl = curl
|
2013-09-06 15:19:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestVote(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
2013-11-13 15:12:47 +00:00
|
|
|
self.path = tempfile.NamedTemporaryFile().name
|
2013-09-06 15:19:25 +00:00
|
|
|
|
|
|
|
def makeClient(self, ip):
|
|
|
|
|
2013-10-06 16:37:05 +00:00
|
|
|
conf = core.Config.load(None)
|
|
|
|
conf.set("general", "dbpath", self.path)
|
|
|
|
conf.set("guard", "enabled", "off")
|
|
|
|
|
|
|
|
class App(Isso, core.Mixin):
|
|
|
|
pass
|
|
|
|
|
|
|
|
app = App(conf)
|
2013-09-06 15:19:25 +00:00
|
|
|
app.wsgi_app = FakeIP(app.wsgi_app, ip)
|
|
|
|
|
|
|
|
return Client(app, Response)
|
|
|
|
|
|
|
|
def testZeroLikes(self):
|
|
|
|
|
|
|
|
rv = self.makeClient("127.0.0.1").post("/new?uri=test", data=json.dumps({"text": "..."}))
|
2013-10-09 14:28:54 +00:00
|
|
|
assert loads(rv.data)['likes'] == loads(rv.data)['dislikes'] == 0
|
2013-09-06 15:19:25 +00:00
|
|
|
|
|
|
|
def testSingleLike(self):
|
|
|
|
|
|
|
|
self.makeClient("127.0.0.1").post("/new?uri=test", data=json.dumps({"text": "..."}))
|
2013-09-19 16:30:46 +00:00
|
|
|
rv = self.makeClient("0.0.0.0").post("/id/1/like")
|
2013-09-06 15:19:25 +00:00
|
|
|
|
|
|
|
assert rv.status_code == 200
|
2013-10-09 14:28:54 +00:00
|
|
|
assert loads(rv.data)["likes"] == 1
|
2013-09-06 15:19:25 +00:00
|
|
|
|
|
|
|
def testSelfLike(self):
|
|
|
|
|
|
|
|
bob = self.makeClient("127.0.0.1")
|
|
|
|
bob.post("/new?uri=test", data=json.dumps({"text": "..."}))
|
2013-09-19 16:30:46 +00:00
|
|
|
rv = bob.post('/id/1/like')
|
2013-09-06 15:19:25 +00:00
|
|
|
|
|
|
|
assert rv.status_code == 200
|
2013-10-09 14:28:54 +00:00
|
|
|
assert loads(rv.data)["likes"] == 0
|
2013-09-06 15:19:25 +00:00
|
|
|
|
|
|
|
def testMultipleLikes(self):
|
|
|
|
|
|
|
|
self.makeClient("127.0.0.1").post("/new?uri=test", data=json.dumps({"text": "..."}))
|
|
|
|
for num in range(15):
|
2013-09-19 16:30:46 +00:00
|
|
|
rv = self.makeClient("1.2.%i.0" % num).post('/id/1/like')
|
2013-09-06 15:19:25 +00:00
|
|
|
assert rv.status_code == 200
|
2013-10-09 14:28:54 +00:00
|
|
|
assert loads(rv.data)["likes"] == num + 1
|
2013-09-06 15:19:25 +00:00
|
|
|
|
2013-09-19 16:30:46 +00:00
|
|
|
def testVoteOnNonexistentComment(self):
|
|
|
|
rv = self.makeClient("1.2.3.4").post('/id/1/like')
|
|
|
|
assert rv.status_code == 200
|
2013-10-09 14:28:54 +00:00
|
|
|
assert loads(rv.data) == None
|
2013-09-19 16:30:46 +00:00
|
|
|
|
2013-09-06 15:19:25 +00:00
|
|
|
def testTooManyLikes(self):
|
|
|
|
|
|
|
|
self.makeClient("127.0.0.1").post("/new?uri=test", data=json.dumps({"text": "..."}))
|
|
|
|
for num in range(256):
|
2013-09-19 16:30:46 +00:00
|
|
|
rv = self.makeClient("1.2.%i.0" % num).post('/id/1/like')
|
2013-09-06 15:19:25 +00:00
|
|
|
assert rv.status_code == 200
|
|
|
|
|
|
|
|
if num >= 142:
|
2013-10-09 14:28:54 +00:00
|
|
|
assert loads(rv.data)["likes"] == 142
|
2013-09-06 15:19:25 +00:00
|
|
|
else:
|
2013-10-09 14:28:54 +00:00
|
|
|
assert loads(rv.data)["likes"] == num + 1
|
2013-09-06 15:19:25 +00:00
|
|
|
|
2013-10-01 12:42:00 +00:00
|
|
|
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
|
2013-10-09 14:28:54 +00:00
|
|
|
assert loads(rv.data)['likes'] == 0
|
|
|
|
assert loads(rv.data)['dislikes'] == 1
|