Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: adding validate check to validate password request payload #21

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ func (c *Client) LoginUser(ctx context.Context, input *LoginUserPayload) (*OAUTH

// ResetPassword is used to reset a user's password on AuthServer
func (c *Client) ResetPassword(ctx context.Context, payload *PasswordResetPayload) (*PasswordResetResponse, error) {
err := payload.Validate()
if err != nil {
return nil, err
}

url := fmt.Sprintf("%s/accounts/password/reset/", c.configurations.AuthServerEndpoint)

resp, err := c.makeRequest(ctx, http.MethodPost, url, &payload, "application/json", true, nil)
Expand Down
15 changes: 12 additions & 3 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ type CreateUserResponse struct {

// PassworResetHeaders defines the object needed when making a password reset request
type PasswordResetPayload struct {
Origin string `json:"origin"`
Variant string `json:"variant"`
Email string `json:"email"`
Origin string `json:"origin" validate:"required"`
Variant string `json:"variant" validate:"required"`
Email string `json:"email" validate:"required,email"`
}

// ResetPasswordResponse defines return the json object returned when password reset instruction has been sent
Expand All @@ -113,3 +113,12 @@ func (s *LoginUserPayload) Validate() error {

return nil
}

// Validate ensures that the reset password payload passes the validation checks
func (p *PasswordResetPayload) Validate() error {
err := v.Struct(p)
if err != nil {
return err
}
return nil
}
43 changes: 43 additions & 0 deletions models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,46 @@ func TestLoginUserPayload_Validate(t *testing.T) {
})
}
}

func TestResetPasswordPayload_Validate(t *testing.T) {
type args struct {
payload PasswordResetPayload
}

tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Happy case: valid payload",
args: args{
payload: PasswordResetPayload{
Email: "testuser@example.com",
Variant: "UzaziSalamaProd",
Origin: "https://localhost:test.com",
},
},
wantErr: false,
},
{
name: "Sad case: invalid email",
args: args{
payload: PasswordResetPayload{
Email: "testuserexample",
Variant: "UzaziSalamaProd",
Origin: "https://localhost:test.com",
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := tt.args.payload
if err := p.Validate(); (err != nil) != tt.wantErr {
t.Errorf("PasswordResetPayload.Validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
Loading