From ee526b4e56ef7b398c099635d2b8c95602e69642 Mon Sep 17 00:00:00 2001 From: Prajjwal Date: Fri, 31 Mar 2023 18:28:14 +0530 Subject: [PATCH] adds tests for subscriber notification feed --- lib/subscribers_test.go | 92 ++++++++++++++++++- ...subscriber_notification_feed_response.json | 40 ++++++++ .../subscriber_notification_feed_unseen.json | 1 + 3 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 testdata/subscriber_notification_feed_response.json create mode 100644 testdata/subscriber_notification_feed_unseen.json diff --git a/lib/subscribers_test.go b/lib/subscribers_test.go index 303d09b..6dc6c0b 100644 --- a/lib/subscribers_test.go +++ b/lib/subscribers_test.go @@ -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" @@ -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) +} diff --git a/testdata/subscriber_notification_feed_response.json b/testdata/subscriber_notification_feed_response.json new file mode 100644 index 0000000..6c0c6cd --- /dev/null +++ b/testdata/subscriber_notification_feed_response.json @@ -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 +} diff --git a/testdata/subscriber_notification_feed_unseen.json b/testdata/subscriber_notification_feed_unseen.json new file mode 100644 index 0000000..9942f8f --- /dev/null +++ b/testdata/subscriber_notification_feed_unseen.json @@ -0,0 +1 @@ +{ "data": { "count": 0 } }