Refactor tests for relay task and refresh cassetes

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2021-04-12 12:01:09 +02:00
parent 5f291336fd
commit ed7b53357f
8 changed files with 187 additions and 273 deletions

View File

@@ -11,6 +11,7 @@ defmodule Mix.Tasks.Mobilizon.ActorsTest do
@username "someone"
@domain "somewhere.tld"
@error "Error: No such actor"
describe "show actor" do
test "show existing local actor" do
@@ -50,7 +51,7 @@ defmodule Mix.Tasks.Mobilizon.ActorsTest do
test "show non-existing actor" do
Show.run([@username])
assert_received {:mix_shell, :error, [message]}
assert message =~ "Error: No such actor"
assert message =~ @error
end
end
end

View File

@@ -3,52 +3,73 @@
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Mix.Tasks.Mobilizon.RelayTest do
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
use Mobilizon.DataCase
alias Mobilizon.Actors
alias Mobilizon.Actors.{Actor, Follower}
alias Mix.Tasks.Mobilizon.Relay.{Follow, Unfollow}
alias Mobilizon.Federation.ActivityPub.Relay
import Mock
Mix.shell(Mix.Shell.Process)
@target_instance "mobilizon1.com"
@output_1 "Requested to follow #{@target_instance}"
@error_1 "Some error"
@error_msg_1 "Error while following #{@target_instance}: \"#{@error_1}\""
@error_msg_1_unfollow "Error while unfollowing #{@target_instance}: \"#{@error_1}\""
@error_msg_2 "mobilizon.relay.follow requires an instance hostname as arguments"
@error_msg_2_unfollow "mobilizon.relay.unfollow requires an instance hostname as arguments"
@output_2 "Unfollowed #{@target_instance}"
describe "running follow" do
test "relay is followed" do
use_cassette "relay/fetch_relay_follow" do
target_instance = "mobilizon1.com"
Follow.run([target_instance])
local_actor = Relay.get_actor()
assert local_actor.url =~ "/relay"
{:ok, target_actor} = Actors.get_actor_by_url("http://#{target_instance}/relay")
refute is_nil(target_actor.domain)
assert Actors.is_following(local_actor, target_actor)
with_mock Relay, [:passthrough], follow: fn @target_instance -> {:ok, nil, nil} end do
Follow.run([@target_instance])
assert_received {:mix_shell, :info, [output_received]}
assert output_received == @output_1
end
end
test "returns an error" do
with_mock Relay, [:passthrough], follow: fn @target_instance -> {:error, @error_1} end do
Follow.run([@target_instance])
assert_received {:mix_shell, :error, [output_received]}
assert output_received == @error_msg_1
end
end
test "without arguments" do
Follow.run([])
assert_received {:mix_shell, :error, [output_received]}
assert output_received == @error_msg_2
end
end
describe "running unfollow" do
test "relay is unfollowed" do
use_cassette "relay/fetch_relay_unfollow" do
target_instance = "mobilizon1.com"
with_mock Relay, [:passthrough], unfollow: fn @target_instance -> {:ok, nil, nil} end do
Unfollow.run([@target_instance])
Follow.run([target_instance])
%Actor{} = local_actor = Relay.get_actor()
{:ok, %Actor{} = target_actor} =
Actors.get_actor_by_url("http://#{target_instance}/relay")
assert %Follower{} = Actors.is_following(local_actor, target_actor)
Unfollow.run([target_instance])
refute Actors.is_following(local_actor, target_actor)
assert_received {:mix_shell, :info, [output_received]}
assert output_received == @output_2
end
end
test "returns an error" do
with_mock Relay, [:passthrough], unfollow: fn @target_instance -> {:error, @error_1} end do
Unfollow.run([@target_instance])
assert_received {:mix_shell, :error, [output_received]}
assert output_received == @error_msg_1_unfollow
end
end
test "without arguments" do
Unfollow.run([])
assert_received {:mix_shell, :error, [output_received]}
assert output_received == @error_msg_2_unfollow
end
end
end