| 1 |
from contextlib import contextmanager |
| 2 |
from tempfile import NamedTemporaryFile |
| 3 |
import os |
| 4 |
import io |
| 5 |
import pathlib |
| 6 |
|
| 7 |
import pytest |
| 8 |
import responses |
| 9 |
from testpath import modified_env |
| 10 |
from unittest.mock import patch |
| 11 |
|
| 12 |
from flit import upload |
| 13 |
from flit.build import ALL_FORMATS |
| 14 |
from flit.upload import get_repository, RepoDetails |
| 15 |
|
| 16 |
samples_dir = pathlib.Path(__file__).parent / 'samples' |
| 17 |
|
| 18 |
repo_settings = upload.RepoDetails( |
| 19 |
url=upload.PYPI, username='user', password='pw' |
| 20 |
) |
| 21 |
|
| 22 |
pypirc1 = """ |
| 23 |
[distutils] |
| 24 |
index-servers = |
| 25 |
pypi |
| 26 |
|
| 27 |
[pypi] |
| 28 |
username: fred |
| 29 |
password: s3cret |
| 30 |
""" |
| 31 |
# That's not a real password. Well, hopefully not. |
| 32 |
|
| 33 |
@contextmanager |
| 34 |
def temp_pypirc(content): |
| 35 |
temp_file = NamedTemporaryFile("w+", delete=False) |
| 36 |
try: |
| 37 |
temp_file.write(content) |
| 38 |
temp_file.close() |
| 39 |
yield temp_file.name |
| 40 |
finally: |
| 41 |
os.unlink(temp_file.name) |
| 42 |
|
| 43 |
|
| 44 |
@responses.activate |
| 45 |
def test_upload(copy_sample): |
| 46 |
responses.add(responses.POST, upload.PYPI, status=200) |
| 47 |
td = copy_sample('module1_toml') |
| 48 |
|
| 49 |
with temp_pypirc(pypirc1) as pypirc, \ |
| 50 |
patch('flit.upload.get_repository', return_value=repo_settings): |
| 51 |
upload.main(td / 'pyproject.toml', repo_name='pypi', pypirc_path=pypirc) |
| 52 |
|
| 53 |
assert len(responses.calls) == 2 |
| 54 |
|
| 55 |
def test_get_repository(): |
| 56 |
with temp_pypirc(pypirc1) as pypirc: |
| 57 |
repo = upload.get_repository(pypirc_path=pypirc, project_name='foo') |
| 58 |
assert repo.url == upload.PYPI |
| 59 |
assert repo.username == 'fred' |
| 60 |
assert repo.password == 's3cret' |
| 61 |
|
| 62 |
def test_get_repository_env(): |
| 63 |
with temp_pypirc(pypirc1) as pypirc, \ |
| 64 |
modified_env({ |
| 65 |
'FLIT_INDEX_URL': 'https://pypi.example.com', |
| 66 |
'FLIT_USERNAME': 'alice', |
| 67 |
'FLIT_PASSWORD': 'p4ssword', # Also not a real password |
| 68 |
}): |
| 69 |
repo = upload.get_repository(pypirc_path=pypirc, project_name='foo') |
| 70 |
# Because we haven't specified a repo name, environment variables should |
| 71 |
# have higher priority than the config file. |
| 72 |
assert repo.url == 'https://pypi.example.com' |
| 73 |
assert repo.username == 'alice' |
| 74 |
assert repo.password == 'p4ssword' |
| 75 |
|
| 76 |
@contextmanager |
| 77 |
def _fake_keyring(d): |
| 78 |
class FakeKeyring: |
| 79 |
@staticmethod |
| 80 |
def get_password(service_name, username): |
| 81 |
return d.get(service_name, {}).get(username, None) |
| 82 |
|
| 83 |
class FakeKeyringErrMod: |
| 84 |
class KeyringError(Exception): |
| 85 |
pass |
| 86 |
|
| 87 |
with patch.dict('sys.modules', { |
| 88 |
'keyring': FakeKeyring(), 'keyring.errors': FakeKeyringErrMod(), |
| 89 |
}): |
| 90 |
yield |
| 91 |
|
| 92 |
pypirc2 = """ |
| 93 |
[distutils] |
| 94 |
index-servers = |
| 95 |
pypi |
| 96 |
|
| 97 |
[pypi] |
| 98 |
username: fred |
| 99 |
""" |
| 100 |
|
| 101 |
def test_get_repository_keyring(monkeypatch): |
| 102 |
monkeypatch.delenv('FLIT_PASSWORD', raising=False) |
| 103 |
with _fake_keyring({upload.PYPI: {'fred': 'tops3cret'}}): |
| 104 |
repo = get_repository(pypirc_path=io.StringIO(pypirc2), project_name='foo') |
| 105 |
assert repo == RepoDetails(upload.PYPI, username='fred', password='tops3cret') |
| 106 |
|
| 107 |
for token_key in ['pypi_token:project:foo', 'pypi_token:user:fred']: |
| 108 |
with _fake_keyring({upload.PYPI: {token_key: 'xyz'}}): |
| 109 |
repo = get_repository(pypirc_path=io.StringIO(pypirc2), project_name='foo') |
| 110 |
assert repo == RepoDetails(upload.PYPI, username='__token__', password='xyz') |
| 111 |
|
| 112 |
|
| 113 |
pypirc3_repo = "https://invalid-repo.inv" |
| 114 |
pypirc3_user = "test" |
| 115 |
pypirc3_pass = "not_a_real_password" |
| 116 |
pypirc3 = f""" |
| 117 |
[distutils] = |
| 118 |
index-servers = |
| 119 |
test123 |
| 120 |
|
| 121 |
[test123] |
| 122 |
repository: {pypirc3_repo} |
| 123 |
username: {pypirc3_user} |
| 124 |
password: {pypirc3_pass} |
| 125 |
""" |
| 126 |
|
| 127 |
|
| 128 |
def test_upload_pypirc_file(copy_sample): |
| 129 |
with temp_pypirc(pypirc3) as pypirc, \ |
| 130 |
patch("flit.upload.upload_file") as upload_file: |
| 131 |
td = copy_sample("module1_toml") |
| 132 |
formats = list(ALL_FORMATS)[:1] |
| 133 |
upload.main( |
| 134 |
td / "pyproject.toml", |
| 135 |
formats=set(formats), |
| 136 |
repo_name="test123", |
| 137 |
pypirc_path=pypirc, |
| 138 |
) |
| 139 |
_, _, repo = upload_file.call_args[0] |
| 140 |
|
| 141 |
assert repo.url == pypirc3_repo |
| 142 |
assert repo.username == pypirc3_user |
| 143 |
assert repo.password == pypirc3_pass |
| 144 |
|
| 145 |
|
| 146 |
def test_upload_invalid_pypirc_file(copy_sample): |
| 147 |
with patch("flit.upload.upload_file"): |
| 148 |
td = copy_sample("module1_toml") |
| 149 |
formats = list(ALL_FORMATS)[:1] |
| 150 |
with pytest.raises(FileNotFoundError): |
| 151 |
upload.main( |
| 152 |
td / "pyproject.toml", |
| 153 |
formats=set(formats), |
| 154 |
repo_name="test123", |
| 155 |
pypirc_path="./file.invalid", |
| 156 |
) |