Skip to content

Commit

Permalink
Simplify use of context in wrapper functionality as per review
Browse files Browse the repository at this point in the history
Co-authored-by: BryceStevenWilley <Bryce.Steven.Willey@gmail.com>
  • Loading branch information
plocket and BryceStevenWilley committed Oct 17, 2024
1 parent 8f8d8df commit 9b2df5b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 53 deletions.
79 changes: 38 additions & 41 deletions lib/docassemble/docassemble_api_interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ const time = require('../utils/time');
let da_i = {};
module.exports = da_i;

const artifacts_path = session_vars.get_artifacts_path_name();
const log = new Log({ path: artifacts_path, context: `api` });
const context = `da api`;
const artifacts_path = session_vars.get_artifacts_path_name();
const log = new Log({ path: artifacts_path, context });


// Constants
Expand Down Expand Up @@ -96,7 +96,7 @@ da_i.throw_an_error_if_server_is_not_responding = async function ( options ) {

// Defaults
let { timeout } = options || { timeout: DEFAULT_MAX_REQUEST_MS };
log.info({ code: `ALK0166`, context: `da api`, },
log.info({ code: `ALK0166`, context, },
`Waiting for the docassemble server to respond. Will wait for ${ timeout/1000 } seconds at most.`
);

Expand All @@ -106,7 +106,7 @@ da_i.throw_an_error_if_server_is_not_responding = async function ( options ) {
let response = await _da_REST.get_dev_id( timeout );
let end_time = Date.now()
if ( response && response.data && response.data.id ) {
log.info({ code: `ALK0167`, context: `da api`, },
log.info({ code: `ALK0167`, context, },
`The docassemble server responded after ${ (end_time - start_time)/1000 } seconds.`
);
return false;
Expand All @@ -117,13 +117,13 @@ da_i.throw_an_error_if_server_is_not_responding = async function ( options ) {
if ( is_timeout_error( error ) ) {
// Anything that's waiting for the server to not be busy does not want
// to move forward without a free server.
log.info({ code: `ALK0168`, context: `da api`, },
log.info({ code: `ALK0168`, context, },
`The docassemble server was still busy after ${ timeout/1000 } seconds.`
);
} else {
// Not sure what to do for other errors. It could be a server
// error, like an 'Access Denied' error, which should be handled.
log.warning({ code: `ALK0169`, context: `da api` },
log.warning({ code: `ALK0169`, context, },
`Checked if the server was busy and ran into a different error`,
error
);
Expand Down Expand Up @@ -194,7 +194,7 @@ da_i.get_api_key_access_status = async function ({ timeout = null, api_key = nul
* to allow for server request.
*/

let auth_data = {
let auth_feedback = {
codes: [ 1 ],
has_auth: null,
messages: [`API key authorization is not yet determined.`],
Expand All @@ -203,50 +203,48 @@ da_i.get_api_key_access_status = async function ({ timeout = null, api_key = nul

// Skip this for tests run from ALKilnInThePlayground
if ( session_vars.get_origin() === 'playground' ) {
auth_data.codes.push( 2 );
auth_data.messages.push( `API keys are unnecessary for tests run in the Playground.` );
auth_feedback.codes.push( 2 );
auth_feedback.messages.push( `API keys are unnecessary for tests run in the Playground.` );
}

if ( typeof api_key !== `string` ) {
auth_data.codes.push( 3 );
auth_data.messages.push( `The given API key is not a string. Type: ${ typeof api_key }`);
auth_feedback.codes.push( 3 );
auth_feedback.messages.push( `The given API key is not a string. Type: ${ typeof api_key }`);
}

if ( auth_data.codes.length === 1 ) {
if ( auth_feedback.codes.length === 1 ) {
// Default timeouts
timeout = timeout || DEFAULT_MAX_REQUEST_MS;
try {
let response = await _da_REST.get_api_key_info( timeout, {
api_key,
server_url: session_vars.get_da_server_url(),
});
auth_data.has_auth = true;
auth_data.codes = [ 6 ];
auth_data.messages = [ `This API key has at least basic authorization on this server.` ];
auth_feedback.has_auth = true;
auth_feedback.codes = [ 6 ];
auth_feedback.messages = [ `This API key has at least basic authorization on this server.` ];

} catch ( error ) {
auth_data.error = error;
auth_feedback.error = error;
if ( error.status === 403 ) {
auth_data.has_auth = false;
auth_data.codes = [ 4 ];
auth_data.messages = [ `This API is unverified on this server. It is probably missing or has been deleted.` ];
auth_feedback.has_auth = false;
auth_feedback.codes = [ 4 ];
auth_feedback.messages = [ `This API is unverified on this server. It is probably missing or has been deleted.` ];
} else {
// Note on 404, non-existent key: 404 == 403 - Since we're using the
// key itself to get its own info, if it doesn't exist, we'll get 403
auth_data.codes = [ 5 ];
auth_data.messages = [ `This API key threw a non-403 error.` ];
auth_feedback.codes = [ 5 ];
auth_feedback.messages = [ `This API key threw a non-403 error.` ];
} // ends error status
} // ends try
} // ends auth_data.codes only has the first code
} // ends auth_feedback.codes only has the first code

// log.debug({ type: `da api info`, code: `ALKx0202`,
// pre: `API key authentication info`, data: auth_data });
log.debug({ code: `ALK0202`, context: `da api`, },
`API key authentication info`,
auth_data
log.debug({ code: `ALK0202`, context, },
`API key - what happened during authentication:`,
auth_feedback
);

return auth_data;
return auth_feedback;
}; // Ends da_i.get_api_key_access_status()

// node -e 'require("./lib/docassemble/docassemble_api_interface.js").delete_project( "create1" )'
Expand All @@ -258,20 +256,19 @@ da_i.delete_project = async function ( delete_options ) {
let { timeout } = delete_options || { timeout: DEFAULT_MAX_REQUEST_MS };

let project_name = session_vars.get_project_name()
log.info({ code: `ALK0170`, context: `da api`, },
log.info({ code: `ALK0170`, context, },
`Trying to delete Project ${ project_name }`
);

try {
let response = await _da_REST.delete_project( timeout );
log.info({ code: `ALK0171`, context: `da api`, },
log.info({ code: `ALK0171`, context, },
`Deleted Project "${ project_name }"`
);
} catch ( error ) {
log.throw({ code: `ALK0172`, context: `da api`, error: error },
log.throw({ code: `ALK0172`, context, error: error },
`Ran into an error when deleting Project "${ project_name }". See https://docassemble.org/docs/api.html#playground_delete_project.`
);
// throw error;
}

}; // Ends da_i.delete_project()
Expand All @@ -295,7 +292,7 @@ da_i.quietly_try_to_delete_interviews = async function ({ interviews = {}, api_k
});
responses.push( sub_responses );
} else {
log.debug({ code: `ALK0191`, context: `da api` },
log.debug({ code: `ALK0191`, context, },
`There is no interview to delete.`
);
}
Expand All @@ -312,7 +309,7 @@ da_i.quietly_delete_one_interview = async function ( timeout, { interview = {},
let responses = [];
for ( let api_key of api_keys ) {
try {
log.debug({ code: `ALK0188`, context: `da api` },
log.debug({ code: `ALK0188`, context, },
`Trying to delete the interview with the id ${ interview.uid } and an API key.`
);

Expand All @@ -323,12 +320,12 @@ da_i.quietly_delete_one_interview = async function ( timeout, { interview = {},
});
responses.push( response );

log.debug({ code: `ALK0189`, context: `da api` },
log.debug({ code: `ALK0189`, context, },
`No error when trying to delete interview ${ interview.uid } at ${ interview.i }.`
);
} catch ( error ) {
error.interview = interview;
log.debug({ code: `ALK0190`, context: `da api` },
log.debug({ code: `ALK0190`, context, },
`Error when deleting the interview. See https://docassemble.org/docs/api.html#session_delete.`,
error
);
Expand Down Expand Up @@ -366,7 +363,7 @@ da_i.get_interview = async function ({ interview = {}, api_keys = [], options =
if ( interview && interview.uid ) {
for ( let api_key of api_keys ) {
try {
log.debug({ code: `ALK0192`, context: `da api` },
log.debug({ code: `ALK0192`, context, },
`Trying to get the interview with the id ${ interview.uid } and an API key.`
);

Expand All @@ -377,26 +374,26 @@ da_i.get_interview = async function ({ interview = {}, api_keys = [], options =
});

if ( response && response.data && response.data.items && response.data.items[0] && response.data.items[0].session ) {
log.debug({ code: `ALK0193`, context: `da api` },
log.debug({ code: `ALK0193`, context, },
`Status ${ response.status }. Got ${ response.data.items[0].session }.`
);
return response.data.items[0];
} else {
log.debug({ code: `ALK0194`, context: `da api` },
log.debug({ code: `ALK0194`, context, },
`Status ${ response.status }. Cannot find ${ interview.uid }`
);
}

} catch ( error ) {
error.interview = interview;
log.debug({ code: `ALK0195`, context: `da api` },
log.debug({ code: `ALK0195`, context, },
`${ error.status || `No error.status.` } ${ error.statusText || error.data || `No error.statusText.` } Ran into an error when getting the interview ${ interview.uid }. See https://docassemble.org/docs/api.html#interviews.`,
error
);
}
} // ends for api_keys
} else {
log.debug({ code: `ALK0196`, context: `da api` },
log.debug({ code: `ALK0196`, context, },
`There is no interview to get.`
);
}
Expand Down
10 changes: 6 additions & 4 deletions lib/docassemble/server_install.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const session_vars = require('../utils/session_vars');
const Log = require('../utils/log');
const fg = require('fast-glob');

const context = `server_install`;

const server_install = async () => {
/** Finds out where in this directory the package's folders are. */
session_vars.save_install_method(`server`);
Expand All @@ -25,17 +27,17 @@ const server_install = async () => {
let chosen_folder = folders[0];
if ( folders.length === 0 ) {
let msg = `ALKiln cannot find the package's files. There are 0 folders at "${ path }". Stopping tests.`;
log.throw({ code: `ALK0044`, context: `server_install`,
log.throw({ code: `ALK0044`, context,
error: new ReferenceError( msg ) // no logs
});
}

if ( folders.length > 1 ) {
log.warn({ code: `ALK0045`, context: `setup`, },
log.warn({ code: `ALK0045`, context, },
`There are ${ folders.length } folders at "${ path }": "${ folders.join('", "') }". ALKiln chose to use the folder "${ chosen_folder }".`
);
} else {
log.info({ code: `ALK0218`, context: `setup`, },
log.info({ code: `ALK0218`, context, },
`ALKiln found the package folder at "${ chosen_folder }".`
);
}
Expand All @@ -50,7 +52,7 @@ session_vars.validateEnvironment();
// Local devs: This will be separate from your test paths
// unless you pass a `path` argument into the cucumber run command
const argv = require(`minimist`)(process.argv.slice(2));
const log = new Log({ path: argv.path, context: `server_install` });
const log = new Log({ path: argv.path, context, });

// 3. run
server_install();
10 changes: 6 additions & 4 deletions lib/docassemble/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const session_vars = require(`../utils/session_vars` );
const files = require(`../utils/files` );
const time = require(`../utils/time` );

const context = `setup`;

const setup = async () => {
/** In the docassemble testing account identified by the
* DOCASSEMBLE_DEVELOPER_API_KEY secret, create a new Project,
Expand All @@ -21,7 +23,7 @@ const setup = async () => {
const starting_msg = `Starting to upload this package to `
+ `a new Project called ${project_name} in the Playground `
+ `of your server's testing account. It may take a couple of minutes.`;
log.info({ code: `ALK0046`, context: `setup` },
log.info({ code: `ALK0046`, context, },
starting_msg
);

Expand Down Expand Up @@ -50,14 +52,14 @@ const setup = async () => {
if ( result.status !== 0 ) {
const install_err_msg = `Status code ${ result.status }. Could not `
+ `dainstall this package on your server. See above for more details.`;
log.throw({ code: `ALK0047`, context: `setup`,
log.throw({ code: `ALK0047`, context,
before: `☝️ ☝️ ☝️ Error data ☝️ ☝️ ☝️\n`,
error: `Error while installing project` },
install_err_msg
);
}

log.success({ code: `ALK0048`, context: `setup`, },
log.success({ code: `ALK0048`, context, },
`ALKiln created a Project in your Playground named "${ project_name }"!`
);
}; // Ends setup();
Expand All @@ -69,7 +71,7 @@ session_vars.validateEnvironment();
// Local devs: This will be separate from your test paths
// unless you pass a `path` argument into the cucumber run command
const argv = require(`minimist`)(process.argv.slice(2));
const log = new Log({ path: argv.path, context: `setup` });
const log = new Log({ path: argv.path, context, });

// 3. run
setup();
10 changes: 6 additions & 4 deletions lib/docassemble/takedown.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ const da_i = require('./docassemble_api_interface');
const session_vars = require('../utils/session_vars');
const Log = require('../utils/log');

const context = `takedown`;

const takedown = async () => {
/** In the docassemble testing account identified by the
* DOCASSEMBLE_DEVELOPER_API_KEY secret, delete the
* Project created for this test.
* For local development, clean up project name file. */
log.info({ code: `ALK0177`, context: `takedown`, },
log.info({ code: `ALK0177`, context, },
`Trying to remove the docassemble Project from the testing account.`
);
// Explicit try-catch prevents "Unhandled promise rejection error"
// avoids showing confusing node errors to users that they shouldn't have to fix
try {
await da_i.delete_project();
session_vars.delete_project_name();
log.info({ code: `ALK0178`, context: `takedown`, },
log.info({ code: `ALK0178`, context, },
`Test takedown finished successfully!`
);
} catch (error) {
log.throw({ code: `ALK0179`, context: `takedown`, error: error },
log.throw({ code: `ALK0179`, context, error: error },
`Unable to delete project`,
);
// we don't re-throw the exception, so make sure to exit
Expand All @@ -40,7 +42,7 @@ session_vars.validateEnvironment();
// Local devs: This will be separate from your test artifacts paths
// unless you pass a `path` argument into the `cucumber` run command
const argv = require(`minimist`)( process.argv.slice(2) );
const log = new Log({ path: argv.path, context: `takedown` });
const log = new Log({ path: argv.path, context, });

// 3. run
takedown();

0 comments on commit 9b2df5b

Please sign in to comment.