2121 get_identity_token ,
2222 main ,
2323)
24- from pypi_attestations ._impl import Attestation
24+ from pypi_attestations ._impl import Attestation , AttestationError
2525
2626ONLINE_TESTS = "CI" in os .environ or "TEST_INTERACTIVE" in os .environ
2727online = pytest .mark .skipif (not ONLINE_TESTS , reason = "online tests not enabled" )
@@ -101,9 +101,7 @@ def test_sign_command(tmp_path: Path) -> None:
101101
102102
103103@online
104- def test_sign_command_failures (
105- tmp_path : Path , monkeypatch : pytest .MonkeyPatch , caplog : pytest .LogCaptureFixture
106- ) -> None :
104+ def test_sign_missing_file (caplog : pytest .LogCaptureFixture ) -> None :
107105 # Missing file
108106 with pytest .raises (SystemExit ):
109107 run_main_with_command (
@@ -115,9 +113,10 @@ def test_sign_command_failures(
115113 )
116114
117115 assert "not_exist.txt is not a file" in caplog .text
118- caplog .clear ()
119116
120- # Signature already exists
117+
118+ @online
119+ def test_sign_signature_already_exists (tmp_path : Path , caplog : pytest .LogCaptureFixture ) -> None :
121120 artifact = tmp_path / artifact_path .with_suffix (".copy2.whl" ).name
122121 artifact .touch (exist_ok = False )
123122
@@ -135,7 +134,11 @@ def test_sign_command_failures(
135134 assert "already exists" in caplog .text
136135 caplog .clear ()
137136
138- # Invalid token
137+
138+ @online
139+ def test_sign_invalid_token (
140+ monkeypatch : pytest .MonkeyPatch , caplog : pytest .LogCaptureFixture
141+ ) -> None :
139142 def return_invalid_token () -> str :
140143 return "invalid-token"
141144
@@ -146,13 +149,49 @@ def return_invalid_token() -> str:
146149 [
147150 "sign" ,
148151 "--staging" ,
149- artifact .as_posix (),
152+ artifact_path .as_posix (),
150153 ]
151154 )
152155
153156 assert "Failed to detect identity" in caplog .text
154157
155158
159+ @online
160+ def test_sign_invalid_artifact (caplog : pytest .LogCaptureFixture , tmp_path : Path ) -> None :
161+ artifact = tmp_path / "pkg-1.0.0.exe"
162+ artifact .touch (exist_ok = False )
163+
164+ with pytest .raises (SystemExit ):
165+ run_main_with_command (["sign" , "--staging" , artifact .as_posix ()])
166+
167+ assert "Invalid Python package distribution" in caplog .text
168+
169+
170+ @online
171+ def test_sign_fail_to_sign (
172+ monkeypatch : pytest .MonkeyPatch , caplog : pytest .LogCaptureFixture , tmp_path : Path
173+ ) -> None :
174+ monkeypatch .setattr (pypi_attestations ._cli , "Attestation" , stub (sign = raiser (AttestationError )))
175+ copied_artifact = tmp_path / artifact_path .with_suffix (".copy.whl" ).name
176+ shutil .copy (artifact_path , copied_artifact )
177+
178+ with pytest .raises (SystemExit ):
179+ run_main_with_command (["sign" , "--staging" , copied_artifact .as_posix ()])
180+
181+ assert "Failed to sign:" in caplog .text
182+
183+
184+ @online
185+ def test_sign_invalid_distribution (tmp_path : Path , caplog : pytest .LogCaptureFixture ) -> None :
186+ copied_artifact = tmp_path / artifact_path .with_suffix (".copy.whl2" ).name
187+ shutil .copy (artifact_path , copied_artifact )
188+
189+ with pytest .raises (SystemExit ):
190+ run_main_with_command (["sign" , "--staging" , copied_artifact .as_posix ()])
191+
192+ assert "Invalid Python package distribution" in caplog .text
193+
194+
156195def test_inspect_command (caplog : pytest .LogCaptureFixture , monkeypatch : pytest .MonkeyPatch ) -> None :
157196 # Happy path
158197 run_main_with_command (["inspect" , attestation_path .as_posix ()])
0 commit comments