1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
|
import configparser
from dataclasses import dataclass
import io
from subprocess import CalledProcessError
from typing import Any, Iterable, Optional, Sequence, Text
from unittest.mock import ANY
import pytest
from pytest_mock import MockerFixture
import passgithelper
@dataclass
class HelperConfig:
xdg_dir: Optional[str]
request: str
entry_data: Optional[bytes]
entry_name: Optional[str] = None
@pytest.fixture
def helper_config(mocker: MockerFixture, request: Any) -> Iterable[Any]:
xdg_mock = mocker.patch("xdg.BaseDirectory.load_first_config")
xdg_mock.return_value = request.param.xdg_dir
mocker.patch("sys.stdin.readlines").return_value = io.StringIO(
request.param.request
)
subprocess_mock = mocker.patch("subprocess.check_output")
if request.param.entry_data:
subprocess_mock.return_value = request.param.entry_data
else:
subprocess_mock.side_effect = CalledProcessError(1, ["pass"], "pass failed")
yield subprocess_mock
if request.param.entry_name is not None:
subprocess_mock.assert_called_once()
subprocess_mock.assert_called_with(
["pass", "show", request.param.entry_name], env=ANY
)
def test_handle_skip_nothing(monkeypatch: Any) -> None:
monkeypatch.delenv("PASS_GIT_HELPER_SKIP", raising=False)
passgithelper.handle_skip()
# should do nothing normally
def test_handle_skip_exits(monkeypatch: Any) -> None:
monkeypatch.setenv("PASS_GIT_HELPER_SKIP", "1")
with pytest.raises(SystemExit):
passgithelper.handle_skip()
class TestSkippingDataExtractor:
class ExtractorImplementation(passgithelper.SkippingDataExtractor):
def configure(self, config: configparser.SectionProxy) -> None:
pass
def __init__(self, skip_characters: int = 0) -> None:
super().__init__(skip_characters)
def _get_raw(
self, entry_text: Text, entry_lines: Sequence[Text] # noqa: ARG002
) -> Optional[Text]:
return entry_lines[0]
def test_smoke(self) -> None:
extractor = self.ExtractorImplementation(4)
assert extractor.get_value("foo", ["testthis"]) == "this"
def test_too_short(self) -> None:
extractor = self.ExtractorImplementation(8)
assert extractor.get_value("foo", ["testthis"]) == ""
extractor = self.ExtractorImplementation(10)
assert extractor.get_value("foo", ["testthis"]) == ""
class TestSpecificLineExtractor:
def test_smoke(self) -> None:
extractor = passgithelper.SpecificLineExtractor(1, 6)
assert (
extractor.get_value("foo", ["line 1", "user: bar", "more lines"]) == "bar"
)
def test_no_such_line(self) -> None:
extractor = passgithelper.SpecificLineExtractor(3, 6)
assert extractor.get_value("foo", ["line 1", "user: bar", "more lines"]) is None
class TestRegexSearchExtractor:
def test_smoke(self) -> None:
extractor = passgithelper.RegexSearchExtractor("^username: (.*)$", "")
assert (
extractor.get_value(
"foo",
[
"thepassword",
"somethingelse",
"username: user",
"username: second ignored",
],
)
== "user"
)
def test_missing_group(self) -> None:
with pytest.raises(ValueError, match="must contain"):
passgithelper.RegexSearchExtractor("^username: .*$", "")
def test_configuration(self) -> None:
extractor = passgithelper.RegexSearchExtractor("^username: (.*)$", "_username")
config = configparser.ConfigParser()
config.read_string(
r"""[test]
regex_username=^foo: (.*)$"""
)
extractor.configure(config["test"])
assert extractor._regex.pattern == r"^foo: (.*)$"
def test_configuration_checks_groups(self) -> None:
extractor = passgithelper.RegexSearchExtractor("^username: (.*)$", "_username")
config = configparser.ConfigParser()
config.read_string(
r"""[test]
regex_username=^foo: .*$"""
)
with pytest.raises(ValueError, match="must contain"):
extractor.configure(config["test"])
class TestEntryNameExtractor:
def test_smoke(self) -> None:
assert passgithelper.EntryNameExtractor().get_value("foo/bar", []) == "bar"
@pytest.mark.parametrize(
"helper_config",
[
HelperConfig(
None,
"",
b"ignored",
),
],
indirect=True,
)
@pytest.mark.usefixtures("helper_config")
def test_parse_mapping_file_missing() -> None:
with pytest.raises(RuntimeError):
passgithelper.parse_mapping(None)
@pytest.mark.parametrize(
"helper_config",
[
HelperConfig(
"test_data/smoke",
"",
b"ignored",
),
],
indirect=True,
)
@pytest.mark.usefixtures("helper_config")
def test_parse_mapping_from_xdg() -> None:
config = passgithelper.parse_mapping(None)
assert "mytest.com" in config
assert config["mytest.com"]["target"] == "dev/mytest"
class TestScript:
def test_help(self, capsys: Any) -> None:
with pytest.raises(SystemExit):
passgithelper.main(["--help"])
assert "usage: " in capsys.readouterr().out
def test_skip(self, monkeypatch: Any, capsys: Any) -> None:
monkeypatch.setenv("PASS_GIT_HELPER_SKIP", "1")
with pytest.raises(SystemExit):
passgithelper.main(["get"])
out, err = capsys.readouterr()
assert not out
assert not err
@pytest.mark.parametrize(
"helper_config",
[
HelperConfig(
"test_data/smoke",
"""
protocol=https
host=mytest.com""",
b"narf",
"dev/mytest",
),
],
indirect=True,
)
@pytest.mark.usefixtures("helper_config")
def test_smoke_resolve(self, capsys: Any) -> None:
passgithelper.main(["get"])
out, _ = capsys.readouterr()
assert out == "password=narf\n"
@pytest.mark.parametrize(
"helper_config",
[
HelperConfig(
"test_data/smoke",
"""
protocol=https
host=mytest.com
path=/foo/bar.git""",
b"ignored",
),
],
indirect=True,
)
@pytest.mark.usefixtures("helper_config")
def test_path_used_if_present_fails(self, capsys: Any) -> None:
with pytest.raises(SystemExit):
passgithelper.main(["get"])
_, err = capsys.readouterr()
assert "No mapping section" in err
@pytest.mark.parametrize(
"helper_config",
[
HelperConfig(
"test_data/with-path",
"""
protocol=https
host=mytest.com
path=subpath/bar.git""",
b"narf",
"dev/mytest",
),
],
indirect=True,
)
@pytest.mark.usefixtures("helper_config")
def test_path_used_if_present(self, capsys: Any) -> None:
passgithelper.main(["get"])
out, _ = capsys.readouterr()
assert out == "password=narf\n"
@pytest.mark.parametrize(
"helper_config",
[
HelperConfig(
"test_data/wildcard",
"""
protocol=https
host=wildcard.com
username=wildcard
path=subpath/bar.git""",
b"narf-wildcard",
"dev/https/wildcard.com/wildcard",
),
],
indirect=True,
)
@pytest.mark.usefixtures("helper_config")
def test_wildcard_matching(self, capsys: Any) -> None:
passgithelper.main(["get"])
out, _ = capsys.readouterr()
assert out == "password=narf-wildcard\n"
@pytest.mark.parametrize(
"helper_config",
[
HelperConfig(
"test_data/with-username",
"""
host=plainline.com""",
b"password\nusername",
"dev/plainline",
),
],
indirect=True,
)
@pytest.mark.usefixtures("helper_config")
def test_username_provided(self, capsys: Any) -> None:
passgithelper.main(["get"])
out, _ = capsys.readouterr()
assert out == "password=password\nusername=username\n"
@pytest.mark.parametrize(
"helper_config",
[
HelperConfig(
"test_data/with-username",
"""
host=plainline.com
username=narf""",
b"password\nusername",
"dev/plainline",
),
],
indirect=True,
)
@pytest.mark.usefixtures("helper_config")
def test_username_skipped_if_provided(self, capsys: Any) -> None:
passgithelper.main(["get"])
out, _ = capsys.readouterr()
assert out == "password=password\n"
@pytest.mark.parametrize(
"helper_config",
[
HelperConfig(
"test_data/with-username",
"""
protocol=https
host=mytest.com""",
b"narf",
"dev/mytest",
),
],
indirect=True,
)
@pytest.mark.usefixtures("helper_config")
def test_custom_mapping_used(self, capsys: Any) -> None:
# this would fail for the default file from with-username
passgithelper.main(["-m", "test_data/smoke/git-pass-mapping.ini", "get"])
out, _ = capsys.readouterr()
assert out == "password=narf\n"
@pytest.mark.parametrize(
"helper_config",
[
HelperConfig(
"test_data/with-username-skip",
"""
protocol=https
host=mytest.com""",
b"password: xyz\nuser: tester",
"dev/mytest",
),
],
indirect=True,
)
@pytest.mark.usefixtures("helper_config")
def test_prefix_skipping(self, capsys: Any) -> None:
passgithelper.main(["get"])
out, _ = capsys.readouterr()
assert out == "password=xyz\nusername=tester\n"
@pytest.mark.parametrize(
"helper_config",
[
HelperConfig(
"test_data/unknown-username-extractor",
"""
protocol=https
host=mytest.com""",
b"ignored",
),
],
indirect=True,
)
@pytest.mark.usefixtures("helper_config")
def test_select_unknown_extractor(self, capsys: Any) -> None:
with pytest.raises(SystemExit):
passgithelper.main(["get"])
_, err = capsys.readouterr()
assert "username_extractor of type 'doesntexist' does not exist" in err
@pytest.mark.parametrize(
"helper_config",
[
HelperConfig(
"test_data/regex-extraction",
"""
protocol=https
host=mytest.com""",
b"xyz\nsomeline\nmyuser: tester\n morestuff\nmyuser: ignore",
"dev/mytest",
),
],
indirect=True,
)
@pytest.mark.usefixtures("helper_config")
def test_regex_username_selection(self, capsys: Any) -> None:
passgithelper.main(["get"])
out, _ = capsys.readouterr()
assert out == "password=xyz\nusername=tester\n"
@pytest.mark.parametrize(
"helper_config",
[
HelperConfig(
"test_data/entry-name-extraction",
"""
protocol=https
host=mytest.com""",
b"xyz",
"dev/mytest/myuser",
),
],
indirect=True,
)
@pytest.mark.usefixtures("helper_config")
def test_entry_name_is_user(self, capsys: Any) -> None:
passgithelper.main(["get"])
out, _ = capsys.readouterr()
assert out == "password=xyz\nusername=myuser\n"
@pytest.mark.parametrize(
"helper_config",
[
HelperConfig(
"test_data/with-encoding",
"""
protocol=https
host=mytest.com""",
"täßt".encode("LATIN1"),
"dev/mytest",
),
],
indirect=True,
)
@pytest.mark.usefixtures("helper_config")
def test_uses_configured_encoding(self, capsys: Any) -> None:
passgithelper.main(["get"])
out, _ = capsys.readouterr()
assert out == "password=täßt\n"
@pytest.mark.parametrize(
"helper_config",
[
HelperConfig(
"test_data/smoke",
"""
protocol=https
host=mytest.com""",
"täßt".encode("UTF-8"),
"dev/mytest",
),
],
indirect=True,
)
@pytest.mark.usefixtures("helper_config")
def test_uses_utf8_by_default(self, capsys: Any) -> None:
passgithelper.main(["get"])
out, _ = capsys.readouterr()
assert out == "password=täßt\n"
@pytest.mark.parametrize(
"helper_config",
[
HelperConfig(
"test_data/smoke",
"""
protocol=https
host=mytest.com""",
None,
"dev/mytest",
),
],
indirect=True,
)
@pytest.mark.usefixtures("helper_config")
def test_fails_gracefully_on_pass_errors(self, capsys: Any) -> None:
with pytest.raises(SystemExit):
passgithelper.main(["get"])
_, err = capsys.readouterr()
assert "Unable to retrieve" in err
@pytest.mark.parametrize(
"helper_config",
[
HelperConfig(
"test_data/smoke",
"""
protocol=https
host=unknown""",
"ignored".encode("UTF-8"),
),
],
indirect=True,
)
@pytest.mark.usefixtures("helper_config")
def test_fails_gracefully_on_missing_entries(self, capsys: Any) -> None:
with pytest.raises(SystemExit):
passgithelper.main(["get"])
_, err = capsys.readouterr()
assert "Unable to retrieve" in err
@pytest.mark.parametrize(
"helper_config",
[
HelperConfig(
"test_data/password_store_dir",
"""
host=example.com""",
"test".encode("UTF-8"),
),
],
indirect=True,
)
def test_supports_switching_password_store_dirs(
self, capsys: Any, helper_config: Any
) -> None:
passgithelper.main(["get"])
out, _ = capsys.readouterr()
assert out == "password=test\n"
assert (
helper_config.mock_calls[-1].kwargs["env"]["PASSWORD_STORE_DIR"]
== "/some/dir"
)
|