Skip to content

Commit ca3165f

Browse files
committed
add missing label tests!
1 parent a467c42 commit ca3165f

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

cmd/auth/label_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

Comments
 (0)