diff --git a/client.go b/client.go index 773b6a4..fe79750 100644 --- a/client.go +++ b/client.go @@ -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) diff --git a/models.go b/models.go index 2b2b11e..91400e7 100644 --- a/models.go +++ b/models.go @@ -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 @@ -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 +} diff --git a/models_test.go b/models_test.go index 002fad1..c45f360 100644 --- a/models_test.go +++ b/models_test.go @@ -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) + } + }) + } +}