import unittest
import time
import json
from funkload.FunkLoadTestCase import FunkLoadTestCase
from func import utiltool


class userTest(FunkLoadTestCase):
    def setUp(self):
        self.url = self.conf_get('main', 'url')

    def test_userQuery(self):
        # init params
        testcase = 'test_userQuery'
        interface_url = self.url + '/user/profile/query'
        params_list = utiltool.get_request_params_list("userTest_data.xlsx", testcase)
        self.assertGreater(params_list.__len__(), 0, 'No testcase found!!!')

        for params in params_list:
            appId_value = str(params['appId'])
            nonce_value = utiltool.random_str(8)
            openId_value = str(params['openId'])
            timestamp_value = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
            token = str(params['token'])
            r_code = str(int(params['r_code']))

            # generate signature
            tmp_str = 'appId=' + appId_value + '&nonce=' + nonce_value + '&openId=' + openId_value + '&timestamp=' \
                      + timestamp_value
            signature_value = utiltool.create_signature(token, tmp_str)

            # generate post params
            params = [['appId', appId_value], ['nonce', nonce_value],
                      ['signature', signature_value], ['timestamp', timestamp_value], ['openId', openId_value]]

            # post requet to server and verify the response
            self.post(interface_url, params=params, description='Restful auth.query')
            ret = self.getBody()
            ret_json = json.loads(ret)
            data = ret_json["data"]
            print ret
            self.assert_('"code":"'+r_code+'"' in ret, msg='code expect: '+r_code+' actuel: ' + ret_json['code'])

    def test_userList(self):
        # init params
        testcase = 'test_userList'
        interface_url = self.url + '/user/list'
        params_list = utiltool.get_request_params_list("userTest_data.xlsx", testcase)
        self.assertGreater(params_list.__len__(), 0, 'No testcase found!!!')

        for params in params_list:
            appId_value = str(params['appId'])
            nonce_value = utiltool.random_str(8)
            openId_value = str(params['openId'])
            timestamp_value = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
            token = str(params['appSecret'])
            r_code = str(int(params['r_code']))

            # generate signature
            tmp_str = 'appId=' + appId_value + '&nonce=' + nonce_value + '&openId=' + openId_value + '&timestamp=' \
                      + timestamp_value
            signature_value = utiltool.create_signature(token, tmp_str)

            # generate post params
            params = [['appId', appId_value], ['nonce', nonce_value],
                      ['signature', signature_value], ['timestamp', timestamp_value], ['openId', openId_value]]

            # post requet to server and verify the response
            self.post(interface_url, params=params, description='Restful auth.query')
            ret = self.getBody()
            ret_json = json.loads(ret)
            data = ret_json["data"]
            print ret
            self.assert_('"code":"'+r_code+'"' in ret, msg='code expect: '+r_code+' actuel: ' + ret_json['code'])

    def tearDown(self):
        pass


if __name__ in ('main', '__main__'):
    unittest.main()
