Exemple #1
0
    def test_delete_comment_by_tid(self):
        site = get_site_by_token(self.token)
        fc = create(site, 25, 0, 1, '127.0.0.5', 'hello')
        create(site, 25, fc.id, 1, '127.0.0.5', 'hello')
        create(site, 25, 0, 1, '127.0.0.6', 'hello')

        data = {'tid': 25}
        response = self.send_request(path = self.delete_by_tid_path, method = 'DELETE', data = json.dumps(data))
        self.assertEqual(falcon.HTTP_200, self.mock.status)
        self.assertFalse(response)
        site_update = get_site_by_token(self.token)
        self.assertEqual(site.comments - 3, site_update.comments)
Exemple #2
0
    def test_delete_block(self):
        site = get_site_by_token(self.token)
        after = site.blocks
        nblock = block(site, '192.168.1.1')

        response = self.send_request(
            path = self.path, method = 'DELETE', \
            data = json.dumps({'id': nblock.id, 'token': self.token}), \
        )

        site = get_site_by_token(self.token)
        self.assertEqual(after, site.blocks)
        self.assertEqual(falcon.HTTP_200, self.mock.status)
        self.assertFalse(response)
Exemple #3
0
    def test_get_comments_by_fid(self):
        site = get_site_by_token(self.token)
        ip = '192.168.101.1'
        f_comment = create(site, 30, 0, 1, ip, 'father')
        create(site, 30, f_comment.id, 1, ip, 'hello')
        create(site, 30, f_comment.id, 2, ip, 'world')

        data = {'tid': 30, 'fid': f_comment.id, 'page':1, 'num':2}
        response = self.send_request(
            path = self.get_comments_by_fid_path, method = 'GET', \
            data = json.dumps(data), \
        )

        result = self.check(response)
        self.assertEqual(result[0]['content'],  'world')

        data = {'tid': 30, 'fid': f_comment.id, 'page':1, 'num':2}
        response = self.send_request(
            path = self.get_comments_by_fid_path, method = 'GET', \
            data = json.dumps(data), \
        )

        result = self.check(response)
        self.assertEqual(result[0]['content'],  'world')
        self.assertEqual(result[1]['content'],  'hello')
Exemple #4
0
    def test_delete_comment_by_ip_500(self):
        site = get_site_by_token(self.token)
        create(site, 24, 0, 1, '127.0.0.5', 'hello')
        from handlers import delete
        self.patch(delete, 'delete_comment', fake_func)

        self.request_delete_by_ip({'ip': '127.0.0.5'})
        self.assertEqual(falcon.HTTP_500, self.mock.status)
Exemple #5
0
 def test_delete_comment_by_tid_500(self):
     site = get_site_by_token(self.token)
     create(site, 25, 0, 1, '127.0.0.5', 'hello')
     from handlers import delete
     self.patch(delete, 'delete_comments_by_tid', fake_func)
     data = {'tid': 25}
     self.send_request(path = self.delete_by_tid_path, method = 'DELETE', data = json.dumps(data))
     self.assertEqual(falcon.HTTP_500, self.mock.status)
Exemple #6
0
    def test_create_block(self):
        site = get_site_by_token(self.token)
        after = site.blocks + 1
        response = self.send_request(
            path = self.path, method = 'PUT', \
            data = json.dumps({'ip': '192.168.1.1', 'token': self.token}), \
        )

        site = get_site_by_token(self.token)
        self.assertTrue(site.blocks, after)
        self.assertTrue(is_iter(response))
        self.assertEqual(falcon.HTTP_201, self.mock.status)
        data = json.loads(''.join(response))
        self.assertIsInstance(data, dict)
        id = data.get('id')
        self.assertTrue(id)
        self.assertIsInstance(id, int)
