BVB Source Codes

isso Show test_comments.py Source code

Return Download isso: download test_comments.py Source code - Download isso Source code - Type:.py
  1. # -*- encoding: utf-8 -*-
  2.  
  3. from __future__ import unicode_literals
  4.  
  5. import os
  6. import json
  7. import tempfile
  8.  
  9. try:
  10.     import unittest2 as unittest
  11. except ImportError:
  12.     import unittest
  13.  
  14. try:
  15.     from urllib.parse import urlencode
  16. except ImportError:
  17.     from urllib import urlencode
  18.  
  19. from werkzeug.wrappers import Response
  20.  
  21. from isso import Isso, core, config, dist
  22. from isso.utils import http
  23. from isso.views import comments
  24.  
  25. from isso.compat import iteritems
  26.  
  27. from fixtures import curl, loads, FakeIP, JSONClient
  28. http.curl = curl
  29.  
  30.  
  31. class TestComments(unittest.TestCase):
  32.  
  33.     def setUp(self):
  34.         fd, self.path = tempfile.mkstemp()
  35.         conf = config.load(os.path.join(dist.location, "share", "isso.conf"))
  36.         conf.set("general", "dbpath", self.path)
  37.         conf.set("guard", "enabled", "off")
  38.         conf.set("hash", "algorithm", "none")
  39.  
  40.         class App(Isso, core.Mixin):
  41.             pass
  42.  
  43.         self.app = App(conf)
  44.         self.app.wsgi_app = FakeIP(self.app.wsgi_app, "192.168.1.1")
  45.  
  46.         self.client = JSONClient(self.app, Response)
  47.         self.get = self.client.get
  48.         self.put = self.client.put
  49.         self.post = self.client.post
  50.         self.delete = self.client.delete
  51.  
  52.     def tearDown(self):
  53.         os.unlink(self.path)
  54.  
  55.     def testGet(self):
  56.  
  57.         self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Lorem ipsum ...'}))
  58.         r = self.get('/id/1')
  59.         self.assertEqual(r.status_code, 200)
  60.  
  61.         rv = loads(r.data)
  62.  
  63.         self.assertEqual(rv['id'], 1)
  64.         self.assertEqual(rv['text'], '<p>Lorem ipsum ...</p>')
  65.  
  66.     def testCreate(self):
  67.  
  68.         rv = self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Lorem ipsum ...'}))
  69.  
  70.         self.assertEqual(rv.status_code, 201)
  71.         self.assertIn("Set-Cookie", rv.headers)
  72.  
  73.         rv = loads(rv.data)
  74.  
  75.         self.assertEqual(rv["mode"], 1)
  76.         self.assertEqual(rv["text"], '<p>Lorem ipsum ...</p>')
  77.  
  78.     def textCreateWithNonAsciiText(self):
  79.  
  80.         rv = self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': '袟写褉邪胁褋褌胁褍泄, 屑懈褉!'}))
  81.  
  82.         self.assertEqual(rv.status_code, 201)
  83.         rv = loads(rv.data)
  84.  
  85.         self.assertEqual(rv["mode"], 1)
  86.         self.assertEqual(rv["text"], '<p>袟写褉邪胁褋褌胁褍泄, 屑懈褉!</p>')
  87.  
  88.     def testCreateMultiple(self):
  89.  
  90.         a = self.post('/new?uri=test', data=json.dumps({'text': '...'}))
  91.         b = self.post('/new?uri=test', data=json.dumps({'text': '...'}))
  92.         c = self.post('/new?uri=test', data=json.dumps({'text': '...'}))
  93.  
  94.         self.assertEqual(loads(a.data)["id"], 1)
  95.         self.assertEqual(loads(b.data)["id"], 2)
  96.         self.assertEqual(loads(c.data)["id"], 3)
  97.  
  98.     def testCreateAndGetMultiple(self):
  99.  
  100.         for i in range(20):
  101.             self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Spam'}))
  102.  
  103.         r = self.get('/?uri=%2Fpath%2F')
  104.         self.assertEqual(r.status_code, 200)
  105.  
  106.         rv = loads(r.data)
  107.         self.assertEqual(len(rv['replies']), 20)
  108.  
  109.     def testCreateInvalidParent(self):
  110.  
  111.         self.post('/new?uri=test', data=json.dumps({'text': '...'}))
  112.         self.post('/new?uri=test', data=json.dumps({'text': '...', 'parent': 1}))
  113.         invalid = self.post('/new?uri=test', data=json.dumps({'text': '...', 'parent': 2}))
  114.  
  115.         self.assertEqual(loads(invalid.data)["parent"], 1)
  116.  
  117.     def testVerifyFields(self):
  118.  
  119.         verify = lambda comment: comments.API.verify(comment)[0]
  120.  
  121.         # text is missing
  122.         self.assertFalse(verify({}))
  123.  
  124.         # invalid types
  125.         self.assertFalse(verify({"text": "...", "parent": "xxx"}))
  126.         for key in ("author", "website", "email"):
  127.             self.assertFalse(verify({"text": True, key: 3.14}))
  128.  
  129.         # text too short and/or blank
  130.         for text in ("", "\n\n\n"):
  131.             self.assertFalse(verify({"text": text}))
  132.  
  133.         # email/website length
  134.         self.assertTrue(verify({"text": "...", "email": "*"*254}))
  135.         self.assertTrue(verify({"text": "...", "website": "google.de/" + "a"*128}))
  136.  
  137.         self.assertFalse(verify({"text": "...", "email": "*"*1024}))
  138.         self.assertFalse(verify({"text": "...", "website": "google.de/" + "*"*1024}))
  139.  
  140.         # valid website url
  141.         self.assertTrue(comments.isurl("example.tld"))
  142.         self.assertTrue(comments.isurl("http://example.tld"))
  143.         self.assertTrue(comments.isurl("https://example.tld"))
  144.         self.assertTrue(comments.isurl("https://example.tld:1337/"))
  145.         self.assertTrue(comments.isurl("https://example.tld:1337/foobar"))
  146.         self.assertTrue(comments.isurl("https://example.tld:1337/foobar?p=1#isso-thread"))
  147.  
  148.         self.assertFalse(comments.isurl("ftp://example.tld/"))
  149.         self.assertFalse(comments.isurl("tel:+1234567890"))
  150.         self.assertFalse(comments.isurl("+1234567890"))
  151.         self.assertFalse(comments.isurl("spam"))
  152.  
  153.     def testGetInvalid(self):
  154.  
  155.         self.assertEqual(self.get('/?uri=%2Fpath%2F&id=123').status_code, 404)
  156.         self.assertEqual(self.get('/?uri=%2Fpath%2Fspam%2F&id=123').status_code, 404)
  157.         self.assertEqual(self.get('/?uri=?uri=%foo%2F').status_code, 404)
  158.  
  159.     def testGetLimited(self):
  160.  
  161.         for i in range(20):
  162.             self.post('/new?uri=test', data=json.dumps({'text': '...'}))
  163.  
  164.         r = self.get('/?uri=test&limit=10')
  165.         self.assertEqual(r.status_code, 200)
  166.  
  167.         rv = loads(r.data)
  168.         self.assertEqual(len(rv['replies']), 10)
  169.  
  170.     def testGetNested(self):
  171.  
  172.         self.post('/new?uri=test', data=json.dumps({'text': '...'}))
  173.         self.post('/new?uri=test', data=json.dumps({'text': '...', 'parent': 1}))
  174.  
  175.         r = self.get('/?uri=test&parent=1')
  176.         self.assertEqual(r.status_code, 200)
  177.  
  178.         rv = loads(r.data)
  179.         self.assertEqual(len(rv['replies']), 1)
  180.  
  181.     def testGetLimitedNested(self):
  182.  
  183.         self.post('/new?uri=test', data=json.dumps({'text': '...'}))
  184.         for i in range(20):
  185.             self.post('/new?uri=test', data=json.dumps({'text': '...', 'parent': 1}))
  186.  
  187.         r = self.get('/?uri=test&parent=1&limit=10')
  188.         self.assertEqual(r.status_code, 200)
  189.  
  190.         rv = loads(r.data)
  191.         self.assertEqual(len(rv['replies']), 10)
  192.  
  193.     def testUpdate(self):
  194.  
  195.         self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Lorem ipsum ...'}))
  196.         self.put('/id/1', data=json.dumps({
  197.             'text': 'Hello World', 'author': 'me', 'website': 'http://example.com/'}))
  198.  
  199.         r = self.get('/id/1?plain=1')
  200.         self.assertEqual(r.status_code, 200)
  201.  
  202.         rv = loads(r.data)
  203.         self.assertEqual(rv['text'], 'Hello World')
  204.         self.assertEqual(rv['author'], 'me')
  205.         self.assertEqual(rv['website'], 'http://example.com/')
  206.         self.assertIn('modified', rv)
  207.  
  208.     def testDelete(self):
  209.  
  210.         self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Lorem ipsum ...'}))
  211.         r = self.delete('/id/1')
  212.         self.assertEqual(r.status_code, 200)
  213.         self.assertEqual(loads(r.data), None)
  214.         self.assertEqual(self.get('/id/1').status_code, 404)
  215.  
  216.     def testDeleteWithReference(self):
  217.  
  218.         client = JSONClient(self.app, Response)
  219.         client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'First'}))
  220.         client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'First', 'parent': 1}))
  221.  
  222.         r = client.delete('/id/1')
  223.         self.assertEqual(r.status_code, 200)
  224.         self.assertEqual(loads(r.data)['mode'], 4)
  225.         self.assertIn('/path/', self.app.db.threads)
  226.  
  227.         data = loads(client.get("/?uri=%2Fpath%2F").data)
  228.         self.assertEqual(data["total_replies"], 1)
  229.  
  230.         self.assertEqual(self.get('/?uri=%2Fpath%2F&id=1').status_code, 200)
  231.         self.assertEqual(self.get('/?uri=%2Fpath%2F&id=2').status_code, 200)
  232.  
  233.         r = client.delete('/id/2')
  234.         self.assertEqual(self.get('/?uri=%2Fpath%2F').status_code, 404)
  235.         self.assertNotIn('/path/', self.app.db.threads)
  236.  
  237.     def testDeleteWithMultipleReferences(self):
  238.         """
  239.        [ comment 1 ]
  240.            |
  241.            --- [ comment 2, ref 1 ]
  242.            |
  243.            --- [ comment 3, ref 1 ]
  244.        [ comment 4 ]
  245.        """
  246.         client = JSONClient(self.app, Response)
  247.  
  248.         client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'First'}))
  249.         client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Second', 'parent': 1}))
  250.         client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Third', 'parent': 1}))
  251.         client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Last'}))
  252.  
  253.         client.delete('/id/1')
  254.         self.assertEqual(self.get('/?uri=%2Fpath%2F').status_code, 200)
  255.         client.delete('/id/2')
  256.         self.assertEqual(self.get('/?uri=%2Fpath%2F').status_code, 200)
  257.         client.delete('/id/3')
  258.         self.assertEqual(self.get('/?uri=%2Fpath%2F').status_code, 200)
  259.         client.delete('/id/4')
  260.         self.assertEqual(self.get('/?uri=%2Fpath%2F').status_code, 404)
  261.  
  262.     def testPathVariations(self):
  263.  
  264.         paths = ['/sub/path/', '/path.html', '/sub/path.html', 'path', '/']
  265.  
  266.         for path in paths:
  267.             self.assertEqual(self.post('/new?' + urlencode({'uri': path}),
  268.                              data=json.dumps({'text': '...'})).status_code, 201)
  269.  
  270.         for i, path in enumerate(paths):
  271.             self.assertEqual(self.get('/?' + urlencode({'uri': path})).status_code, 200)
  272.             self.assertEqual(self.get('/id/%i' % (i + 1)).status_code, 200)
  273.  
  274.     def testDeleteAndCreateByDifferentUsersButSamePostId(self):
  275.  
  276.         mallory = JSONClient(self.app, Response)
  277.         mallory.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Foo'}))
  278.         mallory.delete('/id/1')
  279.  
  280.         bob = JSONClient(self.app, Response)
  281.         bob.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Bar'}))
  282.  
  283.         self.assertEqual(mallory.delete('/id/1').status_code, 403)
  284.         self.assertEqual(bob.delete('/id/1').status_code, 200)
  285.  
  286.     def testHash(self):
  287.  
  288.         a = self.post('/new?uri=%2Fpath%2F', data=json.dumps({"text": "Aaa"}))
  289.         b = self.post('/new?uri=%2Fpath%2F', data=json.dumps({"text": "Bbb"}))
  290.         c = self.post('/new?uri=%2Fpath%2F', data=json.dumps({"text": "Ccc", "email": "..."}))
  291.  
  292.         a = loads(a.data)
  293.         b = loads(b.data)
  294.         c = loads(c.data)
  295.  
  296.         self.assertNotEqual(a['hash'], '192.168.1.1')
  297.         self.assertEqual(a['hash'], b['hash'])
  298.         self.assertNotEqual(a['hash'], c['hash'])
  299.  
  300.     def testVisibleFields(self):
  301.  
  302.         rv = self.post('/new?uri=%2Fpath%2F', data=json.dumps({"text": "...", "invalid": "field"}))
  303.         self.assertEqual(rv.status_code, 201)
  304.  
  305.         rv = loads(rv.data)
  306.  
  307.         for key in comments.API.FIELDS:
  308.             rv.pop(key)
  309.  
  310.         self.assertListEqual(list(rv.keys()), [])
  311.  
  312.     def testCounts(self):
  313.  
  314.         self.assertEqual(self.get('/count?uri=%2Fpath%2F').status_code, 404)
  315.         self.post('/new?uri=%2Fpath%2F', data=json.dumps({"text": "..."}))
  316.  
  317.         rv = self.get('/count?uri=%2Fpath%2F')
  318.         self.assertEqual(rv.status_code, 200)
  319.         self.assertEqual(loads(rv.data), 1)
  320.  
  321.         for x in range(3):
  322.             self.post('/new?uri=%2Fpath%2F', data=json.dumps({"text": "..."}))
  323.  
  324.         rv = self.get('/count?uri=%2Fpath%2F')
  325.         self.assertEqual(rv.status_code, 200)
  326.         self.assertEqual(loads(rv.data), 4)
  327.  
  328.         for x in range(4):
  329.             self.delete('/id/%i' % (x + 1))
  330.  
  331.         rv = self.get('/count?uri=%2Fpath%2F')
  332.         self.assertEqual(rv.status_code, 404)
  333.  
  334.     def testMultipleCounts(self):
  335.  
  336.         expected = {'a': 1, 'b': 2, 'c': 0}
  337.  
  338.         for uri, count in iteritems(expected):
  339.             for _ in range(count):
  340.                 self.post('/new?uri=%s' % uri, data=json.dumps({"text": "..."}))
  341.  
  342.         rv = self.post('/count', data=json.dumps(list(expected.keys())))
  343.         self.assertEqual(loads(rv.data), list(expected.values()))
  344.  
  345.     def testModify(self):
  346.         self.post('/new?uri=test', data=json.dumps({"text": "Tpyo"}))
  347.  
  348.         self.put('/id/1', data=json.dumps({"text": "Tyop"}))
  349.         self.assertEqual(loads(self.get('/id/1').data)["text"], "<p>Tyop</p>")
  350.  
  351.         self.put('/id/1', data=json.dumps({"text": "Typo"}))
  352.         self.assertEqual(loads(self.get('/id/1').data)["text"], "<p>Typo</p>")
  353.  
  354.     def testDeleteCommentRemovesThread(self):
  355.  
  356.         self.client.post('/new?uri=%2F', data=json.dumps({"text": "..."}))
  357.         self.assertIn('/', self.app.db.threads)
  358.         self.client.delete('/id/1')
  359.         self.assertNotIn('/', self.app.db.threads)
  360.  
  361.     def testCSRF(self):
  362.  
  363.         js = "application/json"
  364.         form = "application/x-www-form-urlencoded"
  365.  
  366.         self.post('/new?uri=%2F', data=json.dumps({"text": "..."}))
  367.  
  368.         # no header is fine (default for XHR)
  369.         self.assertEqual(self.post('/id/1/dislike', content_type="").status_code, 200)
  370.  
  371.         # x-www-form-urlencoded is definitely not RESTful
  372.         self.assertEqual(self.post('/id/1/dislike', content_type=form).status_code, 403)
  373.         self.assertEqual(self.post('/new?uri=%2F', data=json.dumps({"text": "..."}),
  374.                                          content_type=form).status_code, 403)
  375.         # just for the record
  376.         self.assertEqual(self.post('/id/1/dislike', content_type=js).status_code, 200)
  377.  
  378.     def testPreview(self):
  379.         response = self.post('/preview', data=json.dumps({'text': 'This is **mark***down*'}))
  380.         self.assertEqual(response.status_code, 200)
  381.  
  382.         rv = loads(response.data)
  383.         self.assertEqual(rv["text"], '<p>This is <strong>mark</strong><em>down</em></p>')
  384.  
  385.  
  386.  
  387. class TestModeratedComments(unittest.TestCase):
  388.  
  389.     def setUp(self):
  390.         fd, self.path = tempfile.mkstemp()
  391.         conf = config.load(os.path.join(dist.location, "share", "isso.conf"))
  392.         conf.set("general", "dbpath", self.path)
  393.         conf.set("moderation", "enabled", "true")
  394.         conf.set("guard", "enabled", "off")
  395.         conf.set("hash", "algorithm", "none")
  396.  
  397.         class App(Isso, core.Mixin):
  398.             pass
  399.  
  400.         self.app = App(conf)
  401.         self.app.wsgi_app = FakeIP(self.app.wsgi_app, "192.168.1.1")
  402.         self.client = JSONClient(self.app, Response)
  403.  
  404.     def tearDown(self):
  405.         os.unlink(self.path)
  406.  
  407.     def testAddComment(self):
  408.  
  409.         rv = self.client.post('/new?uri=test', data=json.dumps({"text": "..."}))
  410.         self.assertEqual(rv.status_code, 202)
  411.  
  412.         self.assertEqual(self.client.get('/id/1').status_code, 200)
  413.         self.assertEqual(self.client.get('/?uri=test').status_code, 404)
  414.  
  415.         self.app.db.comments.activate(1)
  416.         self.assertEqual(self.client.get('/?uri=test').status_code, 200)
  417.  
  418.  
  419. class TestPurgeComments(unittest.TestCase):
  420.  
  421.     def setUp(self):
  422.         fd, self.path = tempfile.mkstemp()
  423.         conf = config.load(os.path.join(dist.location, "share", "isso.conf"))
  424.         conf.set("general", "dbpath", self.path)
  425.         conf.set("moderation", "enabled", "true")
  426.         conf.set("guard", "enabled", "off")
  427.         conf.set("hash", "algorithm", "none")
  428.  
  429.         class App(Isso, core.Mixin):
  430.             pass
  431.  
  432.         self.app = App(conf)
  433.         self.app.wsgi_app = FakeIP(self.app.wsgi_app, "192.168.1.1")
  434.         self.client = JSONClient(self.app, Response)
  435.  
  436.     def testPurgeDoesNoHarm(self):
  437.         self.client.post('/new?uri=test', data=json.dumps({"text": "..."}))
  438.         self.app.db.comments.activate(1)
  439.         self.app.db.comments.purge(0)
  440.         self.assertEqual(self.client.get('/?uri=test').status_code, 200)
  441.  
  442.     def testPurgeWorks(self):
  443.         self.client.post('/new?uri=test', data=json.dumps({"text": "..."}))
  444.         self.app.db.comments.purge(0)
  445.         self.assertEqual(self.client.get('/id/1').status_code, 404)
  446.  
  447.         self.client.post('/new?uri=test', data=json.dumps({"text": "..."}))
  448.         self.app.db.comments.purge(3600)
  449.         self.assertEqual(self.client.get('/id/1').status_code, 200)
  450.  
downloadtest_comments.py Source code - Download isso Source code
Related Source Codes/Software:
hologram - A markdown based documentation system for style gu... 2017-05-12
www.html5rocks.com - ....a top-notch resource for web developer 2017-05-12
MTStatusBarOverlay - A custom iOS status bar overlay seen in Apps like ... 2017-05-12
blueprint - Reverse engineer server configuration ... 2017-05-12
PullToRefresh - A simple iPhone TableViewController for adding the... 2017-05-12
python3-cookbook - "The Python Cookbook" 3 rd Edition Translatio 2017-05-12
TSA-Travel-Sentry-master-keys - 3D reproduction of TSA Master key 2017-05-12
fatfree - A powerful yet easy-to-use PHP micro-framework des... 2017-05-13
rolify - Role management library with the resource scoping 2017-05-13
jquery-mockjax - The jQuery Mockjax Plugin provides a simple and ex... 2017-05-13
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top