Lab03 - Password
Background
Rationale
Users are not very clever. They tend to choose the laziest most convenient option available despite being fully aware of the drawbacks that it entails. Take passwords, for example - how often do people:
1. use a common phrase such as password ,
2. re-use the same password across different accounts, or
3. use/combine simple words (e.g. hearsay ) to make a weak password,
thus sacrificing security for the sake of not having to remember a strong yet difficult string of authentication.
In this lab, your goal is to rate the strength of a password using a set of requirements and produce a test suite that can dynamically verify, to a satisfactory extent, that a particular solution will meet these requirements.
Getting Started
Copy the SSH clone link from Gitlab and clone this repository on either VLAB or your local machine.
In your terminal, change your directory (using the cd command) into the newly cloned lab. To check if you have done this correctly, type ls in this new directory to see if you can see the relevant files (including password.js).
Jest Installation
Jest is a delightful JavaScript Testing Framework with a focus on simplicity. To get started,
1. Install Jest as a development dependency by specifying the option --save-dev . We do this because testing packages are unnecessary in production code.
1 $ npm install --save-dev jest # shortcut: npm i -D jest
2. For compatibility with our new import/export syntax, we will also install @babel/preset-env.
1 $ npm install --save-dev @babel/preset-env # shortcut: npm i -D @babel/preset-env
3. Open package.json and confirm that jest and @babel/preset-env are shown in "devDependencies" , e.g
1 "devDependencies": { "@babel/preset-env": "^7.17.10", "jest": "^28.1.0"
2 }
4. Also, under "scripts" , you should see a section similar to:
1 "scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
2 }
Modify the value of the key test to jest :
1 "scripts": { "test": "jest"
2 }
5. You can run your added scripts with the command npm run [script] . However, since test is a special keyword in npm , you can use any of the three commands below:
1 $ npm run test
2 $ npm test
3 $ npm t
6. To check that you have completed the steps correctly, compare your package.json with our sample package.json in the Additional Information section.
7. Type git status in your terminal. You should see that package.json and package-lock.json have been modified or is untracked. Use git to add, commit and push these changes.
Interface: Data Types
Variable Name Type
password string
Task
Writing Tests
Before implementing checkPassword ,
1. Open the file password.test.js.
2. Write at least 10 tests for checkPassword .
you should avoid repeated/redundant tests.
ensure that each test targets exactly one specific case.
aim to cover as many different cases for the function as you can. You will be assessed on this. try to follow the specification as closely as possible when designing your tests.
3. Run your tests with
4 $ npm t
4. Ensure that there are unexpected errors. You should expect the current stub code in checkPassword to fail most tests. 5. Use git to add, commit and push password.test.js.
Tips with Jest
View the API and documentation at https://jestjs.io/docs/api
You may want to explore test.each - this is a way to write multiple tests succinctly.
Implementation
Once you have written your test suite,
1. Open password.js and implement checkPassword according to its documentation
2. Run your test
3. Fix any bug in your implementation
4. Git add, commit and push your code
Share your experience!
Share your thoughts HERE on any of the following:
1. How did you find this activity? What were the challenges?
2. What is something you learned from this activity?
3. What are a few of your favourite passwords? (just kidding, please don't answer this honestly)
4. What are some other ways we can make passwords stronger?
Submission
Use git to add , commit , and push your changes on your master branch.
Check that your code has been uploaded to your Gitlab repository on this website (you may need to refresh the page).
If you have pushed your latest changes to master on Gitlab no further action is required! At the due date and time, we automatically collect your work from what's on your master branch on Gitlab.
Notes
You should only test the requirements specified in the Interface: Functions.
You should not be testing any undefined behaviour (e.g do not pass in a number instead of a string for a password). You are not allowed to use any other external libraries/modules in this exercise.
Additional Information
Click to view our sample package.json
Miscellaneous
Other information that is not core to the course