This repository has been archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f465bc7
commit f4a9293
Showing
11 changed files
with
164 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/services/password_validation/password_validation_service.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package password_validation | ||
|
||
import ( | ||
"github.com/brandaogabriel7/studio-sol-back-end-test/graph/model" | ||
"github.com/brandaogabriel7/studio-sol-back-end-test/src/strategies/validation" | ||
) | ||
|
||
type PasswordValidationService struct { | ||
strategies map[string]validation.ValidationStrategy | ||
} | ||
|
||
func NewPasswordValidationService(strategies map[string]validation.ValidationStrategy) *PasswordValidationService { | ||
return &PasswordValidationService{strategies: strategies} | ||
} | ||
|
||
func (pvs *PasswordValidationService) Validate(password string, rules []*model.Rule) model.Verify { | ||
verifyResponse := model.Verify{Verify: true, NoMatch: make([]string, 0)} | ||
|
||
for _, rule := range rules { | ||
if strategy, exists := pvs.strategies[rule.Rule]; exists { | ||
if !strategy.IsValid(password, rule.Value) { | ||
verifyResponse.NoMatch = append(verifyResponse.NoMatch, rule.Rule) | ||
} | ||
} | ||
} | ||
verifyResponse.Verify = len(verifyResponse.NoMatch) > 0 | ||
|
||
return verifyResponse | ||
} |
93 changes: 93 additions & 0 deletions
93
src/services/password_validation/password_validation_service_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package password_validation_test | ||
|
||
import ( | ||
"github.com/brandaogabriel7/studio-sol-back-end-test/graph/model" | ||
"github.com/brandaogabriel7/studio-sol-back-end-test/src/services/password_validation" | ||
"github.com/brandaogabriel7/studio-sol-back-end-test/src/strategies/validation" | ||
"github.com/brandaogabriel7/studio-sol-back-end-test/src/utils" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
// ValidationStrategy test double | ||
type mockedValidationStrategy struct { mock.Mock } | ||
|
||
func newMockedValidationStrategy() *mockedValidationStrategy { return &mockedValidationStrategy{} } | ||
|
||
func (m mockedValidationStrategy) IsValid(password string, value int) bool { | ||
args := m.Called(password, value) | ||
return args.Bool(0) | ||
} | ||
|
||
// tests | ||
var _ = Describe("PasswordValidationService", func() { | ||
const FIRST_RULE string = "firstRule" | ||
const SECOND_RULE string = "secondRule" | ||
const THIRD_RULE string = "thirdRule" | ||
|
||
firstRuleStrategy := mockedValidationStrategy{} | ||
secondRuleStrategy := mockedValidationStrategy{} | ||
thirdRuleStrategy := mockedValidationStrategy{} | ||
|
||
mockedValidationStrategies := map[string]*mockedValidationStrategy{ | ||
FIRST_RULE: &firstRuleStrategy, | ||
SECOND_RULE: &secondRuleStrategy, | ||
THIRD_RULE: &thirdRuleStrategy, | ||
} | ||
|
||
validationStrategies := make(map[string]validation.ValidationStrategy) | ||
|
||
for key, value := range mockedValidationStrategies { | ||
validationStrategies[key] = value | ||
} | ||
|
||
pvs := password_validation.NewPasswordValidationService(validationStrategies) | ||
|
||
DescribeTable("Validate password when following rules have not passed", | ||
func (password string, rules []*model.Rule, noMatch []string) { | ||
// overwrite stub for the noMatch strategies | ||
for key, mockedStrategy := range mockedValidationStrategies { | ||
passsedValidation := !utils.Contains(noMatch, key) | ||
mockedStrategy.On("IsValid", password, mock.AnythingOfType("int")).Return(passsedValidation) | ||
} | ||
|
||
verifyResponse := pvs.Validate(password, rules) | ||
|
||
isValid := len(noMatch) > 0 | ||
|
||
Expect(verifyResponse.NoMatch).To(Equal(noMatch)) | ||
Expect(verifyResponse.Verify).To(Equal(isValid)) | ||
}, | ||
Entry( | ||
"third", | ||
"opa", | ||
[]model.Rule{ | ||
{Rule: FIRST_RULE, Value: 3}, | ||
{Rule: SECOND_RULE, Value: 2}, | ||
{Rule: THIRD_RULE, Value: 0}, | ||
}, | ||
[]string{THIRD_RULE}, | ||
), | ||
Entry( | ||
"first, second", | ||
"senhaaa", | ||
[]model.Rule{ | ||
{Rule: FIRST_RULE, Value: 5}, | ||
{Rule: SECOND_RULE, Value: 7}, | ||
{Rule: THIRD_RULE, Value: 2}, | ||
}, | ||
[]string{FIRST_RULE, SECOND_RULE}, | ||
), | ||
Entry( | ||
"-", | ||
"senhaforte", | ||
[]model.Rule{ | ||
{Rule: FIRST_RULE, Value: 0}, | ||
{Rule: SECOND_RULE, Value: 7}, | ||
{Rule: THIRD_RULE, Value: 2}, | ||
}, | ||
[]string{}, | ||
), | ||
) | ||
}) |
13 changes: 13 additions & 0 deletions
13
src/services/password_validation/password_validation_suite_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package password_validation_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestPasswordValidation(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "PasswordValidation Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package utils | ||
|
||
func Contains(s []string, str string) bool { | ||
for _, v := range s { | ||
if v == str { | ||
return true | ||
} | ||
} | ||
return false | ||
} |