Practice
Additional exercises for the auth repository: https://github.com/dsgn425-fall2026/auth
For each of the following: make a branch, open an issue for it (Issues, New issue, paste the story and the definition of done), build it by hand or with your agent, one test per sentence, and open a pull request that closes the issue.
1. One account per email
Sign up with ben@example.com. It works: there are now two users with the same email, and logging in finds whichever one the database returns first.
One account per email
As a visitor, I can't sign up with an email that already has an account, so every email belongs to exactly one person.
Definition of done
- Signing up with an email that already has an account does not create a second user.
- Instead, it sends me back to the sign-up form with a message that the email already has an account.
- Signing up with an email nobody has used still creates the user and logs them in.
Once it works, think about this: the login form gives one message, "Nope.", for a wrong password and for an email with no account, so nobody can use it to find out who has an account. Your sign-up form now tells anyone who asks. Is that a problem? What do real apps do?
2. Legacy data - technical debt
Once password encryption has been implemented, Ben and Brian can no longer log in. Their rows were saved before it: their passwords are still in the plain password column, and password_digest is empty, so login has nothing to check them against. One way out is a one-time script that moves every old row onto a hashed password. That's this exercise.
This one has no ticket. Writing it is the exercise. Before you build anything, write the issue yourself:
- The story. Who is it for, what do they want, and why? (Is the user here Ben? You? The company?)
- The definition of done. Three to five sentences, each one something a test can check. Ask yourself:
- What must be true for Ben and Brian once the script has run?
- What must be true about the
passwordcolumn? - What must stay true for the users who signed up after the change?
- What happens if someone runs the script twice?
- Does a wrong password still fail?
Then build it: a script in scripts/, run with bin/rails runner, the same way you run create_data.rb.
Two things to know:
scripts/create_data.rbputs Ben and Brian back the old way every time it runs, on purpose. Run your script again after it.- Before you call it done, run
bin/rails runner scripts/show_data.rb Userand look.
3. Edit your own post
Posts can be created and deleted, but a typo is forever. Add editing, with the same rule as delete: only your own.
Edit my own post
As a logged-in user, I can fix a post I made, and nobody else can change it.
Definition of done
- On the feed, each post that belongs to me has an Edit link to /posts/:id/edit.
- The edit page is a form filled in with the post's body and image.
- Saving it changes the post and sends me back to the feed, where the new text shows.
- Signed in as a different user, opening the edit page for a post that is not mine sends me back to the feed.
- Signed in as a different user, a PATCH request to /posts/:id for a post that is not mine changes nothing.
- Not signed in at all, the edit page and the PATCH request both send me to the login page, and nothing changes.
Companies already have an edit page and an update action; they're the pattern. The last three sentences are the abuse story, folded into the same ticket. Notice there are two actions to protect, not one: the form and the request that saves it.