Пример #1
0
def testRetryMaxAttempt(monkeypatch):
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    global call
    call = 0

    def request_callback(request):
        global call
        if call < 4:
            call += 1
            return (
                200, headers,
                '{"meta":{"code":999,"message":"PROBLEM","retryable":true, "details":[]},"data":{}}'
            )
        else:
            return (
                200, headers,
                '{"meta":{"code":200,"message":"OK","details":[]},"data":{}}')

    responses.add_callback(responses.GET,
                           'https://region-api.postmen.com/v3/labels',
                           callback=request_callback)
    api = Postmen('KEY', 'REGION')
    before = time.time()
    api.get('labels')
    after = time.time()
    responses.reset()
    monkeypatch.setattr(time, 'sleep', lambda s: None)
Пример #2
0
def testRetryDelay():
    global call
    call = 0

    def request_callback(request):
        global call
        if call == 0:
            call = 1
            return (
                200, headers,
                '{"meta":{"code":999,"message":"PROBLEM","retryable":true, "details":[]},"data":{}}'
            )
        elif call == 1:
            call = 2
            "second attempt"
            return (
                200, headers,
                '{"meta":{"code":999,"message":"PROBLEM","retryable":true,"details":[]},"data":{}}'
            )
        elif call == 2:
            call = 3
            return (
                200, headers,
                '{"meta":{"code":200,"message":"OK","details":[]},"data":{}}')

    responses.add_callback(responses.GET,
                           'https://region-api.postmen.com/v3/labels',
                           callback=request_callback)
    api = Postmen('KEY', 'REGION')
    api.get('labels')
    responses.reset()
Пример #3
0
def testRetryMaxAttemptExceeded(monkeypatch):
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    global call
    call = 0

    def request_callback(request):
        global call
        if call < 5:
            call += 1
            return (
                200, headers,
                '{"meta":{"code":999,"message":"PROBLEM","retryable":true, "details":[]},"data":{}}'
            )
        else:
            pytest.fail("Maximum 5 attempts of retry, test #10 failed")

    responses.add_callback(responses.GET,
                           'https://region-api.postmen.com/v3/labels',
                           callback=request_callback)
    api = Postmen('KEY', 'REGION')
    with pytest.raises(PostmenException) as e:
        api.get('labels')
    responses.reset()
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    assert "PROBLEM" in str(e.value.message())
    assert 999 == e.value.code()
Пример #4
0
def testArgument12(monkeypatch):
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    global call
    call = 0

    def request_callback(request):
        global call
        if call == 0:
            call = 1
            return (
                200, headers,
                '{"meta":{"code":999,"message":"PROBLEM","retryable":false, "details":[]},"data":{}}'
            )
        elif call == 1:
            pytest.fail("Shall not retry if non retryable, test #12 failed")

    responses.add_callback(responses.GET,
                           'https://region-api.postmen.com/v3/labels',
                           callback=request_callback)
    api = Postmen('KEY', 'REGION')
    with pytest.raises(PostmenException) as e:
        api.get('labels')
    responses.reset()
    # print(e)
    assert "PROBLEM" in str(e.value.message())
    assert not e.value.retryable()
    monkeypatch.setattr(time, 'sleep', lambda s: None)
Пример #5
0
def testRateLimit(monkeypatch):
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    global call
    call = 0

    def request_callback(request):
        global call
        if call == 0:
            call = 1
            return (
                200, exceeded,
                '{"meta":{"code":429,"message":"EXCEEDED","retryable":true, "details":[]},"data":{}}'
            )
        elif call == 1:
            return (
                200, headers,
                '{"meta":{"code":200,"message":"OK","details":[]},"data":{}}')

    responses.add_callback(responses.GET,
                           'https://region-api.postmen.com/v3/labels',
                           callback=request_callback)
    api = Postmen('KEY', 'REGION')
    api.get('labels')
    responses.reset()
    monkeypatch.setattr(time, 'sleep', lambda s: None)
