diff --git a/curriculum/challenges/english/05-apis-and-microservices/apis-and-microservices-projects/exercise-tracker.english.md b/curriculum/challenges/english/05-apis-and-microservices/apis-and-microservices-projects/exercise-tracker.english.md index 1931cf7cda..2dbe8df6cc 100644 --- a/curriculum/challenges/english/05-apis-and-microservices/apis-and-microservices-projects/exercise-tracker.english.md +++ b/curriculum/challenges/english/05-apis-and-microservices/apis-and-microservices-projects/exercise-tracker.english.md @@ -23,16 +23,179 @@ Start this project on Glitch using _id. - testString: '' + testString: "async getUserInput => { + const url = getUserInput('url'); + const res = await fetch(url + '/api/exercise/new-user', { + method: 'POST', + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + body: `username=fcc_test_${Date.now()}`.substr(0, 29) + }); + + if (res.ok) { + const { _id, username } = await res.json(); + assert.exists(_id); + assert.exists(username); + } else { + throw new Error(`${res.status} ${res.statusText}`); + } + } + " - text: I can get an array of all users by getting api/exercise/users with the same info as when creating a user. - testString: '' + testString: "async getUserInput => { + const url = getUserInput('url'); + const res = await fetch(url + '/api/exercise/users'); + + if (res.ok) { + const data = await res.json(); + assert.isArray(data); + } else { + throw new Error(`${res.status} ${res.statusText}`); + } + } + " - text: 'I can add an exercise to any user by posting form data userId(_id), description, duration, and optionally date to /api/exercise/add. If no date supplied it will use current date. App will return the user object with the exercise fields added.' - testString: '' + testString: "async getUserInput => { + const url = getUserInput('url'); + const res = await fetch(url + '/api/exercise/new-user', { + method: 'POST', + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + body: `username=fcc_test_${Date.now()}`.substr(0, 29) + }); + + if (res.ok) { + const { _id, username } = await res.json(); + const expected = { + username, + description: 'test', + duration: 60, + _id, + date: 'Mon Jan 01 1990' + }; + + const addRes = await fetch(url + '/api/exercise/add', { + method: 'POST', + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + body: `userId=${_id}&description=${expected.description}&duration=${expected.duration}&date=1990-01-01` + }); + if (addRes.ok) { + const actual = await addRes.json(); + assert.deepEqual(actual, expected); + } else { + throw new Error(`${addRes.status} ${addRes.statusText}`); + } + } else { + throw new Error(`${res.status} ${res.statusText}`); + } + } + " - text: I can retrieve a full exercise log of any user by getting /api/exercise/log with a parameter of userId(_id). App will return the user object with added array log and count (total exercise count). - testString: '' + testString: "async getUserInput => { + const url = getUserInput('url'); + const res = await fetch(url + '/api/exercise/new-user', { + method: 'POST', + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + body: `username=fcc_test_${Date.now()}`.substr(0, 29) + }); + + if (res.ok) { + const { _id, username } = await res.json(); + const expected = { + username, + description: 'test', + duration: 60, + _id, + date: new Date().toDateString() + }; + + const addRes = await fetch(url + '/api/exercise/add', { + method: 'POST', + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + body: `userId=${_id}&description=${expected.description}&duration=${expected.duration}` + }); + if (addRes.ok) { + const logRes = await fetch(url + `/api/exercise/log?userId=${_id}`); + if (logRes.ok) { + const { log } = await logRes.json(); + assert.isArray(log); + assert.equal(1, log.length); + } else { + throw new Error(`${logRes.status} ${logRes.statusText}`); + } + } else { + throw new Error(`${addRes.status} ${addRes.statusText}`); + } + } else { + throw new Error(`${res.status} ${res.statusText}`); + } + } + " - text: 'I can retrieve part of the log of any user by also passing along optional parameters of from & to or limit. (Date format yyyy-mm-dd, limit = int)' - testString: '' + testString: "async getUserInput => { + const url = getUserInput('url'); + const res = await fetch(url + '/api/exercise/new-user', { + method: 'POST', + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + body: `username=fcc_test_${Date.now()}`.substr(0, 29) + }); + + if (res.ok) { + const { _id, username } = await res.json(); + const expected = { + username, + description: 'test', + duration: 60, + _id, + date: new Date().toDateString() + }; + + const addExerciseRes = await fetch(url + '/api/exercise/add', { + method: 'POST', + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + body: `userId=${_id}&description=${expected.description}&duration=${expected.duration}&date=1990-01-01` + }); + const addExerciseTwoRes = await fetch(url + '/api/exercise/add', { + method: 'POST', + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + body: `userId=${_id}&description=${expected.description}&duration=${expected.duration}&date=1990-01-02` + }); + if (addExerciseRes.ok && addExerciseTwoRes.ok) { + const logRes = await fetch( + url + `/api/exercise/log?userId=${_id}&from=1989-12-31&to=1990-01-03` + ); + if (logRes.ok) { + const { log } = await logRes.json(); + assert.isArray(log); + assert.equal(2, log.length); + } else { + throw new Error(`${logRes.status} ${logRes.statusText}`); + } + + const limitRes = await fetch( + url + `/api/exercise/log?userId=${_id}&limit=1` + ); + if (limitRes.ok) { + const { log } = await limitRes.json(); + assert.isArray(log); + assert.equal(1, log.length); + } else { + throw new Error(`${limitRes.status} ${limitRes.statusText}`); + } + } else { + throw new Error(`${res.status} ${res.statusText}`); + } + } else { + throw new Error(`${res.status} ${res.statusText}`); + } + } + " ``` @@ -48,8 +211,8 @@ tests: ```js /** - Backend challenges don't need solutions, - because they would need to be tested against a full working project. + Backend challenges don't need solutions, + because they would need to be tested against a full working project. Please check our contributing guidelines to learn more. */ ```