commit: e121e0621467ec6ce87b11f146656ba655feda56
parent e7d6b835ae908be8e9f6a303a77c87860fb7a377
Author: Mark Felder <feld@feld.me>
Date: Tue, 28 Nov 2023 18:54:50 +0000
Implement a custom uri_equal?/2 to fix comparisons of URLs with unordered query parameters
Diffstat:
2 files changed, 21 insertions(+), 1 deletion(-)
diff --git a/test/pleroma/mfa/totp_test.exs b/test/pleroma/mfa/totp_test.exs
@@ -7,6 +7,8 @@ defmodule Pleroma.MFA.TOTPTest do
alias Pleroma.MFA.TOTP
+ import Pleroma.Tests.Helpers, only: [uri_equal?: 2]
+
test "create provisioning_uri to generate qrcode" do
uri =
TOTP.provisioning_uri("test-secrcet", "test@example.com",
@@ -15,7 +17,9 @@ defmodule Pleroma.MFA.TOTPTest do
period: 60
)
- assert uri ==
+ assert uri_equal?(
+ uri,
"otpauth://totp/test@example.com?digits=8&issuer=Plerome-42&period=60&secret=test-secrcet"
+ )
end
end
diff --git a/test/support/helpers.ex b/test/support/helpers.ex
@@ -10,6 +10,22 @@ defmodule Pleroma.Tests.Helpers do
require Logger
+ @doc "Accepts two URLs/URIs and sorts the query parameters before comparing"
+ def uri_equal?(a, b) do
+ a_parsed = URI.parse(a)
+ b_parsed = URI.parse(b)
+
+ query_sort = fn query -> String.split(query, "&") |> Enum.sort() |> Enum.join("&") end
+
+ a_sorted_query = query_sort.(a_parsed.query)
+ b_sorted_query = query_sort.(b_parsed.query)
+
+ a_sorted = Map.put(a_parsed, :query, a_sorted_query)
+ b_sorted = Map.put(b_parsed, :query, b_sorted_query)
+
+ match?(^a_sorted, b_sorted)
+ end
+
defmacro clear_config(config_path) do
quote do
clear_config(unquote(config_path)) do