def test_not_found(defconfig):
    request = Request('GET', '/invalid-url-wow')
    resp = request.send()
    assert resp.status == 404
    request = Request('GET', '/authorize/uh-oh')
    resp = request.send()
    assert resp.status == 404
def test_send_with_appropriate_method(mocker, req_params):
    req = Request(**req_params)
    methods = Request._allowed_methods
    for method in methods:
        req.method = method

        mock_reqfunc = mocker.patch.object(requests.Session,
                                           method.lower(),
                                           autospec=True)

        assert mock_reqfunc.call_count == 0
        req.send()
        mock_reqfunc.assert_called_once_with(mocker.ANY,
                                             req.build_url(),
                                             data=req._content,
                                             headers=req.headers)
def test_auth_missing_signature(defconfig):
    random_msg = uuid.uuid4().hex
    request = Request('GET', '/authorize', {
        'echo': random_msg,
    })
    resp = request.send()
    assert resp.status == 401
def test_auth_missing_signature(defconfig):
    random_msg = uuid.uuid4().hex
    request = Request('GET', '/authorize', {
        'echo': random_msg,
    })
    # let it bypass actual signing
    request._sign = lambda *args, **kwargs: None
    resp = request.send()
    assert resp.status == 401
def test_auth(defconfig):
    random_msg = uuid.uuid4().hex
    request = Request('GET', '/authorize', {
        'echo': random_msg,
    })
    resp = request.send()
    assert resp.status == 200
    data = resp.json()
    assert data['authorized'] == 'yes'
    assert data['echo'] == random_msg
def test_send_returns_appropriate_sorna_response(mocker, req_params,
                                                 mock_sorna_resp):
    req = Request(**req_params)
    methods = Request._allowed_methods
    for method in methods:
        req.method = method

        mock_reqfunc = mocker.patch.object(requests.Session,
                                           method.lower(),
                                           autospec=True)
        mock_reqfunc.return_value, conf = mock_sorna_resp

        resp = req.send()

        assert resp.status == conf['status_code']
        assert resp.reason == conf['reason']
        assert resp.content_type == conf['headers']['content-type']
        assert resp.content_length == conf['headers']['content-length']
        assert resp.text() == conf['text']
        assert resp.json() == json.loads(conf['text'])
def test_send_not_allowed_request_raises_error(req_params):
    req_params['method'] = 'STRANGE'
    req = Request(**req_params)

    with pytest.raises(AssertionError):
        req.send()
def test_connection(defconfig):
    request = Request('GET', '/')
    resp = request.send()
    assert 'version' in resp.json()
def test_auth_missing_body(defconfig):
    request = Request('GET', '/authorize')
    request.sign()
    resp = request.send()
    assert resp.status == 400
def test_auth_malformed(defconfig):
    request = Request('GET', '/authorize')
    request.content = b'<this is not json>'
    request.sign()
    resp = request.send()
    assert resp.status == 400