Skip to content

Instantly share code, notes, and snippets.

@philippefutureboy
Created September 18, 2019 20:34
Show Gist options
  • Save philippefutureboy/b9eca477f0ef25789aa5e7e1121bf2be to your computer and use it in GitHub Desktop.
Save philippefutureboy/b9eca477f0ef25789aa5e7e1121bf2be to your computer and use it in GitHub Desktop.
e2e test for cognito-express
/* eslint-disable vars-on-top */
global.fetch = require('node-fetch');
const Amplify = require('aws-amplify');
const jwt = require('jsonwebtoken');
const { authenticationMiddleware } = require('./cognito');
const {
COGNITO_USER_EXPIRED_TOKEN,
COGNITO_USER_IMPROPERLY_FORMATTED_TOKEN,
} = process.env;
var COGNITO_USERPOOL_1_TOKEN;
var COGNITO_USERPOOL_2_TOKEN;
var GENERIC_TOKEN;
async function setupTokens() {
var cognitoUser;
try {
Amplify.default.configure({
region: process.env.COGNITO_REGION,
userPoolId: process.env.COGNITO_USERPOOL_ID,
userPoolWebClientId: process.env.COGNITO_CLIENT_ID,
});
const { Auth } = Amplify;
await Auth.signIn(
process.env.COGNITO_USER_USERNAME,
process.env.COGNITO_USER_PASSWORD
);
cognitoUser = await Auth.currentAuthenticatedUser();
COGNITO_USERPOOL_1_TOKEN = cognitoUser.signInUserSession.accessToken.jwtToken;
Amplify.default.configure({
region: process.env.COGNITO_REGION_2,
userPoolId: process.env.COGNITO_USERPOOL_ID_2,
userPoolWebClientId: process.env.COGNITO_CLIENT_ID_2,
});
await Auth.signIn(
process.env.COGNITO_USER_USERNAME,
process.env.COGNITO_USER_PASSWORD
);
cognitoUser = await Auth.currentAuthenticatedUser();
COGNITO_USERPOOL_2_TOKEN = cognitoUser.signInUserSession.accessToken.jwtToken;
GENERIC_TOKEN = jwt.sign(
{},
'6eb24b1decd42bb3be4c5a8434259fd87196d94820f29c1c725a0a3dc86611beab6ab80b8d24f70a6558023e6bd7b1fcfb4927e7acc42d2f09100407261f5cb1',
{ expiresIn: '1h' }
);
} catch (err) {
console.error(err);
throw err;
}
}
beforeAll(setupTokens);
describe(authenticationMiddleware.name, () => {
test('given a properly-formatted, un-expired Amazon Cognito token, should pass validation', async () => {
// arrange
const req = {
headers: {
Authorization: COGNITO_USERPOOL_1_TOKEN,
},
locals: {},
};
const res = {
status: jest.fn(() => res),
send: jest.fn(() => res),
};
const next = jest.fn();
const authMiddleware = authenticationMiddleware();
// act
await authMiddleware(req, res, next);
expect(next).toHaveBeenCalledTimes(1);
expect(res.status).not.toHaveBeenCalled();
expect(res.send).not.toHaveBeenCalled();
});
test('given a properly-formatted, expired Amazon Cognito token, should fail validation', async () => {
// arrange
const req = {
headers: {
Authorization: COGNITO_USER_EXPIRED_TOKEN,
},
locals: {},
};
const res = {
status: jest.fn(() => res),
send: jest.fn(() => res),
};
const next = jest.fn();
const authMiddleware = authenticationMiddleware();
// act
await authMiddleware(req, res, next);
expect(next).not.toHaveBeenCalled();
expect(res.status).toHaveBeenCalled();
expect(res.status.mock.calls[0][0]).toBe(401);
expect(res.send).toHaveBeenCalled();
});
test('given a properly-formatted token produced by another Amazon Cognito user pool, should fail validation', async () => {
// arrange
const req = {
headers: {
Authorization: COGNITO_USERPOOL_2_TOKEN,
},
locals: {},
};
const res = {
status: jest.fn(() => res),
send: jest.fn(() => res),
};
const next = jest.fn();
const authMiddleware = authenticationMiddleware();
// act
await authMiddleware(req, res, next);
expect(next).not.toHaveBeenCalled();
expect(res.status).toHaveBeenCalled();
expect(res.status.mock.calls[0][0]).toBe(401);
expect(res.send).toHaveBeenCalled();
});
test('given a properly-formatted token produced by another token producer than Amazon Cognito, should fail validation', async () => {
// arrange
const req = {
headers: {
Authorization: GENERIC_TOKEN,
},
locals: {},
};
const res = {
status: jest.fn(() => res),
send: jest.fn(() => res),
};
const next = jest.fn();
const authMiddleware = authenticationMiddleware();
// act
await authMiddleware(req, res, next);
expect(next).not.toHaveBeenCalled();
expect(res.status).toHaveBeenCalled();
expect(res.status.mock.calls[0][0]).toBe(401);
expect(res.send).toHaveBeenCalled();
});
test('given an improperly-formatted token, should fail validation', async () => {
// arrange
const req = {
headers: {
Authorization: COGNITO_USER_IMPROPERLY_FORMATTED_TOKEN,
},
locals: {},
};
const res = {
status: jest.fn(() => res),
send: jest.fn(() => res),
};
const next = jest.fn();
const authMiddleware = authenticationMiddleware();
// act
await authMiddleware(req, res, next);
expect(next).not.toHaveBeenCalled();
expect(res.status).toHaveBeenCalled();
expect(res.status.mock.calls[0][0]).toBe(401);
expect(res.send).toHaveBeenCalled();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment