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

View File

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

View File

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

View File

@ -20,7 +20,7 @@ defmodule MobilizonWeb.Schema.CommentType do
@desc "The list of visibility options for a comment" @desc "The list of visibility options for a comment"
enum :comment_visibility do 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(:unlisted, description: "Visible only to people with the link - or invited")
value(:private, value(:private,

View File

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

View File

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

View File

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