From e121e0621467ec6ce87b11f146656ba655feda56 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Tue, 28 Nov 2023 18:54:50 +0000 Subject: [PATCH] Implement a custom uri_equal?/2 to fix comparisons of URLs with unordered query parameters --- test/pleroma/mfa/totp_test.exs | 6 +++++- test/support/helpers.ex | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/test/pleroma/mfa/totp_test.exs b/test/pleroma/mfa/totp_test.exs index 56e4f48ed..f291ed14b 100644 --- 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 index e3bfa73d2..ee18753ed 100644 --- 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