Skip to content

Commit

Permalink
Update sms mock to use request catcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Meldiron committed Sep 19, 2022
1 parent 19eb6c7 commit 6e8a4cc
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions app/controllers/api/account.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -930,7 +929,7 @@
])));
}

$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([
Expand Down Expand Up @@ -2258,7 +2257,7 @@
$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([
Expand Down
2 changes: 1 addition & 1 deletion app/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@ function getDevice($root): Device
$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),
Expand Down
6 changes: 3 additions & 3 deletions app/workers/messaging.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use Appwrite\Auth\SMS;
use Appwrite\SMS\Adapter;
use Appwrite\SMS\Adapter\Mock;
use Appwrite\SMS\Adapter\Telesign;
use Appwrite\SMS\Adapter\TextMagic;
Expand All @@ -19,7 +19,7 @@

class MessagingV1 extends Worker
{
protected ?SMS $sms = null;
protected ?Adapter $sms = null;
protected ?string $from = null;

public function getName(): string
Expand All @@ -34,7 +34,7 @@ public function init(): void
$secret = $dsn->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),
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions src/Appwrite/SMS/Adapter/Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

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
Expand All @@ -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}",
]
);
}
}
27 changes: 23 additions & 4 deletions tests/e2e/Services/Account/AccountCustomClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,19 @@ public function testCreatePhone(): array

$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;

Expand Down Expand Up @@ -931,7 +943,13 @@ public function testPhoneVerification(array $data): array
$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']
]);
}

/**
Expand All @@ -941,6 +959,7 @@ public function testUpdatePhoneVerification($data): array
{
$id = $data['id'] ?? '';
$session = $data['session'] ?? '';
$secret = $data['token'] ?? '';

/**
* Test for SUCCESS
Expand All @@ -952,7 +971,7 @@ public function testUpdatePhoneVerification($data): array
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session,
]), [
'userId' => $id,
'secret' => Mock::$digits,
'secret' => $secret,
]);

$this->assertEquals(200, $response['headers']['status-code']);
Expand All @@ -967,7 +986,7 @@ public function testUpdatePhoneVerification($data): array
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session,
]), [
'userId' => ID::custom('ewewe'),
'secret' => Mock::$digits,
'secret' => $secret,
]);

$this->assertEquals(404, $response['headers']['status-code']);
Expand Down

0 comments on commit 6e8a4cc

Please sign in to comment.