-
Notifications
You must be signed in to change notification settings - Fork 68
feat: add email adapter DSN parsing #117
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
Open
TorstenDittmann
wants to merge
1
commit into
feat-multiple-adapters-failover
Choose a base branch
from
feat-email-adapter-dsn
base: feat-multiple-adapters-failover
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Tests\Adapter\Email; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Utopia\Messaging\Adapter\Email as EmailAdapter; | ||
| use Utopia\Messaging\Adapter\Email\Mailgun; | ||
| use Utopia\Messaging\Adapter\Email\Resend; | ||
| use Utopia\Messaging\Adapter\Email\Sendgrid; | ||
| use Utopia\Messaging\Adapter\Email\SMTP; | ||
|
|
||
| class DsnTest extends TestCase | ||
| { | ||
| public function test_creates_smtp_adapter_from_dsn(): void | ||
| { | ||
| $adapter = EmailAdapter::fromDsn( | ||
| 'smtp://user:pass@mail.example.com:587?secure=tls&autotls=1&xmailer=Appwrite&timeout=60&keepalive=1&timelimit=15' | ||
| ); | ||
|
|
||
| $this->assertInstanceOf(SMTP::class, $adapter); | ||
| $this->assertSame('mail.example.com', $this->readProperty($adapter, 'host')); | ||
| $this->assertSame(587, $this->readProperty($adapter, 'port')); | ||
| $this->assertSame('user', $this->readProperty($adapter, 'username')); | ||
| $this->assertSame('pass', $this->readProperty($adapter, 'password')); | ||
| $this->assertSame('tls', $this->readProperty($adapter, 'smtpSecure')); | ||
| $this->assertTrue($this->readProperty($adapter, 'smtpAutoTLS')); | ||
| $this->assertSame('Appwrite', $this->readProperty($adapter, 'xMailer')); | ||
| $this->assertSame(60, $this->readProperty($adapter, 'timeout')); | ||
| $this->assertTrue($this->readProperty($adapter, 'keepAlive')); | ||
| $this->assertSame(15, $this->readProperty($adapter, 'timelimit')); | ||
| } | ||
|
|
||
| public function test_creates_smtps_adapter_with_implicit_ssl_defaults(): void | ||
| { | ||
| $adapter = EmailAdapter::fromDsn('smtps://user:pass@mail.example.com'); | ||
|
|
||
| $this->assertInstanceOf(SMTP::class, $adapter); | ||
| $this->assertSame(465, $this->readProperty($adapter, 'port')); | ||
| $this->assertSame('ssl', $this->readProperty($adapter, 'smtpSecure')); | ||
| } | ||
|
|
||
| public function test_creates_resend_adapter_from_dsn(): void | ||
| { | ||
| $adapter = EmailAdapter::fromDsn('resend://re_test_key@default'); | ||
|
|
||
| $this->assertInstanceOf(Resend::class, $adapter); | ||
| $this->assertSame('re_test_key', $this->readProperty($adapter, 'apiKey')); | ||
| } | ||
|
|
||
| public function test_creates_sendgrid_adapter_from_dsn(): void | ||
| { | ||
| $adapter = EmailAdapter::fromDsn('sendgrid://sg_test_key@default'); | ||
|
|
||
| $this->assertInstanceOf(Sendgrid::class, $adapter); | ||
| $this->assertSame('sg_test_key', $this->readProperty($adapter, 'apiKey')); | ||
| } | ||
|
|
||
| public function test_creates_mailgun_adapter_from_dsn(): void | ||
| { | ||
| $adapter = EmailAdapter::fromDsn('mailgun://mg_test_key@example.com?eu=1'); | ||
|
|
||
| $this->assertInstanceOf(Mailgun::class, $adapter); | ||
| $this->assertSame('mg_test_key', $this->readProperty($adapter, 'apiKey')); | ||
| $this->assertSame('example.com', $this->readProperty($adapter, 'domain')); | ||
| $this->assertTrue($this->readProperty($adapter, 'isEU')); | ||
| } | ||
|
|
||
| public function test_rejects_unsupported_scheme(): void | ||
| { | ||
| $this->expectException(\InvalidArgumentException::class); | ||
| $this->expectExceptionMessage('Unsupported email DSN scheme "ses".'); | ||
|
|
||
| EmailAdapter::fromDsn('ses://key@default'); | ||
| } | ||
|
|
||
| public function test_rejects_invalid_dsn(): void | ||
| { | ||
| $this->expectException(\InvalidArgumentException::class); | ||
| $this->expectExceptionMessage('Invalid email DSN.'); | ||
|
|
||
| EmailAdapter::fromDsn('not a dsn'); | ||
| } | ||
|
|
||
| public function test_rejects_malformed_smtp_dsn(): void | ||
| { | ||
| $this->expectException(\InvalidArgumentException::class); | ||
| $this->expectExceptionMessage('Invalid email DSN.'); | ||
|
|
||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| EmailAdapter::fromDsn('smtp://'); | ||
| } | ||
|
|
||
| public function test_rejects_missing_resend_api_key(): void | ||
| { | ||
| $this->expectException(\InvalidArgumentException::class); | ||
| $this->expectExceptionMessage('Resend DSN must include an API key.'); | ||
|
|
||
| EmailAdapter::fromDsn('resend://@default'); | ||
| } | ||
|
|
||
| public function test_rejects_invalid_boolean_query_value(): void | ||
| { | ||
| $this->expectException(\InvalidArgumentException::class); | ||
| $this->expectExceptionMessage('Invalid "autotls" option. Expected boolean-like value.'); | ||
|
|
||
| EmailAdapter::fromDsn('smtp://mail.example.com?autotls=maybe'); | ||
| } | ||
|
|
||
| public function test_rejects_invalid_integer_query_value(): void | ||
| { | ||
| $this->expectException(\InvalidArgumentException::class); | ||
| $this->expectExceptionMessage('Invalid "timeout" option. Expected integer value.'); | ||
|
|
||
| EmailAdapter::fromDsn('smtp://mail.example.com?timeout=fast'); | ||
| } | ||
|
|
||
| public function test_rejects_invalid_secure_query_value(): void | ||
| { | ||
| $this->expectException(\InvalidArgumentException::class); | ||
| $this->expectExceptionMessage('Invalid SMTP "secure" option. Expected "", "ssl", or "tls".'); | ||
|
|
||
| EmailAdapter::fromDsn('smtp://mail.example.com?secure=starttls'); | ||
| } | ||
|
|
||
| private function readProperty(object $object, string $property): mixed | ||
| { | ||
| $reflection = new \ReflectionClass($object); | ||
| $property = $reflection->getProperty($property); | ||
|
|
||
| return $property->getValue($object); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The precedence
$query['port'] > $parts['port'] > scheme-defaultmeanssmtp://host:587?port=25resolves to port 25. Most DSN parsers treat the URL authority port as authoritative. This is a subtle gotcha worth a brief inline comment, or consider flipping the precedence toURL port > query port > scheme default.