X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=test%2Fsignature_test.exs;h=f3bba13786ed6ef16bb611f6477caa8795cacbf1;hb=e313aa0977fc6023067d6580b36a7dc71a4d1b5a;hp=840987cd6bacc46b0f4ced6fbe6bf2ee6454a52a;hpb=60b54ee64a9130dd4a3245b90ab49943a9e881c6;p=akkoma diff --git a/test/signature_test.exs b/test/signature_test.exs index 840987cd6..f3bba1378 100644 --- a/test/signature_test.exs +++ b/test/signature_test.exs @@ -1,12 +1,14 @@ # Pleroma: A lightweight social networking server -# Copyright © 2017-2019 Pleroma Authors +# Copyright © 2017-2020 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.SignatureTest do use Pleroma.DataCase + import ExUnit.CaptureLog import Pleroma.Factory import Tesla.Mock + import Mock alias Pleroma.Signature @@ -40,21 +42,22 @@ defmodule Pleroma.SignatureTest do test "it returns key" do expected_result = {:ok, @rsa_public_key} - user = insert(:user, %{info: %{source_data: %{"publicKey" => @public_key}}}) + user = insert(:user, source_data: %{"publicKey" => @public_key}) assert Signature.fetch_public_key(make_fake_conn(user.ap_id)) == expected_result end test "it returns error when not found user" do - assert Signature.fetch_public_key(make_fake_conn("test-ap_id")) == - {:error, :error} + assert capture_log(fn -> + assert Signature.fetch_public_key(make_fake_conn("https://test-ap-id")) == + {:error, :error} + end) =~ "[error] Could not decode user" end test "it returns error if public key is empty" do - user = insert(:user, %{info: %{source_data: %{"publicKey" => %{}}}}) + user = insert(:user, source_data: %{"publicKey" => %{}}) - assert Signature.fetch_public_key(make_fake_conn(user.ap_id)) == - {:error, :error} + assert Signature.fetch_public_key(make_fake_conn(user.ap_id)) == {:error, :error} end end @@ -62,13 +65,13 @@ defmodule Pleroma.SignatureTest do test "it returns key" do ap_id = "https://mastodon.social/users/lambadalambda" - assert Signature.refetch_public_key(make_fake_conn(ap_id)) == - {:ok, @rsa_public_key} + assert Signature.refetch_public_key(make_fake_conn(ap_id)) == {:ok, @rsa_public_key} end test "it returns error when not found user" do - assert Signature.refetch_public_key(make_fake_conn("test-ap_id")) == - {:error, {:error, :ok}} + assert capture_log(fn -> + {:error, _} = Signature.refetch_public_key(make_fake_conn("https://test-ap_id")) + end) =~ "[error] Could not decode user" end end @@ -77,7 +80,7 @@ defmodule Pleroma.SignatureTest do user = insert(:user, %{ ap_id: "https://mastodon.social/users/lambadalambda", - info: %{keys: @private_key} + keys: @private_key }) assert Signature.sign( @@ -91,8 +94,7 @@ defmodule Pleroma.SignatureTest do end test "it returns error" do - user = - insert(:user, %{ap_id: "https://mastodon.social/users/lambadalambda", info: %{keys: ""}}) + user = insert(:user, %{ap_id: "https://mastodon.social/users/lambadalambda", keys: ""}) assert Signature.sign( user, @@ -100,4 +102,38 @@ defmodule Pleroma.SignatureTest do ) == {:error, []} end end + + describe "key_id_to_actor_id/1" do + test "it properly deduces the actor id for misskey" do + assert Signature.key_id_to_actor_id("https://example.com/users/1234/publickey") == + {:ok, "https://example.com/users/1234"} + end + + test "it properly deduces the actor id for mastodon and pleroma" do + assert Signature.key_id_to_actor_id("https://example.com/users/1234#main-key") == + {:ok, "https://example.com/users/1234"} + end + + test "it calls webfinger for 'acct:' accounts" do + with_mock(Pleroma.Web.WebFinger, + finger: fn _ -> %{"ap_id" => "https://gensokyo.2hu/users/raymoo"} end + ) do + assert Signature.key_id_to_actor_id("acct:raymoo@gensokyo.2hu") == + {:ok, "https://gensokyo.2hu/users/raymoo"} + end + end + end + + describe "signed_date" do + test "it returns formatted current date" do + with_mock(NaiveDateTime, utc_now: fn -> ~N[2019-08-23 18:11:24.822233] end) do + assert Signature.signed_date() == "Fri, 23 Aug 2019 18:11:24 GMT" + end + end + + test "it returns formatted date" do + assert Signature.signed_date(~N[2019-08-23 08:11:24.822233]) == + "Fri, 23 Aug 2019 08:11:24 GMT" + end + end end