Introduce group basic federation, event new page and notifications

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2020-02-18 08:57:00 +01:00
parent 300ef8f245
commit 4144e9ffd0
416 changed files with 32220 additions and 16750 deletions

View File

@@ -9,6 +9,11 @@ defmodule Mobilizon.Service.Geospatial.AddokTest do
alias Mobilizon.Config
alias Mobilizon.Service.Geospatial.Addok
@http_options [
follow_redirect: true,
ssl: [{:versions, [:"tlsv1.2"]}]
]
setup do
# Config.instance_user_agent/0 makes database calls so because of ownership connection
# we need to define it like this instead of a constant
@@ -24,17 +29,21 @@ defmodule Mobilizon.Service.Geospatial.AddokTest do
describe "search address" do
test "produces a valid search address", %{httpoison_headers: httpoison_headers} do
with_mock HTTPoison, get: fn _url, _headers -> "{}" end do
with_mock HTTPoison, get: fn _url, _headers, _options -> "{}" end do
Addok.search("10 Rue Jangot")
assert_called(
HTTPoison.get("#{@endpoint}/search/?q=10%20Rue%20Jangot&limit=10", httpoison_headers)
HTTPoison.get(
"#{@endpoint}/search/?q=10%20Rue%20Jangot&limit=10",
httpoison_headers,
@http_options
)
)
end
end
test "produces a valid search address with options", %{httpoison_headers: httpoison_headers} do
with_mock HTTPoison, get: fn _url, _headers -> "{}" end do
with_mock HTTPoison, get: fn _url, _headers, _options -> "{}" end do
Addok.search("10 Rue Jangot",
endpoint: @fake_endpoint,
limit: 5,
@@ -44,7 +53,8 @@ defmodule Mobilizon.Service.Geospatial.AddokTest do
assert_called(
HTTPoison.get(
"#{@fake_endpoint}/search/?q=10%20Rue%20Jangot&limit=5&lat=49&lon=12",
httpoison_headers
httpoison_headers,
@http_options
)
)
end

View File

@@ -8,6 +8,11 @@ defmodule Mobilizon.Service.Geospatial.GoogleMapsTest do
alias Mobilizon.Addresses.Address
alias Mobilizon.Service.Geospatial.GoogleMaps
@http_options [
follow_redirect: true,
ssl: [{:versions, [:"tlsv1.2"]}]
]
describe "search address" do
test "without API Key triggers an error" do
assert_raise ArgumentError, "API Key required to use Google Maps", fn ->
@@ -17,7 +22,7 @@ defmodule Mobilizon.Service.Geospatial.GoogleMapsTest do
test "produces a valid search address with options" do
with_mock HTTPoison,
get: fn _url ->
get: fn _url, _headers, _options ->
{:ok,
%HTTPoison.Response{status_code: 200, body: "{\"status\": \"OK\", \"results\": []}"}}
end do
@@ -29,7 +34,9 @@ defmodule Mobilizon.Service.Geospatial.GoogleMapsTest do
assert_called(
HTTPoison.get(
"https://maps.googleapis.com/maps/api/geocode/json?limit=5&key=toto&language=fr&address=10%20Rue%20Jangot"
"https://maps.googleapis.com/maps/api/geocode/json?limit=5&key=toto&language=fr&address=10%20Rue%20Jangot",
[],
@http_options
)
)
end

View File

@@ -9,6 +9,11 @@ defmodule Mobilizon.Service.Geospatial.MapQuestTest do
alias Mobilizon.Config
alias Mobilizon.Service.Geospatial.MapQuest
@http_options [
follow_redirect: true,
ssl: [{:versions, [:"tlsv1.2"]}]
]
setup do
# Config.instance_user_agent/0 makes database calls so because of ownership connection
# we need to define it like this instead of a constant
@@ -28,7 +33,7 @@ defmodule Mobilizon.Service.Geospatial.MapQuestTest do
test "produces a valid search address with options", %{httpoison_headers: httpoison_headers} do
with_mock HTTPoison,
get: fn _url, _headers ->
get: fn _url, _headers, _options ->
{:ok,
%HTTPoison.Response{
status_code: 200,
@@ -44,7 +49,8 @@ defmodule Mobilizon.Service.Geospatial.MapQuestTest do
assert_called(
HTTPoison.get(
"https://open.mapquestapi.com/geocoding/v1/address?key=toto&location=10%20Rue%20Jangot&maxResults=5",
httpoison_headers
httpoison_headers,
@http_options
)
)
end

View File

@@ -7,6 +7,11 @@ defmodule Mobilizon.Service.Geospatial.NominatimTest do
alias Mobilizon.Config
alias Mobilizon.Service.Geospatial.Nominatim
@http_options [
follow_redirect: true,
ssl: [{:versions, [:"tlsv1.2"]}]
]
setup do
# Config.instance_user_agent/0 makes database calls so because of ownership connection
# we need to define it like this instead of a constant
@@ -20,7 +25,7 @@ defmodule Mobilizon.Service.Geospatial.NominatimTest do
describe "search address" do
test "produces a valid search address with options", %{httpoison_headers: httpoison_headers} do
with_mock HTTPoison,
get: fn _url, _headers ->
get: fn _url, _headers, _options ->
{:ok, %HTTPoison.Response{status_code: 200, body: "[]"}}
end do
Nominatim.search("10 Rue Jangot",
@@ -31,7 +36,8 @@ defmodule Mobilizon.Service.Geospatial.NominatimTest do
assert_called(
HTTPoison.get(
"https://nominatim.openstreetmap.org/search?format=geocodejson&q=10%20Rue%20Jangot&limit=5&accept-language=fr&addressdetails=1&namedetails=1",
httpoison_headers
httpoison_headers,
@http_options
)
)
end

View File

@@ -9,6 +9,11 @@ defmodule Mobilizon.Service.Geospatial.PhotonTest do
alias Mobilizon.Config
alias Mobilizon.Service.Geospatial.Photon
@http_options [
follow_redirect: true,
ssl: [{:versions, [:"tlsv1.2"]}]
]
setup do
# Config.instance_user_agent/0 makes database calls so because of ownership connection
# we need to define it like this instead of a constant
@@ -22,7 +27,7 @@ defmodule Mobilizon.Service.Geospatial.PhotonTest do
describe "search address" do
test "produces a valid search address with options", %{httpoison_headers: httpoison_headers} do
with_mock HTTPoison,
get: fn _url, _headers ->
get: fn _url, _headers, _options ->
{:ok, %HTTPoison.Response{status_code: 200, body: "{\"features\": []"}}
end do
Photon.search("10 Rue Jangot",
@@ -33,7 +38,8 @@ defmodule Mobilizon.Service.Geospatial.PhotonTest do
assert_called(
HTTPoison.get(
"https://photon.komoot.de/api/?q=10%20Rue%20Jangot&lang=fr&limit=5",
httpoison_headers
httpoison_headers,
@http_options
)
)
end

View File

@@ -0,0 +1,147 @@
defmodule Mobilizon.Service.Notifications.SchedulerTest do
@moduledoc """
Test the scheduler module
"""
alias Mobilizon.Events.{Event, Participant}
alias Mobilizon.Service.Notifications.Scheduler
alias Mobilizon.Service.Workers.Notification
alias Mobilizon.Users.User
use Mobilizon.DataCase
import Mobilizon.Factory
use Oban.Testing, repo: Mobilizon.Storage.Repo
describe "Joining an event registers a job for notification before event" do
test "if the user has allowed it" do
%User{id: user_id} = user = insert(:user)
settings = insert(:settings, user_id: user_id, notification_before_event: true)
user = Map.put(user, :settings, settings)
actor = insert(:actor, user: user)
%Participant{id: participant_id, event: %Event{begins_on: begins_on}} =
participant = insert(:participant, actor: actor)
Scheduler.before_event_notification(participant)
scheduled_at = DateTime.add(begins_on, -3600, :second)
assert_enqueued(
worker: Notification,
args: %{participant_id: participant_id, op: :before_event_notification},
scheduled_at: scheduled_at
)
end
test "not if the user hasn't allowed it" do
%User{} = user = insert(:user)
actor = insert(:actor, user: user)
%Participant{id: participant_id} = participant = insert(:participant, actor: actor)
Scheduler.before_event_notification(participant)
refute_enqueued(
worker: Notification,
args: %{participant_id: participant_id, op: :before_event_notification}
)
end
end
describe "Joining an event registers a job for notification on the day of the event" do
test "if the user has allowed it" do
%User{id: user_id} = user = insert(:user)
settings =
insert(:settings, user_id: user_id, notification_on_day: true, timezone: "Europe/Paris")
user = Map.put(user, :settings, settings)
actor = insert(:actor, user: user)
%DateTime{} = tomorrow = DateTime.utc_now() |> DateTime.add(3600 * 24)
begins_on = %{tomorrow | hour: 16, minute: 0, second: 0, microsecond: {0, 0}}
%Event{begins_on: begins_on} = event = insert(:event, begins_on: begins_on)
%Participant{} = participant = insert(:participant, actor: actor, event: event)
Scheduler.on_day_notification(participant)
assert_enqueued(
worker: Notification,
args: %{user_id: user_id, op: :on_day_notification},
scheduled_at: %{DateTime.shift_zone!(begins_on, settings.timezone) | hour: 8}
)
end
test "not if the user hasn't allowed it" do
%User{id: user_id} = user = insert(:user)
actor = insert(:actor, user: user)
%Participant{} = participant = insert(:participant, actor: actor)
Scheduler.on_day_notification(participant)
refute_enqueued(
worker: Notification,
args: %{user_id: user_id, op: :on_day_notification}
)
end
test "not if it's too late" do
%User{id: user_id} = user = insert(:user)
settings =
insert(:settings, user_id: user_id, notification_on_day: true, timezone: "Europe/Paris")
user = Map.put(user, :settings, settings)
actor = insert(:actor, user: user)
%Event{} = event = insert(:event, begins_on: DateTime.add(DateTime.utc_now(), -3600))
%Participant{} = participant = insert(:participant, actor: actor, event: event)
Scheduler.on_day_notification(participant)
refute_enqueued(
worker: Notification,
args: %{user_id: user_id, op: :on_day_notification}
)
end
test "only once" do
%User{id: user_id} = user = insert(:user)
settings =
insert(:settings, user_id: user_id, notification_on_day: true, timezone: "Europe/Paris")
user = Map.put(user, :settings, settings)
actor = insert(:actor, user: user)
%DateTime{} = tomorrow = DateTime.utc_now() |> DateTime.add(3600 * 24)
begins_on = %{tomorrow | hour: 16, minute: 0, second: 0, microsecond: {0, 0}}
%Event{begins_on: begins_on} = event = insert(:event, begins_on: begins_on)
%Participant{} = participant = insert(:participant, actor: actor, event: event)
Scheduler.on_day_notification(participant)
assert_enqueued(
worker: Notification,
args: %{user_id: user_id, op: :on_day_notification},
scheduled_at: %{DateTime.shift_zone!(begins_on, settings.timezone) | hour: 8}
)
%DateTime{} = tomorrow = DateTime.utc_now() |> DateTime.add(3600 * 24)
begins_on = %{tomorrow | hour: 19, minute: 0, second: 0, microsecond: {0, 0}}
%Event{} = event = insert(:event, begins_on: begins_on)
%Participant{} = participant = insert(:participant, actor: actor, event: event)
Scheduler.on_day_notification(participant)
end
end
end

View File

@@ -0,0 +1,210 @@
defmodule Mobilizon.Service.Workers.NotificationTest do
@moduledoc """
Test the scheduler module
"""
alias Mobilizon.Actors.Actor
alias Mobilizon.Events
alias Mobilizon.Events.{Event, Participant}
alias Mobilizon.Service.Workers.Notification
alias Mobilizon.Users.User
alias Mobilizon.Web.Email.Notification, as: NotificationMailer
use Mobilizon.DataCase
use Bamboo.Test
import Mobilizon.Factory
describe "A before_event_notification job sends an email" do
test "if the user is still participating" do
%User{id: user_id} = user = insert(:user)
settings =
insert(:settings,
user_id: user_id,
notification_before_event: true
)
user = Map.put(user, :settings, settings)
%Actor{} = actor = insert(:actor, user: user)
%Participant{id: participant_id} =
participant = insert(:participant, role: :participant, actor: actor)
Notification.perform(
%{"op" => "before_event_notification", "participant_id" => participant_id},
nil
)
assert_delivered_email(
NotificationMailer.before_event_notification(
participant.actor.user.email,
participant
)
)
end
test "unless the person is no longer participating" do
%Event{id: event_id} = insert(:event)
%User{} = user = insert(:user)
%Actor{id: actor_id} = insert(:actor, user: user)
{:ok, %Participant{id: participant_id} = participant} =
Events.create_participant(%{actor_id: actor_id, event_id: event_id, role: :participant})
actor = Map.put(participant.actor, :user, user)
participant = Map.put(participant, :actor, actor)
assert {:ok, %Participant{}} = Events.delete_participant(participant)
Notification.perform(
%{"op" => "before_event_notification", "participant_id" => participant_id},
nil
)
refute_delivered_email(
NotificationMailer.before_event_notification(
participant.actor.user.email,
participant
)
)
end
test "unless the event has been cancelled" do
%Event{} = event = insert(:event, status: :cancelled)
%Participant{id: participant_id} =
participant = insert(:participant, role: :participant, event: event)
Notification.perform(
%{"op" => "before_event_notification", "participant_id" => participant_id},
nil
)
refute_delivered_email(
NotificationMailer.before_event_notification(
participant.actor.user.email,
participant
)
)
end
end
describe "A on_day_notification job sends an email" do
test "if the user is still participating" do
%User{id: user_id} = user = insert(:user)
settings =
insert(:settings, user_id: user_id, notification_on_day: true, timezone: "Europe/Paris")
user = Map.put(user, :settings, settings)
%Actor{} = actor = insert(:actor, user: user)
%Participant{} = participant = insert(:participant, role: :participant, actor: actor)
Notification.perform(
%{"op" => "on_day_notification", "user_id" => user_id},
nil
)
assert_delivered_email(
NotificationMailer.on_day_notification(
user,
[participant],
1
)
)
end
test "unless the person is no longer participating" do
%Event{id: event_id} = insert(:event)
%User{id: user_id} = user = insert(:user)
settings =
insert(:settings, user_id: user_id, notification_on_day: true, timezone: "Europe/Paris")
user = Map.put(user, :settings, settings)
%Actor{} = actor = insert(:actor, user: user)
{:ok, %Participant{} = participant} =
Events.create_participant(%{actor_id: actor.id, event_id: event_id, role: :participant})
actor = Map.put(participant.actor, :user, user)
participant = Map.put(participant, :actor, actor)
assert {:ok, %Participant{}} = Events.delete_participant(participant)
Notification.perform(
%{"op" => "on_day_notification", "user_id" => user_id},
nil
)
refute_delivered_email(
NotificationMailer.on_day_notification(
user,
[participant],
1
)
)
end
test "unless the event has been cancelled" do
%User{id: user_id} = user = insert(:user)
settings =
insert(:settings, user_id: user_id, notification_on_day: true, timezone: "Europe/Paris")
user = Map.put(user, :settings, settings)
%Actor{} = actor = insert(:actor, user: user)
%Event{} = event = insert(:event, status: :cancelled)
%Participant{} =
participant = insert(:participant, role: :participant, event: event, actor: actor)
Notification.perform(
%{"op" => "on_day_notification", "user_id" => user_id},
nil
)
refute_delivered_email(
NotificationMailer.on_day_notification(
user,
[participant],
1
)
)
end
test "with a lot of events" do
%User{id: user_id} = user = insert(:user)
settings =
insert(:settings, user_id: user_id, notification_on_day: true, timezone: "Europe/Paris")
user = Map.put(user, :settings, settings)
%Actor{} = actor = insert(:actor, user: user)
participants =
Enum.reduce(0..10, [], fn _i, acc ->
%Participant{} = participant = insert(:participant, role: :participant, actor: actor)
acc ++ [participant]
end)
Notification.perform(
%{"op" => "on_day_notification", "user_id" => user_id},
nil
)
refute_delivered_email(
NotificationMailer.on_day_notification(
user,
participants,
3
)
)
end
end
end