Exemple #7
0
    def test_check_block(self):
        site = get_site_by_token(self.token)
        ip = '192.168.1.11'
        block(site, ip)

        self.assertTrue(check_block(site.id, ip))
        rds.delete(config.BLOCK_PREFIX.format(sid = site.id, ip = ip))
        self.assertTrue(check_block(site.id, ip))
        self.assertFalse(check_block(site.id, '192.168.1.12'))
Exemple #8
0
    def test_create_comment(self):
        site = get_site_by_token(self.token)
        after = site.comments + 1
        data = {'tid':1, 'fid':1, 'uid':1, 'ip':'192.168.8.1', 'content':'Hello World'}
        response = self.send_request(
            path = self.path, method = 'PUT', \
            data = json.dumps(data), \
        )

        site = get_site_by_token(self.token)
        self.assertEqual(site.comments, after)
        self.assertTrue(is_iter(response))
        self.assertEqual(falcon.HTTP_201, self.mock.status)
        data = json.loads(''.join(response))
        self.assertIsInstance(data, dict)
        id = data.get('id')
        self.assertTrue(id)
        self.assertIsInstance(id, int)
Exemple #9
0
    def test_delete_comment(self):
        site = get_site_by_token(self.token)
        o1 = site.comments

        ip = '192.168.101.1'
        fc = create(site, 20, 0, 1, ip, 'hello')
        create(site, 20, fc.id, 1, ip, 'hello')
        create(site, 20, fc.id, 1, ip, 'hello')
        create(site, 20, fc.id, 1, ip, 'hello')
        d2 = create(site, 20, fc.id, 1, ip, 'hello')

        site = get_site_by_token(self.token)
        self.assertEqual(o1 + 5, site.comments)
        o1 = site.comments
        fc = get_comment(site, fc.id)
        self.assertEqual(fc.count, 4)
        o2 = fc.count

        data = {'id': d2.id}
        response = self.send_request(
            path = self.path, method = 'DELETE', \
            data = json.dumps(data), \
        )

        self.assertEqual(falcon.HTTP_200, self.mock.status)
        self.assertFalse(response)

        fc = get_comment(site, fc.id)
        self.assertEqual(fc.count, o2 - 1)
        site = get_site_by_token(self.token)
        self.assertEqual(o1 - 1, site.comments)
        o1 = site.comments

        data = {'id': fc.id}
        response = self.send_request(
            path = self.path, method = 'DELETE', \
            data = json.dumps(data), \
        )

        self.assertEqual(falcon.HTTP_200, self.mock.status)
        self.assertFalse(response)
        site = get_site_by_token(self.token)
        self.assertEqual(o1 - 4, site.comments)
Exemple #10
0
    def test_delete_block_500(self):
        site = get_site_by_token(self.token)
        nblock = block(site, '192.168.1.1')
        from handlers import site
        self.patch(site, 'delete_block', fake_func)

        self.send_request(
            path = self.path, method = 'DELETE', \
            data = json.dumps({'id': nblock.id, 'token': self.token}), \
        )
        self.assertEqual(falcon.HTTP_500, self.mock.status)
Exemple #11
0
    def test_create_comment_403(self):
        site = get_site_by_token(self.token)
        ip = '192.168.9.1'
        block(site, ip)
        data = {'tid':1, 'fid':1, 'uid':1, 'ip':ip, 'content':'Hello World'}
        response = self.send_request(
            path = self.path, method = 'PUT', \
            data = json.dumps(data), \
        )

        self.assertIsInstance(response, list)
        self.assertEqual(falcon.HTTP_403, self.mock.status)
Exemple #12
0
    def test_delete_500(self):
        site = get_site_by_token(self.token)
        fc = create(site, 20, 0, 1, '192.168.101.2', 'hello')
        from handlers import comment
        self.patch(comment, 'delete_comment', fake_func)

        self.send_request(
            path = self.path, \
            data = json.dumps({'id': fc.id}), \
            method = 'DELETE'
        )
        self.assertEqual(falcon.HTTP_500, self.mock.status)
