Add anonymous and remote participations

This commit is contained in:
Thomas Citharel
2019-12-20 13:04:34 +01:00
parent 17e0b3968f
commit 2ed9050a90
135 changed files with 10141 additions and 2271 deletions

View File

@@ -40,6 +40,7 @@ defmodule Mobilizon.Federation.ActivityPub.TransmogrifierTest do
"https://test.mobilizon.org/events/39026210-0c69-4238-b3cc-986f33f98ed0/activity"
assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
#
# assert data["cc"] == [
# "https://framapiaf.org/users/admin/followers",
@@ -843,7 +844,8 @@ defmodule Mobilizon.Federation.ActivityPub.TransmogrifierTest do
%Event{} = event = insert(:event, organizer_actor: organizer, join_options: :restricted)
{:ok, join_activity, participation} = ActivityPub.join(event, participant_actor)
{:ok, join_activity, participation} =
ActivityPub.join(event, participant_actor, false, %{metadata: %{role: :not_approved}})
accept_data =
File.read!("test/fixtures/mastodon-accept-activity.json")

View File

@@ -13,7 +13,7 @@
"id": "http://mobilizon.test/follows/fdfds"
},
"nickname": "lain",
"id": "\"id\": \"http://mobilizon.test/accepts/follows/fdfds",
"id": "http://mobilizon.test/accepts/follows/fdfds",
"actor": "http://mobilizon.test/@thomas0",
"@context": [
"https://www.w3.org/ns/activitystreams",

View File

@@ -1,26 +1,53 @@
defmodule Mobilizon.GraphQL.Resolvers.ConfigTest do
use Mobilizon.Web.ConnCase
alias Mobilizon.Actors
alias Mobilizon.Actors.Actor
use Bamboo.Test
alias Mobilizon.GraphQL.AbsintheHelpers
describe "Resolver: Get config" do
test "get_config/3 returns the instance config", context do
Cachex.clear("full_config")
Mobilizon.Config.clear_config_cache()
query = """
{
config {
name,
registrationsOpen
anonymous {
participation {
allowed,
validation {
email {
enabled,
confirmationRequired
}
}
},
actor_id
}
}
}
"""
res =
context.conn
|> get("/api", AbsintheHelpers.query_skeleton(query, "config"))
|> AbsintheHelpers.graphql_query(query: query)
assert json_response(res, 200)["data"]["config"]["name"] == "Test instance"
assert json_response(res, 200)["data"]["config"]["registrationsOpen"] == true
assert res["data"]["config"]["name"] == "Test instance"
assert res["data"]["config"]["registrationsOpen"] == true
assert res["data"]["config"]["anonymous"]["participation"]["validation"]["email"]["enabled"] ==
true
assert res["data"]["config"]["anonymous"]["participation"]["validation"]["email"][
"confirmationRequired"
] == true
{:ok, %Actor{id: actor_id}} = Actors.get_or_create_internal_actor("anonymous")
assert res["data"]["config"]["anonymous"]["actor_id"] == to_string(actor_id)
end
end
end

View File

@@ -1,14 +1,16 @@
defmodule Mobilizon.GraphQL.Resolvers.ParticipantTest do
use Mobilizon.Web.ConnCase
use Bamboo.Test
use Mobilizon.Tests.Helpers
alias Mobilizon.Config
alias Mobilizon.Events
alias Mobilizon.Events.{Event, EventParticipantStats, Participant}
alias Mobilizon.GraphQL.AbsintheHelpers
alias Mobilizon.Web.Email
import Mobilizon.Factory
alias Mobilizon.Events
alias Mobilizon.GraphQL.AbsintheHelpers
alias Mobilizon.Web.Email
@event %{
description: "some body",
title: "some title",
@@ -480,7 +482,7 @@ defmodule Mobilizon.GraphQL.Resolvers.ParticipantTest do
query = """
{
event(uuid: "#{event.uuid}") {
participants(page: 2, limit: 1, roles: "participant,moderator,administrator,creator", actorId: "#{
participants(page: 1, limit: 1, roles: "participant,moderator,administrator,creator", actorId: "#{
actor.id
}") {
role,
@@ -513,7 +515,7 @@ defmodule Mobilizon.GraphQL.Resolvers.ParticipantTest do
query = """
{
event(uuid: "#{event.uuid}") {
participants(page: 1, limit: 1, roles: "participant,moderator,administrator,creator", actorId: "#{
participants(page: 2, limit: 1, roles: "participant,moderator,administrator,creator", actorId: "#{
actor.id
}") {
role,
@@ -697,7 +699,7 @@ defmodule Mobilizon.GraphQL.Resolvers.ParticipantTest do
assert json_response(res, 200)["data"]["updateParticipation"]["actor"]["id"] ==
to_string(actor.id)
participation = Mobilizon.Events.get_participant(participation_id)
participation = Events.get_participant(participation_id)
assert_delivered_email(Email.Participation.participation_updated(user, participation))
@@ -843,7 +845,7 @@ defmodule Mobilizon.GraphQL.Resolvers.ParticipantTest do
assert json_response(res, 200)["data"]["updateParticipation"]["actor"]["id"] ==
to_string(actor.id)
participation = Mobilizon.Events.get_participant(participation_id)
participation = Events.get_participant(participation_id)
assert_delivered_email(Email.Participation.participation_updated(user, participation))
res =
@@ -917,4 +919,426 @@ defmodule Mobilizon.GraphQL.Resolvers.ParticipantTest do
"Provided moderator actor ID doesn't have permission on this event"
end
end
describe "Participate with anonymous user" do
@email "test@test.tld"
@mutation """
mutation JoinEvent($actorId: ID!, $eventId: ID!, $email: String) {
joinEvent(
actorId: $actorId,
eventId: $eventId,
email: $email
) {
role,
actor {
id
},
event {
id
}
}
}
"""
@confirmation_mutation """
mutation ConfirmParticipation($confirmationToken: String!) {
confirmParticipation(confirmationToken: $confirmationToken) {
role,
actor {
id
},
event {
id
}
}
}
"""
@cancel_participation_mutation """
mutation LeaveEvent($actorId: ID!, $eventId: ID!, $token: String) {
leaveEvent(
actorId: $actorId,
eventId: $eventId,
token: $token
) {
id
}
}
"""
clear_config([:anonymous, :participation])
setup %{conn: conn, actor: actor, user: user} do
Mobilizon.Config.clear_config_cache()
anonymous_actor_id = Config.anonymous_actor_id()
{:ok, conn: conn, actor: actor, user: user, anonymous_actor_id: anonymous_actor_id}
end
test "I can't participate if anonymous participation is enabled on the server but disabled for this event",
%{conn: conn, anonymous_actor_id: actor_id} do
event = insert(:event, options: %{anonymous_participation: false})
Config.put([:anonymous, :participation, :allowed], true)
Config.put([:anonymous, :participation, :validation, :email, :enabled], true)
Config.put([:anonymous, :participation, :validation, :email, :confirmation_required], false)
res =
conn
|> AbsintheHelpers.graphql_query(
query: @mutation,
variables: %{actorId: actor_id, eventId: event.id}
)
assert hd(res["errors"])["message"] == "Anonymous participation is not enabled"
end
test "I can't participate if anonymous participation is enabled on the server and enabled for the event but the event is remote",
%{conn: conn, anonymous_actor_id: actor_id} do
event = insert(:event, options: %{anonymous_participation: true}, local: false)
Config.put([:anonymous, :participation, :allowed], true)
Config.put([:anonymous, :participation, :validation, :email, :enabled], true)
Config.put([:anonymous, :participation, :validation, :email, :confirmation_required], false)
res =
conn
|> AbsintheHelpers.graphql_query(
query: @mutation,
variables: %{actorId: actor_id, eventId: event.id}
)
assert hd(res["errors"])["message"] == "Anonymous participation is not enabled"
end
test "I can't participate without being logged in when using anonymous user and providing no email when required",
%{conn: conn, anonymous_actor_id: actor_id} do
event = insert(:event, options: %{anonymous_participation: true})
Config.put([:anonymous, :participation, :allowed], true)
Config.put([:anonymous, :participation, :validation, :email, :enabled], true)
Config.put([:anonymous, :participation, :validation, :email, :confirmation_required], false)
res =
conn
|> AbsintheHelpers.graphql_query(
query: @mutation,
variables: %{actorId: actor_id, eventId: event.id}
)
assert hd(res["errors"])["message"] == "A valid email is required by your instance"
res =
conn
|> AbsintheHelpers.graphql_query(
query: @mutation,
variables: %{actorId: actor_id, eventId: event.id, email: "bad_email"}
)
assert hd(res["errors"])["message"] == "A valid email is required by your instance"
end
test "I can participate without being logged in when using anonymous user when providing email",
%{conn: conn, anonymous_actor_id: actor_id} do
event = insert(:event, options: %{anonymous_participation: true})
Config.put([:anonymous, :participation, :allowed], true)
Config.put([:anonymous, :participation, :validation, :email, :enabled], true)
Config.put([:anonymous, :participation, :validation, :email, :confirmation_required], false)
res =
conn
|> AbsintheHelpers.graphql_query(
query: @mutation,
variables: %{actorId: actor_id, eventId: event.id, email: @email}
)
assert res["errors"] == nil
assert res["data"]["joinEvent"]["role"] == "PARTICIPANT"
assert res["data"]["joinEvent"]["event"]["id"] == to_string(event.id)
assert res["data"]["joinEvent"]["actor"]["id"] == to_string(actor_id)
%Participant{} = participant = event.id |> Events.list_participants_for_event() |> hd
assert participant.metadata.email == @email
# When confirmation_required is false, participant has already the participant role
assert participant.role == :participant
res =
conn
|> AbsintheHelpers.graphql_query(
query: @mutation,
variables: %{actorId: actor_id, eventId: event.id, email: @email}
)
assert hd(res["errors"])["message"] == "You are already a participant of this event"
end
test "Participating without being logged in when using anonymous user and providing email sends a confirmation email",
%{conn: conn, anonymous_actor_id: actor_id} do
event = insert(:event, options: %{anonymous_participation: true})
Config.put([:anonymous, :participation, :allowed], true)
Config.put([:anonymous, :participation, :validation, :email, :enabled], true)
Config.put([:anonymous, :participation, :validation, :email, :confirmation_required], true)
res =
conn
|> AbsintheHelpers.graphql_query(
query: @mutation,
variables: %{actorId: actor_id, eventId: event.id, email: @email}
)
assert res["errors"] == nil
# Not approved until email confirmation
assert res["data"]["joinEvent"]["role"] == "NOT_CONFIRMED"
assert res["data"]["joinEvent"]["event"]["id"] == to_string(event.id)
assert res["data"]["joinEvent"]["actor"]["id"] == to_string(actor_id)
assert %Participant{
metadata: %{confirmation_token: confirmation_token},
role: :not_confirmed
} = participant = event.id |> Events.list_participants_for_event([]) |> hd()
# hack to avoid preloading event in participant
participant = Map.put(participant, :event, event)
res =
conn
|> AbsintheHelpers.graphql_query(
query: @mutation,
variables: %{actorId: actor_id, eventId: event.id, email: @email}
)
assert hd(res["errors"])["message"] == "You are already a participant of this event"
assert_delivered_email(
Email.Participation.anonymous_participation_confirmation(@email, participant)
)
conn
|> AbsintheHelpers.graphql_query(
query: @confirmation_mutation,
variables: %{confirmationToken: confirmation_token}
)
assert %Participant{role: :participant} =
event.id |> Events.list_participants_for_event() |> hd()
end
test "I can participate anonymously and and confirm my participation with bad token",
%{conn: conn, anonymous_actor_id: actor_id} do
event = insert(:event, options: %{anonymous_participation: true})
Config.put([:anonymous, :participation, :allowed], true)
Config.put([:anonymous, :participation, :validation, :email, :enabled], true)
Config.put([:anonymous, :participation, :validation, :email, :confirmation_required], true)
res =
conn
|> AbsintheHelpers.graphql_query(
query: @mutation,
variables: %{actorId: actor_id, eventId: event.id, email: @email}
)
assert res["errors"] == nil
assert res["data"]["joinEvent"]["role"] == "NOT_CONFIRMED"
assert res["data"]["joinEvent"]["event"]["id"] == to_string(event.id)
assert res["data"]["joinEvent"]["actor"]["id"] == to_string(actor_id)
%Participant{} = participant = event.id |> Events.list_participants_for_event([]) |> hd
assert participant.metadata.email == @email
# When confirmation_required is false, participant has already the participant role
assert participant.role == :not_confirmed
res =
conn
|> AbsintheHelpers.graphql_query(
query: @confirmation_mutation,
variables: %{confirmationToken: "bad token"}
)
assert hd(res["errors"])["message"] == "This token is invalid"
assert %Participant{role: :not_confirmed} =
event.id |> Events.list_participants_for_event([]) |> hd()
end
test "I can participate anonymously but change my mind and cancel my participation",
%{conn: conn, anonymous_actor_id: actor_id} do
event = insert(:event, options: %{anonymous_participation: true})
Config.put([:anonymous, :participation, :allowed], true)
Config.put([:anonymous, :participation, :validation, :email, :enabled], true)
Config.put([:anonymous, :participation, :validation, :email, :confirmation_required], true)
res =
conn
|> AbsintheHelpers.graphql_query(
query: @mutation,
variables: %{actorId: actor_id, eventId: event.id, email: @email}
)
assert res["errors"] == nil
assert res["data"]["joinEvent"]["role"] == "NOT_CONFIRMED"
assert res["data"]["joinEvent"]["event"]["id"] == to_string(event.id)
assert res["data"]["joinEvent"]["actor"]["id"] == to_string(actor_id)
{:ok, %Event{participant_stats: %{not_confirmed: 1}}} = Events.get_event(event.id)
%Participant{} = participant = event.id |> Events.list_participants_for_event([]) |> hd
assert participant.metadata.email == @email
# When confirmation_required is false, participant has already the participant role
assert participant.role == :not_confirmed
res =
conn
|> AbsintheHelpers.graphql_query(
query: @cancel_participation_mutation,
variables: %{
actorId: actor_id,
eventId: event.id,
token: "bad token"
}
)
assert hd(res["errors"])["message"] == "Participant not found"
assert %Participant{
id: participant_id,
role: :not_confirmed,
metadata: %{cancellation_token: cancellation_token}
} = event.id |> Events.list_participants_for_event([]) |> hd()
res =
conn
|> AbsintheHelpers.graphql_query(
query: @cancel_participation_mutation,
variables: %{
actorId: actor_id,
eventId: event.id,
token: cancellation_token
}
)
assert res["data"]["leaveEvent"]["id"] == participant_id
{:ok, %Event{participant_stats: %{not_confirmed: 0}}} = Events.get_event(event.id)
assert Events.list_participants_for_event(event.id, []) == []
end
test "I can participate anonymously, confirm my participation and then be confirmed by the organizer",
%{conn: conn, anonymous_actor_id: actor_id, actor: event_creator_actor, user: user} do
event =
insert(:event,
options: %{anonymous_participation: true},
join_options: :restricted,
organizer_actor: event_creator_actor,
participant_stats: %{creator: 1}
)
insert(:participant, event: event, actor: event_creator_actor, role: :creator)
Config.put([:anonymous, :participation, :allowed], true)
Config.put([:anonymous, :participation, :validation, :email, :enabled], true)
Config.put([:anonymous, :participation, :validation, :email, :confirmation_required], true)
assert {:ok,
%Event{
participant_stats: %EventParticipantStats{
not_confirmed: 0,
not_approved: 0,
creator: 1,
participant: 0
}
}} = Events.get_event(event.id)
res =
conn
|> AbsintheHelpers.graphql_query(
query: @mutation,
variables: %{actorId: actor_id, eventId: event.id, email: @email}
)
assert res["errors"] == nil
assert res["data"]["joinEvent"]["role"] == "NOT_CONFIRMED"
assert res["data"]["joinEvent"]["event"]["id"] == to_string(event.id)
assert res["data"]["joinEvent"]["actor"]["id"] == to_string(actor_id)
assert {:ok,
%Event{
participant_stats: %EventParticipantStats{
not_confirmed: 1,
not_approved: 0,
creator: 1,
participant: 0
}
}} = Events.get_event(event.id)
assert %Participant{
role: :not_confirmed,
metadata: %{confirmation_token: confirmation_token, email: @email}
} = event.id |> Events.list_participants_for_event([]) |> hd
conn
|> AbsintheHelpers.graphql_query(
query: @confirmation_mutation,
variables: %{confirmationToken: confirmation_token}
)
assert {:ok,
%Event{
participant_stats: %EventParticipantStats{
not_confirmed: 0,
not_approved: 1,
creator: 1,
participant: 0
}
}} = Events.get_event(event.id)
assert %Participant{role: :not_approved, id: participant_id} =
event.id |> Events.list_participants_for_event([]) |> hd
update_participation_mutation = """
mutation UpdateParticipation($participantId: ID!, $role: String!, $moderatorActorId: ID!) {
updateParticipation(id: $participantId, role: $role, moderatorActorId: $moderatorActorId) {
id,
role,
actor {
id
},
event {
id
}
}
}
"""
res =
conn
|> auth_conn(user)
|> AbsintheHelpers.graphql_query(
query: update_participation_mutation,
variables: %{
participantId: participant_id,
role: "PARTICIPANT",
moderatorActorId: event_creator_actor.id
}
)
assert res["errors"] == nil
assert %Participant{role: :participant} =
event.id |> Events.list_participants_for_event([]) |> hd
assert {:ok,
%Event{
participant_stats: %EventParticipantStats{
not_confirmed: 0,
not_approved: 0,
creator: 1,
participant: 1
}
}} = Events.get_event(event.id)
participant = Events.get_participant(participant_id)
assert_delivered_email(Email.Participation.participation_updated(@email, participant))
end
end
end

View File

@@ -25,6 +25,7 @@ defmodule Mobilizon.AddressesTest do
street: "some updated streetAddress",
geom: %Geo.Point{coordinates: {20, -20}, srid: 4326}
}
# @invalid_attrs %{
# addressCountry: nil,
# addressLocality: nil,

View File

@@ -1,7 +1,7 @@
defmodule Mobilizon.Service.Geospatial.AddokTest do
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
use Mobilizon.DataCase, async: false
use Mobilizon.DataCase
import Mock
@@ -9,28 +9,31 @@ defmodule Mobilizon.Service.Geospatial.AddokTest do
alias Mobilizon.Config
alias Mobilizon.Service.Geospatial.Addok
@httpoison_headers [
{"User-Agent",
"#{Config.instance_name()} #{Config.instance_hostname()} - Mobilizon #{
Mix.Project.config()[:version]
}"}
]
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
# See https://hexdocs.pm/ecto_sql/Ecto.Adapters.SQL.Sandbox.html
{:ok,
httpoison_headers: [
{"User-Agent", Config.instance_user_agent()}
]}
end
@endpoint get_in(Application.get_env(:mobilizon, Addok), [:endpoint])
@fake_endpoint "https://domain.tld"
describe "search address" do
test "produces a valid search address" do
test "produces a valid search address", %{httpoison_headers: httpoison_headers} do
with_mock HTTPoison, get: fn _url, _headers -> "{}" 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)
)
end
end
test "produces a valid search address with options" do
test "produces a valid search address with options", %{httpoison_headers: httpoison_headers} do
with_mock HTTPoison, get: fn _url, _headers -> "{}" end do
Addok.search("10 Rue Jangot",
endpoint: @fake_endpoint,
@@ -41,7 +44,7 @@ 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
)
)
end

View File

@@ -1,7 +1,7 @@
defmodule Mobilizon.Service.Geospatial.GoogleMapsTest do
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
use Mobilizon.DataCase, async: false
use Mobilizon.DataCase
import Mock

View File

@@ -1,7 +1,7 @@
defmodule Mobilizon.Service.Geospatial.MapQuestTest do
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
use Mobilizon.DataCase, async: false
use Mobilizon.DataCase
import Mock
@@ -9,12 +9,15 @@ defmodule Mobilizon.Service.Geospatial.MapQuestTest do
alias Mobilizon.Config
alias Mobilizon.Service.Geospatial.MapQuest
@httpoison_headers [
{"User-Agent",
"#{Config.instance_name()} #{Config.instance_hostname()} - Mobilizon #{
Mix.Project.config()[:version]
}"}
]
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
# See https://hexdocs.pm/ecto_sql/Ecto.Adapters.SQL.Sandbox.html
{:ok,
httpoison_headers: [
{"User-Agent", Config.instance_user_agent()}
]}
end
describe "search address" do
test "without API Key triggers an error" do
@@ -23,7 +26,7 @@ defmodule Mobilizon.Service.Geospatial.MapQuestTest do
end
end
test "produces a valid search address with options" do
test "produces a valid search address with options", %{httpoison_headers: httpoison_headers} do
with_mock HTTPoison,
get: fn _url, _headers ->
{:ok,
@@ -41,7 +44,7 @@ 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
)
)
end

