English
Checkpoint
What must work
powershell
dotnet buildpowershell
dotnet test --solution Chatex.slnxpowershell
git diff --no-index Source/Chatex.Core/Users D:\Work\Chatex-Reference\Source\Chatex.Core\UsersCommit:
powershell
git add .powershell
git commit -m "Add profiles, password changes, user search and avatars"Common mistakes
Table 9.1. Common mistakes in the profile service
| Symptom | Cause | Fix |
|---|---|---|
Files.Count is 2 after replacing an avatar | the previous file is not deleted, or the wrong path is deleted | remember previous before changing AvatarPath; delete it after saving |
| tests write files into a temporary folder | the fake storage is registered before AddCore | call services.Replace(...) after AddCore |
| an oversized upload with a small claimed length is accepted | the content is not wrapped in LimitedReadStream | save new LimitedReadStream(upload.Content, MaxBytes) |
FileTooLargeException reaches the test | the exception is not caught in AvatarStore.SaveAsync | catch it and return TooLarge |
| the old session still works after a password change | the refresh tokens are not revoked | call RevokeAllForUserAsync after a successful change |
a blank bio is stored as "" | the bio is trimmed but not turned into null | string.IsNullOrWhiteSpace(command.Bio) ? null : command.Bio.Trim() |
Self-check questions
- Which parts of an upload come from the client, and why can none of them be trusted?
- How does
LimitedReadStreamenforce a limit that the claimed length cannot? Why must every read method be overridden? - Why are SVG images not allowed as avatars?
- In which order does
SetAvatarAsyncsave the new file, update the database and delete the old file? What would go wrong in another order? - Why does a password change revoke all refresh tokens? What about the current access token?
- Why does
ChangePasswordAsyncturnPasswordMismatchinto a field error oncurrentPassword? - What is a fake? How is the in-memory storage different from a mock?
- Why is the fake storage registered with
ReplaceafterAddCore, while the fake clock is registered before it?
Exercises
- Content sniffing. A client can send an HTML file with the media type
image/png. Write a small method that checks the first bytes of an upload against the signatures of PNG (89 50 4E 47), JPEG (FF D8 FF) and WebP (RIFF....WEBP). Where inAvatarStorewould you call it? (The API does a similar check in topic 13.) - A test for the size limit. Write
SetAvatarAsync_rejects_an_image_that_is_larger_than_it_claimswithUpload(6 * 1024 * 1024, "image/png", claimedLength: 100). Which error must come back, and how many files must be stored? - Who removed my avatar?
RemoveAvatarAsyncof a user without an avatar succeeds. Is that right? Write the test that pins the behaviour.