Skip to content

Commit

Permalink
fix warn-upload-overwrite tests
Browse files Browse the repository at this point in the history
  • Loading branch information
m5r committed Oct 24, 2023
1 parent 6cbe57b commit 6985575
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
36 changes: 21 additions & 15 deletions src/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ const url = require('url');

const cache = new Map();

const request = (...args) => retry(() => rpn(...args), { retries: 0 });
const _request = (method) => (...args) => retry(() => rpn[method](...args), { retries: 0 });
const request = {
get: _request('get'),
head: _request('head'),
options: _request('options'),
post: _request('post'),
put: _request('put'),
patch: _request('patch'),
delete: _request('delete'),
};

const logDeprecatedTransitions = (settings) => {
const appSettings = JSON.parse(settings);
Expand All @@ -18,7 +27,7 @@ const logDeprecatedTransitions = (settings) => {
}

const uri = `${environment.instanceUrl}/api/v1/settings/deprecated-transitions`;
return request({ uri, method: 'GET', json: true })
return request.get({ uri, json: true })
.then(transitions => {
(transitions || []).forEach(transition => {
const transitionSetting = appSettings.transitions[transition.name];
Expand All @@ -37,8 +46,7 @@ const logDeprecatedTransitions = (settings) => {
};

const updateAppSettings = (settings) => {
return request({
method: 'PUT',
return request.put({
url: `${environment.apiUrl}/_design/medic/_rewrite/update_settings/medic?replace=1`,
headers: {'Content-Type': 'application/json'},
body: settings,
Expand All @@ -48,7 +56,7 @@ const updateAppSettings = (settings) => {
const api = {
getAppSettings: () => {
const url = `${environment.apiUrl}/_design/medic/_rewrite/app_settings/medic`;
return request({ url, json: true })
return request.get({ url, json: true })
.catch(err => {
if(err.statusCode === 404) {
throw new Error(`Failed to fetch existing app_settings from ${url}.\n` +
Expand All @@ -69,22 +77,20 @@ const api = {
},

createUser(userData) {
return request({
return request.post({
uri: `${environment.instanceUrl}/api/v1/users`,
method: 'POST',
json: true,
body: userData,
});
},

getUserInfo(queryParams) {
return request(`${environment.instanceUrl}/api/v1/users-info`, { qs: queryParams, json: true });
return request.get(`${environment.instanceUrl}/api/v1/users-info`, { qs: queryParams, json: true });
},

uploadSms(messages) {
return request({
return request.post({
uri: `${environment.instanceUrl}/api/sms`,
method: 'POST',
json: true,
body: { messages },
});
Expand All @@ -99,8 +105,9 @@ const api = {
const url = `${environment.apiUrl}/`;
log.info(`Checking that ${url} is available...`);
try {
await request(url);
await request.get(url);
} catch (err) {
console.log('err', err);

Check failure on line 110 in src/lib/api.js

View workflow job for this annotation

GitHub Actions / Build for Node version 8.x

Unexpected console statement

Check failure on line 110 in src/lib/api.js

View workflow job for this annotation

GitHub Actions / Build for Node version 10.x

Unexpected console statement

Check failure on line 110 in src/lib/api.js

View workflow job for this annotation

GitHub Actions / Build for Node version 12.x

Unexpected console statement

Check failure on line 110 in src/lib/api.js

View workflow job for this annotation

GitHub Actions / Build for Node version 14.x

Unexpected console statement

Check failure on line 110 in src/lib/api.js

View workflow job for this annotation

GitHub Actions / Build for Node version 16.x

Unexpected console statement

Check failure on line 110 in src/lib/api.js

View workflow job for this annotation

GitHub Actions / Build for Node version 18.x

Unexpected console statement
if (err.statusCode === 401) {
throw new Error(`Authentication failed connecting to ${url}. `
+ 'Check the supplied username and password and try again.');
Expand All @@ -119,7 +126,7 @@ const api = {
},

version() {
return request({ uri: `${environment.instanceUrl}/api/deploy-info`, method: 'GET', json: true }) // endpoint added in 3.5
return request.get({ uri: `${environment.instanceUrl}/api/deploy-info`, json: true }) // endpoint added in 3.5
.then(deploy_info => deploy_info && deploy_info.version);
},

Expand Down Expand Up @@ -153,8 +160,7 @@ const api = {
// (old version), so we assume form is valid but return special result
return Promise.resolve({ok: true, formsValidateEndpointFound: false});
}
return request({
method: 'POST',
return request.post({
uri: `${environment.instanceUrl}/api/v1/forms/validate`,
headers: { 'Content-Type': 'application/xml' },
body: formXml,
Expand Down Expand Up @@ -191,7 +197,7 @@ const api = {
if (cache.has('compressibleTypes')) {
return cache.get('compressibleTypes');
}
const resp = await request({ url: configUrl, json: true });
const resp = await request.get({ url: configUrl, json: true });
const compressibleTypes = resp.compressible_types.split(',').map(s=>s.trim());
cache.set('compressibleTypes', compressibleTypes);
return compressibleTypes;
Expand Down
4 changes: 2 additions & 2 deletions test/lib/warn-upload-overwrite.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ describe('warn-upload-overwrite', () => {
const localDoc = { _id: 'a', value: 2 };
return warnUploadOverwrite.preUploadDoc(api.db, localDoc).then(() => {
assert.equal(calls.length, 1);
assert.equal(request.args[0][0].url, 'http://admin:pass@localhost:35423/api/couch-config-attachments');
assert.equal(request.callCount, 1);
assert.equal(request.get.args[0][0].url, 'http://admin:pass@localhost:35423/api/couch-config-attachments');
assert.equal(request.get.callCount, 1);
assert.equal(calls[0][0], ' {\n\u001b[31m- _rev: "x"\u001b[39m\n\u001b[31m- value: 1\u001b[39m\n\u001b[32m+ value: 2\u001b[39m\n }\n');
});
});
Expand Down

0 comments on commit 6985575

Please sign in to comment.