2022-12-12 07:54:51 +00:00
|
|
|
import pytest
|
|
|
|
|
2023-04-11 15:35:36 +00:00
|
|
|
from activities.models import Post, PostAttachment, PostAttachmentStates
|
2023-01-30 00:46:22 +00:00
|
|
|
|
2022-12-12 07:54:51 +00:00
|
|
|
|
|
|
|
@pytest.mark.django_db
|
2023-04-11 15:35:36 +00:00
|
|
|
def test_post_status(api_client, identity):
|
2023-02-14 02:40:10 +00:00
|
|
|
"""
|
|
|
|
Tests posting, editing and deleting a status
|
|
|
|
"""
|
2023-04-11 15:35:36 +00:00
|
|
|
# Create media attachment
|
|
|
|
attachment = PostAttachment.objects.create(
|
|
|
|
mimetype="image/webp",
|
|
|
|
name=None,
|
|
|
|
state=PostAttachmentStates.fetched,
|
|
|
|
author=identity,
|
|
|
|
)
|
2023-02-14 02:40:10 +00:00
|
|
|
# Post new one
|
|
|
|
response = api_client.post(
|
2022-12-12 07:54:51 +00:00
|
|
|
"/api/v1/statuses",
|
|
|
|
content_type="application/json",
|
|
|
|
data={
|
|
|
|
"status": "Hello, world!",
|
|
|
|
"visibility": "unlisted",
|
2023-04-11 15:35:36 +00:00
|
|
|
"media_ids": [attachment.id],
|
2022-12-12 07:54:51 +00:00
|
|
|
},
|
|
|
|
).json()
|
|
|
|
assert response["content"] == "<p>Hello, world!</p>"
|
|
|
|
assert response["visibility"] == "unlisted"
|
2023-04-11 15:35:36 +00:00
|
|
|
assert response["media_attachments"][0]["description"] is None
|
2023-02-14 02:40:10 +00:00
|
|
|
status_id = response["id"]
|
|
|
|
# Retrieve "source" version an edit would use
|
|
|
|
response = api_client.get(f"/api/v1/statuses/{status_id}/source").json()
|
|
|
|
assert response["text"] == "Hello, world!"
|
|
|
|
# Post an edit
|
|
|
|
response = api_client.put(
|
|
|
|
f"/api/v1/statuses/{status_id}",
|
|
|
|
content_type="application/json",
|
|
|
|
data={
|
|
|
|
"status": "Hello, world! Again!",
|
2023-04-11 15:35:36 +00:00
|
|
|
"media_ids": [attachment.id],
|
|
|
|
"media_attributes": [
|
|
|
|
{"id": attachment.id, "description": "the alt text"},
|
|
|
|
],
|
2023-02-14 02:40:10 +00:00
|
|
|
},
|
|
|
|
).json()
|
|
|
|
# Check it stuck
|
|
|
|
response = api_client.get(f"/api/v1/statuses/{status_id}").json()
|
|
|
|
assert response["content"] == "<p>Hello, world! Again!</p>"
|
2023-04-11 15:35:36 +00:00
|
|
|
assert response["media_attachments"][0]["description"] == "the alt text"
|
2023-02-14 02:40:10 +00:00
|
|
|
# Delete it
|
|
|
|
response = api_client.delete(f"/api/v1/statuses/{status_id}")
|
|
|
|
assert response.status_code == 200
|
|
|
|
# Check it's gone
|
|
|
|
response = api_client.get(f"/api/v1/statuses/{status_id}")
|
|
|
|
assert response.status_code == 404
|
2023-01-30 00:46:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
2023-02-14 02:40:10 +00:00
|
|
|
def test_mention_format(api_client, identity, remote_identity):
|
2023-01-30 00:46:22 +00:00
|
|
|
"""
|
|
|
|
Ensures mentions work, and only have one link around them.
|
|
|
|
"""
|
|
|
|
# Make a local post and check it
|
2023-02-14 02:40:10 +00:00
|
|
|
response = api_client.post(
|
2023-01-30 00:46:22 +00:00
|
|
|
"/api/v1/statuses",
|
|
|
|
data={
|
|
|
|
"status": "Hello, @test!",
|
|
|
|
"visibility": "unlisted",
|
|
|
|
},
|
|
|
|
).json()
|
|
|
|
assert (
|
|
|
|
response["content"]
|
|
|
|
== '<p>Hello, <a href="https://example.com/@test/">@test</a>!</p>'
|
|
|
|
)
|
|
|
|
assert response["visibility"] == "unlisted"
|
|
|
|
|
|
|
|
# Make a remote post and check it
|
|
|
|
post = Post.objects.create(
|
|
|
|
local=False,
|
|
|
|
author=remote_identity,
|
|
|
|
content='<p>Hey <a href="https://example.com/@test/" class="u-url mention" rel="nofollow">@test</a></p>',
|
|
|
|
object_uri="https://remote.test/status/12345",
|
|
|
|
)
|
|
|
|
post.mentions.add(identity)
|
2023-02-14 02:40:10 +00:00
|
|
|
response = api_client.get(
|
2023-01-30 00:46:22 +00:00
|
|
|
f"/api/v1/statuses/{post.id}",
|
|
|
|
).json()
|
|
|
|
assert (
|
|
|
|
response["text"] == '<p>Hey <a href="https://example.com/@test/">@test</a></p>'
|
|
|
|
)
|
2023-02-15 17:42:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
def test_post_question_status(api_client):
|
|
|
|
response = api_client.post(
|
|
|
|
"/api/v1/statuses",
|
|
|
|
content_type="application/json",
|
|
|
|
data={
|
|
|
|
"status": "Hello, world!",
|
|
|
|
"poll": {
|
|
|
|
"options": ["Option 1", "Option 2"],
|
|
|
|
"expires_in": 300,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
).json()
|
|
|
|
|
|
|
|
assert response["poll"]["id"] == response["id"]
|
|
|
|
assert response["poll"]["options"] == [
|
|
|
|
{"title": "Option 1", "votes_count": 0},
|
|
|
|
{"title": "Option 2", "votes_count": 0},
|
|
|
|
]
|
|
|
|
assert not response["poll"]["expired"]
|
|
|
|
assert not response["poll"]["multiple"]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
def test_question_format(api_client, remote_identity):
|
|
|
|
"""
|
|
|
|
Ensures incoming questions are property parsed.
|
|
|
|
"""
|
|
|
|
# Make a remote question post and check it
|
|
|
|
post = Post.objects.create(
|
|
|
|
local=False,
|
|
|
|
author=remote_identity,
|
|
|
|
content="<p>Test Question</p>",
|
|
|
|
object_uri="https://remote.test/status/123456",
|
|
|
|
type=Post.Types.question,
|
|
|
|
type_data={
|
|
|
|
"type": "Question",
|
|
|
|
"mode": "oneOf",
|
|
|
|
"options": [
|
|
|
|
{"name": "Option 1", "type": "Note", "votes": 10},
|
|
|
|
{"name": "Option 2", "type": "Note", "votes": 20},
|
|
|
|
],
|
|
|
|
"voter_count": 30,
|
|
|
|
"end_time": "2022-01-01T23:04:45+00:00",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
response = api_client.get(f"/api/v1/statuses/{post.id}").json()
|
|
|
|
assert response["text"] == "<p>Test Question</p>"
|
|
|
|
assert response["poll"] == {
|
|
|
|
"id": str(post.id),
|
|
|
|
"expires_at": "2022-01-01T23:04:45.000Z",
|
|
|
|
"expired": True,
|
|
|
|
"multiple": False,
|
|
|
|
"votes_count": 30,
|
2023-03-16 00:46:12 +00:00
|
|
|
"voters_count": 30,
|
2023-02-15 17:42:56 +00:00
|
|
|
"voted": False,
|
|
|
|
"own_votes": [],
|
|
|
|
"options": [
|
|
|
|
{"title": "Option 1", "votes_count": 10},
|
|
|
|
{"title": "Option 2", "votes_count": 20},
|
|
|
|
],
|
|
|
|
"emojis": [],
|
|
|
|
}
|