|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/url" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestExtractIssuerAndLabel(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + name string |
| 11 | + rawURL string |
| 12 | + expectIssuer string |
| 13 | + expectLabel string |
| 14 | + }{ |
| 15 | + { |
| 16 | + name: "with issuer param and colon", |
| 17 | + rawURL: "otpauth://totp/Example:alice@example.com?secret=XYZ&issuer=Example", |
| 18 | + expectIssuer: "Example", |
| 19 | + expectLabel: "alice@example.com", |
| 20 | + }, |
| 21 | + { |
| 22 | + name: "with issuer param only", |
| 23 | + rawURL: "otpauth://totp/alice@example.com?secret=XYZ&issuer=OnlyIssuer", |
| 24 | + expectIssuer: "OnlyIssuer", |
| 25 | + expectLabel: "alice@example.com", |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "colon-only fallback", |
| 29 | + rawURL: "otpauth://totp/FallbackCorp:bob?secret=123", |
| 30 | + expectIssuer: "FallbackCorp", |
| 31 | + expectLabel: "bob", |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "no colon or issuer", |
| 35 | + rawURL: "otpauth://totp/bob?secret=123", |
| 36 | + expectIssuer: "bob", |
| 37 | + expectLabel: "bob", |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "url-encoded", |
| 41 | + rawURL: "otpauth://totp/Google%3Aross%40reedstrom.org?secret=ABC&issuer=Google", |
| 42 | + expectIssuer: "Google", |
| 43 | + expectLabel: "ross@reedstrom.org", |
| 44 | + }, |
| 45 | + } |
| 46 | + |
| 47 | + for _, tt := range tests { |
| 48 | + t.Run(tt.name, func(t *testing.T) { |
| 49 | + u, err := url.Parse(tt.rawURL) |
| 50 | + if err != nil { |
| 51 | + t.Fatalf("Invalid URL: %v", err) |
| 52 | + } |
| 53 | + |
| 54 | + gotIssuer, gotLabel := extractIssuerAndLabel(u) |
| 55 | + |
| 56 | + if gotIssuer != tt.expectIssuer { |
| 57 | + t.Errorf("Expected issuer %q, got %q", tt.expectIssuer, gotIssuer) |
| 58 | + } |
| 59 | + if gotLabel != tt.expectLabel { |
| 60 | + t.Errorf("Expected label %q, got %q", tt.expectLabel, gotLabel) |
| 61 | + } |
| 62 | + }) |
| 63 | + } |
| 64 | +} |
0 commit comments