1
0
Fork 0
mirror of https://framagit.org/framasoft/mobilizon.git synced 2024-12-24 08:54:08 +00:00

Merge branch 'feature/event-tests' into 'master'

Exhaustive create/update event tests

See merge request framasoft/mobilizon!186
This commit is contained in:
Thomas Citharel 2019-09-23 10:28:17 +02:00
commit 0af855fdb8
7 changed files with 135 additions and 103 deletions

View file

@ -21,8 +21,8 @@ config :mobilizon, :instance,
upload_limit: 16_000_000,
avatar_upload_limit: 2_000_000,
banner_upload_limit: 4_000_000,
email_from: "noreply@localhost",
email_reply_to: "noreply@localhost"
email_from: System.get_env("MOBILIZON_INSTANCE_EMAIL") || "noreply@localhost",
email_reply_to: System.get_env("MOBILIZON_INSTANCE_EMAIL") || "noreply@localhost"
config :mime, :types, %{
"application/activity+json" => ["activity-json"],

View file

@ -13,7 +13,7 @@
<div class="column">
<form v-if="!validationSent">
<b-field
:label="t('Username')"
:label="$t('Username')"
:type="errors.preferred_username ? 'is-danger' : null"
:message="errors.preferred_username"
>

View file

@ -3,6 +3,7 @@ defmodule MobilizonWeb.API.Events do
API for Events
"""
alias Mobilizon.Events.Event
alias Mobilizon.Actors.Actor
alias Mobilizon.Service.ActivityPub
alias Mobilizon.Service.ActivityPub.Utils, as: ActivityPubUtils
alias Mobilizon.Service.ActivityPub.Activity
@ -13,39 +14,19 @@ defmodule MobilizonWeb.API.Events do
"""
@spec create_event(map()) :: {:ok, Activity.t(), Event.t()} | any()
def create_event(%{organizer_actor: organizer_actor} = args) do
with %{
title: title,
physical_address: physical_address,
picture: picture,
content_html: content_html,
tags: tags,
to: to,
cc: cc,
begins_on: begins_on,
ends_on: ends_on,
category: category,
join_options: join_options,
options: options
} <- prepare_args(args),
with args <- prepare_args(args),
event <-
ActivityPubUtils.make_event_data(
organizer_actor.url,
%{to: to, cc: cc},
title,
content_html,
picture,
tags,
%{
begins_on: begins_on,
ends_on: ends_on,
physical_address: physical_address,
category: category,
options: options,
join_options: join_options
}
args.organizer_actor.url,
%{to: args.to, cc: args.cc},
args.title,
args.content_html,
args.picture,
args.tags,
args.metadata
) do
ActivityPub.create(%{
to: ["https://www.w3.org/ns/activitystreams#Public"],
to: args.to,
actor: organizer_actor,
object: event,
local: true
@ -64,42 +45,21 @@ defmodule MobilizonWeb.API.Events do
%Event{} = event
) do
with args <- Map.put(args, :tags, Map.get(args, :tags, [])),
%{
title: title,
physical_address: physical_address,
picture: picture,
content_html: content_html,
tags: tags,
to: to,
cc: cc,
begins_on: begins_on,
ends_on: ends_on,
category: category,
join_options: join_options,
options: options
} <-
prepare_args(Map.merge(event, args)),
args <- prepare_args(Map.merge(event, args)),
event <-
ActivityPubUtils.make_event_data(
organizer_actor.url,
%{to: to, cc: cc},
title,
content_html,
picture,
tags,
%{
begins_on: begins_on,
ends_on: ends_on,
physical_address: physical_address,
category: category,
join_options: join_options,
options: options
},
args.organizer_actor.url,
%{to: args.to, cc: args.cc},
args.title,
args.content_html,
args.picture,
args.tags,
args.metadata,
event.uuid,
event.url
) do
ActivityPub.update(%{
to: ["https://www.w3.org/ns/activitystreams#Public"],
to: args.to,
actor: organizer_actor.url,
cc: [],
object: event,
@ -108,37 +68,33 @@ defmodule MobilizonWeb.API.Events do
end
end
defp prepare_args(
%{
organizer_actor: organizer_actor,
title: title,
description: description,
options: options,
tags: tags,
begins_on: begins_on,
category: category,
join_options: join_options
} = args
) do
with physical_address <- Map.get(args, :physical_address, nil),
title <- String.trim(title),
defp prepare_args(args) do
with %Actor{} = organizer_actor <- Map.get(args, :organizer_actor),
title <- args |> Map.get(:title, "") |> String.trim(),
visibility <- Map.get(args, :visibility, :public),
picture <- Map.get(args, :picture, nil),
description <- Map.get(args, :description),
tags <- Map.get(args, :tags),
{content_html, tags, to, cc} <-
Utils.prepare_content(organizer_actor, description, visibility, tags, nil) do
%{
title: title,
physical_address: physical_address,
picture: picture,
content_html: content_html,
picture: Map.get(args, :picture),
tags: tags,
organizer_actor: organizer_actor,
to: to,
cc: cc,
begins_on: begins_on,
ends_on: Map.get(args, :ends_on, nil),
category: category,
join_options: join_options,
options: options
metadata: %{
begins_on: Map.get(args, :begins_on),
ends_on: Map.get(args, :ends_on),
physical_address: Map.get(args, :physical_address),
category: Map.get(args, :category),
options: Map.get(args, :options),
join_options: Map.get(args, :join_options),
status: Map.get(args, :status),
online_address: Map.get(args, :online_address),
phone_address: Map.get(args, :phone_address)
}
}
end
end

View file

@ -20,7 +20,7 @@ defmodule MobilizonWeb.Schema.CommentType do
@desc "The list of visibility options for a comment"
enum :comment_visibility do
value(:public, description: "Publically listed and federated. Can be shared.")
value(:public, description: "Publicly listed and federated. Can be shared.")
value(:unlisted, description: "Visible only to people with the link - or invited")
value(:private,

View file

@ -62,6 +62,9 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Event do
"category" => object["category"],
"visibility" => visibility,
"join_options" => object["joinOptions"],
"status" => object["status"],
"online_address" => object["onlineAddress"],
"phone_address" => object["phoneAddress"],
"url" => object["id"],
"uuid" => object["uuid"],
"tags" => tags,

View file

@ -329,6 +329,9 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
"actor" => actor,
"id" => url || Routes.page_url(Endpoint, :event, uuid),
"joinOptions" => metadata.join_options,
"status" => metadata.status,
"onlineAddress" => metadata.online_address,
"phoneAddress" => metadata.phone_address,
"uuid" => uuid,
"tag" =>
tags |> Enum.uniq() |> Enum.map(fn tag -> %{"type" => "Hashtag", "name" => "##{tag}"} end)

View file

@ -119,22 +119,39 @@ defmodule MobilizonWeb.Resolvers.EventResolverTest do
end
test "create_event/3 creates an event with options", %{conn: conn, actor: actor, user: user} do
begins_on = DateTime.utc_now() |> DateTime.truncate(:second) |> DateTime.to_iso8601()
ends_on = DateTime.utc_now() |> DateTime.truncate(:second) |> DateTime.to_iso8601()
mutation = """
mutation {
createEvent(
title: "come to my event",
description: "it will be fine",
begins_on: "#{
DateTime.utc_now() |> DateTime.truncate(:second) |> DateTime.to_iso8601()
}",
begins_on: "#{begins_on}",
ends_on: "#{ends_on}",
status: TENTATIVE,
visibility: UNLISTED,
organizer_actor_id: "#{actor.id}",
online_address: "toto@example.com",
phone_address: "0000000000",
category: "super_category",
options: {
maximumAttendeeCapacity: 30,
showRemainingAttendeeCapacity: true
}
) {
title,
uuid,
description,
begins_on,
ends_on,
status,
visibility,
organizer_actor {
id
},
online_address,
phone_address,
category,
options {
maximumAttendeeCapacity,
showRemainingAttendeeCapacity
@ -150,14 +167,20 @@ defmodule MobilizonWeb.Resolvers.EventResolverTest do
assert json_response(res, 200)["errors"] == nil
assert json_response(res, 200)["data"]["createEvent"]["title"] == "come to my event"
event = json_response(res, 200)["data"]["createEvent"]
assert json_response(res, 200)["data"]["createEvent"]["options"]["maximumAttendeeCapacity"] ==
30
assert json_response(res, 200)["data"]["createEvent"]["options"][
"showRemainingAttendeeCapacity"
] == true
assert event["title"] == "come to my event"
assert event["description"] == "it will be fine"
assert event["begins_on"] == begins_on
assert event["ends_on"] == ends_on
assert event["status"] == "TENTATIVE"
assert event["visibility"] == "UNLISTED"
assert event["organizer_actor"]["id"] == "#{actor.id}"
assert event["online_address"] == "toto@example.com"
assert event["phone_address"] == "0000000000"
assert event["category"] == "super_category"
assert event["options"]["maximumAttendeeCapacity"] == 30
assert event["options"]["showRemainingAttendeeCapacity"] == true
end
test "create_event/3 creates an event with tags", %{conn: conn, actor: actor, user: user} do
@ -476,25 +499,55 @@ defmodule MobilizonWeb.Resolvers.EventResolverTest do
test "update_event/3 updates an event", %{conn: conn, actor: actor, user: user} do
event = insert(:event, organizer_actor: actor)
address = insert(:address)
begins_on = DateTime.utc_now() |> DateTime.truncate(:second) |> DateTime.to_iso8601()
ends_on = DateTime.utc_now() |> DateTime.truncate(:second) |> DateTime.to_iso8601()
mutation = """
mutation {
updateEvent(
event_id: #{event.id},
title: "my event updated",
description: "description updated",
begins_on: "#{begins_on}",
event_id: #{event.id},
ends_on: "#{ends_on}",
status: TENTATIVE,
tags: ["tag1_updated", "tag2_updated"],
online_address: "toto@example.com",
phone_address: "0000000000",
category: "birthday",
tags: ["tag1_updated", "tag2_updated"]
options: {
maximumAttendeeCapacity: 30,
showRemainingAttendeeCapacity: true
},
physical_address: {
street: "#{address.street}",
locality: "#{address.locality}"
}
) {
title,
uuid,
url,
title,
description,
begins_on,
ends_on,
status,
tags {
title,
slug
},
online_address,
phone_address,
category,
options {
maximumAttendeeCapacity,
showRemainingAttendeeCapacity
},
physicalAddress {
url,
geom,
street
}
}
}
@ -506,11 +559,28 @@ defmodule MobilizonWeb.Resolvers.EventResolverTest do
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
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"]["tags"] == [
event_res = json_response(res, 200)["data"]["updateEvent"]
assert event_res["title"] == "my event updated"
assert event_res["description"] == "description updated"
assert event_res["begins_on"] == "#{begins_on}"
assert event_res["ends_on"] == "#{ends_on}"
assert event_res["status"] == "TENTATIVE"
assert event_res["online_address"] == "toto@example.com"
assert event_res["phone_address"] == "0000000000"
assert event_res["category"] == "birthday"
assert event_res["options"]["maximumAttendeeCapacity"] == 30
assert event_res["options"]["showRemainingAttendeeCapacity"] == true
assert event_res["uuid"] == event.uuid
assert event_res["url"] == event.url
assert event_res["physicalAddress"]["street"] == address.street
refute event_res["physicalAddress"]["url"] == address.url
assert event_res["tags"] == [
%{"slug" => "tag1-updated", "title" => "tag1_updated"},
%{"slug" => "tag2-updated", "title" => "tag2_updated"}
]