Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Commit

Permalink
feat: noRepeted validation strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
brandaogabriel7 committed Dec 19, 2022
1 parent 3151c07 commit f465bc7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,17 @@ A expressão regular é: `\d`

### MinSpecialChars

A lógica do `minSpecialChars` é a mesma do minDigit, mas a expressão regular é: `[!@#$%^&*()\-+\\\/{}\[\]]`
A lógica do `minSpecialChars` é a mesma do minDigit, mas a expressão regular é: `[!@#$%^&*()\-+\\\/{}\[\]]`

### NoRepeted

A regra `noRepeted` foge mais da lógica das outras regras. A solução que eu pensei foi a seguinte, comprimir todos os caracteres repetidos consecutivos da senha em um só e comparar o tamanho da senha comprimida com a senha original.

Se a senha comprimida e a senha original forem iguais, a regra passou. Se elas forem diferentes, a regra não passou.

Exemplos:
- Sucesso: A senha *"abacate123"*, depois de comprimida, continua *"abacate123"*.

- Falha: A senha *"Opaaa73"*, depois de comprimida, vira *"Opa73"* (diferente da original).


17 changes: 17 additions & 0 deletions src/strategies/validation/no_repeted.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package validation

import "strings"

type NoRepetedStrategy struct {}

func (nr *NoRepetedStrategy) IsValid(password string, _ int) bool {
compressedPasswordSb := &strings.Builder{}
var previous rune
for _, r := range password {
if r != previous {
compressedPasswordSb.WriteRune(r)
previous = r
}
}
return compressedPasswordSb.String() == password
}
34 changes: 34 additions & 0 deletions src/strategies/validation/no_repeted_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package validation_test

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

"github.com/brandaogabriel7/studio-sol-back-end-test/src/strategies/validation"
)

var _ = Describe("NoRepeted", func() {
noRepetedStrategy := validation.NoRepetedStrategy{}

DescribeTable("When password has no consecutive repeated character",
func (password string) {
isValid := noRepetedStrategy.IsValid(password, 0)

Expect(isValid).To(BeTrue())
},
Entry("abacate123", "abacate123"),
Entry("SenhaForte!", "SenhaForte!"),
Entry("aopa", "aopa"),
)

DescribeTable("When password has consecutive repeated characters",
func (password string) {
isValid := noRepetedStrategy.IsValid(password, 0)

Expect(isValid).To(BeFalse())
},
Entry("P@ssw0rd", "P@ssw0rd"),
Entry("Opaaaa123", "Opaaaa123"),
Entry("1111111", "1111111"),
)
})

0 comments on commit f465bc7

Please sign in to comment.