Skip to content

Commit

Permalink
feat: adds tests for subscriber preferences endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
prajjwaldimri committed Mar 31, 2023
1 parent 1cecb7a commit c22f046
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 3 deletions.
80 changes: 77 additions & 3 deletions lib/subscribers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package lib_test
import (
"context"
"encoding/json"
"github.com/novuhq/go-novu/lib"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"

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

const subscriberID = "62b51a44da1af31d109f5da7"
Expand Down Expand Up @@ -165,3 +168,74 @@ func TestSubscriberService_Delete_Success(t *testing.T) {
assert.Equal(t, expectedResponse, resp)
})
}

func TestSubscriberService_GetSubscriber_Success(t *testing.T) {
var expectedResponse lib.SubscriberResponse
fileToStruct(filepath.Join("../testdata", "subscriber_response.json"), &expectedResponse)

httpServer := createTestServer(t, TestServerOptions[io.Reader, *lib.SubscriberResponse]{
expectedURLPath: fmt.Sprintf("/v1/subscribers/%s", subscriberID),
expectedSentMethod: http.MethodGet,
expectedSentBody: http.NoBody,
responseStatusCode: http.StatusOK,
responseBody: &expectedResponse,
})

ctx := context.Background()
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(httpServer.URL)})
resp, err := c.SubscriberApi.Get(ctx, subscriberID)

require.NoError(t, err)
require.Equal(t, resp, expectedResponse)
}

func TestSubscriberService_GetPreferences_Success(t *testing.T) {
var expectedResponse *lib.SubscriberPreferencesResponse
fileToStruct(filepath.Join("../testdata", "subscriber_preferences_response.json"), &expectedResponse)

httpServer := createTestServer(t, TestServerOptions[map[string]string, *lib.SubscriberPreferencesResponse]{
expectedURLPath: fmt.Sprintf("/v1/subscribers/%s/preferences", subscriberID),
expectedSentMethod: http.MethodGet,
expectedSentBody: map[string]string{},
responseStatusCode: http.StatusOK,
responseBody: expectedResponse,
})

ctx := context.Background()
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(httpServer.URL)})
resp, err := c.SubscriberApi.GetPreferences(ctx, subscriberID)

require.NoError(t, err)
require.Equal(t, resp, expectedResponse)
}

func TestSubscriberService_UpdatePreferences_Success(t *testing.T) {
var topicID = "topicId"

var expectedResponse *lib.SubscriberPreferencesResponse
fileToStruct(filepath.Join("../testdata", "subscriber_preferences_response.json"), &expectedResponse)

var opts *lib.UpdateSubscriberPreferencesOptions = &lib.UpdateSubscriberPreferencesOptions{
Enabled: true,
Channel: []lib.UpdateSubscriberPreferencesChannel{
{
Type: "email",
Enabled: true,
},
},
}
httpServer := createTestServer(t, TestServerOptions[*lib.UpdateSubscriberPreferencesOptions, *lib.SubscriberPreferencesResponse]{
expectedURLPath: fmt.Sprintf("/v1/subscribers/%s/preferences/%s", subscriberID, topicID),
expectedSentMethod: http.MethodPatch,
expectedSentBody: opts,
responseStatusCode: http.StatusOK,
responseBody: expectedResponse,
})

ctx := context.Background()
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(httpServer.URL)})
resp, err := c.SubscriberApi.UpdatePreferences(ctx, subscriberID, topicID, opts)

require.NoError(t, err)
require.Equal(t, resp, expectedResponse)
}
55 changes: 55 additions & 0 deletions testdata/subscriber_preferences_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"data": [
{
"template": {
"_id": "6c65c6b0eaf3900ecab1aa33",
"name": "Build Events",
"critical": false
},
"preference": {
"enabled": true,
"channels": {
"in_app": true
}
}
},
{
"template": {
"_id": "d773a4a4a4f4e405d51c0ecf",
"name": "New user signup",
"critical": false
},
"preference": {
"enabled": true,
"channels": {}
}
},
{
"template": {
"_id": "5c1c5b8d98c1a7f4fb5fbd2e",
"name": "Test Notification",
"critical": true
},
"preference": {
"enabled": true,
"channels": {
"email": true,
"chat": true
}
}
},
{
"template": {
"_id": "0e22a702b31f1d40bc46d87a",
"name": "updates",
"critical": false
},
"preference": {
"enabled": true,
"channels": {
"in_app": true
}
}
}
]
}

0 comments on commit c22f046

Please sign in to comment.