Exemple #13
0
    def test_refresh_cache_and_get_new_block(self):
        site = get_site_by_token(self.token)
        ip = '192.168.1.10'
        block(site, ip)
        response = self.send_request(
            path = self.path, method='GET', \
            data = json.dumps({'page': 1, 'token': self.token, 'num': 1}), \
        )

        self.assertTrue(is_iter(response))
        self.assertEqual(falcon.HTTP_200, self.mock.status)
        data = json.loads(''.join(response))
        self.assertIsInstance(data, list)
        self.assertTrue(data[0]['ip'] == ip)
Exemple #14
0
    def test_delete_comment_by_ip(self):
        site = get_site_by_token(self.token)
        fc = create(site, 21, 0, 1, '127.0.0.1', 'hello')
        create(site, 21, fc.id, 1, '127.0.0.2', 'world')

        fc2 = create(site, 22, 0, 1, '127.0.0.3', 'hi')
        create(site, 22, fc2.id, 2, '127.0.0.4', 'man')
        create(site, 22, fc2.id, 3, '127.0.0.3', 'yo')

        create(site, 23, 0, 1, '127.0.0.3', 'hi')

        data = {'ip': '127.0.0.2'}
        response = self.request_delete_by_ip(data)

        self.assertEqual(falcon.HTTP_200, self.mock.status)
        self.assertFalse(response)
        fc_update = get_comment(site, fc.id)
        site_update = get_site_by_token(self.token)
        self.assertEqual(site.comments - 1, site_update.comments)
        self.assertEqual(fc_update.count, 0)

        data = {'ip': '127.0.0.3', 'tid': 23}
        response = self.request_delete_by_ip(data)

        self.assertEqual(falcon.HTTP_200, self.mock.status)
        self.assertFalse(response)
        fc2_update = get_comment(site, fc2.id)
        self.assertTrue(fc2_update)

        site = get_site_by_token(self.token)
        data = {'ip': '127.0.0.3'}
        response = self.request_delete_by_ip(data)
        self.assertEqual(falcon.HTTP_200, self.mock.status)
        self.assertFalse(response)
        site_update = get_site_by_token(self.token)
        self.assertEqual(site.comments - 3, site_update.comments)
Exemple #15
0
    def test_get_comment_no_expand(self):
        site = get_site_by_token(self.token)
        ncomment_1 = create(site, 10, 0, 1, '192.168.10.1', 'hello')

        data = {'tid':10, 'page':1, 'num':1, 'expand':0}
        response = self.send_request(
            path = self.path, method = 'GET', \
            data = json.dumps(data), \
        )

        self.assertTrue(is_iter(response))
        self.assertEqual(falcon.HTTP_200, self.mock.status)
        result = json.loads(''.join(response))
        self.assertTrue(result)

        data = result[0][0]
        self.assertEqual(data['ip'], ncomment_1.ip)
        self.assertEqual(data['content'], ncomment_1.content)
        self.assertEqual(data['id'], ncomment_1.id)
Exemple #16
0
    def test_get_comments_by_ip(self):
        site = get_site_by_token(self.token)
        ip = '192.168.100.1'
        create(site, 20, 0, 1, ip, 'hello')
        create(site, 21, 0, 2, ip, 'world')

        data = {'ip': ip}
        response = self.send_request(
            path = self.get_comments_by_ip_path, method = 'GET', \
            data = json.dumps(data), \
        )
        self.check(response)

        data = {'ip': ip, 'tid': 20}
        response = self.send_request(
            path = self.get_comments_by_ip_path, method = 'GET', \
            data = json.dumps(data), \
        )
        self.check(response)
Exemple #17
0
 def get_site(self, token):
     site = get_site_by_token(token)
     if not site:
         raise falcon.HTTPNotFound()
     return site
Exemple #18
0
 def setUp(self):
     super(TestUpComment, self).setUp()
     self.path = '/u/%s' % self.token
     self.site = get_site_by_token(self.token)