User Changed webhook: add 'nickname' and 'old_nickname' #93582

Merged
Sybren A. Stüvel merged 2 commits from webhook/gitea-username into main 2023-05-01 15:38:14 +02:00
2 changed files with 83 additions and 6 deletions

View File

@ -21,7 +21,7 @@ USER_SAVE_INTERESTING_FIELDS = {
"avatar", "avatar",
"date_deletion_requested", "date_deletion_requested",
"confirmed_email_at", "confirmed_email_at",
# FIXME(anna): add nickname "nickname",
} }
WEBHOOK_TIMEOUT_SECS = 5 WEBHOOK_TIMEOUT_SECS = 5
@ -115,6 +115,7 @@ def modified_user_to_webhooks(sender, user: UserModel, **kwargs):
# either database ID or email address. # either database ID or email address.
webhook_pre_save = getattr(user, "webhook_pre_save", {}) webhook_pre_save = getattr(user, "webhook_pre_save", {})
old_email = webhook_pre_save.get("email") old_email = webhook_pre_save.get("email")
old_nickname = webhook_pre_save.get("nickname") or ""
# Map all falsey values to empty string for more consistent comparison later. # Map all falsey values to empty string for more consistent comparison later.
# An empty avatar can be either '' or None. # An empty avatar can be either '' or None.
@ -144,11 +145,10 @@ def modified_user_to_webhooks(sender, user: UserModel, **kwargs):
"email": user.email, "email": user.email,
"roles": sorted(user.public_roles()), "roles": sorted(user.public_roles()),
"avatar_changed": old_avatar != cur_avatar, "avatar_changed": old_avatar != cur_avatar,
"date_deletion_requested": user.date_deletion_requested.isoformat() "date_deletion_requested": isoformat(user.date_deletion_requested),
if user.date_deletion_requested else None, "confirmed_email_at": isoformat(user.confirmed_email_at),
"confirmed_email_at": user.confirmed_email_at.isoformat() "old_nickname": old_nickname,
if user.confirmed_email_at else None, "nickname": user.nickname,
# FIXME(anna): add nickname
} }
json_payload = json.dumps(payload).encode() json_payload = json.dumps(payload).encode()
@ -156,3 +156,10 @@ def modified_user_to_webhooks(sender, user: UserModel, **kwargs):
for hook in hooks: for hook in hooks:
log.debug("Sending to %s, %s", hook, hook.url) log.debug("Sending to %s, %s", hook, hook.url)
hook.send(json_payload, sess) hook.send(json_payload, sess)
def isoformat(timestamp):
"""None-safe timestamp.isoformat()."""
if not timestamp:
return None
return timestamp.isoformat()

View File

@ -63,6 +63,8 @@ class WebhookTest(WebhookBaseTest):
"avatar_changed": False, "avatar_changed": False,
"date_deletion_requested": None, "date_deletion_requested": None,
"confirmed_email_at": None, "confirmed_email_at": None,
"nickname": "",
"old_nickname": "",
}, },
payload, payload,
) )
@ -99,6 +101,56 @@ class WebhookTest(WebhookBaseTest):
"avatar_changed": False, "avatar_changed": False,
"date_deletion_requested": None, "date_deletion_requested": None,
"confirmed_email_at": None, "confirmed_email_at": None,
"nickname": "",
"old_nickname": "",
},
payload,
)
@responses.activate
def test_modify_user_nickname(self):
responses.add(responses.POST, self.HOOK_URL, json={"status": "success"}, status=200)
user = UserModel.objects.create_user("test@user.com", "123456")
user.nickname = "propellorolleporp"
user.save()
user.nickname = "maimuţă"
user.save()
self.assertTrue(user.webhook_user_modified)
self.assertEqual(2, len(responses.calls))
payload = json.loads(responses.calls[0].request.body)
self.assertEqual(
{
"id": user.id,
"old_email": "test@user.com",
"full_name": "",
"email": "test@user.com",
"roles": [],
"avatar_changed": False,
"date_deletion_requested": None,
"confirmed_email_at": None,
"nickname": "propellorolleporp",
"old_nickname": "",
},
payload,
)
payload = json.loads(responses.calls[1].request.body)
self.assertEqual(
{
"id": user.id,
"old_email": "test@user.com",
"full_name": "",
"email": "test@user.com",
"roles": [],
"avatar_changed": False,
"date_deletion_requested": None,
"confirmed_email_at": None,
"nickname": "maimuţă",
"old_nickname": "propellorolleporp",
}, },
payload, payload,
) )
@ -127,6 +179,8 @@ class WebhookTest(WebhookBaseTest):
"avatar_changed": True, "avatar_changed": True,
"date_deletion_requested": None, "date_deletion_requested": None,
"confirmed_email_at": None, "confirmed_email_at": None,
"nickname": "",
"old_nickname": "",
}, },
payload, payload,
) )
@ -170,6 +224,8 @@ class WebhookTest(WebhookBaseTest):
"roles": ["cloud_subscriber"], "roles": ["cloud_subscriber"],
"date_deletion_requested": None, "date_deletion_requested": None,
"confirmed_email_at": None, "confirmed_email_at": None,
"nickname": "",
"old_nickname": "",
}, },
payload, payload,
) )
@ -184,6 +240,8 @@ class WebhookTest(WebhookBaseTest):
"roles": [], "roles": [],
"date_deletion_requested": None, "date_deletion_requested": None,
"confirmed_email_at": None, "confirmed_email_at": None,
"nickname": "",
"old_nickname": "",
}, },
payload, payload,
) )
@ -214,6 +272,8 @@ class WebhookTest(WebhookBaseTest):
"avatar_changed": False, "avatar_changed": False,
"date_deletion_requested": "2020-12-31T23:02:03+00:00", "date_deletion_requested": "2020-12-31T23:02:03+00:00",
"confirmed_email_at": None, "confirmed_email_at": None,
"nickname": "",
"old_nickname": "",
}, },
payload, payload,
) )
@ -244,6 +304,8 @@ class WebhookTest(WebhookBaseTest):
"avatar_changed": False, "avatar_changed": False,
"date_deletion_requested": None, "date_deletion_requested": None,
"confirmed_email_at": "2021-01-01T02:05:06+00:00", "confirmed_email_at": "2021-01-01T02:05:06+00:00",
"nickname": "",
"old_nickname": "",
}, },
payload, payload,
) )
@ -275,6 +337,8 @@ class WebhookTest(WebhookBaseTest):
"roles": [], "roles": [],
"date_deletion_requested": None, "date_deletion_requested": None,
"confirmed_email_at": None, "confirmed_email_at": None,
"nickname": "",
"old_nickname": "",
}, },
payload, payload,
"The payload in the queue should be the POSTed JSON", "The payload in the queue should be the POSTed JSON",
@ -310,6 +374,8 @@ class WebhookTest(WebhookBaseTest):
"roles": [], "roles": [],
"date_deletion_requested": None, "date_deletion_requested": None,
"confirmed_email_at": None, "confirmed_email_at": None,
"nickname": "",
"old_nickname": "",
}, },
payload, payload,
"The payload in the queue should be the POSTed JSON", "The payload in the queue should be the POSTed JSON",
@ -353,6 +419,8 @@ class WebhookTest(WebhookBaseTest):
"roles": [], "roles": [],
"date_deletion_requested": None, "date_deletion_requested": None,
"confirmed_email_at": None, "confirmed_email_at": None,
"nickname": "",
"old_nickname": "",
}, },
payload, payload,
) )
@ -454,6 +522,8 @@ class WebhookTest(WebhookBaseTest):
"roles": [], "roles": [],
"date_deletion_requested": None, "date_deletion_requested": None,
"confirmed_email_at": None, "confirmed_email_at": None,
"nickname": "",
"old_nickname": "",
}, },
payload, payload,
) )