예제 #1
0
 def test_auth(self):
     client = APIClient(self.access_key, self.secret_key, self.api_server)
     req = APIRequest(client, self.endpoint, self.version)
     r = req.get('/resource')
     self.assertEqual(r.status_code, 200)
     r = req.get('/resource/not_exist')
     self.assertEqual(r.status_code, 404)
예제 #2
0
 def test_auth(self):
     client = APIClient(self.access_key, self.secret_key, self.api_server)
     req = APIRequest(client, self.endpoint, self.version)
     r = req.get('/resource')
     self.assertEqual(r.status_code, 200)
     r = req.get('/resource/not_exist')
     self.assertEqual(r.status_code, 404)
예제 #3
0
    def test_aes_get(self):
        client = APIClient(self.access_key, self.secret_key, self.api_server)
        req = APIRequest(client, self.endpoint, self.version, encrypt_type='aes')

        r = req.get('/resource/')

        self.assertEqual(r.status_code, 200)
        self.assertEqual(utf8('get'), utf8(r.content))
예제 #4
0
    def test_aes_get(self):
        client = APIClient(self.access_key, self.secret_key, self.api_server)
        req = APIRequest(client, self.endpoint, self.version, encrypt_type='aes')

        r = req.get('/resource/')

        self.assertEqual(r.status_code, 200)
        self.assertEqual(utf8('get'), utf8(r.content))
예제 #5
0
    def test_get(self):
        client = APIClient(self.access_key, self.secret_key, self.api_server)
        req = APIRequest(client, self.endpoint, self.version,
                         require_hmac=False, require_response_sign=False)

        r = req.get('/resource/')

        self.assertEqual(r.status_code, 200)
        self.assertEqual(utf8('get'), utf8(r.content))
예제 #6
0
    def test_post_img(self):
        client = APIClient(self.access_key, self.secret_key, self.api_server)
        req = APIRequest(client, self.endpoint, self.version)

        with open(IMG_FILE, 'rb') as f:
            body = f.read()
            r = req.post('/resource/', data=body)

            self.assertEqual(r.status_code, 200)
            self.assertEqual(utf8(r.content), utf8(body))
예제 #7
0
    def test_acl(self):
        client = APIClient(self.access_key, self.secret_key, self.api_server)
        req = APIRequest(client, self.endpoint, self.version)
        r = req.get('/resource')
        self.assertEqual(r.status_code, 200)

        client = APIClient(self.access_key, self.secret_key, self.api_server)
        req = APIRequest(client, self.endpoint, self.version)
        r = req.get('/forbidden/')
        self.assertEqual(r.status_code, GATEWAY_ERROR_STATUS_CODE)
예제 #8
0
    def test_post_img(self):
        client = APIClient(self.access_key, self.secret_key, self.api_server)
        req = APIRequest(client, self.endpoint, self.version)

        with open('img.jpg', 'rb') as f:
            body = f.read()
            r = req.post('/resource/', data=body)

            self.assertEqual(r.status_code, 200)
            self.assertEqual(utf8(r.content), utf8(body))
예제 #9
0
    def test_signature(self):
        client = APIClient(self.access_key, 'bad secret key', self.api_server)
        req = APIRequest(client, self.endpoint, self.version)
        r = req.get('/resource/')
        self.assertEqual(r.status_code, GATEWAY_ERROR_STATUS_CODE)

        client = APIClient(self.access_key, 'bad secret key', self.api_server)
        req = APIRequest(client, self.endpoint, self.version)

        r = req.get('/resource/')
        self.assertEqual(r.status_code, GATEWAY_ERROR_STATUS_CODE)
예제 #10
0
    def test_post_json(self):
        client = APIClient(self.access_key, self.secret_key, self.api_server)
        req = APIRequest(client, self.endpoint, self.version)
        json_data = {
            'a': 1,
            'b': 'test string',
            'c': '中文'
        }

        body = json.dumps(json_data, ensure_ascii=False)
        r = req.post('/resource/', json=json_data)

        self.assertEqual(r.status_code, 200)
        self.assertEqual(utf8(r.content), utf8(body))
예제 #11
0
    def test_post_json(self):
        client = APIClient(self.access_key, self.secret_key, self.api_server)
        req = APIRequest(client, self.endpoint, self.version)
        json_data = {
            'a': 1,
            'b': 'test string',
            'c': '中文'
        }

        body = json.dumps(json_data, ensure_ascii=False)
        r = req.post('/resource/', json=json_data)

        self.assertEqual(r.status_code, 200)
        self.assertEqual(utf8(r.content), utf8(body))
예제 #12
0
    def test_acl(self):
        client = APIClient(self.access_key, self.secret_key, self.api_server)
        req = APIRequest(client, self.endpoint, self.version)
        r = req.get('/resource')
        self.assertEqual(r.status_code, 200)

        client = APIClient(self.access_key, self.secret_key, self.api_server)
        req = APIRequest(client, self.endpoint, self.version)
        r = req.get('/forbidden/')
        self.assertEqual(r.status_code, GATEWAY_ERROR_STATUS_CODE)
