Skip to content

Commit

Permalink
feat: add functionality to update persons
Browse files Browse the repository at this point in the history
  • Loading branch information
fritterhoff committed Aug 22, 2023
1 parent 9376501 commit 36aac07
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sectigo/person.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ func (c *PersonService) DeletePerson(id int) error {
_, err := DeleteWithoutJSONResponse(context.Background(), c.Client, fmt.Sprintf("/person/v1/%v", id))
return err
}

// UpdatePerson updates a person using the provided id and information.
func (c *PersonService) UpdatePerson(id int, q person.UpdateRequest) error {
_, err := PutWithoutJSONResponse(context.Background(), c.Client, fmt.Sprintf("/person/v1/%v", id), q)
return err
}
10 changes: 10 additions & 0 deletions sectigo/person/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ type CreateRequest struct {
Upn string `json:"upn"`
}

// UpdateRequest represents the information required for updating a person.
type UpdateRequest struct {
FirstName string `json:"firstName"`
MiddleName string `json:"middleName"`
LastName string `json:"lastName"`
OrganizationID int `json:"organizationId"`
ValidationType string `json:"validationType"`
CommonName string `json:"commonName"`
}

// ListParams provides the possible filters that can be passed to the PersonService.List method.
type ListParams struct {
Position int `url:"position,omitempty"`
Expand Down
33 changes: 33 additions & 0 deletions sectigo/person_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sectigo

import (
"encoding/json"
"net/http"
"strconv"
"testing"
Expand Down Expand Up @@ -68,6 +69,38 @@ func TestPersonService_List(t *testing.T) {
assert.Len(t, *list, 1)
}

func TestPersonService_Update(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("PUT", "https://cert-manager.com/api/person/v1/1", func(req *http.Request) (*http.Response, error) {
update := person.UpdateRequest{}
if err := json.NewDecoder(req.Body).Decode(&update); err != nil {
return httpmock.NewStringResponse(400, ""), nil
}
assert.Equal(t, person.UpdateRequest{
FirstName: "Tester",
MiddleName: "",
LastName: "",
OrganizationID: 3105,
ValidationType: "STANDARD",
CommonName: "Tester",
}, update)
return httpmock.NewStringResponse(200, ""), nil
})

logger, _ := zap.NewProduction()
c := NewClient(http.DefaultClient, logger, "", "", "")
err := c.PersonService.UpdatePerson(1, person.UpdateRequest{
FirstName: "Tester",
MiddleName: "",
LastName: "",
OrganizationID: 3105,
ValidationType: "STANDARD",
CommonName: "Tester",
})
assert.Nil(t, err)
}

func TestPersonService_ListFiltered(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
Expand Down
7 changes: 7 additions & 0 deletions sectigo/sectigo.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ func DeleteWithoutJSONResponse(ctx context.Context, c *Client, path string) (*ht
return resp, err
}

// DeleteWithoutJSONResponse executes a DELETE-Request without expecting a JSON response.
// Custom handling of the response can be done using the returned http.Response.
func PutWithoutJSONResponse(ctx context.Context, c *Client, path string, payload interface{}) (*http.Response, error) {
_, resp, err := makeRequest[any](ctx, c, http.MethodPut, path, payload, false)
return resp, err
}

func sendRequestAndParse[T any](ctx context.Context, c *Client, req *http.Request, response bool) (*T, *http.Response, error) {
if ctx == nil {
return nil, nil, errors.New("context must be non-nil")
Expand Down

0 comments on commit 36aac07

Please sign in to comment.