Correctly handle event update

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2019-09-04 18:24:31 +02:00
parent 6845825db2
commit f5c3dbf128
27 changed files with 493 additions and 161 deletions

View File

@@ -1,24 +1,24 @@
{
"type": "Update",
"object": {
"url": "http://mastodon.example.org/@gargron",
"url": "https://framapiaf.org/@framasoft",
"type": "Person",
"summary": "<p>Some bio</p>",
"publicKey": {
"publicKeyPem": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0gs3VnQf6am3R+CeBV4H\nlfI1HZTNRIBHgvFszRZkCERbRgEWMu+P+I6/7GJC5H5jhVQ60z4MmXcyHOGmYMK/\n5XyuHQz7V2Ssu1AxLfRN5Biq1ayb0+DT/E7QxNXDJPqSTnstZ6C7zKH/uAETqg3l\nBonjCQWyds+IYbQYxf5Sp3yhvQ80lMwHML3DaNCMlXWLoOnrOX5/yK5+dedesg2\n/HIvGk+HEt36vm6hoH7bwPuEkgA++ACqwjXRe5Mta7i3eilHxFaF8XIrJFARV0t\nqOu4GID/jG6oA+swIWndGrtR2QRJIt9QIBFfK3HG5M0koZbY1eTqwNFRHFL3xaD\nUQIDAQAB\n-----END PUBLIC KEY-----\n",
"owner": "http://mastodon.example.org/users/gargron",
"id": "http://mastodon.example.org/users/gargron#main-key"
"owner": "https://framapiaf.org/users/framasoft",
"id": "https://framapiaf.org/users/framasoft#main-key"
},
"preferredUsername": "gargron",
"outbox": "http://mastodon.example.org/users/gargron/outbox",
"name": "gargle",
"preferredUsername": "framasoft",
"outbox": "https://framapiaf.org/users/framasoft/outbox",
"name": "nextsoft",
"manuallyApprovesFollowers": false,
"inbox": "http://mastodon.example.org/users/gargron/inbox",
"id": "http://mastodon.example.org/users/gargron",
"following": "http://mastodon.example.org/users/gargron/following",
"followers": "http://mastodon.example.org/users/gargron/followers",
"inbox": "https://framapiaf.org/users/framasoft/inbox",
"id": "https://framapiaf.org/users/framasoft",
"following": "https://framapiaf.org/users/framasoft/following",
"followers": "https://framapiaf.org/users/framasoft/followers",
"endpoints": {
"sharedInbox": "http://mastodon.example.org/inbox"
"sharedInbox": "https://framapiaf.org/inbox"
},
"icon":{
"type":"Image",
@@ -31,8 +31,8 @@
"url":"https://files.mastodon.social/accounts/headers/000/000/001/original/c91b871f294ea63e.png"
}
},
"id": "http://mastodon.example.org/users/gargron#updates/1519563538",
"actor": "http://mastodon.example.org/users/gargron",
"id": "https://framapiaf.org/users/gargron#updates/1519563538",
"actor": "https://framapiaf.org/users/framasoft",
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",

View File

@@ -270,10 +270,8 @@ defmodule Mobilizon.ActorsTest do
assert %Actor{} = actor
assert actor.summary == "some updated description"
assert actor.name == "some updated name"
assert actor.domain == "some updated domain"
assert actor.keys == "some updated keys"
refute actor.suspended
assert actor.preferred_username == "some updated username"
end
test "update_actor/2 with valid data updates the actor and it's media files", %{
@@ -310,10 +308,8 @@ defmodule Mobilizon.ActorsTest do
assert %Actor{} = actor
assert actor.summary == "some updated description"
assert actor.name == "some updated name"
assert actor.domain == "some updated domain"
assert actor.keys == "some updated keys"
refute actor.suspended
assert actor.preferred_username == "some updated username"
refute File.exists?(
Mobilizon.CommonConfig.get!([MobilizonWeb.Uploaders.Local, :uploads]) <>

View File

@@ -9,6 +9,7 @@ defmodule Mobilizon.Service.ActivityPub.ActivityPubTest do
import Mobilizon.Factory
alias Mobilizon.Events
alias Mobilizon.Events.Event
alias Mobilizon.Actors.Actor
alias Mobilizon.Actors
alias Mobilizon.Service.HTTPSignatures.Signature
@@ -137,11 +138,14 @@ defmodule Mobilizon.Service.ActivityPub.ActivityPubTest do
end
describe "update" do
@updated_actor_summary "This is an updated actor"
test "it creates an update activity with the new actor data" do
actor = insert(:actor)
actor_data = MobilizonWeb.ActivityPub.ActorView.render("actor.json", %{actor: actor})
actor_data = Map.put(actor_data, "summary", @updated_actor_summary)
{:ok, update, _} =
{:ok, update, updated_actor} =
ActivityPub.update(%{
actor: actor_data["url"],
to: [actor.url <> "/followers"],
@@ -153,6 +157,42 @@ defmodule Mobilizon.Service.ActivityPub.ActivityPubTest do
assert update.data["to"] == [actor.url <> "/followers"]
assert update.data["object"]["id"] == actor_data["id"]
assert update.data["object"]["type"] == actor_data["type"]
assert update.data["object"]["summary"] == @updated_actor_summary
refute updated_actor.summary == actor.summary
{:ok, %Actor{} = database_actor} = Mobilizon.Actors.get_actor_by_url(actor.url)
assert database_actor.summary == @updated_actor_summary
assert database_actor.preferred_username == actor.preferred_username
end
@updated_start_time DateTime.utc_now() |> DateTime.truncate(:second)
test "it creates an update activity with the new event data" do
actor = insert(:actor)
event = insert(:event, organizer_actor: actor)
event_data = Mobilizon.Service.ActivityPub.Converters.Event.model_to_as(event)
event_data = Map.put(event_data, "startTime", @updated_start_time)
{:ok, update, updated_event} =
ActivityPub.update(%{
actor: actor.url,
to: [actor.url <> "/followers"],
cc: [],
object: event_data
})
assert update.data["actor"] == actor.url
assert update.data["to"] == [actor.url <> "/followers"]
assert update.data["object"]["id"] == event_data["id"]
assert update.data["object"]["type"] == event_data["type"]
assert update.data["object"]["startTime"] == @updated_start_time
refute updated_event.begins_on == event.begins_on
%Event{} = database_event = Mobilizon.Events.get_event_by_url(event.url)
assert database_event.begins_on == @updated_start_time
assert database_event.title == event.title
end
end
end

View File

@@ -17,7 +17,7 @@ defmodule Mobilizon.Service.ActivityPub.Converters.ActorTest do
actor =
ActorConverter.as_to_model_data(%{
"type" => "Person",
"preferred_username" => "test_account"
"preferredUsername" => "test_account"
})
assert actor["type"] == :Person

View File

@@ -330,7 +330,7 @@ defmodule Mobilizon.Service.ActivityPub.TransmogrifierTest do
assert data["object"] == comment.url
end
test "it works for incoming update activities" do
test "it works for incoming update activities on actors" do
data = File.read!("test/fixtures/mastodon-post-activity.json") |> Jason.decode!()
{:ok, %Activity{data: data, local: false}, _} = Transmogrifier.handle_incoming(data)
@@ -349,11 +349,37 @@ defmodule Mobilizon.Service.ActivityPub.TransmogrifierTest do
{:ok, %Activity{data: data, local: false}, _} = Transmogrifier.handle_incoming(update_data)
{:ok, %Actor{} = actor} = Actors.get_actor_by_url(data["actor"])
assert actor.name == "gargle"
assert actor.name == "nextsoft"
assert actor.summary == "<p>Some bio</p>"
end
test "it works for incoming update activities on events" do
data = File.read!("test/fixtures/mobilizon-post-activity.json") |> Jason.decode!()
{:ok, %Activity{data: data, local: false}, _} = Transmogrifier.handle_incoming(data)
update_data = File.read!("test/fixtures/mastodon-update.json") |> Jason.decode!()
object =
data["object"]
|> Map.put("actor", data["actor"])
|> Map.put("name", "My updated event")
|> Map.put("id", data["object"]["id"])
|> Map.put("type", "Event")
update_data =
update_data
|> Map.put("actor", data["actor"])
|> Map.put("object", object)
{:ok, %Activity{data: data, local: false}, _} = Transmogrifier.handle_incoming(update_data)
%Event{} = event = Events.get_event_by_url(data["object"]["id"])
assert event.title == "My updated event"
assert event.description == data["object"]["content"]
end
# test "it works for incoming update activities which lock the account" do
# data = File.read!("test/fixtures/mastodon-post-activity.json") |> Jason.decode!()

View File

@@ -418,12 +418,12 @@ defmodule MobilizonWeb.Resolvers.EventResolverTest do
assert json_response(res, 200)["data"]["createEvent"]["picture"]["url"]
end
test "update_event/3 should check the event exists", %{conn: conn, actor: actor, user: user} do
test "update_event/3 should check the event exists", %{conn: conn, actor: _actor, user: user} do
mutation = """
mutation {
updateEvent(
event_id: 45,
title: "my event updated",
title: "my event updated"
) {
title,
uuid,
@@ -445,7 +445,7 @@ defmodule MobilizonWeb.Resolvers.EventResolverTest do
test "update_event/3 should check the user is an administrator", %{
conn: conn,
actor: actor,
actor: _actor,
user: user
} do
event = insert(:event)
@@ -454,6 +454,7 @@ defmodule MobilizonWeb.Resolvers.EventResolverTest do
mutation {
updateEvent(
title: "my event updated",
event_id: #{event.id}
) {
title,
uuid,
@@ -470,11 +471,11 @@ defmodule MobilizonWeb.Resolvers.EventResolverTest do
|> auth_conn(user)
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert json_response(res, 200)["errors"] == nil
assert hd(json_response(res, 200)["errors"])["message"] == "User doesn't own actor"
end
test "update_event/3 updates an event", %{conn: conn, actor: actor, user: user} do
event = insert(:event)
event = insert(:event, organizer_actor: actor)
begins_on = DateTime.utc_now() |> DateTime.truncate(:second) |> DateTime.to_iso8601()
@@ -484,12 +485,14 @@ defmodule MobilizonWeb.Resolvers.EventResolverTest do
title: "my event updated",
description: "description updated",
begins_on: "#{begins_on}",
event_id: #{event.id},
organizer_actor_id: "#{actor.id}",
category: "birthday",
tags: ["tag1_updated", "tag2_updated"]
) {
title,
uuid,
url,
tags {
title,
slug
@@ -505,13 +508,76 @@ defmodule MobilizonWeb.Resolvers.EventResolverTest do
assert json_response(res, 200)["errors"] == nil
assert json_response(res, 200)["data"]["updateEvent"]["title"] == "my event updated"
assert json_response(res, 200)["data"]["updateEvent"]["uuid"] == event.uuid
assert json_response(res, 200)["data"]["updateEvent"]["url"] == event.url
assert json_response(res, 200)["data"]["createEvent"]["tags"] == [
%{"slug" => "tag1_updated", "title" => "tag1_updated"},
%{"slug" => "tag2_updated", "title" => "tag2_updated"}
assert json_response(res, 200)["data"]["updateEvent"]["tags"] == [
%{"slug" => "tag1-updated", "title" => "tag1_updated"},
%{"slug" => "tag2-updated", "title" => "tag2_updated"}
]
end
test "update_event/3 updates an event with a new picture", %{
conn: conn,
actor: actor,
user: user
} do
event = insert(:event, organizer_actor: actor)
begins_on = DateTime.utc_now() |> DateTime.truncate(:second) |> DateTime.to_iso8601()
mutation = """
mutation {
updateEvent(
title: "my event updated",
description: "description updated",
begins_on: "#{begins_on}",
event_id: #{event.id},
organizer_actor_id: "#{actor.id}",
category: "birthday",
picture: {
picture: {
name: "picture for my event",
alt: "A very sunny landscape",
file: "event.jpg",
actor_id: "#{actor.id}"
}
}
) {
title,
uuid,
url,
picture {
name,
url
}
}
}
"""
map = %{
"query" => mutation,
"event.jpg" => %Plug.Upload{
path: "test/fixtures/picture.png",
filename: "event.jpg"
}
}
res =
conn
|> auth_conn(user)
|> put_req_header("content-type", "multipart/form-data")
|> post("/api", map)
assert json_response(res, 200)["errors"] == nil
assert json_response(res, 200)["data"]["updateEvent"]["title"] == "my event updated"
assert json_response(res, 200)["data"]["updateEvent"]["uuid"] == event.uuid
assert json_response(res, 200)["data"]["updateEvent"]["url"] == event.url
assert json_response(res, 200)["data"]["updateEvent"]["picture"]["name"] ==
"picture for my event"
end
test "list_events/3 returns events", context do
event = insert(:event)

View File

@@ -67,8 +67,8 @@ defmodule Mobilizon.Factory do
def tag_factory do
%Mobilizon.Events.Tag{
title: "MyTag",
slug: sequence("MyTag")
title: sequence("MyTag"),
slug: sequence("my-tag")
}
end