| 
									
										
										
										
											2019-02-22 11:49:12 +00:00
										 |  |  | /* global describe xdescribe it expect */ | 
					
						
							| 
									
										
										
										
											2020-06-23 10:01:21 +02:00
										 |  |  | import { first, find } from 'lodash'; | 
					
						
							| 
									
										
										
										
											2019-02-22 11:49:12 +00:00
										 |  |  | import sinon from 'sinon'; | 
					
						
							|  |  |  | import { mockReq, mockRes } from 'sinon-express-mock'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { | 
					
						
							| 
									
										
										
										
											2019-02-23 21:43:51 +00:00
										 |  |  |   buildUserUpdate, | 
					
						
							| 
									
										
										
										
											2019-02-22 11:49:12 +00:00
										 |  |  |   buildChallengeUrl, | 
					
						
							|  |  |  |   createChallengeUrlResolver, | 
					
						
							|  |  |  |   createRedirectToCurrentChallenge, | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:50 +00:00
										 |  |  |   getFirstChallenge, | 
					
						
							| 
									
										
										
										
											2020-08-21 18:55:28 +02:00
										 |  |  |   isValidChallengeCompletion | 
					
						
							| 
									
										
										
										
											2019-02-22 11:49:12 +00:00
										 |  |  | } from '../boot/challenge'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-23 21:43:51 +00:00
										 |  |  | import { | 
					
						
							|  |  |  |   firstChallengeUrl, | 
					
						
							|  |  |  |   requestedChallengeUrl, | 
					
						
							| 
									
										
										
										
											2020-06-23 10:01:21 +02:00
										 |  |  |   mockAllChallenges, | 
					
						
							| 
									
										
										
										
											2019-02-23 21:43:51 +00:00
										 |  |  |   mockChallenge, | 
					
						
							|  |  |  |   mockUser, | 
					
						
							|  |  |  |   mockGetFirstChallenge, | 
					
						
							|  |  |  |   mockCompletedChallenge, | 
					
						
							| 
									
										
										
										
											2020-08-21 18:55:28 +02:00
										 |  |  |   mockCompletedChallenges | 
					
						
							| 
									
										
										
										
											2019-02-23 21:43:51 +00:00
										 |  |  | } from './fixtures'; | 
					
						
							| 
									
										
										
										
											2019-02-22 11:49:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('boot/challenge', () => { | 
					
						
							| 
									
										
										
										
											2019-10-18 01:17:37 +01:00
										 |  |  |   xdescribe('backendChallengeCompleted', () => {}); | 
					
						
							| 
									
										
										
										
											2019-02-22 11:49:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-23 21:43:51 +00:00
										 |  |  |   describe('buildUserUpdate', () => { | 
					
						
							|  |  |  |     it('returns an Object with a nested "completedChallenges" property', () => { | 
					
						
							|  |  |  |       const result = buildUserUpdate( | 
					
						
							|  |  |  |         mockUser, | 
					
						
							|  |  |  |         '123abc', | 
					
						
							|  |  |  |         mockCompletedChallenge, | 
					
						
							|  |  |  |         'UTC' | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |       expect(result).toHaveProperty('updateData.$set.completedChallenges'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-26 00:38:31 +05:30
										 |  |  |     // eslint-disable-next-line max-len
 | 
					
						
							| 
									
										
										
										
											2019-02-23 21:43:51 +00:00
										 |  |  |     it('preserves file contents if the completed challenge is a JS Project', () => { | 
					
						
							|  |  |  |       const jsChallengeId = 'aa2e6f85cab2ab736c9a9b24'; | 
					
						
							|  |  |  |       const completedChallenge = { | 
					
						
							|  |  |  |         ...mockCompletedChallenge, | 
					
						
							|  |  |  |         completedDate: Date.now(), | 
					
						
							|  |  |  |         id: jsChallengeId | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |       const result = buildUserUpdate( | 
					
						
							|  |  |  |         mockUser, | 
					
						
							|  |  |  |         jsChallengeId, | 
					
						
							|  |  |  |         completedChallenge, | 
					
						
							|  |  |  |         'UTC' | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |       const firstCompletedChallenge = first( | 
					
						
							|  |  |  |         result.updateData.$set.completedChallenges | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(firstCompletedChallenge).toEqual(completedChallenge); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('preserves the original completed date of a challenge', () => { | 
					
						
							|  |  |  |       const completedChallengeId = 'aaa48de84e1ecc7c742e1124'; | 
					
						
							|  |  |  |       const completedChallenge = { | 
					
						
							|  |  |  |         ...mockCompletedChallenge, | 
					
						
							|  |  |  |         completedDate: Date.now(), | 
					
						
							|  |  |  |         id: completedChallengeId | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |       const originalCompletion = find( | 
					
						
							|  |  |  |         mockCompletedChallenges, | 
					
						
							|  |  |  |         x => x.id === completedChallengeId | 
					
						
							|  |  |  |       ).completedDate; | 
					
						
							|  |  |  |       const result = buildUserUpdate( | 
					
						
							|  |  |  |         mockUser, | 
					
						
							|  |  |  |         completedChallengeId, | 
					
						
							|  |  |  |         completedChallenge, | 
					
						
							|  |  |  |         'UTC' | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const firstCompletedChallenge = first( | 
					
						
							|  |  |  |         result.updateData.$set.completedChallenges | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(firstCompletedChallenge.completedDate).toEqual(originalCompletion); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-26 00:38:31 +05:30
										 |  |  |     // eslint-disable-next-line max-len
 | 
					
						
							| 
									
										
										
										
											2019-02-23 21:43:51 +00:00
										 |  |  |     it('does not attempt to update progressTimestamps for a previously completed challenge', () => { | 
					
						
							|  |  |  |       const completedChallengeId = 'aaa48de84e1ecc7c742e1124'; | 
					
						
							|  |  |  |       const completedChallenge = { | 
					
						
							|  |  |  |         ...mockCompletedChallenge, | 
					
						
							|  |  |  |         completedDate: Date.now(), | 
					
						
							|  |  |  |         id: completedChallengeId | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |       const { updateData } = buildUserUpdate( | 
					
						
							|  |  |  |         mockUser, | 
					
						
							|  |  |  |         completedChallengeId, | 
					
						
							|  |  |  |         completedChallenge, | 
					
						
							|  |  |  |         'UTC' | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const hasProgressTimestamps = | 
					
						
							|  |  |  |         '$push' in updateData && 'progressTimestamps' in updateData.$push; | 
					
						
							|  |  |  |       expect(hasProgressTimestamps).toBe(false); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-26 00:38:31 +05:30
										 |  |  |     // eslint-disable-next-line max-len
 | 
					
						
							| 
									
										
										
										
											2019-02-23 21:43:51 +00:00
										 |  |  |     it('provides a progressTimestamps update for new challenge completion', () => { | 
					
						
							|  |  |  |       expect.assertions(2); | 
					
						
							|  |  |  |       const { updateData } = buildUserUpdate( | 
					
						
							|  |  |  |         mockUser, | 
					
						
							|  |  |  |         '123abc', | 
					
						
							|  |  |  |         mockCompletedChallenge, | 
					
						
							|  |  |  |         'UTC' | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |       expect(updateData).toHaveProperty('$push'); | 
					
						
							|  |  |  |       expect(updateData.$push).toHaveProperty('progressTimestamps'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('removes repeat completions from the completedChallenges array', () => { | 
					
						
							|  |  |  |       const completedChallengeId = 'aaa48de84e1ecc7c742e1124'; | 
					
						
							|  |  |  |       const completedChallenge = { | 
					
						
							|  |  |  |         ...mockCompletedChallenge, | 
					
						
							|  |  |  |         completedDate: Date.now(), | 
					
						
							|  |  |  |         id: completedChallengeId | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |       const { | 
					
						
							|  |  |  |         updateData: { | 
					
						
							|  |  |  |           $set: { completedChallenges } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } = buildUserUpdate( | 
					
						
							|  |  |  |         mockUser, | 
					
						
							|  |  |  |         completedChallengeId, | 
					
						
							|  |  |  |         completedChallenge, | 
					
						
							|  |  |  |         'UTC' | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(completedChallenges.length).toEqual( | 
					
						
							|  |  |  |         mockCompletedChallenges.length | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-26 00:38:31 +05:30
										 |  |  |     // eslint-disable-next-line max-len
 | 
					
						
							| 
									
										
										
										
											2019-02-23 21:43:51 +00:00
										 |  |  |     it('adds newly completed challenges to the completedChallenges array', () => { | 
					
						
							|  |  |  |       const { | 
					
						
							|  |  |  |         updateData: { | 
					
						
							|  |  |  |           $set: { completedChallenges } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } = buildUserUpdate(mockUser, '123abc', mockCompletedChallenge, 'UTC'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(completedChallenges.length).toEqual( | 
					
						
							|  |  |  |         mockCompletedChallenges.length + 1 | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-02-22 11:49:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('buildChallengeUrl', () => { | 
					
						
							|  |  |  |     it('resolves the correct Url for the provided challenge', () => { | 
					
						
							|  |  |  |       const result = buildChallengeUrl(mockChallenge); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(result).toEqual(requestedChallengeUrl); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-08 13:29:10 -05:00
										 |  |  |     it('can handle non-url-compliant challenge names', () => { | 
					
						
							| 
									
										
										
										
											2019-02-22 11:49:12 +00:00
										 |  |  |       const challenge = { ...mockChallenge, superBlock: 'my awesome' }; | 
					
						
							|  |  |  |       const expected = '/learn/my-awesome/actual/challenge'; | 
					
						
							|  |  |  |       const result = buildChallengeUrl(challenge); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(result).toEqual(expected); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('challengeUrlResolver', () => { | 
					
						
							|  |  |  |     it('resolves to the first challenge url by default', async () => { | 
					
						
							| 
									
										
										
										
											2020-06-23 10:01:21 +02:00
										 |  |  |       const challengeUrlResolver = await createChallengeUrlResolver( | 
					
						
							|  |  |  |         mockAllChallenges, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           _getFirstChallenge: mockGetFirstChallenge | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2019-02-22 11:49:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       return challengeUrlResolver().then(url => { | 
					
						
							|  |  |  |         expect(url).toEqual(firstChallengeUrl); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-06-23 10:01:21 +02:00
										 |  |  |     }, 10000); | 
					
						
							| 
									
										
										
										
											2019-02-22 11:49:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-22 16:38:45 +03:00
										 |  |  |     // eslint-disable-next-line max-len
 | 
					
						
							| 
									
										
										
										
											2019-02-22 11:49:12 +00:00
										 |  |  |     it('returns the first challenge url if the provided id does not relate to a challenge', async () => { | 
					
						
							| 
									
										
										
										
											2020-06-23 10:01:21 +02:00
										 |  |  |       const challengeUrlResolver = await createChallengeUrlResolver( | 
					
						
							|  |  |  |         mockAllChallenges, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           _getFirstChallenge: mockGetFirstChallenge | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2019-02-22 11:49:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       return challengeUrlResolver('not-a-real-challenge').then(url => { | 
					
						
							|  |  |  |         expect(url).toEqual(firstChallengeUrl); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('resolves the correct url for the requested challenge', async () => { | 
					
						
							| 
									
										
										
										
											2020-06-23 10:01:21 +02:00
										 |  |  |       const challengeUrlResolver = await createChallengeUrlResolver( | 
					
						
							|  |  |  |         mockAllChallenges, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           _getFirstChallenge: mockGetFirstChallenge | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2019-02-22 11:49:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       return challengeUrlResolver('123abc').then(url => { | 
					
						
							|  |  |  |         expect(url).toEqual(requestedChallengeUrl); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('getFirstChallenge', () => { | 
					
						
							|  |  |  |     it('returns the correct challenge url from the model', async () => { | 
					
						
							| 
									
										
										
										
											2020-06-23 10:01:21 +02:00
										 |  |  |       const result = await getFirstChallenge(mockAllChallenges); | 
					
						
							| 
									
										
										
										
											2019-02-22 11:49:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(result).toEqual(firstChallengeUrl); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-02-23 21:43:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-22 11:49:12 +00:00
										 |  |  |     it('returns the learn base if no challenges found', async () => { | 
					
						
							| 
									
										
										
										
											2020-06-23 10:01:21 +02:00
										 |  |  |       const result = await getFirstChallenge([]); | 
					
						
							| 
									
										
										
										
											2019-02-22 11:49:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(result).toEqual('/learn'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:50 +00:00
										 |  |  |   describe('isValidChallengeCompletion', () => { | 
					
						
							|  |  |  |     const validObjectId = '5c716d1801013c3ce3aa23e6'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('declares a 403 for an invalid id in the body', () => { | 
					
						
							|  |  |  |       expect.assertions(3); | 
					
						
							|  |  |  |       const req = mockReq({ | 
					
						
							|  |  |  |         body: { id: 'not-a-real-id' } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       const res = mockRes(); | 
					
						
							|  |  |  |       const next = sinon.spy(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       isValidChallengeCompletion(req, res, next); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-24 17:30:23 +05:30
										 |  |  |       expect(res.status.called).toBe(true); | 
					
						
							|  |  |  |       expect(res.status.getCall(0).args[0]).toBe(403); | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:50 +00:00
										 |  |  |       expect(next.called).toBe(false); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('declares a 403 for an invalid challengeType in the body', () => { | 
					
						
							|  |  |  |       expect.assertions(3); | 
					
						
							|  |  |  |       const req = mockReq({ | 
					
						
							|  |  |  |         body: { id: validObjectId, challengeType: 'ponyfoo' } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       const res = mockRes(); | 
					
						
							|  |  |  |       const next = sinon.spy(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       isValidChallengeCompletion(req, res, next); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-24 17:30:23 +05:30
										 |  |  |       expect(res.status.called).toBe(true); | 
					
						
							|  |  |  |       expect(res.status.getCall(0).args[0]).toBe(403); | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:50 +00:00
										 |  |  |       expect(next.called).toBe(false); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('declares a 403 for an invalid solution in the body', () => { | 
					
						
							|  |  |  |       expect.assertions(3); | 
					
						
							|  |  |  |       const req = mockReq({ | 
					
						
							|  |  |  |         body: { | 
					
						
							|  |  |  |           id: validObjectId, | 
					
						
							|  |  |  |           challengeType: '1', | 
					
						
							|  |  |  |           solution: 'https://not-a-url' | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       const res = mockRes(); | 
					
						
							|  |  |  |       const next = sinon.spy(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       isValidChallengeCompletion(req, res, next); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-24 17:30:23 +05:30
										 |  |  |       expect(res.status.called).toBe(true); | 
					
						
							|  |  |  |       expect(res.status.getCall(0).args[0]).toBe(403); | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:50 +00:00
										 |  |  |       expect(next.called).toBe(false); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('calls next if the body is valid', () => { | 
					
						
							|  |  |  |       const req = mockReq({ | 
					
						
							|  |  |  |         body: { | 
					
						
							|  |  |  |           id: validObjectId, | 
					
						
							|  |  |  |           challengeType: '1', | 
					
						
							|  |  |  |           solution: 'https://www.freecodecamp.org' | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       const res = mockRes(); | 
					
						
							|  |  |  |       const next = sinon.spy(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       isValidChallengeCompletion(req, res, next); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(next.called).toBe(true); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('calls next if only the id is provided', () => { | 
					
						
							|  |  |  |       const req = mockReq({ | 
					
						
							|  |  |  |         body: { | 
					
						
							|  |  |  |           id: validObjectId | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       const res = mockRes(); | 
					
						
							|  |  |  |       const next = sinon.spy(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       isValidChallengeCompletion(req, res, next); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(next.called).toBe(true); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-02-24 10:12:51 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('can handle an "int" challengeType', () => { | 
					
						
							|  |  |  |       const req = mockReq({ | 
					
						
							|  |  |  |         body: { | 
					
						
							|  |  |  |           id: validObjectId, | 
					
						
							|  |  |  |           challengeType: 1 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       const res = mockRes(); | 
					
						
							|  |  |  |       const next = sinon.spy(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       isValidChallengeCompletion(req, res, next); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(next.called).toBe(true); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-02-23 16:12:50 +00:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-18 01:17:37 +01:00
										 |  |  |   xdescribe('modernChallengeCompleted', () => {}); | 
					
						
							| 
									
										
										
										
											2019-02-22 11:49:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-18 01:17:37 +01:00
										 |  |  |   xdescribe('projectCompleted', () => {}); | 
					
						
							| 
									
										
										
										
											2019-02-22 11:49:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('redirectToCurrentChallenge', () => { | 
					
						
							|  |  |  |     const mockHomeLocation = 'https://www.example.com'; | 
					
						
							|  |  |  |     const mockLearnUrl = `${mockHomeLocation}/learn`; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-09 21:27:26 +03:00
										 |  |  |     it('redirects to the learn base url for non-users', async done => { | 
					
						
							| 
									
										
										
										
											2019-02-22 11:49:12 +00:00
										 |  |  |       const redirectToCurrentChallenge = createRedirectToCurrentChallenge( | 
					
						
							|  |  |  |         () => {}, | 
					
						
							|  |  |  |         { _homeLocation: mockHomeLocation, _learnUrl: mockLearnUrl } | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |       const req = mockReq(); | 
					
						
							|  |  |  |       const res = mockRes(); | 
					
						
							|  |  |  |       const next = sinon.spy(); | 
					
						
							|  |  |  |       await redirectToCurrentChallenge(req, res, next); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.redirect.calledWith(mockLearnUrl)); | 
					
						
							|  |  |  |       done(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-22 16:38:45 +03:00
										 |  |  |     // eslint-disable-next-line max-len
 | 
					
						
							| 
									
										
										
										
											2019-02-22 11:49:12 +00:00
										 |  |  |     it('redirects to the url provided by the challengeUrlResolver', async done => { | 
					
						
							| 
									
										
										
										
											2020-06-23 10:01:21 +02:00
										 |  |  |       const challengeUrlResolver = await createChallengeUrlResolver( | 
					
						
							|  |  |  |         mockAllChallenges, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           _getFirstChallenge: mockGetFirstChallenge | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2019-02-22 11:49:12 +00:00
										 |  |  |       const expectedUrl = `${mockHomeLocation}${requestedChallengeUrl}`; | 
					
						
							|  |  |  |       const redirectToCurrentChallenge = createRedirectToCurrentChallenge( | 
					
						
							|  |  |  |         challengeUrlResolver, | 
					
						
							|  |  |  |         { _homeLocation: mockHomeLocation, _learnUrl: mockLearnUrl } | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |       const req = mockReq({ | 
					
						
							|  |  |  |         user: mockUser | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       const res = mockRes(); | 
					
						
							|  |  |  |       const next = sinon.spy(); | 
					
						
							|  |  |  |       await redirectToCurrentChallenge(req, res, next); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.redirect.calledWith(expectedUrl)).toBe(true); | 
					
						
							|  |  |  |       done(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-22 16:38:45 +03:00
										 |  |  |     // eslint-disable-next-line max-len
 | 
					
						
							| 
									
										
										
										
											2019-02-22 11:49:12 +00:00
										 |  |  |     it('redirects to the first challenge for users without a currentChallengeId', async done => { | 
					
						
							| 
									
										
										
										
											2020-06-23 10:01:21 +02:00
										 |  |  |       const challengeUrlResolver = await createChallengeUrlResolver( | 
					
						
							|  |  |  |         mockAllChallenges, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           _getFirstChallenge: mockGetFirstChallenge | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2019-02-22 11:49:12 +00:00
										 |  |  |       const redirectToCurrentChallenge = createRedirectToCurrentChallenge( | 
					
						
							|  |  |  |         challengeUrlResolver, | 
					
						
							|  |  |  |         { _homeLocation: mockHomeLocation, _learnUrl: mockLearnUrl } | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |       const req = mockReq({ | 
					
						
							|  |  |  |         user: { ...mockUser, currentChallengeId: '' } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       const res = mockRes(); | 
					
						
							|  |  |  |       const next = sinon.spy(); | 
					
						
							|  |  |  |       await redirectToCurrentChallenge(req, res, next); | 
					
						
							|  |  |  |       const expectedUrl = `${mockHomeLocation}${firstChallengeUrl}`; | 
					
						
							|  |  |  |       expect(res.redirect.calledWith(expectedUrl)).toBe(true); | 
					
						
							|  |  |  |       done(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); |