Skip to content

Commit

Permalink
Merge pull request #29 from NonsoAmadi10/ft-inbound-parser-mx-record-…
Browse files Browse the repository at this point in the history
…validation

[NV-GO-1] API 🚀: Validate MX Record Setup for Inbound Parse Functionality
  • Loading branch information
unicodeveloper authored Oct 3, 2023
2 parents 36ced35 + a86085a commit cdb0872
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ Class | Method
*IntegrationsApi* | [**Get**](https://docs.novu.co/platform/integrations) | **Get** /integrations | Get all integrations
*IntegrationsApi* | [**GetActive**](https://docs.novu.co/platform/intergations) | **Get** /integrations/active | Get all active integrations

*InboundParserApi* | [**Get**](https://docs.novu.co/platform/inbound-parse-webhook/) | **Get** /inbound-parse/mx/status | Validate the mx record setup for the inbound parse functionality

## Authorization (api-key)
## Authorization (api-key)

- **Type**: API key
Expand Down
29 changes: 29 additions & 0 deletions lib/inbound_parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package lib

import (
"context"
"net/http"
)

type IInboundParser interface {
Get(ctx context.Context) bool
}
type InboundParserService service

func (i InboundParserService) Get(ctx context.Context) (*InboundParserResponse, error) {

var resp InboundParserResponse

URL := i.client.config.BackendURL.JoinPath("inbound-parse", "mx", "status")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, URL.String(), http.NoBody)

if err != nil {
return &InboundParserResponse{}, err
}
_, err = i.client.sendRequest(req, &resp)

if err != nil {
return &InboundParserResponse{}, err
}
return &resp, nil
}
52 changes: 52 additions & 0 deletions lib/inbound_parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package lib_test

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/novuhq/go-novu/lib"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestInboundParserService_Get_Success(t *testing.T) {
expectedResponse := &lib.InboundParserResponse{
Data: lib.MxRecordConfiguredStatus{MxRecordConfigured: false},
}

inboundParserService := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

// Write the expected response as JSON
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(expectedResponse)
assert.NoError(t, err)

t.Run("Header must contain ApiKey", func(t *testing.T) {
authKey := r.Header.Get("Authorization")
assert.True(t, strings.Contains(authKey, novuApiKey))
assert.True(t, strings.HasPrefix(authKey, "ApiKey"))
})

t.Run("URL and request method is as expected", func(t *testing.T) {
expectedURL := "/v1/inbound-parse/mx/status"
assert.Equal(t, http.MethodGet, r.Method)
assert.Equal(t, expectedURL, r.RequestURI)
})

}))

defer inboundParserService.Close()

ctx := context.Background()
i := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(inboundParserService.URL)})

resp, err := i.InboundParserApi.Get(ctx)

require.Nil(t, err)
require.Equal(t, resp, expectedResponse)
}
6 changes: 6 additions & 0 deletions lib/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,9 @@ type BroadcastEventToAll struct {
TransactionId string `json:"transactionId,omitempty"`
Actor interface{} `json:"actor,omitempty"`
}
type MxRecordConfiguredStatus struct {
MxRecordConfigured bool `json:"mxRecordConfigured"`
}
type InboundParserResponse struct {
Data MxRecordConfiguredStatus `json:"data"`
}
11 changes: 6 additions & 5 deletions lib/novu.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ type APIClient struct {
common service

// Api Service
SubscriberApi *SubscriberService
EventApi *EventService
TopicsApi *TopicService
IntegrationsApi *IntegrationService
SubscriberApi *SubscriberService
EventApi *EventService
TopicsApi *TopicService
IntegrationsApi *IntegrationService
InboundParserApi *InboundParserService
}

type service struct {
Expand All @@ -54,7 +55,7 @@ func NewAPIClient(apiKey string, cfg *Config) *APIClient {
c.SubscriberApi = (*SubscriberService)(&c.common)
c.TopicsApi = (*TopicService)(&c.common)
c.IntegrationsApi = (*IntegrationService)(&c.common)

c.InboundParserApi = (*InboundParserService)(&c.common)
return c
}

Expand Down

0 comments on commit cdb0872

Please sign in to comment.