On this page:

22.12smileutil: Test Script Function

 

The execute-script-function command may be used to test scripts that are intended for use in the Smile CDR Javascript Execution Environment. It executes scripts in an environment that has the same Javascript capabilities and utilties that will be present in an actual Smile CDR deployment so it can be useful for development workflow or automated testing.

22.12.1Usage

 
bin/smileutil execute-script-function --script file:/path/authenticate.js --function authenticate --argument file:/path/usernameandpassword.json

22.12.2Options

 
  • -s [script] (or --script [script]) – This argument provides the script that should be executed. This can be the raw contents of a script, or a pointer to a file with the format file:/path/filename.
  • -f [function name] (or --function [function name]) – The name of the function to execute. See below for details on available functions.
  • -o [output file] (or --output [output file]) – This argument specifies the filename for a file to which the resulting output for the function should be written. The value must be in the format file:[file path and name].
  • -a [argument name]=[argument value] (or --argument [argument name]=[argument value]
  • -l (or --logging) – (optional) Provide more verbose logging

22.12.3A Complete Example: Authenticate

 

The following example shows an authenticate(...) function for use in the Script Inbound Security module being tested.

First, a file called authenticate.js is created. This file should have the following contents:

/**
 * This method is called when an authorization is requested, BEFORE the
 * token is created and access is granted
 * @param theRequest The incoming theRequest
 * @param theOutcomeFactory This object is a factory for a successful
 *      response or a failure response
 * @returns {*}
 */
function authenticate(theRequest, theOutcomeFactory) {

	// Add a log line
	Log.info("Received username of: " + theRequest.getUsername());

	var outcome = theOutcomeFactory.newSuccess();
	outcome.username = theRequest.getUsername();
	outcome.addAuthority('FHIR_READ_ALL_IN_COMPARTMENT', 'Patient/123');
	outcome.addApprovedScope('patient/*.read');

	return outcome;
}

Next, an input document is created that represents the object that will be passed into theRequest. Note that per the function documentation, the object should have the structure defined by the UsernamePasswordAuthenticationRequest object. We'll call this file request.json:

{
   "username":"testuser",
   "password":"secret",
   "remoteAddress":"10.0.0.99",
}

Now, to test the function:

bin/smileutil execute-script-function --script file:authenticate.js --function authenticate --argument file:/path/request.json

The output will resemble the following:

2019-04-03 13:33:10 [main] INFO  c.c.c.T.JS User testuser
2019-04-03 13:33:10 [main] INFO  ca.uhn.fhir.cli.App Smile CDR CLI is shutting down...
2019-04-03 13:33:10 [main] INFO  c.c.c.TestScriptFunctionCommandTest Output was:
{
  "username" : "testuser",
  "systemUser" : false,
  "authorities" : [ {
    "permission" : "FHIR_READ_ALL_IN_COMPARTMENT",
    "argument" : "Patient/123"
  } ],
  "external" : false,
  "approvedScopes" : [ "patient/*.read" ]
}

22.12.4Available Functions

 

The sections below outline the functions that are able to be tested using this command.

22.12.5Function: authenticate

 

Function Name: authenticate

Arguments:

Name Must be supplied? Type
theRequest Yes UsernamePasswordAuthenticationRequest
theOutcomeFactory No