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')
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)
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)
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)
def on_put(self, req, resp, token): site = self.get_site(token) msg = json.load(req.stream) tid = msg.get('tid', 0) fid = msg.get('fid', 0) uid = msg.get('uid', 0) ip = msg.get('ip', 0) content = msg.get('content', None) if not tid or not uid or not ip or not content: raise falcon.HTTPBadRequest(config.HTTP_400, 'invalid params') if check_block(site.id, ip): logger.info('IP %s deny' % ip) raise falcon.HTTPForbidden(config.HTTP_403, 'ip %s deny' % ip) try: comment = create(site, tid, fid, uid, ip, content) except Exception: logger.exception('create comment') raise falcon.HTTPInternalServerError(config.HTTP_500, 'create comment failed') resp.status = falcon.HTTP_201 resp.stream = ijson.dump({'id': comment.id})
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)
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)
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)
def test_get_comment_with_expand(self): site = get_site_by_token(self.token) ncomment_1 = create(site, 11, 0, 1, '192.168.10.1', 'hello') ncomment_2 = create(site, 11, ncomment_1.id, 1, '192.168.10.1', 'world') data = {'tid':11, 'page':1, 'num':3, 'expand':1} 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) c1, c2 = result[0] self.assertEqual(c1['ip'], ncomment_1.ip) self.assertEqual(c1['content'], ncomment_1.content) self.assertEqual(c1['id'], ncomment_1.id) self.assertEqual(c2['ip'], ncomment_2.ip) self.assertEqual(c2['content'], ncomment_2.content) self.assertEqual(c2['id'], ncomment_2.id)
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)