fix(api): remove redirects from api

They should be handled either by nginx or by the client. Turned out a
lot of code, including the path migration, existed to support them.
Hence the large number of removals
This commit is contained in:
Oliver Eyton-Williams
2020-08-21 18:55:28 +02:00
committed by Mrugesh Mohapatra
parent 4a45b5ac1c
commit 1ad5f756e0
12 changed files with 3 additions and 234 deletions

View File

@ -5,7 +5,7 @@
*
*/
import { Observable } from 'rx';
import { isEmpty, pick, omit, find, uniqBy, last } from 'lodash';
import { isEmpty, pick, omit, find, uniqBy } from 'lodash';
import debug from 'debug';
import dedent from 'dedent';
import { ObjectID } from 'mongodb';
@ -16,7 +16,6 @@ import { homeLocation } from '../../../config/env';
import { ifNoUserSend } from '../utils/middleware';
import { dasherize } from '../../../utils/slugs';
import _pathMigrations from '../resources/pathMigration.json';
import { fixCompletedChallengeItem } from '../../common/utils';
import { getChallenges } from '../utils/get-curriculum';
@ -26,7 +25,6 @@ export default async function bootChallenge(app, done) {
const send200toNonUser = ifNoUserSend(true);
const api = app.loopback.Router();
const router = app.loopback.Router();
const redirectToLearn = createRedirectToLearn(_pathMigrations);
const challengeUrlResolver = await createChallengeUrlResolver(
await getChallenges()
);
@ -57,12 +55,6 @@ export default async function bootChallenge(app, done) {
router.get('/challenges/current-challenge', redirectToCurrentChallenge);
router.get('/challenges', redirectToLearn);
router.get('/challenges/*', redirectToLearn);
router.get('/map', redirectToLearn);
app.use(api);
app.use(router);
done();
@ -357,18 +349,3 @@ export function createRedirectToCurrentChallenge(
return res.redirect(`${_homeLocation}${challengeUrl}`);
};
}
export function createRedirectToLearn(
pathMigrations,
base = homeLocation,
learn = learnURL
) {
return function redirectToLearn(req, res) {
const maybeChallenge = last(req.path.split('/'));
if (maybeChallenge in pathMigrations) {
const redirectPath = pathMigrations[maybeChallenge];
return res.status(302).redirect(`${base}${redirectPath}`);
}
return res.status(302).redirect(learn);
};
}

View File

@ -9,8 +9,7 @@ import {
createChallengeUrlResolver,
createRedirectToCurrentChallenge,
getFirstChallenge,
isValidChallengeCompletion,
createRedirectToLearn
isValidChallengeCompletion
} from '../boot/challenge';
import {
@ -21,8 +20,7 @@ import {
mockUser,
mockGetFirstChallenge,
mockCompletedChallenge,
mockCompletedChallenges,
mockPathMigrationMap
mockCompletedChallenges
} from './fixtures';
describe('boot/challenge', () => {
@ -394,33 +392,4 @@ describe('boot/challenge', () => {
done();
});
});
describe('redirectToLearn', () => {
const mockHome = 'https://example.com';
const mockLearn = 'https://example.com/learn';
const redirectToLearn = createRedirectToLearn(
mockPathMigrationMap,
mockHome,
mockLearn
);
it('redirects to learn by default', () => {
const req = mockReq({ path: '/challenges' });
const res = mockRes();
redirectToLearn(req, res);
expect(res.redirect.calledWith(mockLearn)).toBe(true);
});
it('maps to the correct redirect if the path matches a challenge', () => {
const req = mockReq({ path: '/challenges/challenge-two' });
const res = mockRes();
const expectedRedirect =
'https://example.com/learn/superblock/block/challenge-two';
redirectToLearn(req, res);
expect(res.redirect.calledWith(expectedRedirect)).toBe(true);
});
});
});

View File

@ -169,11 +169,6 @@ export const firstChallengeQuery = {
where: { challengeOrder: 0, superOrder: 1, order: 0 }
};
export const mockPathMigrationMap = {
'challenge-one': '/learn/superblock/block/challenge-one',
'challenge-two': '/learn/superblock/block/challenge-two'
};
export const fullStackChallenges = [
{
completedDate: 1585210952511,

View File

@ -1 +0,0 @@
# this file is here to add an empty dir to git