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

feat: Allow NoAuthentication for OnPremise MAIL destination #5137

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/angry-ways-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sap-cloud-sdk/mail-client': minor
---

Allow NoAuthentication for OnPremise MAIL destination
47 changes: 47 additions & 0 deletions packages/mail-client/src/mail-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,53 @@ describe('mail client', () => {
).resolves.not.toThrow();
});

it('should work with destination from service - proxy-type OnPremise with Authentication set to NoAuthentication', async () => {
const { connection } = mockSocketConnection();
jest
.spyOn(nodemailer, 'createTransport')
.mockReturnValue(mockTransport as any);

jest.spyOn(mockTransport, 'sendMail').mockImplementation(() => {
connection.socket.on('data', () => {});
});

const mailOptions1: MailConfig = {
from: '[email protected]',
to: '[email protected]'
};

const mailClientOptions: MailClientOptions = {
tls: {
rejectUnauthorized: false
}
};

const mailDestinationResponse: DestinationConfiguration = {
Name: 'MyMailDestination',
Type: 'MAIL',
Authentication: 'NoAuthentication',
ProxyType: 'OnPremise',
'mail.smtp.host': 'smtp.gmail.com',
'mail.smtp.port': '587'
};

mockServiceBindings();
// the mockServiceToken() method does not work outside connectivity module.
jest
.spyOn(tokenAccessor, 'serviceToken')
.mockImplementation(() => Promise.resolve(providerServiceToken));

mockFetchDestinationCalls(mailDestinationResponse);

await expect(
sendMail(
{ destinationName: mailDestinationResponse.Name },
[mailOptions1],
mailClientOptions
)
).resolves.not.toThrow();
});

it('should work with registered destination', async () => {
jest
.spyOn(nodemailer, 'createTransport')
Expand Down
6 changes: 6 additions & 0 deletions packages/mail-client/src/mail-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ function getCredentials(
destination: Destination,
originalProperties: Record<string, any>
): { username: string; password: string } {
if (
destination.proxyType === 'OnPremise' &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Here, since internet smtp server can also have no username/password, I would leave this out.

destination.authentication === 'NoAuthentication'
) {
return { username: '', password: '' };
Copy link
Contributor

@ZhongpinWang ZhongpinWang Nov 6, 2024

Choose a reason for hiding this comment

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

Not sure if this is the best way setting empty username and password. According to https://nodemailer.com/smtp/#authentication, if authentication is not needed, maybe we should leave auth out in createTransport. Could you please test this?

}
const username = originalProperties['mail.user'];
const password = originalProperties['mail.password'];

Expand Down