|
| 1 | +defmodule Plausible.Auth.SSO.Domain.ValidationTest do |
| 2 | + use Plausible.DataCase, async: true |
| 3 | + use Plausible |
| 4 | + |
| 5 | + @moduletag :ee_only |
| 6 | + |
| 7 | + on_ee do |
| 8 | + use Plausible.Teams.Test |
| 9 | + |
| 10 | + alias Plasusible.Test.Support.DNSServer |
| 11 | + alias Plausible.Auth.SSO.Domain.Validation |
| 12 | + alias Plug.Conn |
| 13 | + |
| 14 | + setup do |
| 15 | + team = new_site().team |
| 16 | + bypass = Bypass.open() |
| 17 | + |
| 18 | + {:ok, team: team, bypass: bypass} |
| 19 | + end |
| 20 | + |
| 21 | + describe "individual checks" do |
| 22 | + test "dns_txt" do |
| 23 | + {:ok, port} = DNSServer.start("plausible-sso-verification=ex4mpl3") |
| 24 | + |
| 25 | + refute Validation.dns_txt("example.com", "failing-identifier", |
| 26 | + nameservers: [{{0, 0, 0, 0}, port}] |
| 27 | + ) |
| 28 | + |
| 29 | + assert Validation.dns_txt("example.com", "ex4mpl3", nameservers: [{{0, 0, 0, 0}, port}]) |
| 30 | + end |
| 31 | + |
| 32 | + test "url", %{bypass: bypass} do |
| 33 | + Bypass.expect(bypass, "GET", "/test", fn conn -> |
| 34 | + Conn.resp(conn, 200, "ex4mpl3") |
| 35 | + end) |
| 36 | + |
| 37 | + refute Validation.url("example.com", "failing-identifier", |
| 38 | + url_override: "http://localhost:#{bypass.port}/test" |
| 39 | + ) |
| 40 | + |
| 41 | + assert Validation.url("example.com", "ex4mpl3", |
| 42 | + url_override: "http://localhost:#{bypass.port}/test" |
| 43 | + ) |
| 44 | + end |
| 45 | + |
| 46 | + test "meta_tag", %{bypass: bypass} do |
| 47 | + Bypass.expect(bypass, "GET", "/test", fn conn -> |
| 48 | + conn |
| 49 | + |> Conn.put_resp_header("content-type", "text/html") |
| 50 | + |> Conn.resp(200, """ |
| 51 | + <html> |
| 52 | + <meta name="plausible-sso-verification" content="ex4mpl3"/> |
| 53 | + </html> |
| 54 | + """) |
| 55 | + end) |
| 56 | + |
| 57 | + refute Validation.meta_tag("example.com", "failing-identifier", |
| 58 | + url_override: "http://localhost:#{bypass.port}/test" |
| 59 | + ) |
| 60 | + |
| 61 | + assert Validation.meta_tag("example.com", "ex4mpl3", |
| 62 | + url_override: "http://localhost:#{bypass.port}/test" |
| 63 | + ) |
| 64 | + end |
| 65 | + |
| 66 | + test "meta-tag fails on non-html", %{bypass: bypass} do |
| 67 | + Bypass.expect_once(bypass, "GET", "/test", fn conn -> |
| 68 | + Conn.resp(conn, 200, """ |
| 69 | + <html> |
| 70 | + <meta name="plausible-sso-verification" content="ex4mpl3"/> |
| 71 | + </html> |
| 72 | + """) |
| 73 | + end) |
| 74 | + |
| 75 | + refute Validation.meta_tag("example.com", "ex4mpl3", |
| 76 | + url_override: "http://localhost:#{bypass.port}/test" |
| 77 | + ) |
| 78 | + end |
| 79 | + |
| 80 | + test "meta-tag fails on parse failure", %{bypass: bypass} do |
| 81 | + Bypass.expect_once(bypass, "GET", "/test", fn conn -> |
| 82 | + conn |
| 83 | + |> Conn.put_resp_header("content-type", "text/html") |
| 84 | + |> Conn.resp(200, """ |
| 85 | + meta name="plausible-sso-verification" content="ex4mpl3 |
| 86 | + """) |
| 87 | + end) |
| 88 | + |
| 89 | + refute Validation.meta_tag("example.com", "ex4mpl3", |
| 90 | + url_override: "http://localhost:#{bypass.port}/test" |
| 91 | + ) |
| 92 | + end |
| 93 | + |
| 94 | + test "meta_tag succeeds in case of multiple matches", %{bypass: bypass} do |
| 95 | + Bypass.expect(bypass, "GET", "/test", fn conn -> |
| 96 | + conn |
| 97 | + |> Conn.put_resp_header("content-type", "text/html") |
| 98 | + |> Conn.resp(200, """ |
| 99 | + <html> |
| 100 | + <meta name="plausible-sso-verification" content="ex4mpl3"/> |
| 101 | + <meta name="plausible-sso-verification" content="ex4mpl3"/> |
| 102 | + </html> |
| 103 | + """) |
| 104 | + end) |
| 105 | + |
| 106 | + assert Validation.meta_tag("example.com", "ex4mpl3", |
| 107 | + url_override: "http://localhost:#{bypass.port}/test" |
| 108 | + ) |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + describe "all methods" do |
| 113 | + test "DNS matches, no HTTP endpoint is ever called", %{bypass: bypass} do |
| 114 | + {:ok, dns_port} = DNSServer.start("plausible-sso-verification=ex4mpl3") |
| 115 | + |
| 116 | + Bypass.stub(bypass, "GET", "/", fn _conn -> raise "should never be called" end) |
| 117 | + |
| 118 | + assert {:ok, :dns_txt} = |
| 119 | + Validation.run("example.com", "ex4mpl3", |
| 120 | + url_override: "http://localhost:#{bypass.port}/", |
| 121 | + nameservers: [{{0, 0, 0, 0}, dns_port}] |
| 122 | + ) |
| 123 | + end |
| 124 | + |
| 125 | + test "DNS fails to match, url check succeeds", %{bypass: bypass} do |
| 126 | + Bypass.expect_once(bypass, "GET", "/", fn conn -> |
| 127 | + Conn.resp(conn, 200, "ex4mpl3") |
| 128 | + end) |
| 129 | + |
| 130 | + assert {:ok, :url} = |
| 131 | + Validation.run("example.com", "ex4mpl3", |
| 132 | + url_override: "http://localhost:#{bypass.port}/" |
| 133 | + ) |
| 134 | + end |
| 135 | + |
| 136 | + test "DNS and url checks fail to match, meta tag check succeeds", %{bypass: bypass} do |
| 137 | + Bypass.expect(bypass, "GET", "/", fn conn -> |
| 138 | + conn |
| 139 | + |> Conn.put_resp_header("content-type", "text/html") |
| 140 | + |> Conn.resp( |
| 141 | + 200, |
| 142 | + "<html><meta name=\"plausible-sso-verification\" content=\"ex4mpl3\"/></html>" |
| 143 | + ) |
| 144 | + end) |
| 145 | + |
| 146 | + assert {:ok, :meta_tag} = |
| 147 | + Validation.run("example.com", "ex4mpl3", |
| 148 | + url_override: "http://localhost:#{bypass.port}/" |
| 149 | + ) |
| 150 | + end |
| 151 | + end |
| 152 | + end |
| 153 | +end |
0 commit comments