def test_returns_redirect_location_on_found_url(mocker):
    full_url = "http://reddit.com"
    mocker.patch(REPOSITORY_PATH, return_value=full_url)
    application = shared.setup_application()

    resp = application.get("/AbcdEF")

    assert full_url == resp.location
def test_returns_301_status_on_found_url(mocker):
    repository_mock = mocker.patch(REPOSITORY_PATH, return_value="http://reddit.com")
    application = shared.setup_application()
    short_code = "AbcdEF"

    resp = application.get("/" + short_code)

    assert 301 == resp.status_code
    repository_mock.assert_called_once_with(short_code)
def test_returns_404_on_not_found_url(mocker):
    repository_mock = mocker.patch(REPOSITORY_PATH, return_value=None)
    application = shared.setup_application()
    short_code = "AbcdEF"

    resp = application.get("/" + short_code, expect_errors=True)

    assert 404 == resp.status_code
    repository_mock.assert_called_once_with(short_code)
def test_returns_201_status_on_valid_input(mocker):
    mocker.patch(SHORTENER_PATH, return_value="foo")
    application = shared.setup_application()
    headers = {"Content-Type": "application/json"}
    body = {"url": "http://google.com"}

    resp = application.post("/shorten_url", json.dumps(body), headers=headers)

    assert 201 == resp.status_code
def test_returns_shortened_url_in_response_body(mocker):
    shortened_url = "foo"
    mocker.patch(SHORTENER_PATH, return_value=shortened_url)
    application = shared.setup_application()
    headers = {"Content-Type": "application/json"}
    body = {"url": "http://google.com"}

    resp = application.post("/shorten_url", json.dumps(body), headers=headers)

    assert shortened_url == resp.json["shortened_url"]
def test_returns_400_status_on_invalid_original_url(mocker):
    shortener = mocker.patch(SHORTENER_PATH)
    application = shared.setup_application()
    headers = {"Content-Type": "application/json"}
    body = {"url": "some url"}

    resp = application.post("/shorten_url",
                            json.dumps(body),
                            headers=headers,
                            expect_errors=True)

    assert 400 == resp.status_code
    assert 0 == shortener.call_count
def test_returns_500_status():
    application = shared.setup_application()
    resp = application.get("/error", expect_errors=500)
    assert 500 == resp.status_code
def test_returns_error():
    application = shared.setup_application()
    resp = application.get("/error", expect_errors=500)
    assert UNHANDLED_ERROR_TEXT == resp.json["error"]