예제 #1
0
def test_package_not_found_unsafe_missing(monkeypatch):
    safe_join = pretend.call_recorder(
        lambda *a, **k: "/tmp/packages/any/t/test-1.0.tar.gz"
    )

    def raising_open(*args, **kwargs):
        raise IOError

    _open = pretend.call_recorder(raising_open)

    monkeypatch.setattr(simple, "safe_join", safe_join)
    monkeypatch.setattr(simple, "open", _open, raising=False)

    app = pretend.stub(
        config=pretend.stub(
            paths=pretend.stub(packages="/tmp"),
        ),
    )
    request = pretend.stub()

    with pytest.raises(NotFound):
        simple.package(app, request, path="packages/any/t/test-1.0.tar.gz")

    assert safe_join.calls == [
        pretend.call("/tmp", "packages/any/t/test-1.0.tar.gz"),
    ]
    assert _open.calls == [
        pretend.call("/tmp/packages/any/t/test-1.0.tar.gz", "rb"),
    ]
예제 #2
0
def test_package_not_found_unsafe_missing(monkeypatch):
    safe_join = pretend.call_recorder(
        lambda *a, **k: "/tmp/packages/any/t/test-1.0.tar.gz")

    def raising_open(*args, **kwargs):
        raise IOError

    _open = pretend.call_recorder(raising_open)

    monkeypatch.setattr(simple, "safe_join", safe_join)
    monkeypatch.setattr(simple, "open", _open, raising=False)

    app = pretend.stub(config=pretend.stub(
        paths=pretend.stub(packages="/tmp"), ), )
    request = pretend.stub()

    with pytest.raises(NotFound):
        simple.package(app, request, path="packages/any/t/test-1.0.tar.gz")

    assert safe_join.calls == [
        pretend.call("/tmp", "packages/any/t/test-1.0.tar.gz"),
    ]
    assert _open.calls == [
        pretend.call("/tmp/packages/any/t/test-1.0.tar.gz", "rb"),
    ]
예제 #3
0
def test_package_not_found_unsafe(monkeypatch):
    safe_join = pretend.call_recorder(lambda *a, **k: None)
    monkeypatch.setattr(simple, "safe_join", safe_join)

    app = pretend.stub(config=pretend.stub(
        paths=pretend.stub(packages="/tmp"), ), )
    request = pretend.stub()

    with pytest.raises(NotFound):
        simple.package(app, request, path="packages/any/t/test-1.0.tar.gz")

    assert safe_join.calls == [
        pretend.call("/tmp", "packages/any/t/test-1.0.tar.gz"),
    ]
예제 #4
0
def test_package(serial, md5_hash, monkeypatch):
    safe_join = pretend.call_recorder(
        lambda *a, **k: "/tmp/packages/any/t/test-1.0.tar.gz"
    )
    _fp = pretend.stub(__enter__=lambda: None, __exit__=lambda *a: None)
    _open = pretend.call_recorder(lambda *a, **k: _fp)
    wrap_file = lambda *a, **k: None
    mtime = pretend.call_recorder(lambda f: 123457)
    getsize = pretend.call_recorder(lambda f: 54321)

    monkeypatch.setattr(simple, "safe_join", safe_join)
    monkeypatch.setattr(simple, "open", _open, raising=False)
    monkeypatch.setattr(simple, "wrap_file", wrap_file)
    monkeypatch.setattr(os.path, "getmtime", mtime)
    monkeypatch.setattr(os.path, "getsize", getsize)

    gpff = pretend.call_recorder(lambda p: "test")
    get_md5 = pretend.call_recorder(
        lambda p: md5_hash
    )
    get_last_serial = pretend.call_recorder(lambda p: serial)

    app = pretend.stub(
        config=pretend.stub(
            cache=pretend.stub(browser=False, varnish=False),
            paths=pretend.stub(packages="/tmp"),
        ),
        models=pretend.stub(
            packaging=pretend.stub(
                get_project_for_filename=gpff,
                get_filename_md5=get_md5,
                get_last_serial=get_last_serial,
            ),
        ),
    )
    request = pretend.stub(environ=create_environ())

    resp = simple.package(app, request, path="packages/any/t/test-1.0.tar.gz")

    if serial:
        assert resp.headers["X-PyPI-Last-Serial"] == str(serial)
    else:
        assert "X-PyPI-Last-Serial" not in resp.headers

    assert resp.headers["Surrogate-Key"] == "package package~test"
    assert resp.headers["Content-Length"] == "54321"

    assert safe_join.calls == [
        pretend.call("/tmp", "packages/any/t/test-1.0.tar.gz"),
    ]
    assert _open.calls == [
        pretend.call("/tmp/packages/any/t/test-1.0.tar.gz", "rb"),
    ]
    assert mtime.calls == [pretend.call("/tmp/packages/any/t/test-1.0.tar.gz")]
    assert getsize.calls == [
        pretend.call("/tmp/packages/any/t/test-1.0.tar.gz"),
    ]
    assert gpff.calls == [pretend.call("test-1.0.tar.gz")]
    assert get_md5.calls == [pretend.call("test-1.0.tar.gz")]
    assert get_last_serial.calls == [pretend.call("test")]
