|
| 1 | +"""Minimal mock Salesforce server for CI integration tests. |
| 2 | +
|
| 3 | +Intercepts the HTTP calls made during ``sf data-code-extension script|function run`` |
| 4 | +so that neither a real Salesforce org nor real Data Cloud data is required. |
| 5 | +
|
| 6 | +Endpoints handled |
| 7 | +----------------- |
| 8 | +GET /services/oauth2/userinfo |
| 9 | + Called by ``sf org login access-token`` to resolve a username from a token, |
| 10 | + and by ``connection.refreshAuth()`` in the SF CLI plugin. |
| 11 | +
|
| 12 | +POST /services/oauth2/token |
| 13 | + Called by ``connection.refreshAuth()`` when it tries to exchange a token. |
| 14 | +
|
| 15 | +POST /services/data/v66.0/ssot/query-sql |
| 16 | + Called by SFCLIDataCloudReader when the script entrypoint reads a DLO/DMO. |
| 17 | + Returns fake rows with the columns expected by the default script template |
| 18 | + (Account_std__dll: description__c, sfdcorganizationid__c, kq_id__c). |
| 19 | +
|
| 20 | +GET /* (catch-all) |
| 21 | + Returns {"status": "ok"} for any other SF API path the CLI may probe. |
| 22 | +
|
| 23 | +Usage |
| 24 | +----- |
| 25 | + python scripts/mock_sf_server.py # listens on port 8888 |
| 26 | + MOCK_SF_PORT=9000 python scripts/mock_sf_server.py |
| 27 | + python scripts/mock_sf_server.py 9000 |
| 28 | +""" |
| 29 | + |
| 30 | +from __future__ import annotations |
| 31 | + |
| 32 | +from http.server import BaseHTTPRequestHandler, HTTPServer |
| 33 | +import json |
| 34 | +import os |
| 35 | +import sys |
| 36 | + |
| 37 | +PORT = ( |
| 38 | + int(sys.argv[1]) |
| 39 | + if len(sys.argv) > 1 |
| 40 | + else int(os.environ.get("MOCK_SF_PORT", "8888")) |
| 41 | +) |
| 42 | + |
| 43 | +_USERINFO = { |
| 44 | + "sub": "https://test.salesforce.com/id/00D000000000001AAA/005000000000001AAA", |
| 45 | + "user_id": "005000000000001AAA", |
| 46 | + "organization_id": "00D000000000001AAA", |
| 47 | + "preferred_username": "dev1@example.com", |
| 48 | + "name": "Dev User", |
| 49 | + "email": "dev1@example.com", |
| 50 | + "username": "dev1@example.com", |
| 51 | + "active": True, |
| 52 | +} |
| 53 | + |
| 54 | +_TOKEN_RESPONSE = { |
| 55 | + "access_token": "fake_access_token_00D000000000001AAA", |
| 56 | + "instance_url": f"http://localhost:{PORT}", |
| 57 | + "token_type": "Bearer", |
| 58 | + "scope": "api", |
| 59 | +} |
| 60 | + |
| 61 | +# Fake rows for Account_std__dll (used by the default script template). |
| 62 | +# Columns: description__c, sfdcorganizationid__c, kq_id__c |
| 63 | +_QUERY_RESPONSE = { |
| 64 | + "metadata": [ |
| 65 | + {"name": "description__c"}, |
| 66 | + {"name": "sfdcorganizationid__c"}, |
| 67 | + {"name": "kq_id__c"}, |
| 68 | + ], |
| 69 | + "data": [ |
| 70 | + ["hello world", "org123", "kq001"], |
| 71 | + ["another row", "org123", "kq002"], |
| 72 | + ], |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +class MockSFHandler(BaseHTTPRequestHandler): |
| 77 | + def _send_json(self, payload: object, status: int = 200) -> None: |
| 78 | + body = json.dumps(payload).encode() |
| 79 | + self.send_response(status) |
| 80 | + self.send_header("Content-Type", "application/json") |
| 81 | + self.send_header("Content-Length", str(len(body))) |
| 82 | + self.end_headers() |
| 83 | + self.wfile.write(body) |
| 84 | + |
| 85 | + def log_message(self, fmt: str, *args: object) -> None: # type: ignore[override] |
| 86 | + print(f"[MOCK SF] {self.command} {self.path} — {fmt % args}", flush=True) |
| 87 | + |
| 88 | + def do_GET(self) -> None: |
| 89 | + path = self.path.split("?")[0] |
| 90 | + if path == "/services/oauth2/userinfo": |
| 91 | + self._send_json(_USERINFO) |
| 92 | + else: |
| 93 | + self._send_json({"status": "ok"}) |
| 94 | + |
| 95 | + def do_POST(self) -> None: |
| 96 | + path = self.path.split("?")[0] |
| 97 | + length = int(self.headers.get("Content-Length", 0)) |
| 98 | + body = self.rfile.read(length) if length else b"" |
| 99 | + print(f"[MOCK SF] POST body: {body[:200]!r}", flush=True) |
| 100 | + |
| 101 | + if path == "/services/oauth2/token": |
| 102 | + self._send_json(_TOKEN_RESPONSE) |
| 103 | + elif path.endswith("/ssot/query-sql"): |
| 104 | + self._send_json(_QUERY_RESPONSE) |
| 105 | + else: |
| 106 | + self._send_json({"status": "ok"}) |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + server = HTTPServer(("localhost", PORT), MockSFHandler) |
| 111 | + server.allow_reuse_address = True |
| 112 | + print(f"[MOCK SF] Listening on http://localhost:{PORT}", flush=True) |
| 113 | + server.serve_forever() |
0 commit comments