def testArguments7():
    response = '{"meta":{"code":999,"message":"NOT OK","details":[]},"data":{}}'
    responses.add(responses.GET, 'https://REGION-api.postmen.com/v3/labels', adding_headers=headers, body=response, status=200, content_type='text/plain')
    api = Postmen('KEY', 'REGION')
    api.get('labels', safe=True)
    responses.reset()
    e = api.getError()
    assert "NOT OK" in str(e.message())
def testNonSerializableJSON():
    response = 'THIS IS NOT A VALID JSON OBJECT'
    responses.add(responses.GET, 'https://REGION-api.postmen.com/v3/labels', adding_headers=headers, body=response, status=200, content_type='text/plain')
    api = Postmen('KEY', 'REGION')
    with pytest.raises(PostmenException) as e:
        api.get('labels')
    responses.reset()
    assert "Something went wrong on Postmen's end" in str(e.value)
    assert 500 == e.value.code()
    assert not e.value.retryable()
def testArgument14():
    response = '{"meta":{"code":429,"message":"EXCEEDED","retryable":true, "details":[]},"data":{}}'
    responses.add(responses.GET, 'https://REGION-api.postmen.com/v3/labels', adding_headers=exceeded, body=response, status=200)
    api = Postmen('KEY', 'REGION', rate=False)
    with pytest.raises(PostmenException) as e:
        api.get('labels')
    responses.reset()
    assert "EXCEEDED" in str(e.value.message())
    assert 429 == e.value.code()
    assert e.value.retryable()
Пример #9
0
def testNotRaiseException():
    response = '{"meta":{"code":200,"message":"OK","details":[]},"data":{}}'
    responses.add(responses.GET,
                  'https://region-api.postmen.com/v3/labels',
                  adding_headers=headers,
                  body=response,
                  status=200)
    api = Postmen('KEY', 'REGION')
    api.get('labels')
    responses.reset()
def testException4():
    response = '{"meta":{"code":999,"message":"PROBLEM","retryable":false,"details":[]},"data":{}}'
    responses.add(responses.GET, 'https://REGION-api.postmen.com/v3/labels', adding_headers=headers, body=response, status=200)
    api = Postmen('KEY', 'REGION')
    with pytest.raises(PostmenException) as e:
        api.get('labels')
    responses.reset()
    assert "PROBLEM" in str(e.value.message())
    assert 999 == e.value.code()
    assert not e.value.retryable()
Пример #11
0
def testArguments7():
    response = '{"meta":{"code":999,"message":"NOT OK","details":[]},"data":{}}'
    responses.add(responses.GET,
                  'https://region-api.postmen.com/v3/labels',
                  adding_headers=headers,
                  body=response,
                  status=200,
                  content_type='text/plain')
    api = Postmen('KEY', 'REGION')
    api.get('labels', safe=True)
    responses.reset()
    e = api.getError()
    assert "NOT OK" in str(e.message())
Пример #12
0
def testException4():
    response = '{"meta":{"code":999,"message":"PROBLEM","retryable":false,"details":[]},"data":{}}'
    responses.add(responses.GET,
                  'https://region-api.postmen.com/v3/labels',
                  adding_headers=headers,
                  body=response,
                  status=200)
    api = Postmen('KEY', 'REGION')
    with pytest.raises(PostmenException) as e:
        api.get('labels')
    responses.reset()
    assert "PROBLEM" in str(e.value.message())
    assert 999 == e.value.code()
    assert not e.value.retryable()
Пример #13
0
def testArgument14():
    response = '{"meta":{"code":429,"message":"EXCEEDED","retryable":true, "details":[]},"data":{}}'
    responses.add(responses.GET,
                  'https://region-api.postmen.com/v3/labels',
                  adding_headers=exceeded,
                  body=response,
                  status=200)
    api = Postmen('KEY', 'REGION', rate=False)
    with pytest.raises(PostmenException) as e:
        api.get('labels')
    responses.reset()
    assert "EXCEEDED" in str(e.value.message())
    assert 429 == e.value.code()
    assert e.value.retryable()
