From 9abb2f3526a949b211eb82c66542621a5524008e Mon Sep 17 00:00:00 2001
From: Thomas Citharel <tcit@tcit.fr>
Date: Mon, 23 Sep 2019 10:26:23 +0200
Subject: [PATCH] Fix tests

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
---
 lib/mobilizon_web/api/events.ex               | 118 ++++++------------
 lib/service/activity_pub/converters/event.ex  |   3 +
 lib/service/activity_pub/utils.ex             |   3 +
 .../resolvers/event_resolver_test.exs         |  14 +--
 4 files changed, 50 insertions(+), 88 deletions(-)

diff --git a/lib/mobilizon_web/api/events.ex b/lib/mobilizon_web/api/events.ex
index a38896971..6a0c726ca 100644
--- a/lib/mobilizon_web/api/events.ex
+++ b/lib/mobilizon_web/api/events.ex
@@ -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
diff --git a/lib/service/activity_pub/converters/event.ex b/lib/service/activity_pub/converters/event.ex
index bba57d009..b0586829d 100644
--- a/lib/service/activity_pub/converters/event.ex
+++ b/lib/service/activity_pub/converters/event.ex
@@ -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,
diff --git a/lib/service/activity_pub/utils.ex b/lib/service/activity_pub/utils.ex
index 112f6df08..f1a2027b1 100644
--- a/lib/service/activity_pub/utils.ex
+++ b/lib/service/activity_pub/utils.ex
@@ -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)
diff --git a/test/mobilizon_web/resolvers/event_resolver_test.exs b/test/mobilizon_web/resolvers/event_resolver_test.exs
index 4bb1b6216..0d842de1a 100644
--- a/test/mobilizon_web/resolvers/event_resolver_test.exs
+++ b/test/mobilizon_web/resolvers/event_resolver_test.exs
@@ -173,11 +173,11 @@ defmodule MobilizonWeb.Resolvers.EventResolverTest do
       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["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["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
@@ -566,9 +566,9 @@ defmodule MobilizonWeb.Resolvers.EventResolverTest do
       assert event_res["description"] == "description updated"
       assert event_res["begins_on"] == "#{begins_on}"
       assert event_res["ends_on"] == "#{ends_on}"
-      #      assert eventResponse["status"] == "TENTATIVE"
-      #      assert eventResponse["online_address"] == "toto@example.com"
-      #      assert eventResponse["phone_address"] == "0000000000"
+      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