예제 #5
0
def test_package_not_found_unsafe(monkeypatch):
    safe_join = pretend.call_recorder(lambda *a, **k: None)
    monkeypatch.setattr(simple, "safe_join", safe_join)

    app = pretend.stub(
        config=pretend.stub(
            paths=pretend.stub(packages="/tmp"),
        ),
    )
    request = pretend.stub()

    with pytest.raises(NotFound):
        simple.package(app, request, path="packages/any/t/test-1.0.tar.gz")

    assert safe_join.calls == [
        pretend.call("/tmp", "packages/any/t/test-1.0.tar.gz"),
    ]
예제 #6
0
def test_package(serial, md5_hash, monkeypatch):
    safe_join = pretend.call_recorder(
        lambda *a, **k: "/tmp/packages/any/t/test-1.0.tar.gz")
    _fp = pretend.stub(__enter__=lambda: None, __exit__=lambda *a: None)
    _open = pretend.call_recorder(lambda *a, **k: _fp)
    wrap_file = lambda *a, **k: None
    mtime = pretend.call_recorder(lambda f: 123457)
    getsize = pretend.call_recorder(lambda f: 54321)

    monkeypatch.setattr(simple, "safe_join", safe_join)
    monkeypatch.setattr(simple, "open", _open, raising=False)
    monkeypatch.setattr(simple, "wrap_file", wrap_file)
    monkeypatch.setattr(os.path, "getmtime", mtime)
    monkeypatch.setattr(os.path, "getsize", getsize)

    gpff = pretend.call_recorder(lambda p: "test")
    get_md5 = pretend.call_recorder(lambda p: md5_hash)
    get_last_serial = pretend.call_recorder(lambda p: serial)

    app = pretend.stub(
        config=pretend.stub(
            cache=pretend.stub(browser=False, varnish=False),
            paths=pretend.stub(packages="/tmp"),
        ),
        models=pretend.stub(packaging=pretend.stub(
            get_project_for_filename=gpff,
            get_filename_md5=get_md5,
            get_last_serial=get_last_serial,
        ), ),
    )
    request = pretend.stub(environ=create_environ())

    resp = simple.package(app, request, path="packages/any/t/test-1.0.tar.gz")

    if serial:
        assert resp.headers["X-PyPI-Last-Serial"] == str(serial)
    else:
        assert "X-PyPI-Last-Serial" not in resp.headers

    assert resp.headers["Surrogate-Key"] == "package package~test"
    assert resp.headers["Content-Length"] == "54321"

    assert safe_join.calls == [
        pretend.call("/tmp", "packages/any/t/test-1.0.tar.gz"),
    ]
    assert _open.calls == [
        pretend.call("/tmp/packages/any/t/test-1.0.tar.gz", "rb"),
    ]
    assert mtime.calls == [pretend.call("/tmp/packages/any/t/test-1.0.tar.gz")]
    assert getsize.calls == [
        pretend.call("/tmp/packages/any/t/test-1.0.tar.gz"),
    ]
    assert gpff.calls == [pretend.call("test-1.0.tar.gz")]
    assert get_md5.calls == [pretend.call("test-1.0.tar.gz")]
    assert get_last_serial.calls == [pretend.call("test")]