Implement a custom uri_equal?/2 to fix comparisons of URLs with unordered query parameters

This commit is contained in:
Mark Felder 2023-11-28 18:54:50 +00:00
parent e7d6b835ae
commit e121e06214
2 changed files with 21 additions and 1 deletions

View File

@ -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

View File

@ -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