Skip to content

Commit

Permalink
Update the NodeJS got module to latest version (#20)
Browse files Browse the repository at this point in the history
* Update Node to lts/iron in devcontainer
* Update ex-02 dependencies, bump got version, upd code/tests
* Update ex-04 dep, bump got, update code and tests
* Update ex-05 dep, bump got, upd code/tests
* Update ex-09 dep, dump got, update code/tests
* Upd ex-10 dep for client and api. Bump got, update code/tests
* Update ex-11, part 1 (missing obo for episodes)
* Upd ex-11, episodes, got o-b-o, code, tests  - fixed prehandler to avoid resends
  • Loading branch information
larskaare authored May 23, 2024
1 parent 45e87d6 commit 09ae593
Show file tree
Hide file tree
Showing 52 changed files with 9,210 additions and 11,296 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"ghcr.io/devcontainers-contrib/features/pre-commit:2": {},
"ghcr.io/eitsupi/devcontainer-features/jq-likes:2": {},
"ghcr.io/devcontainers/features/node:1": {
"version": "18.19"
"version": "20.13.1"
},
"ghcr.io/devcontainers/features/python:1": {
"version": "3.10.13",
Expand Down
18 changes: 13 additions & 5 deletions ex-02/lib/auth-utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const got = require('got');
const logger = require('./logger.js').logger;
const appConfig = require('./app-config.js');
var __ = require('underscore');
Expand Down Expand Up @@ -29,12 +28,17 @@ async function requestAccessTokenUsingAuthCode(authCode) {
logger.info(
'Requesting access token at ' + appConfig.serverConfig.tokenEndpoint
);

const got = (await import('got')).default;


const response = await got.post(appConfig.serverConfig.tokenEndpoint, {
headers: requestHeaders,
body: requestBody,
responseType: 'json'
});

var responseBody = JSON.parse(response.body);
var responseBody = response.body;

logger.debug('AccessToken in response : ' + responseBody.access_token);
return responseBody.access_token;
Expand Down Expand Up @@ -97,16 +101,20 @@ async function readInbox(accessToken) {

try {
logger.info('Preparing to read inbox ...');

const got = (await import('got')).default;

const response = await got.get(
"https://graph.microsoft.com/v1.0/me/mailFolders('Inbox')/messages?$select=sender,subject",
{
headers: requestHeaders
headers: requestHeaders,
responseType: 'json'
}
);

logger.debug('Got inbox - building response');

var mailBody = JSON.parse(response.body);
var mailBody = response.body;

__.each(mailBody.value, function (item, index) {
newMails.push(item.sender.emailAddress.name + ' - ' + item.subject);
Expand Down
1,766 changes: 835 additions & 931 deletions ex-02/package-lock.json

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions ex-02/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@
"license": "MIT",
"dependencies": {
"@fastify/formbody": "^7.4.0",
"@fastify/static": "^6.12.0",
"@fastify/view": "^8.2.0",
"fastify": "^4.24.3",
"@fastify/static": "^7.0.4",
"@fastify/view": "^9.1.0",
"fastify": "^4.27.0",
"fastify-favicon": "^4.3.0",
"got": "^11.8.3",
"got": "^14.2.1",
"handlebars": "^4.7.8",
"path": "^0.12.7",
"pino": "^8.16.2",
"pino-pretty": "^10.2.3",
"pino": "^9.1.0",
"pino-pretty": "^11.0.0",
"random-string": "^0.2.0",
"underscore": "^1.13.6"
},
"devDependencies": {
"nodemon": "^3.0.2",
"sinon": "^17.0.1",
"snyk": "^1.1260.0",
"tap": "^18.6.1"
"nodemon": "^3.1.0",
"sinon": "^18.0.0",
"snyk": "^1.1291.0",
"tap": "^18.8.0"
}
}
98 changes: 52 additions & 46 deletions ex-02/test/auth-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,26 @@ const { test } = require('tap');
const authUtils = require('../lib/auth-utils.js');
var __ = require('underscore');
var sinon = require('sinon');
const got = require('got');
const { getGot, resetGot } = require('./gotHelper.js');

test('Request access token should respond with access token', async (t) => {

test('Request accesstoken should respond with access token', async (t) => {

const authCode = '1234'

//Creating response object, some objects are strings of JSON!
const responseObject = {"body" : '{"access_token": "eyABCDE"}'};
const responseObject = {body : {access_token: 'eyABCDE'}};

sinon.stub(got,'post');
got.post.callsFake(async function() {
return responseObject;
});
// Reset got instance before stubbing
resetGot();

const got = await getGot();
sinon.stub(got, 'post').resolves(responseObject);

var accessToken = await authUtils.requestAccessTokenUsingAuthCode(
authCode
);
authCode
);

console.log('AT: ' + accessToken);
console.log('AT: ' + accessToken);

t.ok((__.size(accessToken) > 0), 'Access token received');
t.ok(__.isString(accessToken),'Access token should be string');
Expand All @@ -42,9 +43,11 @@ test('Request accesstoken should respond with access token', async (t) => {
test('Failed request to aquire access token should fail', async (t) => {
const authCode = '1234';


sinon.stub(got, 'get');
got.get.throws("Test: Error from request to token endpoint");
// Reset got instance before stubbing
resetGot();

const got = await getGot();
sinon.stub(got, 'get').throws(new Error("Test: Error from request to token endpoint"));

var accessToken = await authUtils.requestAccessTokenUsingAuthCode(authCode);

Expand All @@ -58,38 +61,39 @@ test('Failed request to aquire access token should fail', async (t) => {


test('Request to read inbox should return inbox content', async (t) => {

const accessToken = 'eyABCDE';
const emailReturnObject = {
"value": [
{
"subject": "Test Melding 1",
"sender": {
"emailAddress": {
"name": "Robert Knallert",
"address": "[email protected]"
}
"value": [
{
"subject": "Test Melding 1",
"sender": {
"emailAddress": {
"name": "Robert Knallert",
"address": "[email protected]"
}
},
{
"subject": "Test Melding 2",
"sender": {
"emailAddress": {
"name": "Jon Snow",
"address": "[email protected]"
}
}
},
{
"subject": "Test Melding 2",
"sender": {
"emailAddress": {
"name": "Jon Snow",
"address": "[email protected]"
}
}
]

}
]
};

const responseObject = { body: JSON.stringify(emailReturnObject) };
// Mock response object
const responseObject = { body: emailReturnObject };

sinon.stub(got, 'get');
got.get.callsFake(async function () {
return responseObject;
});
// Reset got instance before stubbing
resetGot();

// Get the got instance and stub the get method
const got = await getGot();
sinon.stub(got, 'get').resolves(responseObject);

var newMails = await authUtils.readInbox(accessToken);

Expand All @@ -105,13 +109,17 @@ test('Request to read inbox should return inbox content', async (t) => {

test('Request to read inbox should return empty when error', async (t) => {
const accessToken = 'eyABCDE';

sinon.stub(got, 'get');
got.get.throws('Test: Error from request to ms graph inbox');

// Reset got instance before stubbing
resetGot();

// Get the got instance and stub the get method to throw an error
const got = await getGot();
sinon.stub(got, 'get').throws(new Error('Test: Error from request to ms graph inbox'));

var newMails = await authUtils.readInbox(accessToken);
t.ok(__.size(newMails) == 0, 'Empty array of new mails received');

t.ok(__.size(newMails) === 0, 'Empty array of new mails received');
t.ok(__.isArray(newMails), 'New mails should be in array');

got.get.restore();
Expand Down Expand Up @@ -157,6 +165,4 @@ test('user-agent header should be correct', async (t) => {

t.end();
});

t.end();
});
});
16 changes: 16 additions & 0 deletions ex-02/test/gotHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
let gotInstance;

async function getGot() {
if (!gotInstance) {
const got = await import('got');
gotInstance = got.default;
}
return gotInstance;
}

// For testing purposes: Allow resetting gotInstance
function resetGot() {
gotInstance = null;
}

module.exports = { getGot, resetGot };
4 changes: 3 additions & 1 deletion ex-04/lib/auth-utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const got = require('got');
const logger = require('./logger.js').logger;
const appConfig = require('./app-config.js');
var __ = require('underscore');
Expand Down Expand Up @@ -89,6 +88,9 @@ async function readInbox(accessToken) {

try {
logger.info('Preparing to read inbox ...');

const got = (await import('got')).default;

const response = await got.get(
"https://graph.microsoft.com/v1.0/me/mailFolders('Inbox')/messages?$select=sender,subject",
{
Expand Down
Loading

0 comments on commit 09ae593

Please sign in to comment.