1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
require_relative "../test_helper"

require "pro_tacts/proxy_auth"

# What the header says. That a request naming nobody is refused, and how,
# is Web's (see WebTest).
class ProxyAuthTest < Minitest::Test
  def login(value = "alpha@example.com", sent_as: "HTTP_REMOTE_USER")
    env = {} #: Hash[String, String]
    env[sent_as] = value unless value.nil?
    ProTacts::ProxyAuth.login(env)
  end

  def test_the_header_is_the_login
    assert_equal "alpha@example.com", login
  end

  def test_no_header_is_nobody
    assert_raises(ProTacts::ProxyAuth::MissingLogin) { login(nil) }
  end

  def test_an_empty_login_is_nobody
    assert_raises(ProTacts::ProxyAuth::MissingLogin) { login("") }
  end

  def test_a_blank_login_is_nobody
    assert_raises(ProTacts::ProxyAuth::MissingLogin) { login("   ") }
  end

  def test_a_login_on_another_header_is_nobody
    assert_raises(ProTacts::ProxyAuth::MissingLogin) { login(sent_as: "HTTP_TAILSCALE_USER_LOGIN") }
  end

  # The proxy copies the value through, so a non-ASCII one arrives as the
  # UTF-8 it was written in rather than MIME-encoded.
  def test_a_non_ascii_login_is_read_as_utf8
    assert_equal "zoë@example.com", login((+"zoë@example.com").force_encoding(Encoding::BINARY))
  end

  def test_a_login_that_is_not_utf8_is_nobody
    assert_raises(ProTacts::ProxyAuth::MissingLogin) { login((+"zo\xEB").force_encoding(Encoding::BINARY)) }
  end
end