|
| 1 | +defmodule OTPAuthTest do |
| 2 | + use ExUnit.Case, async: true |
| 3 | + doctest OTPAuth |
| 4 | + |
| 5 | + describe "decompose_uri" do |
| 6 | + test "supports the absence of issuer" do |
| 7 | + uri = "otpauth://totp/alice?secret=MFRGGZA" |
| 8 | + assert {:ok, "abcd", "alice", %{}} == OTPAuth.decompose_uri(uri) |
| 9 | + end |
| 10 | + |
| 11 | + test "extracts the issuer from the prefix" do |
| 12 | + uri = "otpauth://totp/Acme:alice?secret=MFRGGZA" |
| 13 | + |
| 14 | + assert {:ok, "abcd", "alice", %{"issuer" => "Acme"}} == |
| 15 | + OTPAuth.decompose_uri(uri) |
| 16 | + end |
| 17 | + |
| 18 | + test "extracts the issuer from the URI params" do |
| 19 | + uri = "otpauth://totp/alice?secret=MFRGGZA&issuer=Acme" |
| 20 | + |
| 21 | + assert {:ok, "abcd", "alice", %{"issuer" => "Acme"}} == |
| 22 | + OTPAuth.decompose_uri(uri) |
| 23 | + end |
| 24 | + |
| 25 | + test "accepts identical issuers in prefix and URI params" do |
| 26 | + uri = "otpauth://totp/Acme:alice?secret=MFRGGZA&issuer=Acme" |
| 27 | + |
| 28 | + assert {:ok, "abcd", "alice", %{"issuer" => "Acme"}} == |
| 29 | + OTPAuth.decompose_uri(uri) |
| 30 | + end |
| 31 | + |
| 32 | + test "preserves extra URI params" do |
| 33 | + uri = "otpauth://totp/Acme:alice?secret=MFRGGZA&issuer=Acme&extra=1" |
| 34 | + |
| 35 | + {:ok, "abcd", "alice", params} = OTPAuth.decompose_uri(uri) |
| 36 | + assert [{"extra", "1"}, {"issuer", "Acme"}] == Enum.sort(params) |
| 37 | + end |
| 38 | + |
| 39 | + test "rejects an empty label with no prefix issuer" do |
| 40 | + uri = "otpauth://totp/?secret=MFRGGZA" |
| 41 | + |
| 42 | + assert :error == OTPAuth.decompose_uri(uri) |
| 43 | + end |
| 44 | + |
| 45 | + test "rejects an empty label with a prefix issuer" do |
| 46 | + uri = "otpauth://totp/Acme:?secret=MFRGGZA" |
| 47 | + |
| 48 | + assert :error == OTPAuth.decompose_uri(uri) |
| 49 | + end |
| 50 | + |
| 51 | + test "rejects an empty prefix issuer" do |
| 52 | + uri = "otpauth://totp/:alice?secret=MFRGGZA" |
| 53 | + |
| 54 | + assert :error == OTPAuth.decompose_uri(uri) |
| 55 | + end |
| 56 | + |
| 57 | + test "rejects an empty issuer from URI params" do |
| 58 | + uri = "otpauth://totp/alice?secret=MFRGGZA&issuer=" |
| 59 | + |
| 60 | + assert :error == OTPAuth.decompose_uri(uri) |
| 61 | + end |
| 62 | + |
| 63 | + test "rejects different issuers in prefix and URI params" do |
| 64 | + uri = "otpauth://totp/Acme:alice?secret=MFRGGZA&issuer=Corp" |
| 65 | + |
| 66 | + assert :error == OTPAuth.decompose_uri(uri) |
| 67 | + end |
| 68 | + |
| 69 | + test "rejects URI with wrong scheme or host" do |
| 70 | + uri = "otpauth://hotp/Acme:alice?secret=MFRGGZA&issuer=Corp" |
| 71 | + |
| 72 | + assert :error == OTPAuth.decompose_uri(uri) |
| 73 | + end |
| 74 | + |
| 75 | + test "rejects URI if issuer contains ':'" do |
| 76 | + uri = "otpauth://hotp/alice?secret=MFRGGZA&issuer=Acme:Corp" |
| 77 | + |
| 78 | + assert :error == OTPAuth.decompose_uri(uri) |
| 79 | + end |
| 80 | + |
| 81 | + test "rejects URI if label contains ':'" do |
| 82 | + uri = "otpauth://hotp/Acme:Corp:alice?secret=MFRGGZA" |
| 83 | + |
| 84 | + assert :error == OTPAuth.decompose_uri(uri) |
| 85 | + end |
| 86 | + |
| 87 | + test "decode an URI containing encoded label" do |
| 88 | + uri = |
| 89 | + "otpauth://totp/T%C3%A9l%C3%A9com%20Paris:user?secret=MFRGGZA&issuer=T%C3%A9l%C3%A9com%20Paris" |
| 90 | + |
| 91 | + assert {:ok, "abcd", "user", %{"issuer" => "Télécom Paris"}} == |
| 92 | + OTPAuth.decompose_uri(uri) |
| 93 | + end |
| 94 | + end |
| 95 | +end |
0 commit comments