diff --git a/api-server/server/boot/challenge.js b/api-server/server/boot/challenge.js index 758a35fa5b..4ef3a5db1f 100644 --- a/api-server/server/boot/challenge.js +++ b/api-server/server/boot/challenge.js @@ -16,7 +16,7 @@ import { homeLocation } from '../../../config/env'; import { ifNoUserSend } from '../utils/middleware'; import { dasherize } from '../utils'; -import pathMigrations from '../resources/pathMigration.json'; +import _pathMigrations from '../resources/pathMigration.json'; import { fixCompletedChallengeItem } from '../../common/utils'; const log = debug('fcc:boot:challenges'); @@ -25,6 +25,7 @@ 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(app); const redirectToCurrentChallenge = createRedirectToCurrentChallenge( challengeUrlResolver @@ -352,11 +353,17 @@ export function createRedirectToCurrentChallenge( }; } -function redirectToLearn(req, res) { - const maybeChallenge = last(req.path.split('/')); - if (maybeChallenge in pathMigrations) { - const redirectPath = pathMigrations[maybeChallenge]; - return res.status(302).redirect(`${learnURL}${redirectPath}`); - } - return res.status(302).redirect(learnURL); +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); + }; } diff --git a/api-server/server/boot_tests/challenge.test.js b/api-server/server/boot_tests/challenge.test.js index 68d173684b..ca98f9017f 100644 --- a/api-server/server/boot_tests/challenge.test.js +++ b/api-server/server/boot_tests/challenge.test.js @@ -9,7 +9,8 @@ import { createChallengeUrlResolver, createRedirectToCurrentChallenge, getFirstChallenge, - isValidChallengeCompletion + isValidChallengeCompletion, + createRedirectToLearn } from '../boot/challenge'; import { @@ -22,7 +23,8 @@ import { mockGetFirstChallenge, firstChallengeQuery, mockCompletedChallenge, - mockCompletedChallenges + mockCompletedChallenges, + mockPathMigrationMap } from './fixtures'; describe('boot/challenge', () => { @@ -372,5 +374,32 @@ describe('boot/challenge', () => { }); }); - xdescribe('redirectToLearn'); + 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); + }); + }); }); diff --git a/api-server/server/boot_tests/fixtures.js b/api-server/server/boot_tests/fixtures.js index 8c94335c25..3bb66fe191 100644 --- a/api-server/server/boot_tests/fixtures.js +++ b/api-server/server/boot_tests/fixtures.js @@ -81,3 +81,8 @@ export const firstChallengeQuery = { // first challenge of the first block of the first superBlock where: { challengeOrder: 0, superOrder: 1, order: 0 } }; + +export const mockPathMigrationMap = { + 'challenge-one': '/learn/superblock/block/challenge-one', + 'challenge-two': '/learn/superblock/block/challenge-two' +};