View File

@@ -1,23 +1,24 @@
defmodule Mobilizon.Service.Geospatial.NominatimTest do
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
use Mobilizon.DataCase, async: false
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
import Mock
alias Mobilizon.Addresses.Address
alias Mobilizon.Config
alias Mobilizon.Service.Geospatial.Nominatim
@httpoison_headers [
{"User-Agent",
"#{Config.instance_name()} #{Config.instance_hostname()} - Mobilizon #{
Mix.Project.config()[:version]
}"}
]
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
# See https://hexdocs.pm/ecto_sql/Ecto.Adapters.SQL.Sandbox.html
{:ok,
httpoison_headers: [
{"User-Agent", Config.instance_user_agent()}
]}
end
describe "search address" do
test "produces a valid search address with options" do
test "produces a valid search address with options", %{httpoison_headers: httpoison_headers} do
with_mock HTTPoison,
get: fn _url, _headers ->
{:ok, %HTTPoison.Response{status_code: 200, body: "[]"}}
@@ -30,7 +31,7 @@ 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
)
)
end

View File

@@ -1,7 +1,7 @@
defmodule Mobilizon.Service.Geospatial.PhotonTest do
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
use Mobilizon.DataCase, async: false
use Mobilizon.DataCase
import Mock
@@ -9,15 +9,18 @@ defmodule Mobilizon.Service.Geospatial.PhotonTest do
alias Mobilizon.Config
alias Mobilizon.Service.Geospatial.Photon
@httpoison_headers [
{"User-Agent",
"#{Config.instance_name()} #{Config.instance_hostname()} - Mobilizon #{
Mix.Project.config()[:version]
}"}
]
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
# See https://hexdocs.pm/ecto_sql/Ecto.Adapters.SQL.Sandbox.html
{:ok,
httpoison_headers: [
{"User-Agent", Config.instance_user_agent()}
]}
end
describe "search address" do
test "produces a valid search address with options" do
test "produces a valid search address with options", %{httpoison_headers: httpoison_headers} do
with_mock HTTPoison,
get: fn _url, _headers ->
{:ok, %HTTPoison.Response{status_code: 200, body: "{\"features\": []"}}
@@ -30,7 +33,7 @@ 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
)
)
end

