Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test for resolving users with aliases #3432

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions bookwyrm/tests/activitypub/test_base_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ def setUp(self):
# don't try to load the user icon
del self.userdata["icon"]

remote_datafile = pathlib.Path(__file__).parent.joinpath(
"../data/ap_user_external.json"
)
self.remote_userdata = json.loads(remote_datafile.read_bytes())
del self.remote_userdata["icon"]

alias_datafile = pathlib.Path(__file__).parent.joinpath(
"../data/ap_user_aliased.json"
)
self.alias_userdata = json.loads(alias_datafile.read_bytes())
del self.alias_userdata["icon"]

image_path = pathlib.Path(__file__).parent.joinpath(
"../../static/images/default_avi.jpg"
)
Expand Down Expand Up @@ -118,6 +130,38 @@ def test_resolve_remote_id(self, *_):
self.assertEqual(result.remote_id, "https://example.com/user/mouse")
self.assertEqual(result.name, "MOUSE?? MOUSE!!")

@responses.activate
def test_resolve_remote_alias(self, *_):
"""look up or load user who has an unknown alias"""

self.assertEqual(models.User.objects.count(), 1)

# remote user with unknown user as an alias
responses.add(
responses.GET,
"https://example.com/user/moose",
json=self.alias_userdata,
status=200,
)

responses.add(
responses.GET,
"https://example.com/user/ali",
json=self.remote_userdata,
status=200,
)

with patch("bookwyrm.models.user.set_remote_server.delay"):
result = resolve_remote_id(
"https://example.com/user/moose", model=models.User
)
self.assertIsInstance(result, models.User)
self.assertEqual(result.name, "moose?? moose!!")
self.assertEqual(models.User.objects.count(), 3) # created moose plus the alias
alias = models.User.objects.last()
self.assertEqual(alias.name, "Ali As")
self.assertEqual(result.also_known_as.first(), alias) # Ali is alias of Moose

def test_to_model_invalid_model(self, *_):
"""catch mismatch between activity type and model type"""
instance = ActivityObject(id="a", type="b")
Expand Down
40 changes: 40 additions & 0 deletions bookwyrm/tests/data/ap_user_aliased.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
{
"manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
"schema": "http://schema.org#",
"PropertyValue": "schema:PropertyValue",
"value": "schema:value"
}
],
"id": "https://example.com/user/moose",
"type": "Person",
"preferredUsername": "moose",
"name": "moose?? moose!!",
"inbox": "https://example.com/user/moose/inbox",
"outbox": "https://example.com/user/moose/outbox",
"followers": "https://example.com/user/moose/followers",
"following": "https://example.com/user/moose/following",
"summary": "",
"publicKey": {
"id": "https://example.com/user/moose/#main-key",
"owner": "https://example.com/user/moose",
"publicKeyPem": "-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC6QisDrjOQvkRo/MqNmSYPwqtt\nCxg/8rCW+9jKbFUKvqjTeKVotEE85122v/DCvobCCdfQuYIFdVMk+dB1xJ0iPGPg\nyU79QHY22NdV9mFKA2qtXVVxb5cxpA4PlwOHM6PM/k8B+H09OUrop2aPUAYwy+vg\n+MXyz8bAXrIS1kq6fQIDAQAB\n-----END PUBLIC KEY-----"
},
"endpoints": {
"sharedInbox": "https://example.com/inbox"
},
"bookwyrmUser": true,
"manuallyApprovesFollowers": false,
"discoverable": false,
"alsoKnownAs": ["https://example.com/user/ali"],
"devices": "",
"tag": [],
"icon": {
"type": "Image",
"mediaType": "image/png",
"url": "https://example.com/images/avatars/AL-3-crop-50.png"
}
}
40 changes: 40 additions & 0 deletions bookwyrm/tests/data/ap_user_external.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
{
"manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
"schema": "http://schema.org#",
"PropertyValue": "schema:PropertyValue",
"value": "schema:value"
}
],
"id": "https://example.com/user/ali",
"type": "Person",
"preferredUsername": "alias",
"name": "Ali As",
"inbox": "https://example.com/user/ali/inbox",
"outbox": "https://example.com/user/ali/outbox",
"followers": "https://example.com/user/ali/followers",
"following": "https://example.com/user/ali/following",
"summary": "",
"publicKey": {
"id": "https://example.com/user/ali/#main-key",
"owner": "https://example.com/user/ali",
"publicKeyPem": "-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC6QisDrjOQvkRo/MqNmSYPwqtt\nCxg/8rCW+9jKbFUKvqjTeKVotEE85122v/DCvobCCdfQuYIFdVMk+dB1xJ0iPGPg\nyU79QHY22NdV9mFKA2qtXVVxb5cxpA4PlwOHM6PM/k8B+H09OUrop2aPUAYwy+vg\n+MXyz8bAXrIS1kq6fQIDAQAB\n-----END PUBLIC KEY-----"
},
"endpoints": {
"sharedInbox": "https://example.com/inbox"
},
"bookwyrmUser": true,
"manuallyApprovesFollowers": false,
"alsoKnownAs": [],
"discoverable": false,
"devices": "",
"tag": [],
"icon": {
"type": "Image",
"mediaType": "image/png",
"url": "https://example.com/images/avatars/ALIAS-2-crop-50.png"
}
}
Loading