Skip to content
This repository was archived by the owner on Aug 6, 2025. It is now read-only.

Commit b9641c1

Browse files
committed
dochub tests and refactor
1 parent 2dd486d commit b9641c1

File tree

9 files changed

+79
-135
lines changed

9 files changed

+79
-135
lines changed

worker/jobTypes/publishDochubJob.js

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const workerUtils = require('../utils/utils');
22
const validator = require('validator');
3-
const mongo = require('../utils/mongo');
43
const invalidJobDef = new Error('job not valid');
54
const invalidEnvironment = new Error(
65
'environment variables missing for jobtype'
@@ -14,22 +13,6 @@ function safeString(stringToCheck) {
1413
return validator.isAscii(stringToCheck);
1514
}
1615

17-
// retrieve the target map to upload to fastly
18-
async function getTargetMap() {
19-
return mongo.getDochubArray();
20-
}
21-
22-
async function filtermap(map) {
23-
let filteredMap = [];
24-
map.forEach(element => {
25-
let page = 'https://dochub.mongodb.org/core/' + element.name;
26-
if (workerUtils.validateUrl(page)) {
27-
filteredMap.push(element);
28-
}
29-
});
30-
return filteredMap;
31-
}
32-
3316
function safePublishDochub(currentJob) {
3417
if (
3518
!currentJob ||
@@ -67,7 +50,7 @@ async function runPublishDochub(currentJob) {
6750
!currentJob.payload ||
6851
!currentJob.payload.source ||
6952
!currentJob.payload.target ||
70-
!currentJob.payload.email
53+
!currentJob.email
7154
) {
7255
workerUtils.logInMongo(
7356
currentJob,
@@ -76,35 +59,27 @@ async function runPublishDochub(currentJob) {
7659
throw invalidJobDef;
7760
}
7861

79-
let map = await getTargetMap();
80-
if (map === undefined) {
81-
console.log('no docs in map');
82-
workerUtils.logInMongo(
83-
currentJob,
84-
`${'(DOCHUB)'.padEnd(15)}failed due to no targets defined`
85-
);
86-
throw invalidJobDef;
87-
}
62+
let map = {
63+
'source': currentJob.payload.source,
64+
'target': currentJob.payload.target
65+
};
8866

89-
map = await filtermap(map);
9067
if (map === undefined) {
9168
workerUtils.logInMongo(
9269
currentJob,
93-
`${'(DOCHUB)'.padEnd(15)}failed due to no valid targets`
70+
`${'(DOCHUB)'.padEnd(15)}failed due to no targets defined`
9471
);
9572
throw invalidJobDef;
9673
}
97-
98-
const job = new FastlyJob(currentJob);
99-
await job.connectAndUpsert(map).catch(err => {
100-
console.log('could not complete map');
74+
75+
const initFastly = new FastlyJob(currentJob);
76+
await initFastly.connectAndUpsert(map).then().catch (err => {
10177
workerUtils.logInMongo(currentJob, `could not complete map ${err}`);
78+
throw invalidEnvironment;
10279
});
10380
}
10481

10582
module.exports = {
10683
runPublishDochub,
107-
safePublishDochub,
108-
filtermap,
109-
getTargetMap
84+
safePublishDochub
11085
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/** ******************************************************************
2+
* Sample Class for Testing *
3+
******************************************************************* */
4+
5+
const env = require('../../utils/environment');
6+
7+
describe('Test Class', () => {
8+
beforeAll(() => {});
9+
10+
afterAll(() => {});
11+
12+
beforeEach(() => {});
13+
14+
afterEach(() => {});
15+
16+
it('test env defaults', async () => {
17+
expect(env.EnvironmentClass.getAtlasPassword()).toEqual('passwordTest');
18+
expect(env.EnvironmentClass.getAtlasUsername()).toEqual('usernameTest');
19+
expect(env.EnvironmentClass.getDB()).toEqual('pool_test');
20+
expect(env.EnvironmentClass.getDochubMap()).toEqual('dochubMap');
21+
expect(env.EnvironmentClass.getFastlyServiceId()).toEqual('testId');
22+
expect(env.EnvironmentClass.getFastlyToken()).toBeUndefined();
23+
expect(env.EnvironmentClass.getXlarge()).toEqual(false);
24+
25+
});
26+
});

worker/tests/unit/fastlyJob.test.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const FastlyJob = require('../../utils/fastlyJob').FastlyJobClass;
77
const payloadObj = {
88
source: 'someSource',
99
target: 'someTarget',
10-
1110
};
1211

1312
const goodDochuhJob = {
@@ -20,6 +19,11 @@ const doc = {
2019
name: 'collections'
2120
};
2221

22+
const map = {
23+
'source': 'source',
24+
'target': 'target'
25+
};
26+
2327
describe('Fastly Job Test Class', () => {
2428
beforeAll(() => {});
2529

@@ -29,16 +33,10 @@ describe('Fastly Job Test Class', () => {
2933

3034
afterEach(() => {});
3135

32-
it('Fastly Job Class Test', async () => {
33-
const fastlyJob = new FastlyJob(goodDochuhJob);
34-
fastlyJob.connectAndUpsert = jest.fn().mockImplementation(() => {
35-
return { success: true };
36-
});
37-
expect(fastlyJob.connectAndUpsert([doc])).toEqual({ success: true });
38-
});
39-
4036
it('FastlyJob test connect and upsert', async () => {
41-
const fastlyJob = new FastlyJob(goodDochuhJob);
42-
expect(fastlyJob.connectAndUpsert([doc])).toBeDefined();
37+
const fastly = new FastlyJob(this.goodDochuhJob);
38+
return fastly.connectAndUpsert(map).catch(error => {
39+
expect(error).toBeDefined();
40+
});
4341
});
4442
});

worker/tests/unit/mongo.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const { MongoClient } = require('mongodb');
22
const mongo = require('../../utils/mongo');
3+
const env = require('../../utils/environment');
34

4-
const runXlarge =
5-
process.env.XLARGE === undefined ? false : Boolean(process.env.XLARGE);
5+
const runXlarge = env.EnvironmentClass.getXlarge();
66
const Monitor = require('../../utils/monitor').Monitor;
77

88
// Helper function to add n days to the current date

worker/tests/unit/publishDochubJob.unit.test.js

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
const job = require('../../jobTypes/publishDochubJob');
22
const workerUtils = require('../../utils/utils');
3-
const mongo = require('../../utils/mongo');
43

54
const payloadObj = {
65
source: 'someSource',
76
target: 'someTarget',
8-
97
};
108

119
const payloadObjBadSource = {
12-
source: 'some source',
10+
sourcebad: 'some source',
1311
target: 'someTarget'
1412
};
1513

1614
const payloadObjBadTarget = {
1715
source: 'someSource',
18-
target: 'some target'
16+
targetbad: 'some target'
1917
};
2018

2119
const payloadNoEmail = {
@@ -24,29 +22,24 @@ const payloadNoEmail = {
2422
};
2523

2624
const testPayloadGood = {
27-
payload: payloadObj
25+
payload: payloadObj,
26+
email: 'testemail'
2827
};
2928

3029
const testPayloadWithoutEmail = {
3130
payload: payloadNoEmail
3231
};
3332

3433
const testPayloadBadSource = {
35-
payload: payloadObjBadSource
34+
payload: payloadObjBadSource,
35+
email: 'testemail'
3636
};
3737

3838
const testPayloadBadTarget = {
39-
payload: payloadObjBadTarget
39+
payload: payloadObjBadTarget,
40+
email: 'testemail'
4041
};
4142

42-
const doc = [
43-
{
44-
_id: { $oid: '4db32eacdbd1ff5a7a24ff17' },
45-
url: 'http://www.mongodb.org/display/DOCS/Collections',
46-
name: 'collections'
47-
}
48-
];
49-
5043
const error = new Error('job not valid');
5144

5245
describe('Test Class', () => {
@@ -60,29 +53,18 @@ describe('Test Class', () => {
6053
// Tests for dochubpublish() function
6154

6255
it('runPublishDochub() rejects properly killed', async () => {
63-
mongo.getDochubArray = jest.fn().mockResolvedValue(doc);
6456
expect(await job.runPublishDochub(testPayloadGood)).toBeUndefined();
6557
});
6658
// Tests for RunPublishDochub Function
6759

6860
it('runPublishDochub() rejects lack of map', async () => {
69-
mongo.getDochubArray = jest.fn().mockResolvedValue(undefined);
70-
let thrownError;
71-
try {
72-
job.safePublishDochub(testPayloadGood).toBeCalled();
73-
} catch (e) {
74-
thrownError = e;
75-
}
76-
expect(thrownError).toEqual(error);
61+
expect(job.safePublishDochub(testPayloadGood)).toBeTruthy();
7762
});
7863

7964
it('runPublishDochub(): no email --> should fail to run', async () => {
80-
job.build = jest.fn().mockRejectedValue(error);
8165
await expect(job.runPublishDochub(testPayloadWithoutEmail)).rejects.toEqual(
8266
error
8367
);
84-
jest.runAllTimers();
85-
expect(job.build).toHaveBeenCalledTimes(0);
8668
});
8769

8870
// Sanitize

worker/utils/environment.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const fastlyToken = process.env.FASTLY_TOKEN;
22
const fastlyServiceId = process.env.FASTLY_SERVICE_ID;
33
const dochubMap = process.env.FASTLY_DOCHUB_MAP;
4-
const dochubDB = process.env.DOCHUB_DB;
54
const atlasUsername = process.env.MONGO_ATLAS_USERNAME;
65
const atlasPassword = process.env.MONGO_ATLAS_PASSWORD;
76
const xlarge = process.env.XLARGE;
@@ -19,13 +18,13 @@ class EnvironmentClass {
1918
if (xlarge === undefined) {
2019
return false;
2120
}
22-
return Boolean(xlarge);
21+
if (xlarge === 'true') {
22+
return true;
23+
}
24+
return false;
2325
}
2426

2527
static getFastlyToken() {
26-
if (fastlyToken === undefined) {
27-
return 'testToken';
28-
}
2928
return fastlyToken;
3029
}
3130

@@ -36,13 +35,6 @@ class EnvironmentClass {
3635
return dochubMap;
3736
}
3837

39-
static getDochubDB() {
40-
if (dochubDB === undefined) {
41-
return 'dochub';
42-
}
43-
return dochubDB;
44-
}
45-
4638
static getAtlasUsername() {
4739
if (atlasUsername === undefined) {
4840
return 'usernameTest';

worker/utils/fastlyJob.js

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,33 @@
11
const utils = require('../utils/utils');
22
const environment = require('../utils/environment').EnvironmentClass;
3-
43
const fastly = require('fastly')(environment.getFastlyToken());
54

5+
66
class FastlyJobClass {
77
// pass in a job payload to setup class
88
constructor(currentJob) {
99
this.currentJob = currentJob;
10-
if (fastly == undefined) {
10+
if (fastly === undefined) {
1111
utils.logInMongo(currentJob, 'fastly connectivity not found');
1212
}
1313
}
1414

1515
// upserts {source: target} mappings
1616
// to the fastly edge dictionary
1717
async connectAndUpsert(map) {
18-
for (let doc in map) {
19-
console.log(`iterating ${map[doc].url}`);
20-
const options = {
21-
item_value: map[doc].url
22-
};
23-
await this.runFastlyMapCall(map[doc], options)
24-
.catch(err => {
25-
return err;
26-
})
27-
.then();
28-
}
29-
}
30-
31-
async runFastlyMapCall(doc, options) {
32-
const connectString = `/service/${environment.getFastlyServiceId()}/dictionary/${environment.getDochubMap()}/item/${
33-
doc.name
34-
}`;
35-
36-
let promise1 = (method, url, options) => {
37-
return new Promise((resolve, reject) => {
38-
fastly.request(method, url, options, function(err, obj) {
39-
if (err) return reject(err);
40-
resolve(obj);
41-
});
42-
});
18+
const options = {
19+
item_value: map.target
4320
};
21+
const connectString = `/service/${environment.getFastlyServiceId()}/dictionary/${environment.getDochubMap()}/item/${
22+
map.source
23+
}`;
4424

45-
promise1('POST', connectString, options)
46-
.then()
47-
.catch(error => {
48-
throw error;
25+
return new Promise((resolve, reject) => {
26+
fastly.request('PUT', connectString, options, function (err, obj) {
27+
if (err) reject(err);
28+
resolve(obj);
4929
});
30+
})
5031
}
5132
}
5233

worker/utils/mongo.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ const DB_NAME = EnvironmentClass.getDB(); // Database name of the queue in Mongo
1313
const COLL_NAME = 'queue'; // Collection name of the queue in MongoDB Atlas
1414
const META_NAME = 'meta';
1515
const MONITOR_NAME = 'monitor';
16-
const FASTLY_NAME = 'keys';
17-
const DOCHUB_NAME = EnvironmentClass.getDochubDB();
1816

1917
// Hold onto the client
2018
let client;
@@ -50,19 +48,6 @@ module.exports = {
5048
return null;
5149
},
5250

53-
getDochubCollection() {
54-
if (client) {
55-
return client.db(DOCHUB_NAME).collection(FASTLY_NAME);
56-
}
57-
return null;
58-
},
59-
60-
async getDochubArray() {
61-
return await this.getDochubCollection()
62-
.find({})
63-
.toArray();
64-
},
65-
6651
async reportStatus(monitor) {
6752
monitor.setXlarge(runXlarge);
6853
monitor.setEnvType(DB_NAME);

0 commit comments

Comments
 (0)