# Data export and account deletion ## What this builds Two things a person can do from their account page: download a copy of everything the app holds about them, and delete their account. Deletion waits a number of days, can be cancelled until then, and afterwards leaves nothing behind except what the app has written down a reason to keep. Read `spekkr.inputs.json` first. A deletion is carried out `grace_days` days after it is asked for. `export_format` says what the export holds. `admin_requests` decides whether R9 exists. Use `app_name` on the pages and in every email. This spec needs an app with a `users` table and sign-in. It does not build them. ## Out of scope - Legal advice. The spec builds the machinery. What must be kept, and for how long, is for the person to decide and write into the registry in R1. - Removing data from backups. - Deleting a whole workspace. ## Requirements Each requirement has an id. Tests name the id they cover. ### R1. A registry of personal data - One list in code names every table that holds data about a person. Each entry says how to find one person's rows, whether they go in the export, and what happens to them at deletion: `delete`, `anonymize` with the columns to blank, or `keep` with a written reason. - A test reads the database schema and fails if any table that refers to `users` is missing from the list. A new table cannot be forgotten. - Columns that must never be exported, such as password hashes and tokens, are marked in the same list. Accept when: adding a table with a `user_id` column and no registry entry makes the test suite fail with a message that names the table. ### R2. Ask for an export - A signed-in person asks for an export. They must have signed in during the last 10 minutes, or do so again. - The export is built in the background. A person has one export in progress at a time and can ask for a new one once every 24 hours. Accept when: a second request inside 24 hours is refused with the time when the next is allowed, and a request with an older sign-in asks to sign in again. ### R3. What the export holds - A zip file with one JSON file for each registered table that is marked for export, holding that person's rows, and a `README.txt` that says what each file is. - If `export_format` is `json_and_csv`, each table is there as a CSV file too. - Files the person uploaded are included as they were. - Nothing about any other person is included, and no column marked as never exported. - The export is built as a stream. An account with a million rows does not need a million rows of memory. Accept when: the export of one person contains every row of theirs from every exported table, and a search of it for another person's email address and for any password hash finds nothing. ### R4. Download the export - When the export is ready the person gets an email with a link to the account page, where the download is. - The download works only for that person, signed in. It works for 7 days, and then the file is deleted. Accept when: another signed-in person gets 404 for the download, and the file is gone from storage after 7 days. ### R5. Ask for deletion - A signed-in person asks to delete their account. They must have signed in during the last 10 minutes and must type their email address to confirm. - If R8 finds a reason the account cannot be deleted yet, the page says what to do first and the request is not accepted. - The account is then marked for deletion in `grace_days` days. Every session is ended and the person gets an email that says when, with a link to cancel. - Until then the account cannot be used. Signing in leads to one page, which offers to cancel or to sign out. Accept when: after the request every old session is refused, and signing in again reaches only the cancel page. ### R6. Cancel - Signing in during the wait and pressing cancel restores the account exactly as it was, apart from its sessions. The sessions ended in R5 stay ended, and the person carries on in the one they cancelled from. The person gets an email that says so. Accept when: a cancelled account has every row it had before other than its sessions, can be used again, and a session from before the request is still refused. ### R7. Carry out the deletion - When the wait is over a job goes through the registry and deletes, anonymizes or keeps each table's rows as its entry says, in an order that respects references between tables. - Files the person uploaded are deleted from storage. - The user row is handled last. It is deleted, or if kept rows refer to it, its email address becomes `deleted-ID@deleted.invalid` and every other personal column is blanked. - The job can be stopped at any point and run again. It ends in the same state. - A record of the deletion is kept: the user's id, the time, and how many rows of each table were deleted, anonymized and kept. It holds no personal data. - The address can then be used to sign up afresh, with no link to the old account. Accept when: after deletion a search of every table for the person's email address and name finds nothing outside rows marked `keep`, and a job killed halfway and run again reaches the same end. ### R8. Reasons to wait - One function, `deletion_blockers(user)`, returns the reasons an account cannot be deleted yet, each with a sentence for the person. As built it returns one: the person is the only owner of something that has other members. - It is asked at the request in R5 and again just before R7. If it gives a reason the second time, the deletion is put off a day and the person is emailed. Accept when: a sole owner is told to hand over ownership first, and can delete once they have. ### R9. Requests handled by staff Only if `admin_requests` is on. - An administrator can start an export or a deletion for a person who asked another way. They must enter a note of how the person asked. - The person gets the same emails as if they had asked themselves, and an export still reaches only them. - Each such action is recorded with the administrator, the time and the note. Accept when: an administrator can start both, cannot download the export, and the record names them. ### R10. Pages - A "Your data" part of the account page with both actions and their state, the confirmation page for deletion, and the page a person sees during the wait. - Deletion is never one click. The button that confirms it says "Delete my account", not "OK". Accept when: each page works with a keyboard alone and at a 360 pixel wide screen. ## Definition of done - Every acceptance check above has an automated test, and all tests pass. Tests capture email in memory and set the clock. - A README explains how to register a table, what each deletion choice does, and how to run the tests.