From f5fafc480feca61c1a27fcfe53c6356b35cba01e Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Tue, 15 May 2018 16:15:34 -0400 Subject: [PATCH] Notifications can target robots, but the model did not reflect that This fixes a breakage when deleting robots that have notifications targeting them. --- data/database.py | 2 +- data/model/test/test_user.py | 27 ++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/data/database.py b/data/database.py index 422275f84..5b9dd07fe 100644 --- a/data/database.py +++ b/data/database.py @@ -1033,7 +1033,7 @@ class NotificationKind(BaseModel): class Notification(BaseModel): uuid = CharField(default=uuid_generator, index=True) kind = ForeignKeyField(NotificationKind) - target = QuayUserField(index=True) + target = QuayUserField(index=True, allows_robots=True) metadata_json = TextField(default='{}') created = DateTimeField(default=datetime.now, index=True) dismissed = BooleanField(default=False) diff --git a/data/model/test/test_user.py b/data/model/test/test_user.py index 6f5b1bce5..f8c7f49f7 100644 --- a/data/model/test/test_user.py +++ b/data/model/test/test_user.py @@ -5,10 +5,13 @@ import pytest from mock import patch from data.database import EmailConfirmation, User, DeletedNamespace +from data.model.organization import get_organization +from data.model.notification import create_notification +from data.model.team import create_team, add_user_to_team from data.model.user import create_user_noverify, validate_reset_code, get_active_users from data.model.user import mark_namespace_for_deletion, delete_namespace_via_marker from data.model.user import create_robot, lookup_robot, list_namespace_robots -from data.model.user import InvalidRobotException +from data.model.user import InvalidRobotException, delete_robot from util.timedeltastring import convert_to_timedelta from data.queue import WorkQueue from test.fixtures import * @@ -117,3 +120,25 @@ def test_delete_namespace_via_marker(initialized_db): with pytest.raises(DeletedNamespace.DoesNotExist): DeletedNamespace.get(id=marker_id) + + +def test_delete_robot(initialized_db): + # Create a robot account. + user = create_user_noverify('foobar', 'foo@example.com', email_required=False) + robot, _ = create_robot('foo', user) + + # Add some notifications and other rows pointing to the robot. + create_notification('repo_push', robot) + + team = create_team('someteam', get_organization('buynlarge'), 'member') + add_user_to_team(robot, team) + + # Ensure the robot exists. + assert lookup_robot(robot.username).id == robot.id + + # Delete the robot. + delete_robot(robot.username) + + # Ensure it is gone. + with pytest.raises(InvalidRobotException): + lookup_robot(robot.username)