Skip to content

Commit 69131e2

Browse files
ROSAENG-61177 | test: add tests for identity provider commands (#3355)
Add focused tests for IDP command validators and argument validation: - cmd/create/idp/validators_test.go: validateGitlabHostURL (HTTPS, scheme, query params, fragment, invalid), validateGoogleHostedDomain, validateLdapURL (ldap/ldaps schemes, invalid scheme, bare URL), validateOpenidIssuerURL (HTTPS, scheme, query params, fragment, invalid) - cmd/create/idp/htpasswd_test.go: validateHtUsernameAndPassword (valid credentials, colon in username with message check, reserved cluster-admin with message check) - cmd/dlt/idp/cmd_test.go: Args validator (zero, one, two arguments) Skip cmd/list/idp since all logic is in run() with os.Exit and no extracted helpers.
1 parent b3ea520 commit 69131e2

4 files changed

Lines changed: 166 additions & 0 deletions

File tree

cmd/create/idp/htpasswd_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,25 @@ var _ = Describe("IDP Tests", func() {
101101
Expect(err).To(HaveOccurred())
102102
})
103103
})
104+
105+
Describe("validateHtUsernameAndPassword", func() {
106+
It("accepts a valid username and password", func() {
107+
err := validateHtUsernameAndPassword("testuser", "SecureP@ssword123")
108+
Expect(err).NotTo(HaveOccurred())
109+
})
110+
111+
It("rejects a username containing a colon", func() {
112+
err := validateHtUsernameAndPassword("bad:user", "SecureP@ssword123")
113+
Expect(err).To(HaveOccurred())
114+
Expect(err.Error()).To(ContainSubstring("username must not contain"))
115+
})
116+
117+
It("rejects the reserved cluster-admin username", func() {
118+
err := validateHtUsernameAndPassword("cluster-admin", "SecureP@ssword123")
119+
Expect(err).To(HaveOccurred())
120+
Expect(err.Error()).To(ContainSubstring("cluster-admin"))
121+
})
122+
})
104123
})
105124

106125
func CreateTmpFile(content string) (*os.File, error) {

cmd/create/idp/validators_test.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package idp
2+
3+
import (
4+
. "github.com/onsi/ginkgo/v2"
5+
. "github.com/onsi/gomega"
6+
)
7+
8+
var _ = Describe("IDP Validators", func() {
9+
Context("validateGitlabHostURL", func() {
10+
It("accepts a valid HTTPS URL", func() {
11+
err := validateGitlabHostURL("https://gitlab.example.com")
12+
Expect(err).NotTo(HaveOccurred())
13+
})
14+
15+
It("rejects a non-HTTPS URL", func() {
16+
err := validateGitlabHostURL("http://gitlab.example.com")
17+
Expect(err).To(HaveOccurred())
18+
Expect(err.Error()).To(ContainSubstring("https://"))
19+
})
20+
21+
It("rejects a URL with query parameters", func() {
22+
err := validateGitlabHostURL("https://gitlab.example.com?foo=bar")
23+
Expect(err).To(HaveOccurred())
24+
Expect(err.Error()).To(ContainSubstring("query parameters"))
25+
})
26+
27+
It("rejects an invalid URL", func() {
28+
err := validateGitlabHostURL("not-a-url")
29+
Expect(err).To(HaveOccurred())
30+
Expect(err.Error()).To(ContainSubstring("valid GitLab provider URL"))
31+
})
32+
33+
It("rejects a URL with a fragment", func() {
34+
err := validateGitlabHostURL("https://gitlab.example.com#section")
35+
Expect(err).To(HaveOccurred())
36+
Expect(err.Error()).To(ContainSubstring("valid GitLab provider URL"))
37+
})
38+
})
39+
40+
Context("validateGoogleHostedDomain", func() {
41+
It("accepts a valid domain", func() {
42+
err := validateGoogleHostedDomain("example.com")
43+
Expect(err).NotTo(HaveOccurred())
44+
})
45+
46+
It("rejects an invalid domain", func() {
47+
err := validateGoogleHostedDomain("not a domain")
48+
Expect(err).To(HaveOccurred())
49+
Expect(err.Error()).To(ContainSubstring("not valid"))
50+
})
51+
})
52+
53+
Context("validateLdapURL", func() {
54+
It("accepts an ldap:// URL", func() {
55+
err := validateLdapURL("ldap://ldap.example.com/ou=users,dc=example,dc=com?uid")
56+
Expect(err).NotTo(HaveOccurred())
57+
})
58+
59+
It("accepts an ldaps:// URL", func() {
60+
err := validateLdapURL("ldaps://ldap.example.com/ou=users,dc=example,dc=com?uid")
61+
Expect(err).NotTo(HaveOccurred())
62+
})
63+
64+
It("rejects a non-LDAP scheme", func() {
65+
err := validateLdapURL("https://ldap.example.com")
66+
Expect(err).To(HaveOccurred())
67+
Expect(err.Error()).To(ContainSubstring("ldap://"))
68+
})
69+
70+
It("rejects a bare URL with no scheme", func() {
71+
err := validateLdapURL("ldap.com")
72+
Expect(err).To(HaveOccurred())
73+
Expect(err.Error()).To(ContainSubstring("expected a valid LDAP URL"))
74+
})
75+
})
76+
77+
Context("validateOpenidIssuerURL", func() {
78+
It("accepts a valid HTTPS URL", func() {
79+
err := validateOpenidIssuerURL("https://accounts.google.com")
80+
Expect(err).NotTo(HaveOccurred())
81+
})
82+
83+
It("rejects a non-HTTPS URL", func() {
84+
err := validateOpenidIssuerURL("http://accounts.google.com")
85+
Expect(err).To(HaveOccurred())
86+
Expect(err.Error()).To(ContainSubstring("https://"))
87+
})
88+
89+
It("rejects a URL with query parameters", func() {
90+
err := validateOpenidIssuerURL("https://accounts.google.com?foo=bar")
91+
Expect(err).To(HaveOccurred())
92+
Expect(err.Error()).To(ContainSubstring("query parameters"))
93+
})
94+
95+
It("rejects an invalid URL", func() {
96+
err := validateOpenidIssuerURL("not-a-url")
97+
Expect(err).To(HaveOccurred())
98+
Expect(err.Error()).To(ContainSubstring("valid OpenID issuer URL"))
99+
})
100+
101+
It("rejects a URL with a fragment", func() {
102+
err := validateOpenidIssuerURL("https://accounts.google.com#section")
103+
Expect(err).To(HaveOccurred())
104+
Expect(err.Error()).To(ContainSubstring("valid OpenID issuer URL"))
105+
})
106+
})
107+
})

cmd/dlt/idp/cmd_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package idp
2+
3+
import (
4+
. "github.com/onsi/ginkgo/v2"
5+
. "github.com/onsi/gomega"
6+
)
7+
8+
var _ = Describe("Delete IDP", func() {
9+
Context("Args validator", func() {
10+
It("rejects zero arguments", func() {
11+
err := Cmd.Args(nil, []string{})
12+
Expect(err).To(HaveOccurred())
13+
Expect(err.Error()).To(ContainSubstring("Expected exactly one"))
14+
})
15+
16+
It("accepts exactly one argument", func() {
17+
err := Cmd.Args(nil, []string{"github-1"})
18+
Expect(err).NotTo(HaveOccurred())
19+
})
20+
21+
It("rejects two arguments", func() {
22+
err := Cmd.Args(nil, []string{"github-1", "extra"})
23+
Expect(err).To(HaveOccurred())
24+
Expect(err.Error()).To(ContainSubstring("Expected exactly one"))
25+
})
26+
})
27+
})

cmd/dlt/idp/idp_suite_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package idp
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestDeleteIdp(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Delete IDP Suite")
13+
}

0 commit comments

Comments
 (0)