From ae549a1e9ab45f430520390a673d7b03e0892664 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 14 Mar 2023 07:21:56 +0000 Subject: [PATCH] tests and fixes --- app/controllers/api/projects.php | 5 +- src/Appwrite/Utopia/Response.php | 4 +- .../Utopia/Response/Model/EmailTemplate.php | 2 +- .../Utopia/Response/Model/SmsTemplate.php | 7 +- .../Utopia/Response/Model/Template.php | 1 - .../Projects/ProjectsConsoleClientTest.php | 76 +++++++++++++++++++ 6 files changed, 87 insertions(+), 8 deletions(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index e656b92722..0177728240 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -1800,9 +1800,10 @@ App::get('/v1/projects/:projectId/templates/email/:type/:locale') $template = $templates['email.' . $type . '-' . $locale] ?? null; if (is_null($template)) { + $localeFormated = substr($locale, 0, 2); $template = [ - 'message' => Template::fromFile(__DIR__ . '/../../config/locale/templates/email-base.tpl'), - 'subject' => (new Locale($locale))->getText('emails.' . $type . '.subject'), + 'message' => Template::fromFile(__DIR__ . '/../../config/locale/templates/email-base.tpl')->render(), + 'subject' => (new Locale($localeFormated))->getText('emails.' . $type . '.subject'), 'senderEmail' => App::getEnv('_APP_SYSTEM_EMAIL_ADDRESS', ''), 'senderName' => App::getEnv('_APP_SYSTEM_EMAIL_NAME', '') ]; diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 8aeca8680d..dc0505f647 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -74,7 +74,7 @@ use Appwrite\Utopia\Response\Model\HealthVersion; use Appwrite\Utopia\Response\Model\Mock; // Keep last use Appwrite\Utopia\Response\Model\Provider; use Appwrite\Utopia\Response\Model\Runtime; -use Appwrite\Utopia\Response\Model\Template; +use Appwrite\Utopia\Response\Model\SmsTemplate; use Appwrite\Utopia\Response\Model\UsageBuckets; use Appwrite\Utopia\Response\Model\UsageCollection; use Appwrite\Utopia\Response\Model\UsageDatabase; @@ -345,7 +345,7 @@ class Response extends SwooleResponse ->setModel(new UsageFunctions()) ->setModel(new UsageFunction()) ->setModel(new UsageProject()) - ->setModel(new Template()) + ->setModel(new SmsTemplate()) ->setModel(new EmailTemplate()) // Verification // Recovery diff --git a/src/Appwrite/Utopia/Response/Model/EmailTemplate.php b/src/Appwrite/Utopia/Response/Model/EmailTemplate.php index 824815f90c..e08b0eb4b5 100644 --- a/src/Appwrite/Utopia/Response/Model/EmailTemplate.php +++ b/src/Appwrite/Utopia/Response/Model/EmailTemplate.php @@ -3,12 +3,12 @@ namespace Appwrite\Utopia\Response\Model; use Appwrite\Utopia\Response; -use Appwrite\Utopia\Response\Model; class EmailTemplate extends Template { public function __construct() { + parent::__construct(); $this ->addRule('senderName', [ 'type' => self::TYPE_STRING, diff --git a/src/Appwrite/Utopia/Response/Model/SmsTemplate.php b/src/Appwrite/Utopia/Response/Model/SmsTemplate.php index c99f1ed571..cb8fd58498 100644 --- a/src/Appwrite/Utopia/Response/Model/SmsTemplate.php +++ b/src/Appwrite/Utopia/Response/Model/SmsTemplate.php @@ -3,10 +3,13 @@ namespace Appwrite\Utopia\Response\Model; use Appwrite\Utopia\Response; -use Appwrite\Utopia\Response\Model; -class SmsTemplate extends Model +class SmsTemplate extends Template { + public function __construct() + { + parent::__construct(); + } /** * Get Name * diff --git a/src/Appwrite/Utopia/Response/Model/Template.php b/src/Appwrite/Utopia/Response/Model/Template.php index 39ad93b7be..3ce9cacdb3 100644 --- a/src/Appwrite/Utopia/Response/Model/Template.php +++ b/src/Appwrite/Utopia/Response/Model/Template.php @@ -2,7 +2,6 @@ namespace Appwrite\Utopia\Response\Model; -use Appwrite\Utopia\Response; use Appwrite\Utopia\Response\Model; abstract class Template extends Model diff --git a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php index ac5f3b973f..ae102caded 100644 --- a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php +++ b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php @@ -18,6 +18,9 @@ class ProjectsConsoleClientTest extends Scope use ProjectConsole; use SideClient; + /** + * @group smtpAndTemplates + */ public function testCreateProject(): array { /** @@ -430,6 +433,7 @@ class ProjectsConsoleClientTest extends Scope } /** + * @group smtpAndTemplates * @depends testCreateProject */ public function testUpdateProjectSMTP($data): array @@ -455,6 +459,78 @@ class ProjectsConsoleClientTest extends Scope $this->assertEquals('emailuser', $response['body']['smtpUsername']); $this->assertEquals('securepassword', $response['body']['smtpPassword']); $this->assertEquals('', $response['body']['smtpSecure']); + + /** Test Reading Project */ + $response = $this->client->call(Client::METHOD_GET, '/projects/' . $id, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertTrue($response['body']['smtpEnabled']); + $this->assertEquals('mailer@appwrite.io', $response['body']['smtpSender']); + $this->assertEquals('mail.appwrite.io', $response['body']['smtpHost']); + $this->assertEquals(25, $response['body']['smtpPort']); + $this->assertEquals('emailuser', $response['body']['smtpUsername']); + $this->assertEquals('securepassword', $response['body']['smtpPassword']); + $this->assertEquals('', $response['body']['smtpSecure']); + return $data; + } + + /** + * @group smtpAndTemplates + * @depends testCreateProject */ + public function testUpdateTemplates($data): array + { + $id = $data['projectId']; + + /** Get Default Template */ + $response = $this->client->call(Client::METHOD_GET, '/projects/' . $id . '/templates/email/verification/en_us', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals('Account Verification', $response['body']['subject']); + $this->assertEquals('Appwrite', $response['body']['senderName']); + $this->assertEquals('team@appwrite.io', $response['body']['senderEmail']); + $this->assertEquals('verification', $response['body']['type']); + $this->assertEquals('en_us', $response['body']['locale']); + $this->assertMatchesRegularExpression('//', $response['body']['message']); + + /** Update template */ + $response = $this->client->call(Client::METHOD_PATCH, '/projects/' . $id . '/templates/email/verification/en_us', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'subject' => 'Please verify your email', + 'message' => 'Please verify your email {{url}}', + 'senderName' => 'Appwrite Custom', + 'senderEmail' => 'custom@appwrite.io', + ]); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals('Please verify your email', $response['body']['subject']); + $this->assertEquals('Appwrite Custom', $response['body']['senderName']); + $this->assertEquals('custom@appwrite.io', $response['body']['senderEmail']); + $this->assertEquals('verification', $response['body']['type']); + $this->assertEquals('en_us', $response['body']['locale']); + $this->assertEquals('Please verify your email {{url}}', $response['body']['message']); + + /** Get Updated Template */ + $response = $this->client->call(Client::METHOD_GET, '/projects/' . $id . '/templates/email/verification/en_us', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals('Please verify your email', $response['body']['subject']); + $this->assertEquals('Appwrite Custom', $response['body']['senderName']); + $this->assertEquals('custom@appwrite.io', $response['body']['senderEmail']); + $this->assertEquals('verification', $response['body']['type']); + $this->assertEquals('en_us', $response['body']['locale']); + $this->assertEquals('Please verify your email {{url}}', $response['body']['message']); + return $data; }