Skip to content

Commit

Permalink
adds tests for subscriber notification feed
Browse files Browse the repository at this point in the history
  • Loading branch information
prajjwaldimri committed Mar 31, 2023
1 parent 1999ed6 commit ee526b4
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 3 deletions.
92 changes: 89 additions & 3 deletions lib/subscribers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ 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"
"strconv"
"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 +169,85 @@ func TestSubscriberService_Delete_Success(t *testing.T) {
assert.Equal(t, expectedResponse, resp)
})
}

func TestSubscriberService_GetNotificationFeed_Success(t *testing.T) {
var expectedResponse *lib.SubscriberNotificationFeedResponse
fileToStruct(filepath.Join("../testdata", "subscriber_notification_feed_response.json"), &expectedResponse)

page := 1
seen := true
feedIdentifier := "feed_identifier"

opts := lib.SubscriberNotificationFeedOptions{
Page: &page,
Seen: &seen,
FeedIdentifier: &feedIdentifier,
}

httpServer := createTestServer(t, TestServerOptions[io.Reader, *lib.SubscriberNotificationFeedResponse]{
expectedURLPath: fmt.Sprintf("/v1/subscribers/%s/notifications/feed?feedIdentifier=%s&page=%s&seen=%s", subscriberID, feedIdentifier, strconv.Itoa(page), strconv.FormatBool(seen)),
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.GetNotificationFeed(ctx, subscriberID, &opts)

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

func TestSubscriberService_GetUnseenCount_Success(t *testing.T) {
var expectedResponse *lib.SubscriberUnseenCountResponse
fileToStruct(filepath.Join("../testdata", "subscriber_notification_feed_unseen.json"), &expectedResponse)

seen := false

opts := lib.SubscriberUnseenCountOptions{
Seen: &seen,
}

httpServer := createTestServer(t, TestServerOptions[io.Reader, *lib.SubscriberUnseenCountResponse]{
expectedURLPath: fmt.Sprintf("/v1/subscribers/%s/notifications/unseen?seen=false", 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.GetUnseenCount(ctx, subscriberID, &opts)

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

func TestSubscriberService_MarkMessageSeen(t *testing.T) {
var expectedResponse *lib.SubscriberNotificationFeedResponse
fileToStruct(filepath.Join("../testdata", "subscriber_notification_feed_response.json"), &expectedResponse)

opts := lib.SubscriberMarkMessageSeenOptions{
MessageID: "message_id",
Seen: true,
Read: true,
}

httpServer := createTestServer(t, TestServerOptions[lib.SubscriberMarkMessageSeenOptions, *lib.SubscriberNotificationFeedResponse]{
expectedURLPath: fmt.Sprintf("/v1/subscribers/%s/messages/markAs", subscriberID),
expectedSentMethod: http.MethodPost,
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.MarkMessageSeen(ctx, subscriberID, opts)

require.NoError(t, err)
require.Equal(t, resp, expectedResponse)
}
40 changes: 40 additions & 0 deletions testdata/subscriber_notification_feed_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"data": [
{
"cta": { "action": { "buttons": [] }, "type": "redirect" },
"_id": "63ef751b0cf910b8da36abfa",
"_templateId": "63ef53d0d9ff09b916ead6bb",
"_environmentId": "63e29e4f33a4f29919d35ff0",
"_messageTemplateId": "63ef543fd9ff09b916eaedaf",
"_notificationId": "63ef74fd5e79762aa933341d",
"_organizationId": "63e29e4f33a4f29919d35fea",
"_subscriberId": "63e3381e8c028c44fd5841b1",
"_jobId": "63ef74fd5e79762aa9333426",
"templateIdentifier": "updates",
"_feedId": null,
"channel": "in_app",
"content": "Hello wassup",
"deviceTokens": [],
"seen": true,
"read": true,
"status": "sent",
"transactionId": "3b1f1062-3a9e-4b9f-85e5-8a6592c9189c",
"payload": { "updateMessage": "Hello wassup" },
"deleted": false,
"createdAt": "2023-02-17T12:37:47.856Z",
"updatedAt": "2023-02-17T12:37:50.934Z",
"__v": 0,
"lastReadDate": "2023-02-17T12:37:50.934Z",
"lastSeenDate": "2023-02-17T12:37:50.934Z",
"subscriber": {
"_id": "63e3381e8c028c44fd5841b1",
"subscriberId": "63e3381d33a4f29919e6bb65",
"id": "63e3381e8c028c44fd5841b1"
},
"id": "63ef751b0cf910b8da36abfa"
}
],
"totalCount": 1,
"pageSize": 10,
"page": 0
}
1 change: 1 addition & 0 deletions testdata/subscriber_notification_feed_unseen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "data": { "count": 0 } }

0 comments on commit ee526b4

Please sign in to comment.