|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from contextlib import contextmanager |
| 4 | + |
| 5 | +from o2switch_cli.cli.context import AppContext |
| 6 | +from o2switch_cli.cli.interactive import run_interactive_menu |
| 7 | +from o2switch_cli.config.settings import AppSettings |
| 8 | +from o2switch_cli.core.models import ( |
| 9 | + DomainDescriptor, |
| 10 | + DomainType, |
| 11 | + HostnameSearchResult, |
| 12 | + SearchCategory, |
| 13 | + SubdomainDescriptor, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +def _app_context() -> AppContext: |
| 18 | + return AppContext( |
| 19 | + settings=AppSettings(), |
| 20 | + output_format="text", |
| 21 | + dry_run=False, |
| 22 | + force=False, |
| 23 | + yes=False, |
| 24 | + verbose=False, |
| 25 | + verify_after_mutation=True, |
| 26 | + allow_prompt=True, |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +@contextmanager |
| 31 | +def _noop_status(*args, **kwargs): |
| 32 | + yield None |
| 33 | + |
| 34 | + |
| 35 | +def test_interactive_dns_search_skips_backend_lookup_for_blank_term(monkeypatch) -> None: |
| 36 | + class FakeDomains: |
| 37 | + def list_domains(self) -> list[DomainDescriptor]: |
| 38 | + return [DomainDescriptor(domain="ginutech.com", type=DomainType.ADDON)] |
| 39 | + |
| 40 | + class FakeSubdomains: |
| 41 | + def search(self, term: str) -> list[SubdomainDescriptor]: |
| 42 | + assert term == "" |
| 43 | + return [SubdomainDescriptor(fqdn="app.ginutech.com", label="app", root_domain="ginutech.com")] |
| 44 | + |
| 45 | + class FakeDNS: |
| 46 | + def search(self, term: str) -> list[HostnameSearchResult]: |
| 47 | + raise AssertionError(f"dns.search should not run for blank interactive input, got {term!r}") |
| 48 | + |
| 49 | + class FakeRuntime: |
| 50 | + domains = FakeDomains() |
| 51 | + subdomains = FakeSubdomains() |
| 52 | + dns = FakeDNS() |
| 53 | + |
| 54 | + answers = iter(["DNS: search", "Exit"]) |
| 55 | + messages: list[str] = [] |
| 56 | + |
| 57 | + class FakeSelect: |
| 58 | + def ask(self): |
| 59 | + return next(answers) |
| 60 | + |
| 61 | + monkeypatch.setattr(AppContext, "runtime", lambda self: FakeRuntime()) |
| 62 | + monkeypatch.setattr( |
| 63 | + "o2switch_cli.cli.interactive.questionary.select", |
| 64 | + lambda *args, **kwargs: FakeSelect(), |
| 65 | + ) |
| 66 | + monkeypatch.setattr("o2switch_cli.cli.interactive.TerminalUI.print_banner", lambda self: None) |
| 67 | + monkeypatch.setattr( |
| 68 | + "o2switch_cli.cli.interactive.TerminalUI.prompt_realtime_search", |
| 69 | + lambda self, *args, **kwargs: "", |
| 70 | + ) |
| 71 | + monkeypatch.setattr( |
| 72 | + "o2switch_cli.cli.interactive.TerminalUI.print_info", |
| 73 | + lambda self, message: messages.append(message), |
| 74 | + ) |
| 75 | + monkeypatch.setattr("o2switch_cli.cli.interactive.TerminalUI.browse_pages", lambda self, *args, **kwargs: None) |
| 76 | + monkeypatch.setattr("o2switch_cli.cli.interactive.TerminalUI.status", _noop_status) |
| 77 | + |
| 78 | + run_interactive_menu(_app_context()) |
| 79 | + |
| 80 | + assert messages == ["Enter a hostname, IP, or zone to run a DNS search."] |
| 81 | + |
| 82 | + |
| 83 | +def test_interactive_dns_search_queries_submitted_term_only(monkeypatch) -> None: |
| 84 | + class FakeDomains: |
| 85 | + def list_domains(self) -> list[DomainDescriptor]: |
| 86 | + return [DomainDescriptor(domain="ginutech.com", type=DomainType.ADDON)] |
| 87 | + |
| 88 | + class FakeSubdomains: |
| 89 | + def search(self, term: str) -> list[SubdomainDescriptor]: |
| 90 | + assert term == "" |
| 91 | + return [SubdomainDescriptor(fqdn="app.ginutech.com", label="app", root_domain="ginutech.com")] |
| 92 | + |
| 93 | + calls: list[str] = [] |
| 94 | + |
| 95 | + class FakeDNS: |
| 96 | + def search(self, term: str) -> list[HostnameSearchResult]: |
| 97 | + calls.append(term) |
| 98 | + return [ |
| 99 | + HostnameSearchResult( |
| 100 | + category=SearchCategory.DNS_RECORDS, |
| 101 | + hostname="name.ginutech.com", |
| 102 | + record_type="A", |
| 103 | + value="5.196.30.71", |
| 104 | + zone="ginutech.com", |
| 105 | + ) |
| 106 | + ] |
| 107 | + |
| 108 | + class FakeRuntime: |
| 109 | + domains = FakeDomains() |
| 110 | + subdomains = FakeSubdomains() |
| 111 | + dns = FakeDNS() |
| 112 | + |
| 113 | + answers = iter(["DNS: search", "Exit"]) |
| 114 | + browsed: list[list[HostnameSearchResult]] = [] |
| 115 | + |
| 116 | + class FakeSelect: |
| 117 | + def ask(self): |
| 118 | + return next(answers) |
| 119 | + |
| 120 | + monkeypatch.setattr(AppContext, "runtime", lambda self: FakeRuntime()) |
| 121 | + monkeypatch.setattr( |
| 122 | + "o2switch_cli.cli.interactive.questionary.select", |
| 123 | + lambda *args, **kwargs: FakeSelect(), |
| 124 | + ) |
| 125 | + monkeypatch.setattr("o2switch_cli.cli.interactive.TerminalUI.print_banner", lambda self: None) |
| 126 | + monkeypatch.setattr( |
| 127 | + "o2switch_cli.cli.interactive.TerminalUI.prompt_realtime_search", |
| 128 | + lambda self, *args, **kwargs: "name.ginutech.com", |
| 129 | + ) |
| 130 | + monkeypatch.setattr("o2switch_cli.cli.interactive.TerminalUI.print_info", lambda self, message: None) |
| 131 | + monkeypatch.setattr( |
| 132 | + "o2switch_cli.cli.interactive.TerminalUI.browse_pages", |
| 133 | + lambda self, items, **kwargs: browsed.append(list(items)), |
| 134 | + ) |
| 135 | + monkeypatch.setattr("o2switch_cli.cli.interactive.TerminalUI.status", _noop_status) |
| 136 | + |
| 137 | + run_interactive_menu(_app_context()) |
| 138 | + |
| 139 | + assert calls == ["name.ginutech.com"] |
| 140 | + assert [item.hostname for item in browsed[0]] == ["name.ginutech.com"] |
0 commit comments