More tests
This commit is contained in:
@@ -3,10 +3,11 @@ defmodule EventosWeb.ActivityPubControllerTest do
|
||||
import Eventos.Factory
|
||||
alias EventosWeb.ActivityPub.{ActorView, ObjectView}
|
||||
alias Eventos.{Repo, Actors, Actors.Actor}
|
||||
alias Eventos.Service.ActivityPub
|
||||
alias Eventos.Activity
|
||||
import Logger
|
||||
|
||||
describe "/@:username" do
|
||||
describe "/@:preferred_username" do
|
||||
test "it returns a json representation of the actor", %{conn: conn} do
|
||||
actor = insert(:actor)
|
||||
|
||||
@@ -22,8 +23,8 @@ defmodule EventosWeb.ActivityPubControllerTest do
|
||||
end
|
||||
end
|
||||
|
||||
describe "/events/uuid" do
|
||||
test "it returns a json representation of the object", %{conn: conn} do
|
||||
describe "/events/:uuid" do
|
||||
test "it returns a json representation of the event", %{conn: conn} do
|
||||
event = insert(:event)
|
||||
|
||||
conn =
|
||||
@@ -34,23 +35,60 @@ defmodule EventosWeb.ActivityPubControllerTest do
|
||||
assert json_response(conn, 200) == ObjectView.render("event.json", %{event: event})
|
||||
Logger.error(inspect(ObjectView.render("event.json", %{event: event})))
|
||||
end
|
||||
|
||||
test "it returns 404 for non-public events", %{conn: conn} do
|
||||
event = insert(:event, public: false)
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> put_req_header("accept", "application/activity+json")
|
||||
|> get("/events/#{event.uuid}")
|
||||
|
||||
assert json_response(conn, 404)
|
||||
end
|
||||
end
|
||||
|
||||
# describe "/actors/:username/inbox" do
|
||||
# test "it inserts an incoming activity into the database", %{conn: conn} do
|
||||
# data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
|
||||
#
|
||||
# conn =
|
||||
# conn
|
||||
# |> assign(:valid_signature, true)
|
||||
# |> put_req_header("content-type", "application/activity+json")
|
||||
# |> post("/inbox", data)
|
||||
#
|
||||
# assert "ok" == json_response(conn, 200)
|
||||
# :timer.sleep(500)
|
||||
# assert Activity.get_by_ap_id(data["id"])
|
||||
# end
|
||||
# end
|
||||
describe "/@:preferred_username/inbox" do
|
||||
test "it inserts an incoming event into the database", %{conn: conn} do
|
||||
data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> assign(:valid_signature, true)
|
||||
|> put_req_header("content-type", "application/activity+json")
|
||||
|> post("/inbox", data)
|
||||
|
||||
assert "ok" == json_response(conn, 200)
|
||||
:timer.sleep(500)
|
||||
assert ActivityPub.fetch_object_from_url(data["object"]["id"], :note)
|
||||
end
|
||||
end
|
||||
|
||||
describe "/@:preferred_username/outbox" do
|
||||
test "it returns a note activity in a collection", %{conn: conn} do
|
||||
actor = insert(:actor)
|
||||
comment = insert(:comment, actor: actor)
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> put_req_header("accept", "application/activity+json")
|
||||
|> get("/@#{actor.preferred_username}/outbox")
|
||||
|
||||
assert response(conn, 200) =~ comment.text
|
||||
end
|
||||
|
||||
test "it returns an event activity in a collection", %{conn: conn} do
|
||||
actor = insert(:actor)
|
||||
event = insert(:event, organizer_actor: actor)
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> put_req_header("accept", "application/activity+json")
|
||||
|> get("/@#{actor.preferred_username}/outbox")
|
||||
|
||||
assert response(conn, 200) =~ event.title
|
||||
end
|
||||
end
|
||||
|
||||
# describe "/actors/:nickname/followers" do
|
||||
# test "it returns the followers in a collection", %{conn: conn} do
|
||||
@@ -93,43 +131,43 @@ defmodule EventosWeb.ActivityPubControllerTest do
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# describe "/users/:nickname/following" do
|
||||
# describe "/@:preferred_username/following" do
|
||||
# test "it returns the following in a collection", %{conn: conn} do
|
||||
# user = insert(:user)
|
||||
# user_two = insert(:user)
|
||||
# User.follow(user, user_two)
|
||||
#
|
||||
# actor = insert(:actor)
|
||||
# actor2 = insert(:actor)
|
||||
# Eventos.Service.ActivityPub.follow(actor, actor2)
|
||||
|
||||
# result =
|
||||
# conn
|
||||
# |> get("/users/#{user.nickname}/following")
|
||||
# |> get("/@#{actor.preferred_username}/following")
|
||||
# |> json_response(200)
|
||||
#
|
||||
# assert result["first"]["orderedItems"] == [user_two.ap_id]
|
||||
|
||||
# assert result["first"]["orderedItems"] == [actor2.url]
|
||||
# end
|
||||
#
|
||||
# test "it works for more than 10 users", %{conn: conn} do
|
||||
# user = insert(:user)
|
||||
#
|
||||
|
||||
# test "it works for more than 10 actors", %{conn: conn} do
|
||||
# actor = insert(:actor)
|
||||
|
||||
# Enum.each(1..15, fn _ ->
|
||||
# user = Repo.get(User, user.id)
|
||||
# other_user = insert(:user)
|
||||
# User.follow(user, other_user)
|
||||
# actor = Repo.get(Actor, actor.id)
|
||||
# other_actor = insert(:actor)
|
||||
# Actor.follow(actor, other_actor)
|
||||
# end)
|
||||
#
|
||||
|
||||
# result =
|
||||
# conn
|
||||
# |> get("/users/#{user.nickname}/following")
|
||||
# |> get("/@#{actor.preferred_username}/following")
|
||||
# |> json_response(200)
|
||||
#
|
||||
|
||||
# assert length(result["first"]["orderedItems"]) == 10
|
||||
# assert result["first"]["totalItems"] == 15
|
||||
# assert result["totalItems"] == 15
|
||||
#
|
||||
|
||||
# result =
|
||||
# conn
|
||||
# |> get("/users/#{user.nickname}/following?page=2")
|
||||
# |> get("/@#{actor.preferred_username}/following?page=2")
|
||||
# |> json_response(200)
|
||||
#
|
||||
|
||||
# assert length(result["orderedItems"]) == 5
|
||||
# assert result["totalItems"] == 15
|
||||
# end
|
||||
|
||||
@@ -31,6 +31,64 @@ defmodule EventosWeb.ActorControllerTest do
|
||||
end
|
||||
end
|
||||
|
||||
describe "show actor" do
|
||||
test "show existing actor", %{conn: conn, actor: actor} do
|
||||
actor_id = actor.id
|
||||
conn = get(conn, actor_path(conn, :show, actor.preferred_username))
|
||||
assert %{"data" => %{"id" => actor_id}} = json_response(conn, 200)
|
||||
end
|
||||
|
||||
test "show non-existing actor", %{conn: conn, actor: actor} do
|
||||
actor_id = actor.id
|
||||
conn = get(conn, actor_path(conn, :show, "nonexisting"))
|
||||
assert "" == response(conn, 404)
|
||||
end
|
||||
end
|
||||
|
||||
describe "search for actors" do
|
||||
test "search for existing actors", %{conn: conn, actor: actor} do
|
||||
actor_username = actor.preferred_username
|
||||
conn = get(conn, actor_path(conn, :search, actor_username))
|
||||
assert %{"data" => [%{"username" => actor_username}]} = json_response(conn, 200)
|
||||
end
|
||||
|
||||
test "search for existing actors with similar username", %{conn: conn, actor: actor} do
|
||||
actor_username = actor.preferred_username
|
||||
conn = get(conn, actor_path(conn, :search, "thom"))
|
||||
assert %{"data" => [%{"username" => actor_username}]} = json_response(conn, 200)
|
||||
end
|
||||
|
||||
test "search for nothing", %{conn: conn, actor: actor} do
|
||||
actor_username = actor.preferred_username
|
||||
conn = get(conn, actor_path(conn, :search, "nothing"))
|
||||
assert %{"data" => []} = json_response(conn, 200)
|
||||
end
|
||||
end
|
||||
|
||||
describe "update actor" do
|
||||
test "update actor with valid attrs", %{conn: conn, user: user, actor: actor} do
|
||||
conn = auth_conn(conn, user)
|
||||
|
||||
conn =
|
||||
patch(conn, actor_path(conn, :update, actor.preferred_username), %{
|
||||
"actor" => %{"name" => "glouglou"}
|
||||
})
|
||||
|
||||
assert %{"data" => %{"display_name" => "glouglou"}} = json_response(conn, 200)
|
||||
end
|
||||
|
||||
test "update actor with invalid attrs", %{conn: conn, user: user, actor: actor} do
|
||||
conn = auth_conn(conn, user)
|
||||
|
||||
conn =
|
||||
patch(conn, actor_path(conn, :update, actor.preferred_username), %{
|
||||
"actor" => %{"preferred_username" => nil}
|
||||
})
|
||||
|
||||
assert json_response(conn, 422)["errors"] != %{}
|
||||
end
|
||||
end
|
||||
|
||||
###
|
||||
# Not possible atm
|
||||
###
|
||||
|
||||
@@ -34,7 +34,7 @@ defmodule EventosWeb.AddressControllerTest do
|
||||
floor: nil,
|
||||
postalCode: nil,
|
||||
streetAddress: nil,
|
||||
geom: %{type: nil, data: %{latitude: nil, longitude: nil}}
|
||||
geom: %{type: "oh no", data: %{latitude: nil, longitude: nil}}
|
||||
}
|
||||
|
||||
def fixture(:address) do
|
||||
|
||||
@@ -5,6 +5,7 @@ defmodule EventosWeb.UserControllerTest do
|
||||
|
||||
alias Eventos.Actors
|
||||
alias Eventos.Actors.User
|
||||
use Bamboo.Test
|
||||
|
||||
@create_attrs %{email: "foo@bar.tld", password: "some password_hash", username: "some username"}
|
||||
# @update_attrs %{email: "foo@fighters.tld", password: "some updated password_hash", username: "some updated username"}
|
||||
@@ -18,7 +19,7 @@ defmodule EventosWeb.UserControllerTest do
|
||||
setup %{conn: conn} do
|
||||
user = insert(:user)
|
||||
actor = insert(:actor, user: user)
|
||||
{:ok, conn: conn, user: user}
|
||||
{:ok, conn: conn, user: user, actor: actor}
|
||||
end
|
||||
|
||||
describe "index" do
|
||||
@@ -31,12 +32,14 @@ defmodule EventosWeb.UserControllerTest do
|
||||
|
||||
describe "create user" do
|
||||
test "renders user when data is valid", %{conn: conn} do
|
||||
conn = post(conn, user_path(conn, :create), @create_attrs)
|
||||
conn = post(conn, user_path(conn, :register), @create_attrs)
|
||||
assert %{"email" => "foo@bar.tld"} = json_response(conn, 201)
|
||||
assert {:ok, %User{} = user} = Eventos.Actors.get_user_by_email(@create_attrs.email)
|
||||
assert_delivered_email(Eventos.Email.User.confirmation_email(user))
|
||||
end
|
||||
|
||||
test "renders errors when data is invalid", %{conn: conn} do
|
||||
conn = post(conn, user_path(conn, :create), @invalid_attrs)
|
||||
conn = post(conn, user_path(conn, :register), @invalid_attrs)
|
||||
assert json_response(conn, 422)["errors"] != %{}
|
||||
end
|
||||
|
||||
@@ -47,11 +50,139 @@ defmodule EventosWeb.UserControllerTest do
|
||||
username: "framasoft"
|
||||
}
|
||||
|
||||
conn = post(conn, user_path(conn, :create), attrs)
|
||||
conn = post(conn, user_path(conn, :register), attrs)
|
||||
assert %{"email" => "contact@framasoft.org"} = json_response(conn, 201)
|
||||
end
|
||||
end
|
||||
|
||||
describe "validating user" do
|
||||
test "validate user when token is valid", %{conn: conn} do
|
||||
conn = post(conn, user_path(conn, :create), @create_attrs)
|
||||
assert %{"email" => "foo@bar.tld"} = json_response(conn, 201)
|
||||
assert {:ok, %User{} = user} = Eventos.Actors.get_user_by_email(@create_attrs.email)
|
||||
assert_delivered_email(Eventos.Email.User.confirmation_email(user))
|
||||
|
||||
conn = get(conn, user_path(conn, :validate, user.confirmation_token))
|
||||
assert %{"user" => _, "token" => _} = json_response(conn, 200)
|
||||
end
|
||||
|
||||
test "validate user when token is invalid", %{conn: conn} do
|
||||
conn = post(conn, user_path(conn, :create), @create_attrs)
|
||||
assert %{"email" => "foo@bar.tld"} = json_response(conn, 201)
|
||||
assert {:ok, %User{} = user} = Eventos.Actors.get_user_by_email(@create_attrs.email)
|
||||
assert_delivered_email(Eventos.Email.User.confirmation_email(user))
|
||||
|
||||
conn = get(conn, user_path(conn, :validate, "toto"))
|
||||
assert %{"error" => _} = json_response(conn, 404)
|
||||
end
|
||||
end
|
||||
|
||||
describe "revalidating user" do
|
||||
test "ask to resend token to user when too soon", %{conn: conn} do
|
||||
conn = post(conn, user_path(conn, :create), @create_attrs)
|
||||
assert %{"email" => "foo@bar.tld"} = json_response(conn, 201)
|
||||
assert {:ok, %User{} = user} = Eventos.Actors.get_user_by_email(@create_attrs.email)
|
||||
assert_delivered_email(Eventos.Email.User.confirmation_email(user))
|
||||
|
||||
conn = post(conn, user_path(conn, :resend_confirmation), %{"email" => @create_attrs.email})
|
||||
assert %{"error" => _} = json_response(conn, 404)
|
||||
end
|
||||
|
||||
test "ask to resend token to user when the time is right", %{conn: conn} do
|
||||
conn = post(conn, user_path(conn, :create), @create_attrs)
|
||||
|
||||
assert %{"email" => "foo@bar.tld"} = json_response(conn, 201)
|
||||
assert {:ok, %User{} = user} = Eventos.Actors.get_user_by_email(@create_attrs.email)
|
||||
assert_delivered_email(Eventos.Email.User.confirmation_email(user))
|
||||
|
||||
# Hammer time !
|
||||
{:ok, %User{} = user} =
|
||||
Eventos.Actors.update_user(user, %{
|
||||
confirmation_sent_at: Timex.shift(user.confirmation_sent_at, hours: -3)
|
||||
})
|
||||
|
||||
conn = post(conn, user_path(conn, :resend_confirmation), %{"email" => @create_attrs.email})
|
||||
assert_delivered_email(Eventos.Email.User.confirmation_email(user))
|
||||
assert %{"email" => "foo@bar.tld"} = json_response(conn, 200)
|
||||
end
|
||||
end
|
||||
|
||||
describe "resetting user's password" do
|
||||
test "ask for reset", %{conn: conn, user: user} do
|
||||
user_email = user.email
|
||||
|
||||
# Send reset email
|
||||
conn = post(conn, user_path(conn, :send_reset_password), %{"email" => user_email})
|
||||
assert {:ok, %User{} = user} = Eventos.Actors.get_user_by_email(user.email)
|
||||
assert_delivered_email(Eventos.Email.User.reset_password_email(user))
|
||||
assert %{"email" => user_email} = json_response(conn, 200)
|
||||
|
||||
# Call reset route
|
||||
conn =
|
||||
post(conn, user_path(conn, :reset_password), %{
|
||||
"password" => "new password",
|
||||
"token" => user.reset_password_token
|
||||
})
|
||||
|
||||
user_id = user.id
|
||||
assert %{"user" => %{"id" => user_id}} = json_response(conn, 200)
|
||||
end
|
||||
|
||||
test "ask twice for reset too soon", %{conn: conn, user: user} do
|
||||
user_email = user.email
|
||||
|
||||
# Send reset email
|
||||
conn = post(conn, user_path(conn, :send_reset_password), %{"email" => user.email})
|
||||
assert {:ok, %User{} = user} = Eventos.Actors.get_user_by_email(user.email)
|
||||
assert_delivered_email(Eventos.Email.User.reset_password_email(user))
|
||||
assert %{"email" => user_email} = json_response(conn, 200)
|
||||
|
||||
# Send reset email again
|
||||
conn = post(conn, user_path(conn, :send_reset_password), %{"email" => user.email})
|
||||
|
||||
assert %{"errors" => "You requested a new reset password too early"} =
|
||||
json_response(conn, 404)
|
||||
end
|
||||
|
||||
test "ask twice for reset after a while", %{conn: conn, user: user} do
|
||||
user_email = user.email
|
||||
|
||||
# Send reset email
|
||||
conn = post(conn, user_path(conn, :send_reset_password), %{"email" => user.email})
|
||||
assert {:ok, %User{} = user} = Eventos.Actors.get_user_by_email(user.email)
|
||||
assert_delivered_email(Eventos.Email.User.reset_password_email(user))
|
||||
assert %{"email" => user_email} = json_response(conn, 200)
|
||||
|
||||
# Hammer time !
|
||||
{:ok, %User{} = user} =
|
||||
Eventos.Actors.update_user(user, %{
|
||||
reset_password_sent_at: Timex.shift(user.reset_password_sent_at, hours: -3)
|
||||
})
|
||||
|
||||
# Send reset email again
|
||||
conn = post(conn, user_path(conn, :send_reset_password), %{"email" => user.email})
|
||||
assert {:ok, %User{} = user} = Eventos.Actors.get_user_by_email(user.email)
|
||||
assert_delivered_email(Eventos.Email.User.reset_password_email(user))
|
||||
assert %{"email" => user_email} = json_response(conn, 200)
|
||||
end
|
||||
|
||||
test "ask for reset with wrong address", %{conn: conn} do
|
||||
conn = post(conn, user_path(conn, :send_reset_password), %{"email" => "yolo@coucou"})
|
||||
assert %{"errors" => "Unable to find an user with this email"} = json_response(conn, 404)
|
||||
end
|
||||
|
||||
test "calling reset route with wrong token", %{conn: conn} do
|
||||
conn =
|
||||
post(conn, user_path(conn, :reset_password), %{
|
||||
"password" => "new password",
|
||||
"token" => "just wrong"
|
||||
})
|
||||
|
||||
assert %{"errors" => %{"token" => ["Wrong token for password reset"]}} =
|
||||
json_response(conn, 404)
|
||||
end
|
||||
end
|
||||
|
||||
# describe "update user" do
|
||||
# setup [:create_user]
|
||||
#
|
||||
|
||||
Reference in New Issue
Block a user