Пример #14
0
def testNonSerializableJSON():
    response = 'THIS IS NOT A VALID JSON OBJECT'
    responses.add(responses.GET,
                  'https://region-api.postmen.com/v3/labels',
                  adding_headers=headers,
                  body=response,
                  status=200,
                  content_type='text/plain')
    api = Postmen('KEY', 'REGION')
    with pytest.raises(PostmenException) as e:
        api.get('labels')
    responses.reset()
    assert "Something went wrong on Postmen's end" in str(e.value)
    assert 500 == e.value.code()
    assert not e.value.retryable()
def testRateLimit(monkeypatch):
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    global call
    call = 0
    def request_callback(request):
        global call
        if call == 0 :
            call = 1
            return (200, exceeded,  '{"meta":{"code":429,"message":"EXCEEDED","retryable":true, "details":[]},"data":{}}')
        elif call == 1 :
            return (200, headers,  '{"meta":{"code":200,"message":"OK","details":[]},"data":{}}')
    responses.add_callback(responses.GET, 'https://REGION-api.postmen.com/v3/labels', callback=request_callback)
    api = Postmen('KEY', 'REGION')
    api.get('labels')
    responses.reset()
    monkeypatch.setattr(time, 'sleep', lambda s: None)
def testIncorrectResponseHeaders():
    response = '{"meta":{"code":200,"message":"OK","details":[]},"data":{"key":"value"}}'
    responses.add(responses.GET, 'https://REGION-api.postmen.com/v3/labels', adding_headers=incorrect, body=response, status=200)
    api = Postmen('KEY', 'REGION')
    ret = api.get('labels')
    assert ret['key'] == 'value'
    responses.reset()
def testArgument18() :
    response = '{"meta":{"code":999,"message":"NOT OK","details":[]},"data":{}}'
    responses.add(responses.GET, 'https://REGION-api.postmen.com/v3/labels', adding_headers=headers, body=response, status=200)
    api = Postmen('KEY', 'REGION', raw=True)
    ret = api.get('labels')
    assert ret == response
    responses.reset()
def testRetryMaxAttempt(monkeypatch):
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    global call
    call = 0
    def request_callback(request):
        global call
        if call < 4 :
            call += 1
            return (200, headers,  '{"meta":{"code":999,"message":"PROBLEM","retryable":true, "details":[]},"data":{}}')
        else :
            return (200, headers,  '{"meta":{"code":200,"message":"OK","details":[]},"data":{}}')
    responses.add_callback(responses.GET, 'https://REGION-api.postmen.com/v3/labels', callback=request_callback)
    api = Postmen('KEY', 'REGION')
    before = time.time()
    api.get('labels')
    after = time.time()
    responses.reset()
    monkeypatch.setattr(time, 'sleep', lambda s: None)
def testTime():
    response = '{"meta":{"code":200,"message":"OK","details":[]},"data":{"when": "2016-01-31T16:45:46+00:00"}}'
    responses.add(responses.GET, 'https://REGION-api.postmen.com/v3/labels', adding_headers=headers, body=response, status=200, content_type='text/plain')
    api = Postmen('KEY', 'REGION')
    res = api.get('labels')
    assert res['when'] == '2016-01-31T16:45:46+00:00' 
    res = api.get('labels', time=True)
    assert isinstance(res['when'], datetime)
    api = Postmen('KEY', 'REGION', time=True)
    res = api.get('labels', time=True)
    assert isinstance(res['when'], datetime)
    assert res['when'].year == 2016
    assert res['when'].month == 1
    assert res['when'].day == 31
    assert res['when'].hour == 16
    assert res['when'].minute == 45
    assert res['when'].second == 46
    responses.reset()
def testRetryDelay():
    global call
    call = 0
    def request_callback(request):
        global call
        if call == 0 :
            call = 1
            return (200, headers,  '{"meta":{"code":999,"message":"PROBLEM","retryable":true, "details":[]},"data":{}}')
        elif call == 1 :
            call = 2
            "second attempt"
            return (200, headers,  '{"meta":{"code":999,"message":"PROBLEM","retryable":true,"details":[]},"data":{}}')
        elif call == 2 :
            call = 3
            return (200, headers,  '{"meta":{"code":200,"message":"OK","details":[]},"data":{}}')
    responses.add_callback(responses.GET, 'https://REGION-api.postmen.com/v3/labels', callback=request_callback)
    api = Postmen('KEY', 'REGION')
    api.get('labels')
    responses.reset()
