コード例 #1
0
    def test_service_headers(self, requests_mock):
        """Test service headers."""
        client_name = 'client name'

        def matcher(request):  # NOQA: WPS430
            r = request._request
            assert 'SDK-VERSION' in r.headers
            assert r.headers['SDK-CLIENT-NAME'] == client_name

            assert 'default' in r.headers
            assert r.headers['default'] == 'default'

            assert 'storage_default' not in r.headers

            assert 'service_default' in r.headers
            assert r.headers['service_default'] == 'default'

            return requests.Response()

        api = Api(url='http://127.0.0.1', client_name=client_name)
        api.default_headers['default'] = 'default'
        api.storage_default_headers['storage_default'] = 'default'
        api.service_default_headers['service_default'] = 'default'
        url = '/some_url'
        requests_mock.add_matcher(matcher)
        api.service_post(url, {'a': 1})
コード例 #2
0
ファイル: test_api.py プロジェクト: dyens/sdk-python
    def test_get_new_sid_bad_creds(self):
        """Test fail get new sid.

        Case: bad credentials.
        """
        url = settings.API['url']
        username = settings.API['username']
        password = '******'
        api = Api.with_creds(url, username, password)
        with pytest.raises(InvalidCredentials):
            api.get_new_sid()
コード例 #3
0
 def test_with_sid(self):
     """Test API initialization from sid."""
     url = 'url'
     sid = 'sid'
     api = Api.with_sid(url, sid)
     assert api._creds is None
     assert api._sid == sid
     assert api._api_url == url
     assert api._service_session is None
     assert api._storage_session is None
     assert api.service_retry_params
     assert api.storage_retry_params
コード例 #4
0
    def test_get_sid_bad_creds(self):
        """Test unsuccess get sid from session.

        Case: bad creds
        """
        url = settings.API['url']
        api = Api.with_creds(url, 'bad_us', 'bad_pass')
        session = Session(api=api)

        # If try auth with bad creds. Server can answer:
        # Lockout(Too many failed attemps)
        with pytest.raises((InvalidCredentials, Lockout)):
            session.get_sid('bad_us', 'bad_pass')
コード例 #5
0
def api():
    """Get api.

    :yields: valid  ambra api
    """
    url = settings.API['url']
    username = settings.API['username']
    password = settings.API['password']
    api = Api.with_creds(
        url,
        username,
        password,
    )
    yield api
    api.logout()
コード例 #6
0
    def test__wait_for_service_request(self):
        """Test wait for service request."""
        api = Api.with_creds(  # NOQA:S106
            username='******',
            url='http://127.0.0.1',
            password='******',
        )
        assert api._rate_limits
        assert api._rate_limits_lock
        api._wait_for_service_request('')
        assert api._last_request_time
        assert api._last_call_period

        now = monotonic()
        api._last_request_time = now
        api._last_call_period = 2.0  # 2 seconds
        api._wait_for_service_request('')
        assert monotonic() - now > 2.0
コード例 #7
0
    def test__wait_for_service_request_in_threads(self):
        """Test wait for service request in threads."""
        api = Api.with_creds(  # NOQA:S106
            username='******',
            url='http://127.0.0.1',
            password='******',
            rate_limits=RateLimits(
                default=RateLimit(1, 1),
                get_limit=RateLimit(2, 1),
                special=None,
            ),
        )
        threads = []
        now = monotonic()
        threads_n = 3
        for _ in range(threads_n):  # NOQA:WPS122
            thread = Thread(
                target=api._wait_for_service_request,
                args=('some_url', ),
            )
            threads.append(thread)
            thread.start()
        for thread in threads:  # NOQA:WPS440
            thread.join()
        spent_time = monotonic() - now
        assert spent_time > (threads_n - 1) * 1  # NOQA:WPS345
        assert spent_time < (threads_n + 1) * 1  # NOQA:WPS345

        threads = []
        now = monotonic()
        threads_n = 3
        for _ in range(threads_n):  # NOQA:WPS122
            thread = Thread(  # NOQA:WPS440
                target=api._wait_for_service_request,
                args=('some_url/get', ),
            )
            threads.append(thread)
            thread.start()
        for thread in threads:  # NOQA:WPS440
            thread.join()

        spent_time = monotonic() - now
        assert spent_time > (threads_n - 1) * 0.5
        assert spent_time < (threads_n + 1) * 1  # NOQA:WPS345
コード例 #8
0
    def test_service_headers_modifing_request_args(self, requests_mock):
        """Test service headers modifing request args."""
        count = 0

        def matcher(request):  # NOQA: WPS430
            nonlocal count  # NOQA:WPS420
            count += 1
            r = request._request
            assert 'SDK-VERSION' in r.headers

            # The first requset for auth
            # the second my request
            if count == 1:
                assert 'service_header' not in r.headers
                content = b'{"sid": "1","status": "ok"}'
                resp = requests.Response()
                resp._content = content
                resp.status_code = 200
                resp.headers = {
                    'content-type': 'application/json',
                }
                return resp
            assert 'service_header' in r.headers
            assert r.headers['service_header'] == 'value'
            content = b'{"status": "ok"}'
            resp = requests.Response()
            resp._content = content
            resp.status_code = 200
            return resp

        api = Api.with_creds(  # NOQA:S106
            username='******',
            url='http://127.0.0.1',
            password='******',
        )
        requests_mock.add_matcher(matcher)
        query = api.User.get()
        query.request_args.headers = {'service_header': 'value'}
        query.get()
コード例 #9
0
    def test_special_headers_for_loging(self, requests_mock):
        """Test special headers for loging."""
        def matcher(request):  # NOQA: WPS430
            r = request._request
            assert 'SDK-VERSION' in r.headers
            assert 'spec_header' in r.headers
            content = b'{"sid": "1","status": "ok"}'
            resp = requests.Response()
            resp._content = content
            resp.status_code = 200
            resp.headers = {
                'content-type': 'application/json',
            }
            return resp

        api = Api.with_creds(  # NOQA:S106
            username='******',
            url='http://127.0.0.1',
            password='******',
            special_headers_for_login={'spec_header': 'value'},
        )
        requests_mock.add_matcher(matcher)
        api.get_new_sid()