diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 80d407322e..e0f8013467 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -2071,8 +2071,14 @@ App::patch('/v1/projects/:projectId/smtp') // validate SMTP settings if ($enabled) { + if (empty($username)) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'SMTP Username is required when enabling SMTP.'); + } elseif (empty($password)) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'SMTP Password is required when enabling SMTP.'); + } $mail = new PHPMailer(true); $mail->isSMTP(); + $mail->SMTPAuth = true; $mail->Username = $username; $mail->Password = $password; $mail->Host = $host; diff --git a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php index e297757225..7e81ca9db0 100644 --- a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php +++ b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php @@ -564,6 +564,8 @@ class ProjectsConsoleClientTest extends Scope public function testUpdateProjectSMTP($data): array { $id = $data['projectId']; + + /**Test for SUCCESS: Valid Credentials*/ $response = $this->client->call(Client::METHOD_PATCH, '/projects/' . $id . '/smtp', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -603,6 +605,24 @@ class ProjectsConsoleClientTest extends Scope $this->assertEquals('password', $response['body']['smtpPassword']); $this->assertEquals('', $response['body']['smtpSecure']); + /** Test for Missing or Invalid Credentials*/ + $response = $this->client->call(Client::METHOD_PATCH, '/projects/' . $id . '/smtp', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'enabled' => true, + 'senderEmail' => 'fail@appwrite.io', + 'senderName' => 'Failing Mailer', + 'host' => 'maildev', + 'port' => 1025, + 'username' => 'invalid-user', + 'password' => 'bad-password', + ]); + + $this->assertEquals(400, $response['headers']['status-code']); + $this->assertEquals(Exception::PROJECT_SMTP_CONFIG_INVALID, $response['body']['type']); + $this->assertStringContainsStringIgnoringCase('SMTP authentication failed.', $response['body']['message']); + return $data; }