|
| 1 | +import json |
| 2 | +from types import SimpleNamespace |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from cumulusci.utils.clariti import ( |
| 7 | + ClaritiCheckoutResult, |
| 8 | + ClaritiError, |
| 9 | + build_default_org_name, |
| 10 | + checkout_org_from_pool, |
| 11 | + resolve_pool_id, |
| 12 | + set_sf_alias, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +def test_resolve_pool_id_prefers_explicit(): |
| 17 | + assert resolve_pool_id("Pool42", None) == "Pool42" |
| 18 | + |
| 19 | + |
| 20 | +def test_resolve_pool_id_requires_config_when_missing(tmp_path): |
| 21 | + config_path = tmp_path / ".clariti.json" |
| 22 | + config_path.write_text("{}") |
| 23 | + |
| 24 | + resolved = resolve_pool_id(None, str(tmp_path)) |
| 25 | + |
| 26 | + assert resolved is None |
| 27 | + |
| 28 | + |
| 29 | +def test_resolve_pool_id_missing_file(tmp_path): |
| 30 | + with pytest.raises(ClaritiError) as exc: |
| 31 | + resolve_pool_id(None, str(tmp_path)) |
| 32 | + |
| 33 | + message = str(exc.value) |
| 34 | + assert "pool id" in message |
| 35 | + assert ".clariti.json" in message |
| 36 | + |
| 37 | + |
| 38 | +def test_checkout_org_from_pool_parses_username(monkeypatch): |
| 39 | + payload = { |
| 40 | + "orgId": "00D123", |
| 41 | + "username": "user@example.com", |
| 42 | + "alias": "foo", |
| 43 | + "poolId": "Pool42", |
| 44 | + } |
| 45 | + |
| 46 | + def fake_run(command, check, text, capture_output, env): |
| 47 | + assert command[:4] == ["sf", "clariti", "org", "checkout"] |
| 48 | + assert "--json" in command |
| 49 | + return SimpleNamespace(returncode=0, stdout=json.dumps(payload), stderr="") |
| 50 | + |
| 51 | + monkeypatch.setattr("cumulusci.utils.clariti.subprocess.run", fake_run) |
| 52 | + |
| 53 | + result = checkout_org_from_pool("Pool42", alias="MyAlias") |
| 54 | + |
| 55 | + assert isinstance(result, ClaritiCheckoutResult) |
| 56 | + assert result.username == "user@example.com" |
| 57 | + assert result.alias == "foo" |
| 58 | + assert result.org_id == "00D123" |
| 59 | + assert result.pool_id == "Pool42" |
| 60 | + assert result.raw == payload |
| 61 | + |
| 62 | + |
| 63 | +def test_checkout_org_from_pool_reads_nested_username(monkeypatch): |
| 64 | + payload = { |
| 65 | + "status": 0, |
| 66 | + "result": { |
| 67 | + "org": {"username": "nested@example.com"}, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + def fake_run(command, check, text, capture_output, env): |
| 72 | + assert "--pool-id" in command |
| 73 | + return SimpleNamespace(returncode=0, stdout=json.dumps(payload), stderr="") |
| 74 | + |
| 75 | + monkeypatch.setattr("cumulusci.utils.clariti.subprocess.run", fake_run) |
| 76 | + |
| 77 | + result = checkout_org_from_pool("PoolNested") |
| 78 | + |
| 79 | + assert result.username == "nested@example.com" |
| 80 | + assert result.alias is None |
| 81 | + |
| 82 | + |
| 83 | +def test_checkout_org_from_pool_without_pool_id(monkeypatch): |
| 84 | + payload = { |
| 85 | + "username": "user@example.com", |
| 86 | + "poolId": "Pool-From-Config", |
| 87 | + } |
| 88 | + |
| 89 | + def fake_run(command, check, text, capture_output, env): |
| 90 | + assert "--pool-id" not in command |
| 91 | + return SimpleNamespace(returncode=0, stdout=json.dumps(payload), stderr="") |
| 92 | + |
| 93 | + monkeypatch.setattr("cumulusci.utils.clariti.subprocess.run", fake_run) |
| 94 | + |
| 95 | + result = checkout_org_from_pool(None) |
| 96 | + |
| 97 | + assert result.username == "user@example.com" |
| 98 | + |
| 99 | + |
| 100 | +def test_checkout_org_from_pool_handles_failure(monkeypatch): |
| 101 | + def fake_run(command, check, text, capture_output, env): |
| 102 | + return SimpleNamespace(returncode=1, stdout="", stderr="No orgs available") |
| 103 | + |
| 104 | + monkeypatch.setattr("cumulusci.utils.clariti.subprocess.run", fake_run) |
| 105 | + |
| 106 | + with pytest.raises(ClaritiError) as exc: |
| 107 | + checkout_org_from_pool("EmptyPool") |
| 108 | + |
| 109 | + message = str(exc.value) |
| 110 | + assert "Clariti checkout failed" in message |
| 111 | + assert "No orgs available" in message |
| 112 | + |
| 113 | + |
| 114 | +def test_checkout_org_from_pool_formats_json_error(monkeypatch): |
| 115 | + payload = { |
| 116 | + "name": "ClaritiOrgCheckoutError", |
| 117 | + "message": "Failed to get org from pool: No healthy orgs available in this pool", |
| 118 | + } |
| 119 | + |
| 120 | + def fake_run(command, check, text, capture_output, env): |
| 121 | + return SimpleNamespace( |
| 122 | + returncode=1, stdout=json.dumps(payload), stderr="" |
| 123 | + ) |
| 124 | + |
| 125 | + monkeypatch.setattr("cumulusci.utils.clariti.subprocess.run", fake_run) |
| 126 | + |
| 127 | + with pytest.raises(ClaritiError) as exc: |
| 128 | + checkout_org_from_pool("Pool42") |
| 129 | + |
| 130 | + message = str(exc.value) |
| 131 | + assert "Clariti checkout failed" in message |
| 132 | + assert "ClaritiOrgCheckoutError" in message |
| 133 | + assert "No healthy orgs" in message |
| 134 | + |
| 135 | + |
| 136 | +def test_checkout_org_from_pool_formats_json_error_debug(monkeypatch): |
| 137 | + payload = { |
| 138 | + "name": "ClaritiOrgCheckoutError", |
| 139 | + "message": "Failed", "extra": "details", |
| 140 | + } |
| 141 | + |
| 142 | + def fake_run(command, check, text, capture_output, env): |
| 143 | + return SimpleNamespace( |
| 144 | + returncode=1, stdout=json.dumps(payload), stderr="" |
| 145 | + ) |
| 146 | + |
| 147 | + monkeypatch.setattr("cumulusci.utils.clariti.subprocess.run", fake_run) |
| 148 | + |
| 149 | + from cumulusci.core.debug import set_debug_mode |
| 150 | + |
| 151 | + with set_debug_mode(True): |
| 152 | + with pytest.raises(ClaritiError) as exc: |
| 153 | + checkout_org_from_pool("Pool42") |
| 154 | + |
| 155 | + message = str(exc.value) |
| 156 | + assert "Clariti checkout failed" in message |
| 157 | + assert "Clariti raw response" in message |
| 158 | + assert json.dumps(payload, indent=2, sort_keys=True) in message |
| 159 | + |
| 160 | + |
| 161 | +def test_set_sf_alias_success(monkeypatch): |
| 162 | + def fake_run(command, check, text, capture_output, env): |
| 163 | + assert command == ["sf", "alias", "set", "target=user@example.com"] |
| 164 | + return SimpleNamespace(returncode=0, stdout="", stderr="") |
| 165 | + |
| 166 | + monkeypatch.setattr("cumulusci.utils.clariti.subprocess.run", fake_run) |
| 167 | + |
| 168 | + success, message = set_sf_alias("target", "user@example.com") |
| 169 | + |
| 170 | + assert success is True |
| 171 | + assert message is None |
| 172 | + |
| 173 | + |
| 174 | +def test_set_sf_alias_failure(monkeypatch): |
| 175 | + def fake_run(command, check, text, capture_output, env): |
| 176 | + return SimpleNamespace(returncode=1, stdout="", stderr="Alias failure") |
| 177 | + |
| 178 | + monkeypatch.setattr("cumulusci.utils.clariti.subprocess.run", fake_run) |
| 179 | + |
| 180 | + success, message = set_sf_alias("target", "username") |
| 181 | + |
| 182 | + assert success is False |
| 183 | + assert message is not None and message.startswith("Clariti alias failed") |
| 184 | + assert "Alias failure" in message |
| 185 | + |
| 186 | + |
| 187 | +def test_build_default_org_name_prefers_alias(): |
| 188 | + assert build_default_org_name("user@example.com", "alias") == "alias" |
| 189 | + |
| 190 | + |
| 191 | +def test_build_default_org_name_sanitizes_username(): |
| 192 | + assert ( |
| 193 | + build_default_org_name("user.with+symbol@example.com") |
| 194 | + == "user_with_symbol_example_com" |
| 195 | + ) |
0 commit comments