View File

@@ -122,6 +122,7 @@ defmodule Mobilizon.Factory do
visibility: :public,
tags: build_list(3, :tag),
mentions: [],
local: true,
publish_at: DateTime.utc_now(),
url: Routes.page_url(Endpoint, :event, uuid),
picture: insert(:picture),
@@ -140,7 +141,11 @@ defmodule Mobilizon.Factory do
actor: build(:actor),
role: :creator,
url: "#{Endpoint.url()}/join/event/#{uuid}",
id: uuid
id: uuid,
metadata: %{
email: nil,
confirmation_token: nil
}
}
end

38
test/support/helpers.ex Normal file
View File

@@ -0,0 +1,38 @@
# Portions of this file are derived from Pleroma:
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social>
# SPDX-License-Identifier: AGPL-3.0-only
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/test/support/helpers.ex
defmodule Mobilizon.Tests.Helpers do
@moduledoc """
Helpers for use in tests.
"""
defmacro clear_config(config_path) do
quote do
clear_config(unquote(config_path)) do
end
end
end
defmacro clear_config(config_path, do: yield) do
quote do
setup do
initial_setting = Mobilizon.Config.get(unquote(config_path))
unquote(yield)
on_exit(fn -> Mobilizon.Config.put(unquote(config_path), initial_setting) end)
:ok
end
end
end
defmacro __using__(_opts) do
quote do
import Mobilizon.Tests.Helpers,
only: [
clear_config: 1,
clear_config: 2
]
end
end
end