def testRetryMaxAttemptExceeded(monkeypatch):
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    global call
    call = 0
    def request_callback(request):
        global call
        if call < 5 :
            call += 1
            return (200, headers,  '{"meta":{"code":999,"message":"PROBLEM","retryable":true, "details":[]},"data":{}}')
        else :
            pytest.fail("Maximum 5 attempts of retry, test #10 failed")
    responses.add_callback(responses.GET, 'https://REGION-api.postmen.com/v3/labels', callback=request_callback)
    api = Postmen('KEY', 'REGION')
    with pytest.raises(PostmenException) as e:
        api.get('labels')
    responses.reset()
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    assert "PROBLEM" in str(e.value.message())
    assert 999 == e.value.code()
Пример #22
0
def testArgument18():
    response = '{"meta":{"code":999,"message":"NOT OK","details":[]},"data":{}}'
    responses.add(responses.GET,
                  'https://region-api.postmen.com/v3/labels',
                  adding_headers=headers,
                  body=response,
                  status=200)
    api = Postmen('KEY', 'REGION', raw=True)
    ret = api.get('labels')
    assert ret == response
    responses.reset()
Пример #23
0
def testIncorrectResponseHeaders():
    response = '{"meta":{"code":200,"message":"OK","details":[]},"data":{"key":"value"}}'
    responses.add(responses.GET,
                  'https://region-api.postmen.com/v3/labels',
                  adding_headers=incorrect,
                  body=response,
                  status=200)
    api = Postmen('KEY', 'REGION')
    ret = api.get('labels')
    assert ret['key'] == 'value'
    responses.reset()
def testArgument12(monkeypatch):
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    global call
    call = 0
    def request_callback(request):
        global call
        if call == 0 :
            call = 1
            return (200, headers,  '{"meta":{"code":999,"message":"PROBLEM","retryable":false, "details":[]},"data":{}}')
        elif call == 1 :
            pytest.fail("Shall not retry if non retryable, test #12 failed")
    responses.add_callback(responses.GET, 'https://REGION-api.postmen.com/v3/labels', callback=request_callback)
    api = Postmen('KEY', 'REGION')
    with pytest.raises(PostmenException) as e:
        api.get('labels')
    responses.reset()
    #print e
    assert "PROBLEM" in str(e.value.message())
    assert not e.value.retryable()
    monkeypatch.setattr(time, 'sleep', lambda s: None)
Пример #25
0
def testTime():
    response = '{"meta":{"code":200,"message":"OK","details":[]},"data":{"when": "2016-01-31T16:45:46+00:00"}}'
    responses.add(responses.GET,
                  'https://region-api.postmen.com/v3/labels',
                  adding_headers=headers,
                  body=response,
                  status=200,
                  content_type='text/plain')
    api = Postmen('KEY', 'REGION')
    res = api.get('labels')
    assert res['when'] == '2016-01-31T16:45:46+00:00'
    res = api.get('labels', time=True)
    assert isinstance(res['when'], datetime)
    api = Postmen('KEY', 'REGION', time=True)
    res = api.get('labels', time=True)
    assert isinstance(res['when'], datetime)
    assert res['when'].year == 2016
    assert res['when'].month == 1
    assert res['when'].day == 31
    assert res['when'].hour == 16
    assert res['when'].minute == 45
    assert res['when'].second == 46
    responses.reset()
def testNotRaiseException() :
    response = '{"meta":{"code":200,"message":"OK","details":[]},"data":{}}'
    responses.add(responses.GET, 'https://REGION-api.postmen.com/v3/labels', adding_headers=headers, body=response, status=200)
    api = Postmen('KEY', 'REGION')
    api.get('labels')
    responses.reset()