Skip to content

Commit

Permalink
Add tests to verify password functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
sharms authored and jmcarp committed Mar 7, 2018
1 parent 6f02b5c commit 8d5f029
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
26 changes: 18 additions & 8 deletions password.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"crypto/rand"
"errors"
)

const (
Expand All @@ -14,25 +15,34 @@ const (

type PasswordGenerator func(int) string

type password struct {
Password string
Upper int
Lower int
Number int
Special int
}

func GenerateSecurePassword(n int) string {
for {
p, err := generatePassword(n)
if err != nil {
continue
}
if p.Password[0] != '-' &&
(p.Upper > 0 || p.Lower > 0 || p.Number > 0 || p.Special > 0) {

err = ValidatePassword(p)
if err == nil {
return p.Password
}
}
}

type password struct {
Password string
Upper int
Lower int
Number int
Special int
func ValidatePassword(p password) error {
if p.Password[0] != '-' &&
(p.Upper > 0 || p.Lower > 0 || p.Number > 0 || p.Special > 0) {
return nil
}
return errors.New("Invalid password")
}

func (p *password) AddChar(idx int) {
Expand Down
31 changes: 31 additions & 0 deletions password_test.go
@@ -0,0 +1,31 @@
package main

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("password", func() {
var (
badPassword password
goodPassword password
)

BeforeEach(func() {
badPassword = password{Password: "-badpassword", Upper: 0, Lower: 11, Number: 0, Special: 1}
goodPassword = password{Password: "goodpassword", Upper: 0, Lower: 12, Number: 0, Special: 0}
})

Describe("Password generation", func() {
Context("With a leading dash", func() {
It("should generate a new password", func() {
Expect(ValidatePassword(badPassword)).To(MatchError("Invalid password"))
})
})
Context("Without a leading dash", func() {
It("should accept the password", func() {
Expect(ValidatePassword(goodPassword)).To(BeNil())
})
})
})
})

0 comments on commit 8d5f029

Please sign in to comment.