Skip to content

Commit

Permalink
Update behaviour of ?case=original query to `/service/javascript-ap…
Browse files Browse the repository at this point in the history
…p` - return raw contents (#6813)
  • Loading branch information
eddyashton authored Feb 6, 2025
1 parent 969cd93 commit ef1ca8e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
19 changes: 17 additions & 2 deletions src/node/gov/handlers/service_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,15 @@ namespace ccf::gov::endpoints
auto js_endpoints_handle =
ctx.tx.template ro<ccf::endpoints::EndpointsMap>(
ccf::endpoints::Tables::ENDPOINTS);

using RawEndpointsMap = ccf::kv::RawCopySerialisedMap<
ccf::endpoints::EndpointsMap::Key,
std::vector<uint8_t>>;
auto raw_js_endpoints_handle = ctx.tx.template ro<RawEndpointsMap>(
ccf::endpoints::Tables::ENDPOINTS);

js_endpoints_handle->foreach(
[&endpoints, original_case](
[&endpoints, &raw_js_endpoints_handle, original_case](
const ccf::endpoints::EndpointKey& key,
const ccf::endpoints::EndpointProperties& properties) {
auto ib =
Expand All @@ -347,7 +354,15 @@ namespace ccf::gov::endpoints

if (original_case)
{
operation = properties;
const auto raw_value_opt = raw_js_endpoints_handle->get(key);
if (!raw_value_opt.has_value())
{
throw std::runtime_error(
"Table inconsistency: Cannot access key via raw handle?");
}
const auto& raw_value = raw_value_opt.value();
operation =
nlohmann::json::parse(raw_value.begin(), raw_value.end());
}
else
{
Expand Down
4 changes: 1 addition & 3 deletions tests/js-modules/basic-module-import/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
"js_module": "test_module.js",
"js_function": "test_module",
"forwarding_required": "sometimes",
"redirection_strategy": "none",
"authn_policies": ["user_cert"],
"mode": "readonly",
"openapi": {}
"mode": "readonly"
}
}
}
Expand Down
25 changes: 23 additions & 2 deletions tests/js-modules/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ def compare_app_metadata(expected, actual, api_key_renames, route=[]):
), f"Mismatch at {path}, expected {expected} and found {actual}"


def canonicalise(orig, renames):
if isinstance(orig, dict):
o = {}
for k, v in orig.items():
if k in renames:
k = renames[k]
o[k] = canonicalise(v, renames)
return o
elif isinstance(orig, str) and orig in renames:
return renames[orig]
else:
return orig


@reqs.description("Test module access")
def test_module_access(network, args):
primary, _ = network.find_nodes()
Expand Down Expand Up @@ -98,16 +112,23 @@ def test_module_access(network, args):
}

with primary.api_versioned_client(api_version=args.gov_api_version) as c:
# The response with ?case=original should be almost exactly what was
# submitted (including exactly which fields are present/omitted). The
# only changes are the casing of HTTP verbs, and the prefixing of module
# names.
r = c.get("/gov/service/javascript-app?case=original")
assert r.status_code == http.HTTPStatus.OK, r.status_code
compare_app_metadata(
actual = r.body.json()
expected = canonicalise(
expected_metadata,
r.body.json(),
{
**http_methods_renamed,
**module_names_prefixed,
},
)
assert (
expected == actual
), f"{json.dumps(expected, indent=2)}\nvs\n{json.dumps(actual, indent=2)}"

r = c.get("/gov/service/javascript-app")
assert r.status_code == http.HTTPStatus.OK, r.status_code
Expand Down

0 comments on commit ef1ca8e

Please sign in to comment.