Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Commit

Permalink
Integration tests - part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
elias-ba committed Dec 21, 2021
1 parent 0e7a493 commit 7ec7508
Show file tree
Hide file tree
Showing 4 changed files with 412 additions and 488 deletions.
97 changes: 46 additions & 51 deletions lib/Adaptor.js
Expand Up @@ -144,58 +144,51 @@ function configMigrationHelper(state) {
return state;
} // NOTE: In order to prevent unintended exposure of authentication information
// in the logs, we make use of an axios interceptor.
// axios.interceptors.response.use(
// function (response) {
// const contentType = response.headers['content-type']?.split(';')[0];
// const acceptHeaders = response.config.headers['Accept']
// .split(';')[0]
// .split(',');
// if (response.config.method === 'get') {
// if (indexOf(acceptHeaders, contentType) === -1) {
// const newError = {
// status: 404,
// message: 'Unexpected content returned',
// responseData: response.data,
// };
// Log.error(newError.message);
// return Promise.reject(newError);
// }
// }
// if (
// typeof response?.data === 'string' &&
// contentType === CONTENT_TYPES?.json
// ) {
// try {
// response = { ...response, data: JSON.parse(response.data) };
// } catch (error) {
// Log.warn('Non-JSON response detected, unable to parse.');
// }
// }
// return response;
// },
// function (error) {
// try {
// const details = error.toJSON();
// if (details?.config?.auth) details.config.auth = '--REDACTED--';
// if (details?.config?.data) details.config.data = '--REDACTED--';
// Log.error(details.message);
// return Promise.reject(details);
// } catch (e) {
// // TODO: @Elias, why does this error sometimes already appear to be JSONified?
// // console.log(e) // "not JSONABLE TypeError: error.toJSON is not a function"
// Log.error(error.message);
// return Promise.reject(error);
// }
// }
// );


_axios.default.interceptors.response.use(function (response) {
var _response$headers$con, _response;

const contentType = (_response$headers$con = response.headers['content-type']) === null || _response$headers$con === void 0 ? void 0 : _response$headers$con.split(';')[0];
const acceptHeaders = response.config.headers['Accept'].split(';')[0].split(',');

if (response.config.method === 'get') {
if ((0, _lodash.indexOf)(acceptHeaders, contentType) === -1) {
const newError = {
status: 404,
message: 'Unexpected content returned',
responseData: response.data
};

_Utils.Log.error(newError.message);

return Promise.reject(newError);
}
}

if (typeof ((_response = response) === null || _response === void 0 ? void 0 : _response.data) === 'string' && contentType === (_Utils.CONTENT_TYPES === null || _Utils.CONTENT_TYPES === void 0 ? void 0 : _Utils.CONTENT_TYPES.json)) {
try {
response = { ...response,
data: JSON.parse(response.data)
};
} catch (error) {
_Utils.Log.warn('Non-JSON response detected, unable to parse.');
}
}

return response;
}, function (error) {
try {
var _details$config, _details$config2;

const details = error.toJSON();
if (details === null || details === void 0 ? void 0 : (_details$config = details.config) === null || _details$config === void 0 ? void 0 : _details$config.auth) details.config.auth = '--REDACTED--';
if (details === null || details === void 0 ? void 0 : (_details$config2 = details.config) === null || _details$config2 === void 0 ? void 0 : _details$config2.data) details.config.data = '--REDACTED--';

_Utils.Log.error(details.message);

return Promise.reject(details);
} catch (e) {
// TODO: @Elias, why does this error sometimes already appear to be JSONified?
// console.log(e) // "not JSONABLE TypeError: error.toJSON is not a function"
_Utils.Log.error(error.message);

return Promise.reject(error);
}
});
/**
* Create a record
* @public
Expand Down Expand Up @@ -321,6 +314,8 @@ function create(resourceType, data, options, callback) {
_Utils.Log.success(`Created ${resourceType}: ${result.headers.location}`);

return (0, _Utils.handleResponse)(result, state, callback);
}).catch(error => {
console.log('ERROR', error);
});
};
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Utils.js
Expand Up @@ -42,7 +42,8 @@ exports.Log = Log;
function buildUrl(path, hostUrl, apiVersion) {
const pathSuffix = apiVersion ? `/${apiVersion}${path}` : `${path}`;
return hostUrl + '/api' + pathSuffix;
}
} // Write a unit test for this one


function handleResponse(result, state, callback) {
const {
Expand Down Expand Up @@ -71,8 +72,7 @@ function generateUrl(configuration, options, resourceType) {
} = configuration;
const urlString = '/' + resourceType; // Note that users can override the apiVersion from configuration with args

if (options === null || options === void 0 ? void 0 : options.apiVersion) apiVersion = options.apiVersion; // TODO: discuss how this actually works on DHIS2. I'm not sure I'm following.

if (options === null || options === void 0 ? void 0 : options.apiVersion) apiVersion = options.apiVersion;
const apiMessage = apiVersion ? `Using DHIS2 api version ${apiVersion}` : 'Using latest available version of the DHIS2 api on this server.';
console.log(apiMessage);
return buildUrl(urlString, hostUrl, apiVersion);
Expand Down
2 changes: 1 addition & 1 deletion src/Utils.js
Expand Up @@ -27,6 +27,7 @@ export function buildUrl(path, hostUrl, apiVersion) {
return hostUrl + '/api' + pathSuffix;
}

// Write a unit test for this one
export function handleResponse(result, state, callback) {
const { data } = result;
if (callback) return callback(composeNextState(state, data));
Expand All @@ -50,7 +51,6 @@ export function generateUrl(configuration, options, resourceType) {
// Note that users can override the apiVersion from configuration with args
if (options?.apiVersion) apiVersion = options.apiVersion;

// TODO: discuss how this actually works on DHIS2. I'm not sure I'm following.
const apiMessage = apiVersion
? `Using DHIS2 api version ${apiVersion}`
: 'Using latest available version of the DHIS2 api on this server.';
Expand Down

0 comments on commit 7ec7508

Please sign in to comment.