feat: add tests
This commit is contained in:
parent
be92aa92e5
commit
28a16173ab
3 changed files with 209 additions and 33 deletions
|
@ -1288,11 +1288,13 @@ App::post('/v1/account/sessions/phone')
|
||||||
Authorization::skip(fn () => $dbForProject->createDocument('users', $user));
|
Authorization::skip(fn () => $dbForProject->createDocument('users', $user));
|
||||||
}
|
}
|
||||||
|
|
||||||
$mockNumbers = $project->getAttribute('auths', [])['mockNumbers'] ?? [];
|
|
||||||
$secret = null;
|
$secret = null;
|
||||||
|
$triggerSMS = true;
|
||||||
|
$mockNumbers = $project->getAttribute('auths', [])['mockNumbers'] ?? [];
|
||||||
foreach ($mockNumbers as $mockNumber) {
|
foreach ($mockNumbers as $mockNumber) {
|
||||||
if ($mockNumber['number'] === $phone) {
|
if ($mockNumber['phone'] === $phone) {
|
||||||
$secret = $mockNumber['otp'];
|
$secret = $mockNumber['otp'];
|
||||||
|
$triggerSMS = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1322,21 +1324,23 @@ App::post('/v1/account/sessions/phone')
|
||||||
|
|
||||||
$dbForProject->deleteCachedDocument('users', $user->getId());
|
$dbForProject->deleteCachedDocument('users', $user->getId());
|
||||||
|
|
||||||
$message = Template::fromFile(__DIR__ . '/../../config/locale/templates/sms-base.tpl');
|
if ($triggerSMS) {
|
||||||
|
$message = Template::fromFile(__DIR__ . '/../../config/locale/templates/sms-base.tpl');
|
||||||
|
|
||||||
$customTemplate = $project->getAttribute('templates', [])['sms.login-' . $locale->default] ?? [];
|
$customTemplate = $project->getAttribute('templates', [])['sms.login-' . $locale->default] ?? [];
|
||||||
if (!empty($customTemplate)) {
|
if (!empty($customTemplate)) {
|
||||||
$message = $customTemplate['message'] ?? $message;
|
$message = $customTemplate['message'] ?? $message;
|
||||||
|
}
|
||||||
|
|
||||||
|
$message = $message->setParam('{{token}}', $secret);
|
||||||
|
$message = $message->render();
|
||||||
|
|
||||||
|
$queueForMessaging
|
||||||
|
->setRecipient($phone)
|
||||||
|
->setMessage($message)
|
||||||
|
->trigger();
|
||||||
}
|
}
|
||||||
|
|
||||||
$message = $message->setParam('{{token}}', $secret);
|
|
||||||
$message = $message->render();
|
|
||||||
|
|
||||||
$queueForMessaging
|
|
||||||
->setRecipient($phone)
|
|
||||||
->setMessage($message)
|
|
||||||
->trigger();
|
|
||||||
|
|
||||||
$queueForEvents->setPayload(
|
$queueForEvents->setPayload(
|
||||||
$response->output(
|
$response->output(
|
||||||
$token->setAttribute('secret', $secret),
|
$token->setAttribute('secret', $secret),
|
||||||
|
@ -1428,7 +1432,7 @@ App::put('/v1/account/sessions/phone')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* We act like we're updating and validating
|
* We act like we're updating and validating
|
||||||
* the recovery token but actually we don't need it anymore.
|
* the recovery token but actually we don't need it anymore.
|
||||||
*/
|
*/
|
||||||
$dbForProject->deleteDocument('tokens', $token);
|
$dbForProject->deleteDocument('tokens', $token);
|
||||||
$dbForProject->deleteCachedDocument('users', $user->getId());
|
$dbForProject->deleteCachedDocument('users', $user->getId());
|
||||||
|
@ -2897,11 +2901,13 @@ App::post('/v1/account/verification/phone')
|
||||||
$isPrivilegedUser = Auth::isPrivilegedUser($roles);
|
$isPrivilegedUser = Auth::isPrivilegedUser($roles);
|
||||||
$isAppUser = Auth::isAppUser($roles);
|
$isAppUser = Auth::isAppUser($roles);
|
||||||
|
|
||||||
$mockNumbers = $project->getAttribute('auths', [])['mockNumbers'] ?? [];
|
|
||||||
$secret = null;
|
$secret = null;
|
||||||
|
$triggerSMS = true;
|
||||||
|
$mockNumbers = $project->getAttribute('auths', [])['mockNumbers'] ?? [];
|
||||||
foreach ($mockNumbers as $mockNumber) {
|
foreach ($mockNumbers as $mockNumber) {
|
||||||
if ($mockNumber['number'] === $phone) {
|
if ($mockNumber['phone'] === $phone) {
|
||||||
$secret = $mockNumber['otp'];
|
$secret = $mockNumber['otp'];
|
||||||
|
$triggerSMS = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2931,22 +2937,24 @@ App::post('/v1/account/verification/phone')
|
||||||
|
|
||||||
$dbForProject->deleteCachedDocument('users', $user->getId());
|
$dbForProject->deleteCachedDocument('users', $user->getId());
|
||||||
|
|
||||||
$message = Template::fromFile(__DIR__ . '/../../config/locale/templates/sms-base.tpl');
|
if ($triggerSMS) {
|
||||||
|
$message = Template::fromFile(__DIR__ . '/../../config/locale/templates/sms-base.tpl');
|
||||||
|
|
||||||
$customTemplate = $project->getAttribute('templates', [])['sms.verification-' . $locale->default] ?? [];
|
$customTemplate = $project->getAttribute('templates', [])['sms.verification-' . $locale->default] ?? [];
|
||||||
if (!empty($customTemplate)) {
|
if (!empty($customTemplate)) {
|
||||||
$message = $customTemplate['message'] ?? $message;
|
$message = $customTemplate['message'] ?? $message;
|
||||||
|
}
|
||||||
|
|
||||||
|
$message = $message->setParam('{{token}}', $secret);
|
||||||
|
$message = $message->render();
|
||||||
|
|
||||||
|
$queueForMessaging
|
||||||
|
->setRecipient($phone)
|
||||||
|
->setMessage($message)
|
||||||
|
->trigger()
|
||||||
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
$message = $message->setParam('{{token}}', $secret);
|
|
||||||
$message = $message->render();
|
|
||||||
|
|
||||||
$queueForMessaging
|
|
||||||
->setRecipient($phone)
|
|
||||||
->setMessage($message)
|
|
||||||
->trigger()
|
|
||||||
;
|
|
||||||
|
|
||||||
$queueForEvents
|
$queueForEvents
|
||||||
->setParam('userId', $user->getId())
|
->setParam('userId', $user->getId())
|
||||||
->setParam('tokenId', $verification->getId())
|
->setParam('tokenId', $verification->getId())
|
||||||
|
|
|
@ -35,6 +35,11 @@ class MockNumber extends Validator
|
||||||
*/
|
*/
|
||||||
public function isValid($value): bool
|
public function isValid($value): bool
|
||||||
{
|
{
|
||||||
|
if (!\is_array($value) || !isset($value['phone']) || !isset($value['otp'])) {
|
||||||
|
$this->message = 'Invalid payload structure. Please check the "phone" and "otp" fields';
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
$phone = new Phone();
|
$phone = new Phone();
|
||||||
if (!$phone->isValid($value['phone'])) {
|
if (!$phone->isValid($value['phone'])) {
|
||||||
$this->message = $phone->getDescription();
|
$this->message = $phone->getDescription();
|
||||||
|
@ -43,7 +48,7 @@ class MockNumber extends Validator
|
||||||
|
|
||||||
$otp = new Text(6, 6);
|
$otp = new Text(6, 6);
|
||||||
if (!$otp->isValid($value['otp'])) {
|
if (!$otp->isValid($value['otp'])) {
|
||||||
$this->message = $otp->getDescription();
|
$this->message = 'OTP must be a valid string and exactly 6 characters.';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,8 +18,8 @@ class ProjectsConsoleClientTest extends Scope
|
||||||
use SideClient;
|
use SideClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @group smtpAndTemplates
|
* @group testing
|
||||||
* @group projectsCRUD */
|
* */
|
||||||
public function testCreateProject(): array
|
public function testCreateProject(): array
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
@ -1355,6 +1355,169 @@ class ProjectsConsoleClientTest extends Scope
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group testing
|
||||||
|
* @depends testCreateProject
|
||||||
|
*/
|
||||||
|
public function testUpdateMockNumbers($data)
|
||||||
|
{
|
||||||
|
$id = $data['projectId'] ?? '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for Failure
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** Trying to pass an empty body to the endpoint */
|
||||||
|
$response = $this->client->call(Client::METHOD_PATCH, '/projects/' . $id . '/auth/mock-numbers', array_merge([
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $this->getProject()['$id'],
|
||||||
|
], $this->getHeaders()), [
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals(400, $response['headers']['status-code']);
|
||||||
|
$this->assertEquals('Param "numbers" is not optional.', $response['body']['message']);
|
||||||
|
|
||||||
|
/** Trying to pass body with incorrect structure */
|
||||||
|
$response = $this->client->call(Client::METHOD_PATCH, '/projects/' . $id . '/auth/mock-numbers', array_merge([
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $this->getProject()['$id'],
|
||||||
|
], $this->getHeaders()), [
|
||||||
|
'numbers' => [
|
||||||
|
'phone'=> '+1655513432',
|
||||||
|
'otp' => '123456'
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
$this->assertEquals(400, $response['headers']['status-code']);
|
||||||
|
$this->assertEquals('Invalid `numbers` param: Value must a valid array and Invalid payload structure. Please check the "phone" and "otp" fields', $response['body']['message']);
|
||||||
|
|
||||||
|
/** Trying to pass an OTP longer than 6 characters*/
|
||||||
|
$response = $this->client->call(Client::METHOD_PATCH, '/projects/' . $id . '/auth/mock-numbers', array_merge([
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $this->getProject()['$id'],
|
||||||
|
], $this->getHeaders()), [
|
||||||
|
'numbers' => [
|
||||||
|
[
|
||||||
|
'phone'=> '+1655513432',
|
||||||
|
'otp' => '12345678'
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
$this->assertEquals(400, $response['headers']['status-code']);
|
||||||
|
$this->assertEquals('Invalid `numbers` param: Value must a valid array and OTP must be a valid string and exactly 6 characters.', $response['body']['message']);
|
||||||
|
|
||||||
|
/** Trying to pass an OTP shorter than 6 characters*/
|
||||||
|
$response = $this->client->call(Client::METHOD_PATCH, '/projects/' . $id . '/auth/mock-numbers', array_merge([
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $this->getProject()['$id'],
|
||||||
|
], $this->getHeaders()), [
|
||||||
|
'numbers' => [
|
||||||
|
[
|
||||||
|
'phone'=> '+1655513432',
|
||||||
|
'otp' => '123'
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
$this->assertEquals(400, $response['headers']['status-code']);
|
||||||
|
$this->assertEquals('Invalid `numbers` param: Value must a valid array and OTP must be a valid string and exactly 6 characters.', $response['body']['message']);
|
||||||
|
|
||||||
|
/** Trying to pass an invalid phone number */
|
||||||
|
$response = $this->client->call(Client::METHOD_PATCH, '/projects/' . $id . '/auth/mock-numbers', array_merge([
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $this->getProject()['$id'],
|
||||||
|
], $this->getHeaders()), [
|
||||||
|
'numbers' => [
|
||||||
|
[
|
||||||
|
'phone'=> '1655234',
|
||||||
|
'otp' => '123456'
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
$this->assertEquals(400, $response['headers']['status-code']);
|
||||||
|
$this->assertEquals('Invalid `numbers` param: Value must a valid array and Phone number must start with a \'+\' can have a maximum of fifteen digits.', $response['body']['message']);
|
||||||
|
|
||||||
|
/** Trying to pass a number longer than 15 digits */
|
||||||
|
$response = $this->client->call(Client::METHOD_PATCH, '/projects/' . $id . '/auth/mock-numbers', array_merge([
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $this->getProject()['$id'],
|
||||||
|
], $this->getHeaders()), [
|
||||||
|
'numbers' => [
|
||||||
|
[
|
||||||
|
'phone'=> '+1234567890987654',
|
||||||
|
'otp' => '123456'
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
$this->assertEquals(400, $response['headers']['status-code']);
|
||||||
|
$this->assertEquals('Invalid `numbers` param: Value must a valid array and Phone number must start with a \'+\' can have a maximum of fifteen digits.', $response['body']['message']);
|
||||||
|
|
||||||
|
$numbers = [];
|
||||||
|
for ($i = 0; $i < 11; $i++) {
|
||||||
|
$numbers[] = [
|
||||||
|
'phone'=> '+1655513432',
|
||||||
|
'otp' => '123456'
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
var_dump($numbers);
|
||||||
|
|
||||||
|
/** Trying to pass more than 10 values */
|
||||||
|
$response = $this->client->call(Client::METHOD_PATCH, '/projects/' . $id . '/auth/mock-numbers', array_merge([
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $this->getProject()['$id'],
|
||||||
|
], $this->getHeaders()), [
|
||||||
|
'numbers' => $numbers
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals(400, $response['headers']['status-code']);
|
||||||
|
var_dump($response['body']['message']);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for success
|
||||||
|
*/
|
||||||
|
$response = $this->client->call(Client::METHOD_PATCH, '/projects/' . $id . '/auth/mock-numbers', array_merge([
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $this->getProject()['$id'],
|
||||||
|
], $this->getHeaders()), [
|
||||||
|
'numbers' => []
|
||||||
|
]);
|
||||||
|
$this->assertEquals(200, $response['headers']['status-code']);
|
||||||
|
|
||||||
|
$response = $this->client->call(Client::METHOD_PATCH, '/projects/' . $id . '/auth/mock-numbers', array_merge([
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $this->getProject()['$id'],
|
||||||
|
], $this->getHeaders()), [
|
||||||
|
'numbers' => [
|
||||||
|
[
|
||||||
|
'phone'=> '+1655513432',
|
||||||
|
'otp' => '123456'
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
$this->assertEquals(200, $response['headers']['status-code']);
|
||||||
|
|
||||||
|
// Create phone session for this project and check if the mock number is used
|
||||||
|
$response = $this->client->call(Client::METHOD_POST, '/account/sessions/phone', array_merge([
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $id,
|
||||||
|
]), [
|
||||||
|
'userId' => 'unique()',
|
||||||
|
'phone' => '+1655513432',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals(201, $response['headers']['status-code']);
|
||||||
|
$userId = $response['body']['userId'];
|
||||||
|
|
||||||
|
$response = $this->client->call(Client::METHOD_PUT, '/account/sessions/phone', array_merge([
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $id,
|
||||||
|
]), [
|
||||||
|
'userId' => $userId,
|
||||||
|
'secret' => '123456',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals(201, $response['headers']['status-code']);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @depends testUpdateProjectAuthLimit
|
* @depends testUpdateProjectAuthLimit
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in a new issue