-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Record user deletions in a new DB table
Remove the CLI command for deleting a user: this interacts awkwardly with recording deletions in a table because there's no authenticated user to record as the requester of the deletion. There could be various solutions to this but no one uses this CLI command anyway so let's just delete it.
- Loading branch information
Showing
15 changed files
with
182 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
def repr_(obj, attrs): | ||
class_name = type(obj).__name__ | ||
attrs = {attrname: getattr(obj, attrname) for attrname in attrs} | ||
return f"{class_name}({', '.join(f'{name}={value!r}' for name, value in attrs.items())})" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from datetime import datetime | ||
|
||
from sqlalchemy import func | ||
from sqlalchemy.orm import Mapped, mapped_column | ||
|
||
from h.db import Base | ||
from h.models import helpers | ||
|
||
|
||
class UserDeletion(Base): | ||
"""A record of a user account that was deleted.""" | ||
|
||
__tablename__ = "user_deletion" | ||
|
||
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) | ||
userid: Mapped[str] = mapped_column(nullable=False) | ||
requested_at: Mapped[datetime] = mapped_column( | ||
default=func.now(), # pylint:disable=not-callable | ||
nullable=False, | ||
) | ||
requested_by: Mapped[str] = mapped_column(nullable=False) | ||
tag: Mapped[str] = mapped_column(nullable=False) | ||
created_at: Mapped[datetime] = mapped_column(nullable=False) | ||
num_annotations: Mapped[int] = mapped_column(nullable=False) | ||
|
||
def __repr__(self) -> str: | ||
return helpers.repr_( | ||
self, | ||
[ | ||
"id", | ||
"userid", | ||
"requested_at", | ||
"requested_by", | ||
"tag", | ||
"created_at", | ||
"num_annotations", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from factory import Faker, LazyAttribute, SubFactory | ||
|
||
from h import models | ||
|
||
from .base import ModelFactory | ||
from .user import User | ||
|
||
|
||
class UserDeletion(ModelFactory): | ||
class Meta: | ||
model = models.UserDeletion | ||
exclude = ("user", "requesting_user") | ||
|
||
user = SubFactory(User) | ||
userid = LazyAttribute(lambda o: o.user.userid) | ||
requesting_user = SubFactory(User) | ||
requested_by = LazyAttribute(lambda o: o.requesting_user.userid) | ||
tag = "factory" | ||
created_at = LazyAttribute(lambda o: o.user.registered_date) | ||
num_annotations = Faker("random_int") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from unittest.mock import Mock | ||
|
||
from h.models import helpers | ||
|
||
|
||
def test_repr_(): | ||
obj = Mock(foo="FOO", bar="BAR") | ||
|
||
assert helpers.repr_(obj, ["foo", "bar"]) == "Mock(foo='FOO', bar='BAR')" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,29 @@ | ||
from datetime import datetime | ||
import pytest | ||
|
||
from h.models.job import Job | ||
|
||
def test___repr__(factories, helpers): | ||
job = factories.Job() | ||
repr_ = repr(job) | ||
|
||
class TestJob: | ||
def test___repr__(self): | ||
job = Job( | ||
id=42, | ||
name="job_name", | ||
enqueued_at=datetime( | ||
year=2024, | ||
month=5, | ||
day=8, | ||
hour=11, | ||
minute=51, | ||
second=23, | ||
), | ||
scheduled_at=datetime( | ||
year=2024, | ||
month=6, | ||
day=1, | ||
hour=0, | ||
minute=0, | ||
second=0, | ||
), | ||
expires_at=datetime( | ||
year=2025, | ||
month=1, | ||
day=1, | ||
hour=0, | ||
minute=0, | ||
second=0, | ||
), | ||
priority=3, | ||
tag="job_tag", | ||
kwargs={"foo": "FOO", "bar": "BAR"}, | ||
) | ||
helpers.repr_.assert_called_once_with( | ||
job, | ||
[ | ||
"id", | ||
"name", | ||
"enqueued_at", | ||
"scheduled_at", | ||
"expires_at", | ||
"priority", | ||
"tag", | ||
"kwargs", | ||
], | ||
) | ||
assert repr_ == helpers.repr_.return_value | ||
|
||
assert ( | ||
repr(job) | ||
== "Job(id=42, name='job_name', enqueued_at=datetime.datetime(2024, 5, 8, 11, 51, 23), scheduled_at=datetime.datetime(2024, 6, 1, 0, 0), expires_at=datetime.datetime(2025, 1, 1, 0, 0), priority=3, tag='job_tag', kwargs={'foo': 'FOO', 'bar': 'BAR'})" | ||
) | ||
|
||
@pytest.fixture(autouse=True) | ||
def helpers(mocker): | ||
helpers = mocker.patch("h.models.job.helpers") | ||
# __repr__() needs to return a string or repr() raises. | ||
helpers.repr_.return_value = "test_string_representation" | ||
return helpers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import pytest | ||
|
||
|
||
def test___repr__(factories, helpers): | ||
user_deletion = factories.UserDeletion() | ||
repr_ = repr(user_deletion) | ||
|
||
helpers.repr_.assert_called_once_with( | ||
user_deletion, | ||
[ | ||
"id", | ||
"userid", | ||
"requested_at", | ||
"requested_by", | ||
"tag", | ||
"created_at", | ||
"num_annotations", | ||
], | ||
) | ||
assert repr_ == helpers.repr_.return_value | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def helpers(mocker): | ||
helpers = mocker.patch("h.models.user_deletion.helpers") | ||
# __repr__() needs to return a string or repr() raises. | ||
helpers.repr_.return_value = "test_string_representation" | ||
return helpers |
Oops, something went wrong.