예제 #13
0
    def test_signature(self):
        client = APIClient(self.access_key, 'bad secret key', self.api_server)
        req = APIRequest(client, self.endpoint, self.version)
        r = req.get('/resource/')
        self.assertEqual(r.status_code, GATEWAY_ERROR_STATUS_CODE)

        client = APIClient(self.access_key, 'bad secret key', self.api_server)
        req = APIRequest(client, self.endpoint, self.version)

        r = req.get('/resource/')
        self.assertEqual(r.status_code, GATEWAY_ERROR_STATUS_CODE)
예제 #14
0
    def test_aes_login_refresh_logout(self):
        client = APIClient(self.access_key, self.secret_key, self.api_server)
        req = APIRequest(client, self.endpoint, self.version, encrypt_type='aes')

        r = req.get('/login')
        print(r.content)
        self.assertEqual(r.status_code, 405)
        json_data = {
            'name': 'name',
            'password': '******'
        }
        r = req.post('/login', json=json_data)
        self.assertEqual(r.status_code, 200)
        schema = {
            'code': {
                'type': 'integer',
                'required': True,
                'allowed': [APIStatusCode.SUCCESS]
            },
            'msg': {
                'type': 'string',
                'required': True,
            },
            'data': {
                'type': 'dict',
                'required': True,
            }
        }
        v = Validator(schema=schema, allow_unknown=True)
        logger.debug(r.content)
        j = json_decode(r.content)
        logger.debug(j)
        logger.debug(r.json())
        logger.debug(v.validate(r.json()))
        self.assertEqual(v.validate(r.json()), True)

        # refresh_token
        json_data = {
            'refresh_token': r.json()['data']['refresh_token']
        }
        logger.debug(json_data)

        r = req.post('/token', json=json_data)
        self.assertEqual(r.status_code, 200)
        v = Validator(schema=schema, allow_unknown=True)
        logger.debug(r.json())
        self.assertEqual(v.validate(r.json()), True)

        # ---------------------
        json_data = {
            'test': 'test'
        }

        auth_req = APIRequest(client, 'test_api_login', 'v1', encrypt_type='aes')

        access_token = r.json()['data']['access_token']
        ar = auth_req.post('/protected/?access_token=%s' % access_token,
                           json=json_data)
        self.assertEqual(r.status_code, 200)
        v = Validator(schema=schema, allow_unknown=True)
        logger.debug(ar.content)
        logger.debug(ar.json())
        self.assertEqual(v.validate(ar.json()), True)
        # ---------------------
        # logout
        json_data = {
            'access_token': r.json()['data']['access_token']
        }
        r = req.post('/logout', json=json_data)
        self.assertEqual(r.status_code, 200)
        schema = {
            'code': {
                'type': 'integer',
                'required': True,
                'allowed': [APIStatusCode.SUCCESS]
            },
            'msg': {
                'type': 'string',
                'required': True,
            }
        }
        v = Validator(schema=schema, allow_unknown=True)
        self.assertEqual(v.validate(r.json()), True)
예제 #15
0
    def test_login_refresh_logout(self):
        client = APIClient(self.access_key, self.secret_key, self.api_server)
        req = APIRequest(client, self.endpoint, self.version)

        r = req.get('/login')
        self.assertEqual(r.status_code, 405)
        json_data = {
            'name': 'name',
            'password': '******'
        }
        r = req.post('/login', json=json_data)
        self.assertEqual(r.status_code, 200)
        schema = {
            'code': {
                'type': 'integer',
                'required': True,
                'allowed': [APIStatusCode.SUCCESS]
            },
            'msg': {
                'type': 'string',
                'required': True,
            },
            'data': {
                'type': 'dict',
                'required': True,
            }
        }
        v = Validator(schema=schema, allow_unknown=True)
        logger.debug(r.json())
        logger.debug(v.validate(r.json()))
        self.assertEqual(v.validate(r.json()), True)

        # refresh_token
        json_data = {
            'refresh_token': r.json()['data']['refresh_token']
        }
        logger.debug(json_data)
        r = req.post('/token', json=json_data)
        self.assertEqual(r.status_code, 200)
        v = Validator(schema=schema, allow_unknown=True)
        logger.debug(r.json())
        self.assertEqual(v.validate(r.json()), True)

        # ---------------------
        json_data = {
            'test': 'test'
        }

        auth_req = APIRequest(client, 'test_api_login', 'v1')

        access_token = r.json()['data']['access_token']
        ar = auth_req.post('/protected/?access_token=%s' % access_token,
                           json=json_data)
        self.assertEqual(r.status_code, 200)
        v = Validator(schema=schema, allow_unknown=True)
        logger.debug(ar.json())
        self.assertEqual(v.validate(ar.json()), True)
        # ---------------------
        # logout
        json_data = {
            'access_token': r.json()['data']['access_token']
        }
        r = req.post('/logout', json=json_data)
        self.assertEqual(r.status_code, 200)
        schema = {
            'code': {
                'type': 'integer',
                'required': True,
                'allowed': [APIStatusCode.SUCCESS]
            },
            'msg': {
                'type': 'string',
                'required': True,
            }
        }
        v = Validator(schema=schema, allow_unknown=True)
        self.assertEqual(v.validate(r.json()), True)