microblog.pub/tests/federation_test.py

610 lines
21 KiB
Python
Raw Normal View History

import os
2018-06-16 15:02:10 -05:00
import time
from typing import List
from typing import Tuple
import requests
from html2text import html2text
2018-06-17 13:58:50 -05:00
from little_boxes.collection import parse_collection
2018-06-01 13:29:44 -05:00
def resp2plaintext(resp):
"""Convert the body of a requests reponse to plain text in order to make basic assertions."""
return html2text(resp.text)
2018-05-26 03:43:05 -05:00
class Instance(object):
"""Test instance wrapper."""
def __init__(self, name, host_url, docker_url=None):
2018-05-26 03:43:05 -05:00
self.host_url = host_url
self.docker_url = docker_url or host_url
2018-07-07 07:07:53 -05:00
self._create_delay = 12
2018-06-01 13:29:44 -05:00
with open(
2018-06-17 12:21:59 -05:00
os.path.join(
os.path.dirname(os.path.abspath(__file__)),
f"fixtures/{name}/config/admin_api_key.key",
)
2018-06-01 13:29:44 -05:00
) as f:
api_key = f.read()
2018-06-17 12:21:59 -05:00
self._auth_headers = {"Authorization": f"Bearer {api_key}"}
2018-06-18 15:01:21 -05:00
def _do_req(self, url):
2018-06-01 13:29:44 -05:00
"""Used to parse collection."""
2018-05-27 05:02:14 -05:00
url = url.replace(self.docker_url, self.host_url)
2018-06-18 15:01:21 -05:00
resp = requests.get(url, headers={'Accept': 'application/activity+json'})
2018-05-27 05:02:14 -05:00
resp.raise_for_status()
return resp.json()
def _parse_collection(self, payload=None, url=None):
2018-06-01 13:29:44 -05:00
"""Parses a collection (go through all the pages)."""
2018-06-17 13:58:50 -05:00
return parse_collection(
url=url, payload=payload, fetcher=self._do_req,
2018-06-17 12:21:59 -05:00
)
2018-05-27 05:02:14 -05:00
2018-05-26 03:43:05 -05:00
def ping(self):
"""Ensures the homepage is reachable."""
2018-06-17 12:21:59 -05:00
resp = requests.get(f"{self.host_url}/")
2018-05-26 03:43:05 -05:00
resp.raise_for_status()
assert resp.status_code == 200
def debug(self):
2018-06-01 13:29:44 -05:00
"""Returns the debug infos (number of items in the inbox/outbox."""
resp = requests.get(
2018-06-17 12:21:59 -05:00
f"{self.host_url}/api/debug",
headers={**self._auth_headers, "Accept": "application/json"},
2018-06-01 13:29:44 -05:00
)
resp.raise_for_status()
return resp.json()
2018-06-01 13:29:44 -05:00
def drop_db(self):
2018-06-01 13:29:44 -05:00
"""Drops the MongoDB DB."""
resp = requests.delete(
2018-06-17 12:21:59 -05:00
f"{self.host_url}/api/debug",
headers={**self._auth_headers, "Accept": "application/json"},
2018-06-01 13:29:44 -05:00
)
resp.raise_for_status()
return resp.json()
def block(self, actor_url) -> None:
2018-06-01 13:29:44 -05:00
"""Blocks an actor."""
# Instance1 follows instance2
2018-06-01 13:29:44 -05:00
resp = requests.post(
2018-06-17 12:21:59 -05:00
f"{self.host_url}/api/block",
params={"actor": actor_url},
2018-06-01 13:29:44 -05:00
headers=self._auth_headers,
)
assert resp.status_code == 201
# We need to wait for the Follow/Accept dance
2018-06-17 12:21:59 -05:00
time.sleep(self._create_delay / 2)
return resp.json().get("activity")
2018-06-17 12:21:59 -05:00
def follow(self, instance: "Instance") -> str:
2018-06-01 13:29:44 -05:00
"""Follows another instance."""
2018-05-26 03:43:05 -05:00
# Instance1 follows instance2
2018-06-01 13:29:44 -05:00
resp = requests.post(
2018-06-17 12:21:59 -05:00
f"{self.host_url}/api/follow",
json={"actor": instance.docker_url},
2018-06-01 13:29:44 -05:00
headers=self._auth_headers,
)
2018-05-26 03:43:05 -05:00
assert resp.status_code == 201
2018-05-26 03:43:05 -05:00
# We need to wait for the Follow/Accept dance
2018-05-27 15:30:43 -05:00
time.sleep(self._create_delay)
2018-06-17 12:21:59 -05:00
return resp.json().get("activity")
2018-05-27 15:30:43 -05:00
2018-06-01 13:29:44 -05:00
def new_note(self, content, reply=None) -> str:
"""Creates a new note."""
2018-06-17 12:21:59 -05:00
params = {"content": content}
if reply:
2018-06-17 12:21:59 -05:00
params["reply"] = reply
2018-06-01 13:29:44 -05:00
resp = requests.post(
2018-06-17 12:21:59 -05:00
f"{self.host_url}/api/new_note", json=params, headers=self._auth_headers
2018-06-01 13:29:44 -05:00
)
2018-05-27 15:30:43 -05:00
assert resp.status_code == 201
time.sleep(self._create_delay)
2018-06-17 12:21:59 -05:00
return resp.json().get("activity")
2018-05-27 07:21:06 -05:00
2018-06-01 13:29:44 -05:00
def boost(self, oid: str) -> str:
"""Creates an Announce activity."""
resp = requests.post(
2018-06-17 12:21:59 -05:00
f"{self.host_url}/api/boost", json={"id": oid}, headers=self._auth_headers
2018-06-01 13:29:44 -05:00
)
2018-05-28 12:46:23 -05:00
assert resp.status_code == 201
time.sleep(self._create_delay)
2018-06-17 12:21:59 -05:00
return resp.json().get("activity")
2018-05-28 12:46:23 -05:00
2018-06-01 13:29:44 -05:00
def like(self, oid: str) -> str:
"""Creates a Like activity."""
resp = requests.post(
2018-06-17 12:21:59 -05:00
f"{self.host_url}/api/like", json={"id": oid}, headers=self._auth_headers
2018-06-01 13:29:44 -05:00
)
2018-05-28 12:46:23 -05:00
assert resp.status_code == 201
time.sleep(self._create_delay)
2018-06-17 12:21:59 -05:00
return resp.json().get("activity")
2018-05-28 12:46:23 -05:00
2018-06-01 13:29:44 -05:00
def delete(self, oid: str) -> str:
"""Creates a Delete activity."""
2018-05-29 14:36:05 -05:00
resp = requests.post(
2018-06-17 12:21:59 -05:00
f"{self.host_url}/api/note/delete",
json={"id": oid},
2018-06-01 13:29:44 -05:00
headers=self._auth_headers,
2018-05-29 14:36:05 -05:00
)
assert resp.status_code == 201
time.sleep(self._create_delay)
2018-06-17 12:21:59 -05:00
return resp.json().get("activity")
2018-06-01 13:29:44 -05:00
def undo(self, oid: str) -> str:
"""Creates a Undo activity."""
resp = requests.post(
2018-06-17 12:21:59 -05:00
f"{self.host_url}/api/undo", json={"id": oid}, headers=self._auth_headers
2018-06-01 13:29:44 -05:00
)
2018-05-27 07:21:06 -05:00
assert resp.status_code == 201
# We need to wait for the Follow/Accept dance
2018-05-27 15:30:43 -05:00
time.sleep(self._create_delay)
2018-06-17 12:21:59 -05:00
return resp.json().get("activity")
2018-05-26 03:43:05 -05:00
2018-06-01 13:29:44 -05:00
def followers(self) -> List[str]:
"""Parses the followers collection."""
resp = requests.get(
2018-06-17 12:21:59 -05:00
f"{self.host_url}/followers",
headers={"Accept": "application/activity+json"},
2018-06-01 13:29:44 -05:00
)
2018-05-26 03:43:05 -05:00
resp.raise_for_status()
data = resp.json()
2018-05-27 05:02:14 -05:00
return self._parse_collection(payload=data)
2018-05-26 03:43:05 -05:00
def following(self):
2018-06-01 13:29:44 -05:00
"""Parses the following collection."""
resp = requests.get(
2018-06-17 12:21:59 -05:00
f"{self.host_url}/following",
headers={"Accept": "application/activity+json"},
2018-06-01 13:29:44 -05:00
)
2018-05-26 03:43:05 -05:00
resp.raise_for_status()
2018-05-26 03:43:05 -05:00
data = resp.json()
2018-05-27 05:02:14 -05:00
return self._parse_collection(payload=data)
2018-05-26 03:43:05 -05:00
def outbox(self):
2018-06-01 13:29:44 -05:00
"""Returns the instance outbox."""
resp = requests.get(
2018-06-17 12:21:59 -05:00
f"{self.host_url}/following",
headers={"Accept": "application/activity+json"},
2018-06-01 13:29:44 -05:00
)
resp.raise_for_status()
return resp.json()
2018-05-28 12:46:23 -05:00
def outbox_get(self, aid):
2018-06-01 13:29:44 -05:00
"""Fetches a specific item from the instance outbox."""
resp = requests.get(
aid.replace(self.docker_url, self.host_url),
2018-06-17 12:21:59 -05:00
headers={"Accept": "application/activity+json"},
2018-06-01 13:29:44 -05:00
)
2018-05-28 12:46:23 -05:00
resp.raise_for_status()
return resp.json()
2018-05-27 15:30:43 -05:00
def stream_jsonfeed(self):
2018-06-01 13:29:44 -05:00
"""Returns the "stream"'s JSON feed."""
resp = requests.get(
2018-06-17 12:21:59 -05:00
f"{self.host_url}/api/stream",
headers={**self._auth_headers, "Accept": "application/json"},
2018-06-01 13:29:44 -05:00
)
2018-05-27 15:30:43 -05:00
resp.raise_for_status()
return resp.json()
2018-05-26 03:43:05 -05:00
2018-06-01 13:29:44 -05:00
def _instances() -> Tuple[Instance, Instance]:
"""Initializes the client for the two test instances."""
2018-06-17 12:21:59 -05:00
instance1 = Instance(
"instance1", "http://localhost:5006", "http://instance1_web_1:5005"
)
2018-05-26 03:43:05 -05:00
instance1.ping()
2018-06-17 12:21:59 -05:00
instance2 = Instance(
"instance2", "http://localhost:5007", "http://instance2_web_1:5005"
)
2018-05-26 03:43:05 -05:00
instance2.ping()
2018-06-01 13:29:44 -05:00
# Return the DB
2018-05-27 04:52:27 -05:00
instance1.drop_db()
instance2.drop_db()
2018-06-01 13:29:44 -05:00
2018-05-27 07:21:06 -05:00
return instance1, instance2
2018-05-26 03:43:05 -05:00
2018-06-01 13:29:44 -05:00
def test_follow() -> None:
"""instance1 follows instance2."""
2018-05-27 07:21:06 -05:00
instance1, instance2 = _instances()
2018-05-26 03:43:05 -05:00
# Instance1 follows instance2
instance1.follow(instance2)
instance1_debug = instance1.debug()
2018-06-17 12:21:59 -05:00
assert instance1_debug["inbox"] == 1 # An Accept activity should be there
assert instance1_debug["outbox"] == 1 # We've sent a Follow activity
instance2_debug = instance2.debug()
2018-06-17 12:21:59 -05:00
assert instance2_debug["inbox"] == 1 # An Follow activity should be there
assert instance2_debug["outbox"] == 1 # We've sent a Accept activity
2018-05-26 02:57:27 -05:00
2018-05-26 03:43:05 -05:00
assert instance2.followers() == [instance1.docker_url]
assert instance1.following() == [instance2.docker_url]
2018-05-27 07:21:06 -05:00
def test_follow_unfollow():
2018-06-01 13:29:44 -05:00
"""instance1 follows instance2, then unfollows it."""
2018-05-27 07:21:06 -05:00
instance1, instance2 = _instances()
# Instance1 follows instance2
follow_id = instance1.follow(instance2)
instance1_debug = instance1.debug()
2018-06-17 12:21:59 -05:00
assert instance1_debug["inbox"] == 1 # An Accept activity should be there
assert instance1_debug["outbox"] == 1 # We've sent a Follow activity
2018-05-27 07:21:06 -05:00
instance2_debug = instance2.debug()
2018-06-17 12:21:59 -05:00
assert instance2_debug["inbox"] == 1 # An Follow activity should be there
assert instance2_debug["outbox"] == 1 # We've sent a Accept activity
2018-05-27 07:21:06 -05:00
assert instance2.followers() == [instance1.docker_url]
assert instance1.following() == [instance2.docker_url]
instance1.undo(follow_id)
assert instance2.followers() == []
assert instance1.following() == []
instance1_debug = instance1.debug()
2018-06-17 12:21:59 -05:00
assert instance1_debug["inbox"] == 1 # An Accept activity should be there
assert instance1_debug["outbox"] == 2 # We've sent a Follow and a Undo activity
2018-05-27 07:21:06 -05:00
instance2_debug = instance2.debug()
2018-06-17 12:21:59 -05:00
assert instance2_debug["inbox"] == 2 # An Follow and Undo activity should be there
assert instance2_debug["outbox"] == 1 # We've sent a Accept activity
2018-05-27 07:21:06 -05:00
2018-05-28 12:46:23 -05:00
2018-05-27 15:30:43 -05:00
def test_post_content():
2018-06-01 13:29:44 -05:00
"""Instances follow each other, and instance1 creates a note."""
2018-05-27 15:30:43 -05:00
instance1, instance2 = _instances()
# Instance1 follows instance2
instance1.follow(instance2)
instance2.follow(instance1)
inbox_stream = instance2.stream_jsonfeed()
2018-06-17 12:21:59 -05:00
assert len(inbox_stream["items"]) == 0
2018-05-27 15:30:43 -05:00
2018-06-17 12:21:59 -05:00
create_id = instance1.new_note("hello")
2018-05-27 15:30:43 -05:00
instance2_debug = instance2.debug()
2018-06-17 12:21:59 -05:00
assert (
instance2_debug["inbox"] == 3
) # An Follow, Accept and Create activity should be there
assert instance2_debug["outbox"] == 2 # We've sent a Accept and a Follow activity
2018-05-27 07:21:06 -05:00
2018-05-27 15:30:43 -05:00
# Ensure the post is visible in instance2's stream
inbox_stream = instance2.stream_jsonfeed()
2018-06-17 12:21:59 -05:00
assert len(inbox_stream["items"]) == 1
assert inbox_stream["items"][0]["id"] == create_id
2018-05-28 12:46:23 -05:00
def test_block_and_post_content():
2018-06-01 13:29:44 -05:00
"""Instances follow each other, instance2 blocks instance1, instance1 creates a new note."""
instance1, instance2 = _instances()
# Instance1 follows instance2
instance1.follow(instance2)
instance2.follow(instance1)
inbox_stream = instance2.stream_jsonfeed()
2018-06-17 12:21:59 -05:00
assert len(inbox_stream["items"]) == 0
instance2.block(instance1.docker_url)
2018-06-17 12:21:59 -05:00
instance1.new_note("hello")
instance2_debug = instance2.debug()
2018-06-17 12:21:59 -05:00
assert (
instance2_debug["inbox"] == 2
) # An Follow, Accept activity should be there, Create should have been dropped
assert (
instance2_debug["outbox"] == 3
) # We've sent a Accept and a Follow activity + the Block activity
# Ensure the post is not visible in instance2's stream
inbox_stream = instance2.stream_jsonfeed()
2018-06-17 12:21:59 -05:00
assert len(inbox_stream["items"]) == 0
def test_post_content_and_delete():
2018-06-01 13:29:44 -05:00
"""Instances follow each other, instance1 creates a new note, then deletes it."""
instance1, instance2 = _instances()
# Instance1 follows instance2
instance1.follow(instance2)
instance2.follow(instance1)
inbox_stream = instance2.stream_jsonfeed()
2018-06-17 12:21:59 -05:00
assert len(inbox_stream["items"]) == 0
2018-06-17 12:21:59 -05:00
create_id = instance1.new_note("hello")
instance2_debug = instance2.debug()
2018-06-17 12:21:59 -05:00
assert (
instance2_debug["inbox"] == 3
) # An Follow, Accept and Create activity should be there
assert instance2_debug["outbox"] == 2 # We've sent a Accept and a Follow activity
# Ensure the post is visible in instance2's stream
inbox_stream = instance2.stream_jsonfeed()
2018-06-17 12:21:59 -05:00
assert len(inbox_stream["items"]) == 1
assert inbox_stream["items"][0]["id"] == create_id
2018-06-17 12:21:59 -05:00
instance1.delete(f"{create_id}/activity")
instance2_debug = instance2.debug()
2018-06-17 12:21:59 -05:00
assert (
instance2_debug["inbox"] == 4
) # An Follow, Accept and Create and Delete activity should be there
assert instance2_debug["outbox"] == 2 # We've sent a Accept and a Follow activity
# Ensure the post has been delete from instance2's stream
inbox_stream = instance2.stream_jsonfeed()
2018-06-17 12:21:59 -05:00
assert len(inbox_stream["items"]) == 0
2018-05-28 12:46:23 -05:00
def test_post_content_and_like():
2018-06-01 13:29:44 -05:00
"""Instances follow each other, instance1 creates a new note, instance2 likes it."""
2018-05-28 12:46:23 -05:00
instance1, instance2 = _instances()
# Instance1 follows instance2
instance1.follow(instance2)
instance2.follow(instance1)
2018-06-17 12:21:59 -05:00
create_id = instance1.new_note("hello")
2018-05-28 12:46:23 -05:00
# Ensure the post is visible in instance2's stream
inbox_stream = instance2.stream_jsonfeed()
2018-06-17 12:21:59 -05:00
assert len(inbox_stream["items"]) == 1
assert inbox_stream["items"][0]["id"] == create_id
2018-05-28 12:46:23 -05:00
# Now, instance2 like the note
2018-06-17 12:21:59 -05:00
like_id = instance2.like(f"{create_id}/activity")
2018-05-28 12:46:23 -05:00
instance1_debug = instance1.debug()
2018-06-17 12:21:59 -05:00
assert instance1_debug["inbox"] == 3 # Follow, Accept and Like
assert instance1_debug["outbox"] == 3 # Folllow, Accept, and Create
2018-05-28 12:46:23 -05:00
2018-06-17 12:21:59 -05:00
note = instance1.outbox_get(f"{create_id}/activity")
assert "likes" in note
assert note["likes"]["totalItems"] == 1
likes = instance1._parse_collection(url=note["likes"]["first"])
2018-06-01 13:29:44 -05:00
assert len(likes) == 1
2018-06-17 12:21:59 -05:00
assert likes[0]["id"] == like_id
2018-05-28 12:46:23 -05:00
2018-05-29 11:59:37 -05:00
2018-06-01 13:29:44 -05:00
def test_post_content_and_like_unlike() -> None:
"""Instances follow each other, instance1 creates a new note, instance2 likes it, then unlikes it."""
2018-05-28 12:46:23 -05:00
instance1, instance2 = _instances()
# Instance1 follows instance2
instance1.follow(instance2)
instance2.follow(instance1)
2018-06-17 12:21:59 -05:00
create_id = instance1.new_note("hello")
2018-05-28 12:46:23 -05:00
# Ensure the post is visible in instance2's stream
inbox_stream = instance2.stream_jsonfeed()
2018-06-17 12:21:59 -05:00
assert len(inbox_stream["items"]) == 1
assert inbox_stream["items"][0]["id"] == create_id
2018-05-28 12:46:23 -05:00
# Now, instance2 like the note
2018-06-17 12:21:59 -05:00
like_id = instance2.like(f"{create_id}/activity")
2018-05-28 12:46:23 -05:00
instance1_debug = instance1.debug()
2018-06-17 12:21:59 -05:00
assert instance1_debug["inbox"] == 3 # Follow, Accept and Like
assert instance1_debug["outbox"] == 3 # Folllow, Accept, and Create
2018-05-28 12:46:23 -05:00
2018-06-17 12:21:59 -05:00
note = instance1.outbox_get(f"{create_id}/activity")
assert "likes" in note
assert note["likes"]["totalItems"] == 1
likes = instance1._parse_collection(url=note["likes"]["first"])
2018-06-01 13:29:44 -05:00
assert len(likes) == 1
2018-06-17 12:21:59 -05:00
assert likes[0]["id"] == like_id
2018-05-28 12:46:23 -05:00
instance2.undo(like_id)
instance1_debug = instance1.debug()
2018-06-17 12:21:59 -05:00
assert instance1_debug["inbox"] == 4 # Follow, Accept and Like and Undo
assert instance1_debug["outbox"] == 3 # Folllow, Accept, and Create
2018-05-28 12:46:23 -05:00
2018-06-17 12:21:59 -05:00
note = instance1.outbox_get(f"{create_id}/activity")
assert "likes" in note
assert note["likes"]["totalItems"] == 0
2018-05-28 12:46:23 -05:00
2018-05-29 11:59:37 -05:00
2018-06-01 13:29:44 -05:00
def test_post_content_and_boost() -> None:
"""Instances follow each other, instance1 creates a new note, instance2 "boost" it."""
2018-05-28 12:46:23 -05:00
instance1, instance2 = _instances()
# Instance1 follows instance2
instance1.follow(instance2)
instance2.follow(instance1)
2018-06-17 12:21:59 -05:00
create_id = instance1.new_note("hello")
2018-05-28 12:46:23 -05:00
# Ensure the post is visible in instance2's stream
inbox_stream = instance2.stream_jsonfeed()
2018-06-17 12:21:59 -05:00
assert len(inbox_stream["items"]) == 1
assert inbox_stream["items"][0]["id"] == create_id
2018-05-28 12:46:23 -05:00
# Now, instance2 like the note
2018-06-17 12:21:59 -05:00
boost_id = instance2.boost(f"{create_id}/activity")
2018-05-28 12:46:23 -05:00
instance1_debug = instance1.debug()
2018-06-17 12:21:59 -05:00
assert instance1_debug["inbox"] == 3 # Follow, Accept and Announce
assert instance1_debug["outbox"] == 3 # Folllow, Accept, and Create
2018-05-28 12:46:23 -05:00
2018-06-17 12:21:59 -05:00
note = instance1.outbox_get(f"{create_id}/activity")
assert "shares" in note
assert note["shares"]["totalItems"] == 1
shares = instance1._parse_collection(url=note["shares"]["first"])
2018-06-01 13:29:44 -05:00
assert len(shares) == 1
2018-06-17 12:21:59 -05:00
assert shares[0]["id"] == boost_id
2018-05-28 12:46:23 -05:00
2018-06-01 13:29:44 -05:00
def test_post_content_and_boost_unboost() -> None:
"""Instances follow each other, instance1 creates a new note, instance2 "boost" it, then "unboost" it."""
2018-05-28 12:46:23 -05:00
instance1, instance2 = _instances()
# Instance1 follows instance2
instance1.follow(instance2)
instance2.follow(instance1)
2018-06-17 12:21:59 -05:00
create_id = instance1.new_note("hello")
2018-05-28 12:46:23 -05:00
# Ensure the post is visible in instance2's stream
inbox_stream = instance2.stream_jsonfeed()
2018-06-17 12:21:59 -05:00
assert len(inbox_stream["items"]) == 1
assert inbox_stream["items"][0]["id"] == create_id
2018-05-28 12:46:23 -05:00
# Now, instance2 like the note
2018-06-17 12:21:59 -05:00
boost_id = instance2.boost(f"{create_id}/activity")
2018-05-28 12:46:23 -05:00
instance1_debug = instance1.debug()
2018-06-17 12:21:59 -05:00
assert instance1_debug["inbox"] == 3 # Follow, Accept and Announce
assert instance1_debug["outbox"] == 3 # Folllow, Accept, and Create
2018-05-28 12:46:23 -05:00
2018-06-17 12:21:59 -05:00
note = instance1.outbox_get(f"{create_id}/activity")
assert "shares" in note
assert note["shares"]["totalItems"] == 1
shares = instance1._parse_collection(url=note["shares"]["first"])
2018-06-01 13:29:44 -05:00
assert len(shares) == 1
2018-06-17 12:21:59 -05:00
assert shares[0]["id"] == boost_id
2018-05-28 12:46:23 -05:00
instance2.undo(boost_id)
instance1_debug = instance1.debug()
2018-06-17 12:21:59 -05:00
assert instance1_debug["inbox"] == 4 # Follow, Accept and Announce and Undo
assert instance1_debug["outbox"] == 3 # Folllow, Accept, and Create
2018-05-28 12:46:23 -05:00
2018-06-17 12:21:59 -05:00
note = instance1.outbox_get(f"{create_id}/activity")
assert "shares" in note
assert note["shares"]["totalItems"] == 0
2018-06-01 13:29:44 -05:00
def test_post_content_and_post_reply() -> None:
"""Instances follow each other, instance1 creates a new note, instance2 replies to it."""
instance1, instance2 = _instances()
# Instance1 follows instance2
instance1.follow(instance2)
instance2.follow(instance1)
inbox_stream = instance2.stream_jsonfeed()
2018-06-17 12:21:59 -05:00
assert len(inbox_stream["items"]) == 0
2018-06-17 12:21:59 -05:00
instance1_create_id = instance1.new_note("hello")
instance2_debug = instance2.debug()
2018-06-17 12:21:59 -05:00
assert (
instance2_debug["inbox"] == 3
) # An Follow, Accept and Create activity should be there
assert instance2_debug["outbox"] == 2 # We've sent a Accept and a Follow activity
# Ensure the post is visible in instance2's stream
instance2_inbox_stream = instance2.stream_jsonfeed()
2018-06-17 12:21:59 -05:00
assert len(instance2_inbox_stream["items"]) == 1
assert instance2_inbox_stream["items"][0]["id"] == instance1_create_id
2018-06-01 13:29:44 -05:00
instance2_create_id = instance2.new_note(
2018-06-17 12:21:59 -05:00
f"hey @instance1@{instance1.docker_url}",
reply=f"{instance1_create_id}/activity",
2018-06-01 13:29:44 -05:00
)
instance2_debug = instance2.debug()
2018-06-17 12:21:59 -05:00
assert (
instance2_debug["inbox"] == 3
) # An Follow, Accept and Create activity should be there
assert (
instance2_debug["outbox"] == 3
) # We've sent a Accept and a Follow and a Create activity
instance1_debug = instance1.debug()
2018-06-17 12:21:59 -05:00
assert (
instance1_debug["inbox"] == 3
) # An Follow, Accept and Create activity should be there
assert (
instance1_debug["outbox"] == 3
) # We've sent a Accept and a Follow and a Create activity
instance1_inbox_stream = instance1.stream_jsonfeed()
2018-06-17 12:21:59 -05:00
assert len(instance1_inbox_stream["items"]) == 1
assert instance1_inbox_stream["items"][0]["id"] == instance2_create_id
2018-06-17 12:21:59 -05:00
instance1_note = instance1.outbox_get(f"{instance1_create_id}/activity")
assert "replies" in instance1_note
assert instance1_note["replies"]["totalItems"] == 1
replies = instance1._parse_collection(url=instance1_note["replies"]["first"])
2018-06-01 13:29:44 -05:00
assert len(replies) == 1
2018-06-17 12:21:59 -05:00
assert replies[0]["id"] == f"{instance2_create_id}/activity"
2018-05-29 11:59:37 -05:00
2018-06-01 13:29:44 -05:00
def test_post_content_and_post_reply_and_delete() -> None:
"""Instances follow each other, instance1 creates a new note, instance2 replies to it, then deletes its reply."""
2018-05-29 11:59:37 -05:00
instance1, instance2 = _instances()
# Instance1 follows instance2
instance1.follow(instance2)
instance2.follow(instance1)
inbox_stream = instance2.stream_jsonfeed()
2018-06-17 12:21:59 -05:00
assert len(inbox_stream["items"]) == 0
2018-05-29 11:59:37 -05:00
2018-06-17 12:21:59 -05:00
instance1_create_id = instance1.new_note("hello")
2018-05-29 11:59:37 -05:00
instance2_debug = instance2.debug()
2018-06-17 12:21:59 -05:00
assert (
instance2_debug["inbox"] == 3
) # An Follow, Accept and Create activity should be there
assert instance2_debug["outbox"] == 2 # We've sent a Accept and a Follow activity
2018-05-29 11:59:37 -05:00
# Ensure the post is visible in instance2's stream
instance2_inbox_stream = instance2.stream_jsonfeed()
2018-06-17 12:21:59 -05:00
assert len(instance2_inbox_stream["items"]) == 1
assert instance2_inbox_stream["items"][0]["id"] == instance1_create_id
2018-05-29 11:59:37 -05:00
2018-06-01 13:29:44 -05:00
instance2_create_id = instance2.new_note(
2018-06-17 12:21:59 -05:00
f"hey @instance1@{instance1.docker_url}",
reply=f"{instance1_create_id}/activity",
2018-06-01 13:29:44 -05:00
)
2018-05-29 11:59:37 -05:00
instance2_debug = instance2.debug()
2018-06-17 12:21:59 -05:00
assert (
instance2_debug["inbox"] == 3
) # An Follow, Accept and Create activity should be there
assert (
instance2_debug["outbox"] == 3
) # We've sent a Accept and a Follow and a Create activity
2018-05-29 11:59:37 -05:00
instance1_debug = instance1.debug()
2018-06-17 12:21:59 -05:00
assert (
instance1_debug["inbox"] == 3
) # An Follow, Accept and Create activity should be there
assert (
instance1_debug["outbox"] == 3
) # We've sent a Accept and a Follow and a Create activity
2018-05-29 11:59:37 -05:00
instance1_inbox_stream = instance1.stream_jsonfeed()
2018-06-17 12:21:59 -05:00
assert len(instance1_inbox_stream["items"]) == 1
assert instance1_inbox_stream["items"][0]["id"] == instance2_create_id
2018-05-29 11:59:37 -05:00
2018-06-17 12:21:59 -05:00
instance1_note = instance1.outbox_get(f"{instance1_create_id}/activity")
assert "replies" in instance1_note
assert instance1_note["replies"]["totalItems"] == 1
2018-05-29 11:59:37 -05:00
2018-06-17 12:21:59 -05:00
instance2.delete(f"{instance2_create_id}/activity")
2018-05-29 11:59:37 -05:00
instance1_debug = instance1.debug()
2018-06-17 12:21:59 -05:00
assert (
instance1_debug["inbox"] == 4
) # An Follow, Accept and Create and Delete activity should be there
assert (
instance1_debug["outbox"] == 3
) # We've sent a Accept and a Follow and a Create activity
instance1_note = instance1.outbox_get(f"{instance1_create_id}/activity")
assert "replies" in instance1_note
assert instance1_note["replies"]["totalItems"] == 0