Skip to content

Commit

Permalink
test api in real network env (chyroc#104)
Browse files Browse the repository at this point in the history
* test api in real network env

* tmp

* add log
  • Loading branch information
chyroc authored Jul 27, 2017
1 parent be4819a commit 3104fe3
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 4 deletions.
61 changes: 61 additions & 0 deletions test/rk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-

import os
from hashlib import md5

import requests


class RClient(object):
def __init__(self, username, password, soft_id, soft_key):
self.base_params = {
'username': username,
'password': md5(password.encode('utf-8')).hexdigest(),
'softid': soft_id,
'softkey': soft_key,
}
self.headers = {
'Connection': 'Keep-Alive',
'Expect': '100-continue',
'User-Agent': 'ben',
}

def rk_create(self, im, im_type, timeout=60):
params = {
'typeid': im_type,
'timeout': timeout,
}
params.update(self.base_params)
files = {'image': ('a.jpg', im)}
r = requests.post('http://api.ruokuai.com/create.json', data=params, files=files, headers=self.headers)
return r.json()

def rk_report_error(self, im_id):
params = {
'id': im_id,
}
params.update(self.base_params)
r = requests.post('http://api.ruokuai.com/reporterror.json', data=params, headers=self.headers)
return r.json()


def __identify_image_callback(img, code):
try:
username = os.environ['rk_username']
password = os.environ['rk_password']
id_ = os.environ['rk_id']
key = os.environ['rk_key']
rc = RClient(username, password, id_, key)
result = rc.rk_create(img, code)
print('验证码:', result['Result'])
return result['Result']
except Exception:
raise Exception('识别验证码错误')


def identify_image_callback_ruokuai_search(img):
return __identify_image_callback(img, 3060)


def identify_image_callback_ruokuai_history(img):
return __identify_image_callback(img, 3040)
18 changes: 14 additions & 4 deletions test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import io
import os
import unittest
from nose.tools import assert_equal

from nose.tools import assert_equal, assert_true, assert_in, assert_greater_equal
import httpretty

from wechatsogou.request import WechatSogouRequest
from wechatsogou.api import WechatSogouAPI
from test import fake_data_path, gaokao_keyword
from test.rk import identify_image_callback_ruokuai_search

ws_api = WechatSogouAPI()

Expand Down Expand Up @@ -39,9 +40,18 @@ def test_search_gzh(self):
'新东方在线高考辅导'],
[i['wechat_name'] for i in gzh_list])

@unittest.skip
def test_search_gzh_error(self):
pass # todo
def test_search_gzh_real(self):
gzh_list = ws_api.search_gzh(gaokao_keyword, identify_image_callback=identify_image_callback_ruokuai_search)
assert_equal(10, len(gzh_list))
assert_true(any(gaokao_keyword in i['wechat_name'] for i in gzh_list))

def test_get_gzh_artilce_by_history_real(self):
gzh_artilce = ws_api.get_gzh_artilce_by_history(gaokao_keyword,
identify_image_callback=identify_image_callback_ruokuai_search)
assert_in('gzh_info', gzh_artilce)
assert_in('article', gzh_artilce)
assert_in('wx.qlogo.cn', gzh_artilce['gzh_info']['headimage'])
assert_greater_equal(len(gzh_artilce['article']), 1)

def test_get_sugg(self):
sugg_gaokao = ws_api.get_sugg(gaokao_keyword)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
envlist = py27,py35,py36

[testenv]
passenv = *
deps=
setuptools==34.3.1
requests
Expand Down

0 comments on commit 3104fe3

Please sign in to comment.