View File

@@ -68,6 +68,17 @@ defmodule Mobilizon.Web.ActivityPubControllerTest do
assert json_response(conn, 404)
end
test "it redirects for remote events", %{conn: conn} do
event = insert(:event, local: false, url: "https://someremote.url/events/here")
conn =
conn
|> put_req_header("accept", "application/activity+json")
|> get(Routes.page_url(Endpoint, :event, event.uuid))
assert redirected_to(conn) == "https://someremote.url/events/here"
end
end
describe "/comments/:uuid" do
@@ -83,6 +94,17 @@ defmodule Mobilizon.Web.ActivityPubControllerTest do
PageView.render("comment.activity-json", %{conn: %{assigns: %{object: comment}}})
end
test "it redirects for remote comments", %{conn: conn} do
comment = insert(:comment, local: false, url: "https://someremote.url/comments/here")
conn =
conn
|> put_req_header("accept", "application/activity+json")
|> get(Routes.page_url(Endpoint, :comment, comment.uuid))
assert redirected_to(conn) == "https://someremote.url/comments/here"
end
test "it returns 404 for non-public comments", %{conn: conn} do
comment = insert(:comment, visibility: :private)
@@ -278,6 +300,7 @@ defmodule Mobilizon.Web.ActivityPubControllerTest do
|> json_response(200)
assert length(result["first"]["orderedItems"]) == 10
# assert result["first"]["totalItems"] == 15
# assert result["totalItems"] == 15