mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2024-11-22 15:17:53 -05:00
session tests mock api response various ways
This commit is contained in:
parent
27a7324da0
commit
69e519286d
2 changed files with 99 additions and 31 deletions
|
@ -1,7 +1,6 @@
|
||||||
const keyMirror = require('keymirror');
|
const keyMirror = require('keymirror');
|
||||||
const defaults = require('lodash.defaults');
|
const defaults = require('lodash.defaults');
|
||||||
|
|
||||||
const api = require('../lib/api');
|
|
||||||
const sessionLib = require('../lib/session');
|
const sessionLib = require('../lib/session');
|
||||||
const messageCountActions = require('./message-count.js');
|
const messageCountActions = require('./message-count.js');
|
||||||
const permissionsActions = require('./permissions.js');
|
const permissionsActions = require('./permissions.js');
|
||||||
|
|
|
@ -1,56 +1,92 @@
|
||||||
describe('session library', () => {
|
describe('session library', () => {
|
||||||
// let mockAPIRequest = {};
|
// respond to session requests with empty session object
|
||||||
let mockAPIRequestNoSession = jest.fn((opts, callback) => {
|
let sessionNoUser = jest.fn((opts, callback) => {
|
||||||
console.log(`mocked has been called ${mockAPIRequestNoSession.mock.calls.length} times`);
|
|
||||||
callback(null, {}, {statusCode: 200});
|
callback(null, {}, {statusCode: 200});
|
||||||
});
|
});
|
||||||
// let mockAPIRequestYesSession = jest.fn((opts, callback) => {
|
// respond to session requests with session object that indicates
|
||||||
// console.log(`Yes Session mocked has been called ${mockAPIRequestNoSession.mock.calls.length} times`);
|
// successfully logged-in user
|
||||||
// callback(null, {}, {statusCode: 200});
|
let sessionYesUser = jest.fn((opts, callback) => {
|
||||||
// });
|
callback(null, {user: {username: 'test_username'}}, {statusCode: 200});
|
||||||
// let whichMockAPIRequest = mockAPIRequestNoSession;
|
});
|
||||||
// const mockCurrentAPIRequest = (() => {
|
// respond to first two requests with empty session object; after that,
|
||||||
// whichMockAPIRequest();
|
// respond with user in object
|
||||||
// })();
|
let sessionNoThenYes = jest.fn((opts, callback) => {
|
||||||
jest.mock('../../../src/lib/api', () => (
|
if (sessionNoThenYes.mock.calls.length <= 2) {
|
||||||
mockAPIRequestNoSession
|
callback(null, {}, {statusCode: 200});
|
||||||
));
|
} else {
|
||||||
|
callback(null, {user: {username: 'test_username'}}, {statusCode: 200});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// respond to session requests with response code 404, indicating no session
|
||||||
|
// found for that user
|
||||||
|
let sessionNotFound = jest.fn((opts, callback) => {
|
||||||
|
callback(null, null, {statusCode: 404});
|
||||||
|
});
|
||||||
|
// respond to session requests with response code 503, indicating connection failure
|
||||||
|
let sessionConnectFailure = jest.fn((opts, callback) => {
|
||||||
|
callback(null, null, {statusCode: 503});
|
||||||
|
});
|
||||||
|
|
||||||
|
// by changing whichMockAPIRequest, we can simulate different api responses
|
||||||
|
let whichMockAPIRequest = null;
|
||||||
|
let mockAPIRequest = (opts, callback) => {
|
||||||
|
whichMockAPIRequest(opts, callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
// mock lib/api.js, and include our mocked version in lib/session.js
|
||||||
|
jest.mock('../../../src/lib/api', () => {
|
||||||
|
return mockAPIRequest;
|
||||||
|
});
|
||||||
const sessionLib = require('../../../src/lib/session'); // eslint-disable-line global-require
|
const sessionLib = require('../../../src/lib/session'); // eslint-disable-line global-require
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
jest.clearAllMocks();
|
jest.clearAllMocks();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('requestSessionOnce calls api 1 time', done => {
|
test('requestSessionOnce can call api 1 time, when session found', done => {
|
||||||
//whichMockAPIRequest = mockAPIRequestNoSession;
|
whichMockAPIRequest = sessionYesUser;
|
||||||
// mockAPIRequest = jest.fn((opts, callback) => {
|
|
||||||
// callback(null, {}, {statusCode: 200});
|
|
||||||
// });
|
|
||||||
new Promise((resolve, reject) => { // eslint-disable-line no-undef
|
new Promise((resolve, reject) => { // eslint-disable-line no-undef
|
||||||
sessionLib.requestSessionOnce(resolve, reject);
|
sessionLib.requestSessionOnce(resolve, reject);
|
||||||
}).then(() => {
|
}).then(body => {
|
||||||
expect(mockAPIRequestNoSession).toHaveBeenCalledTimes(1);
|
expect(sessionYesUser).toHaveBeenCalledTimes(1);
|
||||||
|
expect(body).toEqual({user: {username: 'test_username'}});
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('requestSessionOnce can call api 1 time, when session not found', done => {
|
||||||
|
whichMockAPIRequest = sessionNoUser;
|
||||||
|
new Promise((resolve, reject) => { // eslint-disable-line no-undef
|
||||||
|
sessionLib.requestSessionOnce(resolve, reject);
|
||||||
|
}).then(body => {
|
||||||
|
expect(sessionNoUser).toHaveBeenCalledTimes(1);
|
||||||
|
expect(body).toEqual({});
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('requestSessionWithRetry can call api once', done => {
|
||||||
|
whichMockAPIRequest = sessionNoUser;
|
||||||
|
new Promise((resolve, reject) => { // eslint-disable-line no-undef
|
||||||
|
sessionLib.requestSessionWithRetry(resolve, reject, 0, 0);
|
||||||
|
}).then(() => {
|
||||||
|
expect(sessionNoUser).toHaveBeenCalledTimes(1);
|
||||||
done();
|
done();
|
||||||
}, () => {
|
|
||||||
console.log('sessionLib test: err');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('requestSessionWithRetry can call api multiple times', done => {
|
test('requestSessionWithRetry can call api multiple times', done => {
|
||||||
// mockAPIRequest = jest.fn((opts, callback) => {
|
whichMockAPIRequest = sessionNoUser;
|
||||||
// callback(null, {}, {statusCode: 200});
|
|
||||||
// });
|
|
||||||
new Promise((resolve, reject) => { // eslint-disable-line no-undef
|
new Promise((resolve, reject) => { // eslint-disable-line no-undef
|
||||||
sessionLib.requestSessionWithRetry(resolve, reject, 2, 0);
|
sessionLib.requestSessionWithRetry(resolve, reject, 2, 0);
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
expect(mockAPIRequestNoSession).toHaveBeenCalledTimes(3);
|
expect(sessionNoUser).toHaveBeenCalledTimes(3);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('requestSessionWithRetry respects total delay time param within a reasonable tolerance', done => {
|
test('requestSessionWithRetry respects total delay time param within a reasonable tolerance', done => {
|
||||||
// mockAPIRequest = jest.fn((opts, callback) => {
|
whichMockAPIRequest = sessionNoUser;
|
||||||
// callback(null, {}, {statusCode: 200});
|
|
||||||
// });
|
|
||||||
const startTime = new Date().getTime();
|
const startTime = new Date().getTime();
|
||||||
new Promise((resolve, reject) => { // eslint-disable-line no-undef
|
new Promise((resolve, reject) => { // eslint-disable-line no-undef
|
||||||
sessionLib.requestSessionWithRetry(resolve, reject, 2, 2500);
|
sessionLib.requestSessionWithRetry(resolve, reject, 2, 2500);
|
||||||
|
@ -61,4 +97,37 @@ describe('session library', () => {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('requestSessionWithRetry will retry if no user found, then stop when user is found', done => {
|
||||||
|
whichMockAPIRequest = sessionNoThenYes;
|
||||||
|
new Promise((resolve, reject) => { // eslint-disable-line no-undef
|
||||||
|
sessionLib.requestSessionWithRetry(resolve, reject, 4, 3000);
|
||||||
|
}).then(body => {
|
||||||
|
expect(body).toEqual({user: {username: 'test_username'}});
|
||||||
|
expect(sessionNoThenYes).toHaveBeenCalledTimes(3);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('requestSessionWithRetry handles session not found as immediate error', done => {
|
||||||
|
whichMockAPIRequest = sessionNotFound;
|
||||||
|
new Promise((resolve, reject) => { // eslint-disable-line no-undef
|
||||||
|
sessionLib.requestSessionWithRetry(resolve, reject, 2, 0);
|
||||||
|
}).then(() => {}, err => {
|
||||||
|
expect(err).toBeFalsy();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('requestSessionWithRetry handles connection failure by retrying', done => {
|
||||||
|
whichMockAPIRequest = sessionConnectFailure;
|
||||||
|
new Promise((resolve, reject) => { // eslint-disable-line no-undef
|
||||||
|
sessionLib.requestSessionWithRetry(resolve, reject, 2, 0);
|
||||||
|
}).then(body => {
|
||||||
|
expect(sessionConnectFailure).toHaveBeenCalledTimes(3);
|
||||||
|
expect(body).toBeFalsy();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue