@@ -0,0 +1,96 @@
1+package basic
2+
3+import (
4+ . "github.com/onsi/ginkgo"
5+ . "github.com/onsi/gomega"
6+)
7+
8+const (
9+ adminUser = "admin"
10+ adminPassword = "Adm1n1str$t0r"
11+ user1 = "user1"
12+ user1Password = "UsErOn3P455"
13+ user2 = "user2"
14+ user2Password = "us3r2P455W0Rd!"
15+)
16+
17+var _ = Describe("HTPasswd Suite", func() {
18+ Context("with an HTPassword Validator", func() {
19+ assertHtpasswdMapFromFile := func(filePath string) {
20+ var htpasswd *htpasswdMap
21+ var err error
22+
23+ BeforeEach(func() {
24+ var validator Validator
25+ validator, err = NewHTPasswdValidator(filePath)
26+
27+ var ok bool
28+ htpasswd, ok = validator.(*htpasswdMap)
29+ Expect(ok).To(BeTrue())
30+ })
31+
32+ It("does not return an error", func() {
33+ Expect(err).ToNot(HaveOccurred())
34+ })
35+
36+ It("has the correct number of users", func() {
37+ Expect(htpasswd.users).To(HaveLen(3))
38+ })
39+
40+ It("accepts the correct passwords", func() {
41+ Expect(htpasswd.Validate(adminUser, adminPassword)).To(BeTrue())
42+ Expect(htpasswd.Validate(user1, user1Password)).To(BeTrue())
43+ Expect(htpasswd.Validate(user2, user2Password)).To(BeTrue())
44+ })
45+
46+ It("rejects incorrect passwords", func() {
47+ Expect(htpasswd.Validate(adminUser, "asvdfda")).To(BeFalse())
48+ Expect(htpasswd.Validate(user1, "BHEdgbtr")).To(BeFalse())
49+ Expect(htpasswd.Validate(user2, "12345")).To(BeFalse())
50+ })
51+
52+ It("rejects a non existent user", func() {
53+ // Users are case sensitive
54+ Expect(htpasswd.Validate("ADMIN", adminPassword)).To(BeFalse())
55+ })
56+ }
57+
58+ Context("load from file", func() {
59+ Context("with sha1 entries", func() {
60+ const filePath = "./test/htpasswd-sha1.txt"
61+
62+ assertHtpasswdMapFromFile(filePath)
63+ })
64+
65+ Context("with bcrypt entries", func() {
66+ const filePath = "./test/htpasswd-bcrypt.txt"
67+
68+ assertHtpasswdMapFromFile(filePath)
69+ })
70+
71+ Context("with mixed entries", func() {
72+ const filePath = "./test/htpasswd-mixed.txt"
73+
74+ assertHtpasswdMapFromFile(filePath)
75+ })
76+
77+ Context("with a non existent file", func() {
78+ const filePath = "./test/htpasswd-doesnt-exist.txt"
79+ var validator Validator
80+ var err error
81+
82+ BeforeEach(func() {
83+ validator, err = NewHTPasswdValidator(filePath)
84+ })
85+
86+ It("returns an error", func() {
87+ Expect(err).To(MatchError("could not open htpasswd file: open ./test/htpasswd-doesnt-exist.txt: no such file or directory"))
88+ })
89+
90+ It("returns a nil validator", func() {
91+ Expect(validator).To(BeNil())
92+ })
93+ })
94+ })
95+ })
96+})