Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): api docs not generated and added more e2e tests #2665

Merged
merged 1 commit into from
Feb 9, 2023

Conversation

p-fernandez
Copy link
Contributor

What change does this PR introduce?

Adds more defensive checks in the code and avoids to accept bad formatted payloads. Adds more E2E tests for the update of the subscriber preferences after some investigation regarding potential bugs reported by users. Also improves the API docs with more detailed descriptions.

Why was this change needed?

Improve the confidence in the code and clarify the intention to the users.

Other information (Screenshots)

@linear
Copy link

linear bot commented Feb 1, 2023

NV-1620 🐛 Bug Report: Update subscriber preferences expected payload and validation mismatch

📜 Description

Some of our users have reported that the endpoint PATCH /subscribers/:subscriberId/preferences/:templateId is not working properly.

The endpoint controller logic (

) is expecting a payload in the shape:

interface Body {
  channel?: ChannelTypeEnum;
  enabled?: boolean;
}

But the validation applied is reused from the Widget module DTO UpdateSubscriberPreferenceRequestDto:


There the type assigned to channel is ChannelPreference.
export class ChannelPreference {

Which is expected to be a nested object in the shape of:

export class ChannelPreference {
  type: ChannelTypeEnum;
  enabled: boolean;
}

👟 Reproduction steps

In any environment to do a cURL call to the API.
For example, in local environment:

curl -X PATCH \                                                     
 -H "Authorization: ApiKey <API_KEY>" \
 -H "Content-Type: application/json" \
 -d '{
      "channel": "email",
      "enabled": true
    }' \
http://localhost:3000/v1/subscribers/1/preferences/1

This will raise an error:

{"statusCode":400,"message":["nested property channel must be either object or array"],"error":"Bad Request"}%

👍 Expected behavior

Update the controller to satisfy the validation and follow the same structure as in the Widget module.

👎 Actual Behavior with Screenshots

--

📃 Provide any additional context for the Bug.

Test suite for update subscriber preferences is applying a different payload (the one used in Widget module). This means the tests are not accurate. Also we are not checking if the values are updated properly.
We need to increase the test suite robustness to be able to catch these bugs.

👀 Have you spent some time to check if this bug has been raised before?

  • I checked and didn't find similar issue

🏢 Have you read the Contributing Guidelines?

Are you willing to submit PR?

Yes I am willing to submit a PR!

Comment on lines +40 to +44
expect(createdSubscriber?.firstName).to.equal('John');
expect(createdSubscriber?.email).to.equal('john@doe.com');
expect(createdSubscriber?.phone).to.equal('+972523333333');
expect(createdSubscriber?.locale).to.equal('en');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linter warnings fixes

Comment on lines +95 to +101
expect(createdSubscriber?.firstName).to.equal('Mary');
expect(createdSubscriber?.email).to.equal('john@doe.com');
expect(createdSubscriber?.locale).to.equal('en');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linter warnings fixes

@ghost
Copy link

ghost commented Feb 1, 2023

👇 Click on the image for a new way to code review
  • Make big changes easier — review code in small groups of related files

  • Know where to start — see the whole change at a glance

  • Take a code tour — explore the change with an interactive tour

  • Make comments and review — all fully sync’ed with github

    Try it now!

Review these changes using an interactive CodeSee Map

Legend

CodeSee Map Legend

@@ -32,7 +32,7 @@ describe('Delete Subscriber - /subscribers/:subscriberId (DELETE)', function ()
);

const createdSubscriber = await subscriberRepository.findBySubscriberId(session.environment._id, '123');
expect(createdSubscriber.subscriberId).to.equal('123');
expect(createdSubscriber?.subscriberId).to.equal('123');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linter warnings fixes

Comment on lines +53 to +58
expect(createdSubscriber?.firstName).to.equal('John');
expect(createdSubscriber?.lastName).to.equal('Test Changed');
expect(createdSubscriber?.email).to.equal('changed@mail.com');
expect(createdSubscriber?.phone).to.equal('+972523333333');
expect(createdSubscriber?.locale).to.equal('sv');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linter warnings fixes

Comment on lines +95 to +100
const subscriberChannel = createdSubscriber?.channels?.find((channel) => channel.providerId === 'slack');

expect(subscriberChannel.providerId).to.equal('slack');
expect(subscriberChannel.credentials.webhookUrl).to.equal('webhookUrlNew');
expect(subscriberChannel?.providerId).to.equal('slack');
expect(subscriberChannel?.credentials.webhookUrl).to.equal('webhookUrlNew');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linter warnings fixes

Comment on lines +304 to +307
...(typeof body.enabled === 'boolean' && { enabled: body.enabled }),
...(body.channel && { channel: body.channel }),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Protection against wrong payloads.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is undefined considered a wrong payload? if yes shouldn't we then modify the UpdateSubscriberPreferenceRequestDto class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is used in other controller endpoints so for now I'd rather not touch it, just in case.

@p-fernandez p-fernandez force-pushed the nv-1620-bug-report-update-subscriber-preferences branch from 47d87ff to aa02ec0 Compare February 1, 2023 17:30
@github-actions github-actions bot removed the @novu/web label Feb 1, 2023
Copy link
Contributor

@LetItRock LetItRock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you rock🤘

Comment on lines +304 to +307
...(typeof body.enabled === 'boolean' && { enabled: body.enabled }),
...(body.channel && { channel: body.channel }),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is undefined considered a wrong payload? if yes shouldn't we then modify the UpdateSubscriberPreferenceRequestDto class?

* Unless explicitly set to false when creating a user preference we want it to be enabled
* even if not passing at first enabled to true.
*/
enabled: command.enabled !== false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
enabled: command.enabled !== false,
enabled: command.enabled ?? true,

@p-fernandez p-fernandez force-pushed the nv-1620-bug-report-update-subscriber-preferences branch from 06b0bc9 to cd6e76c Compare February 9, 2023 18:31
@p-fernandez p-fernandez force-pushed the nv-1620-bug-report-update-subscriber-preferences branch from cd6e76c to ebde451 Compare February 9, 2023 18:34
@p-fernandez p-fernandez added this pull request to the merge queue Feb 9, 2023
Merged via the queue into next with commit d85ef4f Feb 9, 2023
@p-fernandez p-fernandez deleted the nv-1620-bug-report-update-subscriber-preferences branch February 9, 2023 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants