Skip to content

Commit

Permalink
Merge pull request #4 from octoenergy/err-if-response-is-not-valid
Browse files Browse the repository at this point in the history
Fail domain authentication validation if response body has valid:false
  • Loading branch information
manojkurien authored Jul 14, 2023
2 parents 1601b19 + c795843 commit b76420b
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 9 deletions.
28 changes: 27 additions & 1 deletion sdk/domain_authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
)

Expand Down Expand Up @@ -153,17 +154,42 @@ 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,
Err: err,
}
}

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 == "" {
Expand Down
71 changes: 71 additions & 0 deletions sdk/domain_authentication_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package sendgrid_test

import (
"context"
"errors"
"net/http"
"net/http/httptest"
"reflect"
"testing"

Expand Down Expand Up @@ -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)
}
}
5 changes: 2 additions & 3 deletions sdk/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}

0 comments on commit b76420b

Please sign in to comment.