diff --git a/.env b/.env index 227ea1067..65fb54cb0 100644 --- a/.env +++ b/.env @@ -56,7 +56,7 @@ _APP_SMTP_PORT=1025 _APP_SMTP_SECURE= _APP_SMTP_USERNAME= _APP_SMTP_PASSWORD= -_APP_SMS_PROVIDER=sms://mock +_APP_SMS_PROVIDER=sms://username:password@mock _APP_SMS_FROM=+123456789 _APP_STORAGE_LIMIT=30000000 _APP_STORAGE_PREVIEW_LIMIT=20000000 diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index ef94b251c..207926341 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -2,7 +2,6 @@ use Ahc\Jwt\JWT; use Appwrite\Auth\Auth; -use Appwrite\SMS\Adapter\Mock; use Appwrite\Auth\Validator\Password; use Appwrite\Auth\Validator\Phone; use Appwrite\Detector\Detector; @@ -930,7 +929,7 @@ App::post('/v1/account/sessions/phone') ]))); } - $secret = (App::getEnv('_APP_SMS_PROVIDER') === 'sms://mock') ? Mock::$digits : Auth::codeGenerator(); + $secret = Auth::codeGenerator(); $expire = DateTime::addSeconds(new \DateTime(), Auth::TOKEN_EXPIRATION_PHONE); $token = new Document([ @@ -2258,7 +2257,7 @@ App::post('/v1/account/verification/phone') $isPrivilegedUser = Auth::isPrivilegedUser($roles); $isAppUser = Auth::isAppUser($roles); $verificationSecret = Auth::tokenGenerator(); - $secret = (App::getEnv('_APP_SMS_PROVIDER') === 'sms://mock') ? Mock::$digits : Auth::codeGenerator(); + $secret = Auth::codeGenerator(); $expire = DateTime::addSeconds(new \DateTime(), Auth::TOKEN_EXPIRATION_CONFIRM); $verification = new Document([ diff --git a/app/init.php b/app/init.php index 3c9f785f9..2e1ca7a49 100644 --- a/app/init.php +++ b/app/init.php @@ -1028,7 +1028,7 @@ App::setResource('sms', function () { $secret = $dsn->getPassword(); return match ($dsn->getHost()) { - 'mock' => new Mock('', ''), // used for tests + 'mock' => new Mock($user, $secret), // used for tests 'twilio' => new Twilio($user, $secret), 'text-magic' => new TextMagic($user, $secret), 'telesign' => new Telesign($user, $secret), diff --git a/app/workers/messaging.php b/app/workers/messaging.php index 1fc5deaf4..afd99faa8 100644 --- a/app/workers/messaging.php +++ b/app/workers/messaging.php @@ -1,6 +1,6 @@ getPassword(); $this->sms = match ($dsn->getHost()) { - 'mock' => new Mock('', ''), // used for tests + 'mock' => new Mock($user, $secret), // used for tests 'twilio' => new Twilio($user, $secret), 'text-magic' => new TextMagic($user, $secret), 'telesign' => new Telesign($user, $secret), diff --git a/composer.lock b/composer.lock index 532e73ddb..3f8d4bc04 100644 --- a/composer.lock +++ b/composer.lock @@ -4124,16 +4124,16 @@ }, { "name": "sebastian/comparator", - "version": "4.0.7", + "version": "4.0.8", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/comparator.git", - "reference": "7fa545db548c90bdebeb9da0583001a252be5578" + "reference": "fa0f136dd2334583309d32b62544682ee972b51a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/7fa545db548c90bdebeb9da0583001a252be5578", - "reference": "7fa545db548c90bdebeb9da0583001a252be5578", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/fa0f136dd2334583309d32b62544682ee972b51a", + "reference": "fa0f136dd2334583309d32b62544682ee972b51a", "shasum": "" }, "require": { @@ -4186,7 +4186,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/comparator/issues", - "source": "https://github.com/sebastianbergmann/comparator/tree/4.0.7" + "source": "https://github.com/sebastianbergmann/comparator/tree/4.0.8" }, "funding": [ { @@ -4194,7 +4194,7 @@ "type": "github" } ], - "time": "2022-09-14T06:33:43+00:00" + "time": "2022-09-14T12:41:17+00:00" }, { "name": "sebastian/complexity", diff --git a/src/Appwrite/SMS/Adapter/Mock.php b/src/Appwrite/SMS/Adapter/Mock.php index 147f7ec9b..52f2fec57 100644 --- a/src/Appwrite/SMS/Adapter/Mock.php +++ b/src/Appwrite/SMS/Adapter/Mock.php @@ -4,12 +4,13 @@ namespace Appwrite\SMS\Adapter; use Appwrite\SMS\Adapter; +// Mock adapter used to E2E test worker class Mock extends Adapter { /** * @var string */ - public static string $digits = '123456'; + private string $endpoint = 'http://request-catcher:5000/mock-sms'; /** * @param string $from @@ -19,6 +20,19 @@ class Mock extends Adapter */ public function send(string $from, string $to, string $message): void { - return; + $this->request( + method: 'POST', + url: $this->endpoint, + payload: \json_encode([ + 'message' => $message, + 'from' => $from, + 'to' => $to + ]), + headers: [ + "content-type: application/json", + "x-username: {$this->user}", + "x-key: {$this->secret}", + ] + ); } } diff --git a/tests/e2e/Services/Account/AccountCustomClientTest.php b/tests/e2e/Services/Account/AccountCustomClientTest.php index 541e8e79f..0b7ef2d5b 100644 --- a/tests/e2e/Services/Account/AccountCustomClientTest.php +++ b/tests/e2e/Services/Account/AccountCustomClientTest.php @@ -716,7 +716,19 @@ class AccountCustomClientTest extends Scope $this->assertEquals(400, $response['headers']['status-code']); - $data['token'] = Mock::$digits; + \sleep(2); + + $smsRequest = $this->getLastRequest(); + + $this->assertEquals('http://request-catcher:5000/mock-sms', $smsRequest['url']); + $this->assertEquals('Appwrite Phone Authentication', $smsRequest['headers']['User-Agent']); + $this->assertEquals('username', $smsRequest['headers']['X-Username']); + $this->assertEquals('password', $smsRequest['headers']['X-Key']); + $this->assertEquals('POST', $smsRequest['method']); + $this->assertEquals('+123456789', $smsRequest['data']['from']); + $this->assertEquals($number, $smsRequest['data']['to']); + + $data['token'] = $smsRequest['data']['message']; $data['id'] = $userId; $data['number'] = $number; @@ -931,7 +943,13 @@ class AccountCustomClientTest extends Scope $this->assertEmpty($response['body']['secret']); $this->assertEquals(true, DateTime::isValid($response['body']['expire'])); - return $data; + \sleep(2); + + $smsRequest = $this->getLastRequest(); + + return \array_merge($data, [ + 'token' => $smsRequest['data']['message'] + ]); } /** @@ -941,6 +959,7 @@ class AccountCustomClientTest extends Scope { $id = $data['id'] ?? ''; $session = $data['session'] ?? ''; + $secret = $data['token'] ?? ''; /** * Test for SUCCESS @@ -952,7 +971,7 @@ class AccountCustomClientTest extends Scope 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session, ]), [ 'userId' => $id, - 'secret' => Mock::$digits, + 'secret' => $secret, ]); $this->assertEquals(200, $response['headers']['status-code']); @@ -967,7 +986,7 @@ class AccountCustomClientTest extends Scope 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session, ]), [ 'userId' => ID::custom('ewewe'), - 'secret' => Mock::$digits, + 'secret' => $secret, ]); $this->assertEquals(404, $response['headers']['status-code']);