1
0
Fork 0
mirror of synced 2024-09-20 11:37:45 +12:00

feat: add tests

This commit is contained in:
Christy Jacob 2024-02-14 12:10:19 +05:30
parent be92aa92e5
commit 28a16173ab
3 changed files with 209 additions and 33 deletions

View file

@ -1288,11 +1288,13 @@ App::post('/v1/account/sessions/phone')
Authorization::skip(fn () => $dbForProject->createDocument('users', $user));
}
$mockNumbers = $project->getAttribute('auths', [])['mockNumbers'] ?? [];
$secret = null;
$triggerSMS = true;
$mockNumbers = $project->getAttribute('auths', [])['mockNumbers'] ?? [];
foreach ($mockNumbers as $mockNumber) {
if ($mockNumber['number'] === $phone) {
if ($mockNumber['phone'] === $phone) {
$secret = $mockNumber['otp'];
$triggerSMS = false;
break;
}
}
@ -1322,6 +1324,7 @@ App::post('/v1/account/sessions/phone')
$dbForProject->deleteCachedDocument('users', $user->getId());
if ($triggerSMS) {
$message = Template::fromFile(__DIR__ . '/../../config/locale/templates/sms-base.tpl');
$customTemplate = $project->getAttribute('templates', [])['sms.login-' . $locale->default] ?? [];
@ -1336,6 +1339,7 @@ App::post('/v1/account/sessions/phone')
->setRecipient($phone)
->setMessage($message)
->trigger();
}
$queueForEvents->setPayload(
$response->output(
@ -2897,11 +2901,13 @@ App::post('/v1/account/verification/phone')
$isPrivilegedUser = Auth::isPrivilegedUser($roles);
$isAppUser = Auth::isAppUser($roles);
$mockNumbers = $project->getAttribute('auths', [])['mockNumbers'] ?? [];
$secret = null;
$triggerSMS = true;
$mockNumbers = $project->getAttribute('auths', [])['mockNumbers'] ?? [];
foreach ($mockNumbers as $mockNumber) {
if ($mockNumber['number'] === $phone) {
if ($mockNumber['phone'] === $phone) {
$secret = $mockNumber['otp'];
$triggerSMS = false;
break;
}
}
@ -2931,6 +2937,7 @@ App::post('/v1/account/verification/phone')
$dbForProject->deleteCachedDocument('users', $user->getId());
if ($triggerSMS) {
$message = Template::fromFile(__DIR__ . '/../../config/locale/templates/sms-base.tpl');
$customTemplate = $project->getAttribute('templates', [])['sms.verification-' . $locale->default] ?? [];
@ -2946,6 +2953,7 @@ App::post('/v1/account/verification/phone')
->setMessage($message)
->trigger()
;
}
$queueForEvents
->setParam('userId', $user->getId())

View file

@ -35,6 +35,11 @@ class MockNumber extends Validator
*/
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();
if (!$phone->isValid($value['phone'])) {
$this->message = $phone->getDescription();
@ -43,7 +48,7 @@ class MockNumber extends Validator
$otp = new Text(6, 6);
if (!$otp->isValid($value['otp'])) {
$this->message = $otp->getDescription();
$this->message = 'OTP must be a valid string and exactly 6 characters.';
return false;
}

View file

@ -18,8 +18,8 @@ class ProjectsConsoleClientTest extends Scope
use SideClient;
/**
* @group smtpAndTemplates
* @group projectsCRUD */
* @group testing
* */
public function testCreateProject(): array
{
/**
@ -1355,6 +1355,169 @@ class ProjectsConsoleClientTest extends Scope
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
*/