Skip to content

Commit

Permalink
chore: adding validate check tov validate password request payload
Browse files Browse the repository at this point in the history
  • Loading branch information
kennankole committed Sep 9, 2024
1 parent 7e9a7d5 commit 29273b3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
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 enusere 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: "[email protected]",
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)
}
})
}
}

0 comments on commit 29273b3

Please sign in to comment.