# SPDX-FileCopyrightText: 2024 Ɓukasz Niemier <#@hauleth.dev> # # SPDX-License-Identifier: MPL-2.0 defmodule AwwTest do use ExUnit.Case, async: true use ExUnitProperties @subject Aww doctest @subject test "by default there is no query" do url = @subject.avatar_url("foo@example.com") assert "" == url.query end test "by default HTTPS is used" do url = @subject.avatar_url("foo@example.com") assert "https" == url.scheme end test "can opt out to insecure HTTP query" do url = @subject.avatar_url("foo@example.com", service_opts: [secure?: false]) assert "http" == url.scheme end test "default can be changed" do url = @subject.avatar_url("foo@example.com", default: "monster") assert url.query =~ "d=monster" end test "size can be defined" do url = @subject.avatar_url("foo@example.com", size: 2137) assert url.query =~ "s=2137" end test "enforce default" do url = @subject.avatar_url("foo@example.com", forcedefault?: true) assert url.query =~ "f=y" end test "select rating" do url = @subject.avatar_url("foo@example.com", rating: "R") assert url.query =~ "r=R" end property "email is case insensitive" do check all( local_part <- string(:ascii, min_length: 1), domain <- string(:ascii, min_length: 1) ) do input = "#{local_part}@#{domain}" downcased = String.downcase(input) assert @subject.avatar_url(input) == @subject.avatar_url(downcased) end end property "custom host contains that host" do check all(host <- string(:ascii, min_length: 1)) do url = @subject.avatar_url("foo@example.com", service_opts: [host: host]) assert host == url.host end end property "custom host as URI contains that host" do check all(host <- string(:ascii, min_length: 1), port <- integer(0..0xFFFF)) do uri = %URI{ host: host, port: port } url = @subject.avatar_url("foo@example.com", service_opts: [host: uri]) assert host == url.host assert port == url.port end end defp domain do part = string([?a..?z, ?A..?Z, ?0..?9, ?-], min_length: 1, max_length: 10) gen( all( parts <- list_of(part, min_length: 1, max_length: 4), do: Enum.join(parts, ".") ) ) end defp open_id_uri do gen all( scheme <- one_of([constant("http"), constant("https")]), host <- domain(), port <- integer(0..0xFFFF), segment = string(:alphanumeric, min_length: 1), path <- list_of(segment), user <- string(:alphanumeric, min_length: 1), pass <- string(:alphanumeric, min_length: 1) ) do %URI{ scheme: scheme, host: host, port: port, path: "/" <> Enum.join(path, "/"), userinfo: "#{user}:#{pass}" } end end property "normalizes OpenID addresses" do check all(url <- open_id_uri()) do input = URI.to_string(url) downcased = URI.to_string(%URI{ url | host: String.downcase(url.host) }) assert @subject.avatar_url(input) == @subject.avatar_url(downcased) end end describe "function as a host provider" do def host_func(host, _opts), do: "test.#{host}" test "passed as MFA" do url = @subject.avatar_url("hello@example.com", service_opts: [host: {__MODULE__, :host_func, []}] ) assert url.host == "test.example.com" end test "passed as capture" do url = @subject.avatar_url("hello@example.com", service_opts: [host: &host_func/2] ) assert url.host == "test.example.com" end end end