diff --git a/graph/generated.go b/graph/generated.go index 4649423..ac5580a 100644 --- a/graph/generated.go +++ b/graph/generated.go @@ -2326,7 +2326,7 @@ func (ec *executionContext) unmarshalInputRule(ctx context.Context, obj interfac var err error ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("value")) - it.Value, err = ec.unmarshalOInt2ᚖint(ctx, v) + it.Value, err = ec.unmarshalNInt2int(ctx, v) if err != nil { return it, err } @@ -2777,6 +2777,21 @@ func (ec *executionContext) marshalNBoolean2bool(ctx context.Context, sel ast.Se return res } +func (ec *executionContext) unmarshalNInt2int(ctx context.Context, v interface{}) (int, error) { + res, err := graphql.UnmarshalInt(v) + return res, graphql.ErrorOnPath(ctx, err) +} + +func (ec *executionContext) marshalNInt2int(ctx context.Context, sel ast.SelectionSet, v int) graphql.Marshaler { + res := graphql.MarshalInt(v) + if res == graphql.Null { + if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) { + ec.Errorf(ctx, "the requested element is null which the schema does not allow") + } + } + return res +} + func (ec *executionContext) unmarshalNRule2ᚕᚖgithubᚗcomᚋbrandaogabriel7ᚋstudioᚑsolᚑbackᚑendᚑtestᚋgraphᚋmodelᚐRuleᚄ(ctx context.Context, v interface{}) ([]*model.Rule, error) { var vSlice []interface{} if v != nil { @@ -3139,22 +3154,6 @@ func (ec *executionContext) marshalOBoolean2ᚖbool(ctx context.Context, sel ast return res } -func (ec *executionContext) unmarshalOInt2ᚖint(ctx context.Context, v interface{}) (*int, error) { - if v == nil { - return nil, nil - } - res, err := graphql.UnmarshalInt(v) - return &res, graphql.ErrorOnPath(ctx, err) -} - -func (ec *executionContext) marshalOInt2ᚖint(ctx context.Context, sel ast.SelectionSet, v *int) graphql.Marshaler { - if v == nil { - return graphql.Null - } - res := graphql.MarshalInt(*v) - return res -} - func (ec *executionContext) unmarshalOString2ᚖstring(ctx context.Context, v interface{}) (*string, error) { if v == nil { return nil, nil diff --git a/graph/model/models_gen.go b/graph/model/models_gen.go index ed016ac..6c1ba0a 100644 --- a/graph/model/models_gen.go +++ b/graph/model/models_gen.go @@ -4,7 +4,7 @@ package model type Rule struct { Rule string `json:"rule"` - Value *int `json:"value"` + Value int `json:"value"` } type Verify struct { diff --git a/graph/resolver.go b/graph/resolver.go index a25c09c..2c16ee7 100644 --- a/graph/resolver.go +++ b/graph/resolver.go @@ -1,7 +1,11 @@ package graph +import "github.com/brandaogabriel7/studio-sol-back-end-test/src/services/password_validation" + // This file will not be regenerated automatically. // // It serves as dependency injection for your app, add any dependencies you require here. -type Resolver struct{} +type Resolver struct{ + PasswordValidationService password_validation.PasswordValidationService +} diff --git a/graph/schema.graphqls b/graph/schema.graphqls index f1fbc0a..fe009bf 100644 --- a/graph/schema.graphqls +++ b/graph/schema.graphqls @@ -5,7 +5,7 @@ type Verify { input Rule { rule: String! - value: Int + value: Int! } type Query { diff --git a/graph/schema.resolvers.go b/graph/schema.resolvers.go index 20e33f4..7cc13d0 100644 --- a/graph/schema.resolvers.go +++ b/graph/schema.resolvers.go @@ -12,7 +12,8 @@ import ( // Verify is the resolver for the verify field. func (r *queryResolver) Verify(ctx context.Context, password string, rules []*model.Rule) (*model.Verify, error) { - return &model.Verify{Verify: false}, nil + verifyResponse := r.PasswordValidationService.Validate(password, rules) + return &verifyResponse, nil } // Query returns QueryResolver implementation. diff --git a/src/factories/password_validation_service_factory.go b/src/factories/password_validation_service_factory.go new file mode 100644 index 0000000..7703eff --- /dev/null +++ b/src/factories/password_validation_service_factory.go @@ -0,0 +1,17 @@ +package factories + +import ( + "github.com/brandaogabriel7/studio-sol-back-end-test/src/services/password_validation" + "github.com/brandaogabriel7/studio-sol-back-end-test/src/strategies/validation" +) + +func GetDefaultPasswordValidationService() password_validation.PasswordValidationService { + validationStrategies := map[string]validation.ValidationStrategy{ + string(validation.MIN_SIZE): validation.MinSizeValidationStrategy{}, + string(validation.MIN_DIGIT): validation.NewMinDigitValidationStrategy(), + string(validation.MIN_SPECIAL_CHARS): validation.NewMinSpecialCharsValidationStrategy(), + string(validation.NO_REPETED): validation.NoRepetedStrategy{}, + } + + return *password_validation.NewPasswordValidationService(validationStrategies) +} \ No newline at end of file diff --git a/src/integration_tests/verify_test.go b/src/integration_tests/verify_test.go index 18042ae..b92a39c 100644 --- a/src/integration_tests/verify_test.go +++ b/src/integration_tests/verify_test.go @@ -5,12 +5,16 @@ import ( "github.com/99designs/gqlgen/graphql/handler" "github.com/brandaogabriel7/studio-sol-back-end-test/graph" "github.com/brandaogabriel7/studio-sol-back-end-test/graph/model" + "github.com/brandaogabriel7/studio-sol-back-end-test/src/factories" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" ) var _ = Describe("Verify", func() { - c := client.New(handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{}}))) + c := client.New(handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{ + Resolvers: &graph.Resolver{ + PasswordValidationService: factories.GetDefaultPasswordValidationService(), + }}))) Describe("Checking that password follows the specified rules", func () { DescribeTable("minSize, minSpecialChars, noRepeted, minDigit", @@ -81,7 +85,7 @@ var _ = Describe("Verify", func() { Expect(resp.Verify.NoMatch).To(Equal(noMatch)) }, Entry("minSize", "0p@", 5, 1, 1, []string{"minSize"}), - Entry("minSize, minDigits, minSpecialChars", "SenhaForte!23", 20, 4, 2, []string{"minSize", "minDigits, minSpecialChars"}), + Entry("minSize, minSpecialChars, minDigit", "SenhaForte!2", 20, 4, 2, []string{"minSize", "minSpecialChars", "minDigit"}), Entry("noRepeted", "aaaaaaa!2", 3, 1, 1, []string{"noRepeted"}), ) }) diff --git a/src/services/password_validation/password_validation_service.go b/src/services/password_validation/password_validation_service.go index 6b0d1c1..c2b6ec9 100644 --- a/src/services/password_validation/password_validation_service.go +++ b/src/services/password_validation/password_validation_service.go @@ -23,7 +23,7 @@ func (pvs *PasswordValidationService) Validate(password string, rules []*model.R } } } - verifyResponse.Verify = len(verifyResponse.NoMatch) > 0 + verifyResponse.Verify = len(verifyResponse.NoMatch) == 0 return verifyResponse } \ No newline at end of file diff --git a/src/services/password_validation/password_validation_service_test.go b/src/services/password_validation/password_validation_service_test.go index 030dd4a..99d28ec 100644 --- a/src/services/password_validation/password_validation_service_test.go +++ b/src/services/password_validation/password_validation_service_test.go @@ -54,7 +54,7 @@ var _ = Describe("PasswordValidationService", func() { verifyResponse := pvs.Validate(password, rules) - isValid := len(noMatch) > 0 + isValid := len(noMatch) == 0 Expect(verifyResponse.NoMatch).To(Equal(noMatch)) Expect(verifyResponse.Verify).To(Equal(isValid)) @@ -62,7 +62,7 @@ var _ = Describe("PasswordValidationService", func() { Entry( "third", "opa", - []model.Rule{ + []*model.Rule{ {Rule: FIRST_RULE, Value: 3}, {Rule: SECOND_RULE, Value: 2}, {Rule: THIRD_RULE, Value: 0}, @@ -72,7 +72,7 @@ var _ = Describe("PasswordValidationService", func() { Entry( "first, second", "senhaaa", - []model.Rule{ + []*model.Rule{ {Rule: FIRST_RULE, Value: 5}, {Rule: SECOND_RULE, Value: 7}, {Rule: THIRD_RULE, Value: 2}, @@ -82,7 +82,7 @@ var _ = Describe("PasswordValidationService", func() { Entry( "-", "senhaforte", - []model.Rule{ + []*model.Rule{ {Rule: FIRST_RULE, Value: 0}, {Rule: SECOND_RULE, Value: 7}, {Rule: THIRD_RULE, Value: 2}, diff --git a/src/strategies/validation/validation_types.go b/src/strategies/validation/validation_types.go new file mode 100644 index 0000000..352cdd5 --- /dev/null +++ b/src/strategies/validation/validation_types.go @@ -0,0 +1,8 @@ +package validation + +type ValidationType string + +const MIN_SIZE ValidationType = "minSize" +const MIN_DIGIT ValidationType = "minDigit" +const MIN_SPECIAL_CHARS ValidationType = "minSpecialChars" +const NO_REPETED ValidationType = "noRepeted" \ No newline at end of file