diff --git a/sdk/domain_authentication.go b/sdk/domain_authentication.go index a27251d..d6a1811 100644 --- a/sdk/domain_authentication.go +++ b/sdk/domain_authentication.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "log" "net/http" ) @@ -153,7 +154,7 @@ func (c *Client) ValidateDomainAuthentication(ctx context.Context, id string) Re } } - _, statusCode, err := c.Post(ctx, "POST", "/whitelabel/domains/"+id+"/validate", nil) + respBody, statusCode, err := c.Post(ctx, "POST", "/whitelabel/domains/"+id+"/validate", nil) if err != nil || statusCode != 200 { return RequestError{ StatusCode: statusCode, @@ -161,9 +162,34 @@ func (c *Client) ValidateDomainAuthentication(ctx context.Context, id string) Re } } + res, err := unmarshall(respBody) + if err != nil { + return RequestError{ + StatusCode: http.StatusInternalServerError, + Err: err, + } + } + + if valid, ok := res["valid"].(bool); ok && !valid { + return RequestError{ + StatusCode: http.StatusInternalServerError, + Err: ErrDomainAuthenticationValidationFailed, + } + } + return RequestError{StatusCode: http.StatusOK, Err: nil} } +func unmarshall(respBody string) (map[string]interface{}, error) { + var j map[string]interface{} + if err := json.Unmarshal([]byte(respBody), &j); err != nil { + log.Printf("JSON Unmarshalling of the response body: %s, failed with: %s,", + respBody, err) + return nil, err + } + return j, nil +} + // DeleteDomainAuthentication deletes an DomainAuthentication. func (c *Client) DeleteDomainAuthentication(ctx context.Context, id string) (bool, RequestError) { if id == "" { diff --git a/sdk/domain_authentication_test.go b/sdk/domain_authentication_test.go index 43eb367..ea61a86 100644 --- a/sdk/domain_authentication_test.go +++ b/sdk/domain_authentication_test.go @@ -1,7 +1,10 @@ package sendgrid_test import ( + "context" "errors" + "net/http" + "net/http/httptest" "reflect" "testing" @@ -110,3 +113,71 @@ func Test_parseDomainAuthentication(t *testing.T) { //nolint:funlen }) } } + +var ( + mux *http.ServeMux + server *httptest.Server +) + +func setup() func() { + mux = http.NewServeMux() + server = httptest.NewServer(mux) + return func() { + server.Close() + } +} +func TestClient_ValidateDomainAuthentication_ShouldNotErrorForOkResponseCode(t *testing.T) { + teardown := setup() + defer teardown() + mux.HandleFunc("/whitelabel/domains/123/validate", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(`{"valid":true}`)) + }) + client := sendgrid.NewClient("api-key", server.URL, "on-behalf") + + requestError := client.ValidateDomainAuthentication(context.TODO(), "123") + + if requestError.Err != nil { + t.Errorf("ValidateDomainAuthentication() got = %v, want %v", requestError, "error") + } +} + +func TestClient_ValidateDomainAuthentication_ShouldErrorForErrorResponseCode(t *testing.T) { + teardown := setup() + defer teardown() + mux.HandleFunc("/whitelabel/domains/123/validate", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusBadRequest) + _, _ = w.Write([]byte(`{"valid":true}`)) + }) + client := sendgrid.NewClient("api-key", server.URL, "on-behalf") + + requestError := client.ValidateDomainAuthentication(context.TODO(), "123") + + if requestError.Err == nil { + t.Errorf("ValidateDomainAuthentication() got = %v, want %v", requestError, "error") + } +} + +func TestClient_ValidateDomainAuthentication_ShouldErrorForResponseBodySayingValidFalse(t *testing.T) { + teardown := setup() + defer teardown() + mux.HandleFunc("/whitelabel/domains/123/validate", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(`{"valid":false}`)) + }) + client := sendgrid.NewClient("api-key", server.URL, "on-behalf") + + requestError := client.ValidateDomainAuthentication(context.Background(), "123") + + if requestError.Err == nil { + t.Errorf("ValidateDomainAuthentication() got = %v, want %v", requestError, "error") + } + errMsg := sendgrid.ErrDomainAuthenticationValidationFailed.Error() + if requestError.Err.Error() != errMsg { + t.Errorf("ValidateDomainAuthentication() got = %v, want %v", requestError.Err.Error(), + errMsg) + } +} diff --git a/sdk/errors.go b/sdk/errors.go index 0efeb82..9221969 100644 --- a/sdk/errors.go +++ b/sdk/errors.go @@ -43,9 +43,6 @@ var ( // ErrFailedCreatingSubUser error displayed when the provider can not create a subuser. ErrFailedCreatingSubUser = errors.New("failed creating subUser") - // ErrFailedDeletingSubUser error displayed when the provider can not delete a subuser. - ErrFailedDeletingSubUser = errors.New("failed deleting subUser") - // ErrTemplateIDRequired error displayed when a template ID wasn't specified. ErrTemplateIDRequired = errors.New("a template ID is required") @@ -83,6 +80,8 @@ var ( ErrFailedDeletingDomainAuthentication = errors.New("failed deleting domain authentication") + ErrDomainAuthenticationValidationFailed = errors.New("domain authentication validation failed") + ErrLinkBrandingIDRequired = errors.New("link branding id is required") ErrFailedDeletingLinkBranding = errors.New("failed to delete link branding") diff --git a/sendgrid/resource_sendgrid_domain_authentication_validation.go b/sendgrid/resource_sendgrid_domain_authentication_validation.go index 33c76bb..fe1b8c3 100644 --- a/sendgrid/resource_sendgrid_domain_authentication_validation.go +++ b/sendgrid/resource_sendgrid_domain_authentication_validation.go @@ -20,6 +20,7 @@ import ( sendgrid "github.com/octoenergy/terraform-provider-sendgrid/sdk" ) +// https://docs.sendgrid.com/api-reference/domain-authentication/validate-a-domain-authentication func resourceSendgridDomainAuthenticationValidation() *schema.Resource { //nolint:funlen return &schema.Resource{ CreateContext: resourceSendgridDomainAuthenticationValidationCreate, @@ -93,10 +94,6 @@ func resourceSendgridDomainAuthenticationValidationUpdate( return validateDomain(ctx, d, m) } -func resourceSendgridDomainAuthenticationValidationDelete( - ctx context.Context, - d *schema.ResourceData, - m interface{}, -) diag.Diagnostics { +func resourceSendgridDomainAuthenticationValidationDelete(context.Context, *schema.ResourceData, interface{}) diag.Diagnostics { return nil }