Added phone number to UsersAPI createUser + tests
This commit is contained in:
parent
1e96f8c3d9
commit
3088535806
|
@ -31,21 +31,27 @@ use MaxMind\Db\Reader;
|
|||
use Utopia\Validator\Integer;
|
||||
|
||||
/** TODO: Remove function when we move to using utopia/platform */
|
||||
function createUser(string $hash, mixed $hashOptions, string $userId, string $email, string $password, string $name, Database $dbForProject, Stats $usage, Event $events): Document
|
||||
function createUser(string $hash, mixed $hashOptions, string $userId, ?string $email, ?string $password, ?string $phone, string $name, Database $dbForProject, Stats $usage, Event $events): Document
|
||||
{
|
||||
$hashOptionsObject = (\is_string($hashOptions)) ? \json_decode($hashOptions, true) : $hashOptions; // Cast to JSON array
|
||||
$email = \strtolower($email);
|
||||
|
||||
if(!empty($email)) {
|
||||
$email = \strtolower($email);
|
||||
}
|
||||
|
||||
try {
|
||||
$userId = $userId == 'unique()' ? $dbForProject->getId() : $userId;
|
||||
|
||||
$user = $dbForProject->createDocument('users', new Document([
|
||||
'$id' => $userId,
|
||||
'$read' => ['role:all'],
|
||||
'$write' => ['user:' . $userId],
|
||||
'email' => $email,
|
||||
'emailVerification' => false,
|
||||
'phone' => $phone,
|
||||
'phoneVerification' => false,
|
||||
'status' => true,
|
||||
'password' => $hash === 'plaintext' ? Auth::passwordHash($password, $hash, $hashOptionsObject) : $password,
|
||||
'password' => (!empty($password)) ? ($hash === 'plaintext' ? Auth::passwordHash($password, $hash, $hashOptionsObject) : $password) : null,
|
||||
'hash' => $hash === 'plaintext' ? Auth::DEFAULT_ALGO : $hash,
|
||||
'hashOptions' => $hash === 'plaintext' ? Auth::DEFAULT_ALGO_OPTIONS : $hashOptions,
|
||||
'passwordUpdate' => \time(),
|
||||
|
@ -56,7 +62,7 @@ function createUser(string $hash, mixed $hashOptions, string $userId, string $em
|
|||
'sessions' => null,
|
||||
'tokens' => null,
|
||||
'memberships' => null,
|
||||
'search' => implode(' ', [$userId, $email, $name])
|
||||
'search' => implode(' ', [$userId, $email, $phone, $name])
|
||||
]));
|
||||
} catch (Duplicate $th) {
|
||||
throw new Exception(Exception::USER_ALREADY_EXISTS);
|
||||
|
@ -86,15 +92,16 @@ App::post('/v1/users')
|
|||
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
||||
->label('sdk.response.model', Response::MODEL_USER)
|
||||
->param('userId', '', new CustomId(), 'User ID. Choose your own unique ID or pass the string "unique()" to auto generate it. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.')
|
||||
->param('email', '', new Email(), 'User email.')
|
||||
->param('password', '', new Password(), 'Plain text user password. Must be at least 8 chars.')
|
||||
->param('email', null, new Email(), 'User email.', true)
|
||||
->param('phone', null, new Phone(), 'Phone number. Format this number with a leading \'+\' and a country code, e.g., +16175551212.', true)
|
||||
->param('password', null, new Password(), 'Plain text user password. Must be at least 8 chars.', true)
|
||||
->param('name', '', new Text(128), 'User name. Max length: 128 chars.', true)
|
||||
->inject('response')
|
||||
->inject('dbForProject')
|
||||
->inject('usage')
|
||||
->inject('events')
|
||||
->action(function (string $userId, string $email, string $password, string $name, Response $response, Database $dbForProject, Stats $usage, Event $events) {
|
||||
$user = createUser('plaintext', '{}', $userId, $email, $password, $name, $dbForProject, $usage, $events);
|
||||
->action(function (string $userId, ?string $email, ?string $phone, ?string $password, string $name, Response $response, Database $dbForProject, Stats $usage, Event $events) {
|
||||
$user = createUser('plaintext', '{}', $userId, $email, $password, $phone, $name, $dbForProject, $usage, $events);
|
||||
|
||||
$response->setStatusCode(Response::STATUS_CODE_CREATED);
|
||||
$response->dynamic($user, Response::MODEL_USER);
|
||||
|
@ -121,7 +128,7 @@ App::post('/v1/users/bcrypt')
|
|||
->inject('usage')
|
||||
->inject('events')
|
||||
->action(function (string $userId, string $email, string $password, string $name, Response $response, Database $dbForProject, Stats $usage, Event $events) {
|
||||
$user = createUser('bcrypt', '{}', $userId, $email, $password, $name, $dbForProject, $usage, $events);
|
||||
$user = createUser('bcrypt', '{}', $userId, $email, $password, null, $name, $dbForProject, $usage, $events);
|
||||
|
||||
$response->setStatusCode(Response::STATUS_CODE_CREATED);
|
||||
$response->dynamic($user, Response::MODEL_USER);
|
||||
|
@ -148,7 +155,7 @@ App::post('/v1/users/md5')
|
|||
->inject('usage')
|
||||
->inject('events')
|
||||
->action(function (string $userId, string $email, string $password, string $name, Response $response, Database $dbForProject, Stats $usage, Event $events) {
|
||||
$user = createUser('md5', '{}', $userId, $email, $password, $name, $dbForProject, $usage, $events);
|
||||
$user = createUser('md5', '{}', $userId, $email, $password, null, $name, $dbForProject, $usage, $events);
|
||||
|
||||
$response->setStatusCode(Response::STATUS_CODE_CREATED);
|
||||
$response->dynamic($user, Response::MODEL_USER);
|
||||
|
@ -175,7 +182,7 @@ App::post('/v1/users/argon2')
|
|||
->inject('usage')
|
||||
->inject('events')
|
||||
->action(function (string $userId, string $email, string $password, string $name, Response $response, Database $dbForProject, Stats $usage, Event $events) {
|
||||
$user = createUser('argon2', '{}', $userId, $email, $password, $name, $dbForProject, $usage, $events);
|
||||
$user = createUser('argon2', '{}', $userId, $email, $password, null, $name, $dbForProject, $usage, $events);
|
||||
|
||||
$response->setStatusCode(Response::STATUS_CODE_CREATED);
|
||||
$response->dynamic($user, Response::MODEL_USER);
|
||||
|
@ -209,7 +216,7 @@ App::post('/v1/users/sha')
|
|||
$options = '{"version":"' . $passwordVersion . '"}';
|
||||
}
|
||||
|
||||
$user = createUser('sha', $options, $userId, $email, $password, $name, $dbForProject, $usage, $events);
|
||||
$user = createUser('sha', $options, $userId, $email, $password, null, $name, $dbForProject, $usage, $events);
|
||||
|
||||
$response->setStatusCode(Response::STATUS_CODE_CREATED);
|
||||
$response->dynamic($user, Response::MODEL_USER);
|
||||
|
@ -236,7 +243,7 @@ App::post('/v1/users/phpass')
|
|||
->inject('usage')
|
||||
->inject('events')
|
||||
->action(function (string $userId, string $email, string $password, string $name, Response $response, Database $dbForProject, Stats $usage, Event $events) {
|
||||
$user = createUser('phpass', '{}', $userId, $email, $password, $name, $dbForProject, $usage, $events);
|
||||
$user = createUser('phpass', '{}', $userId, $email, $password, null, $name, $dbForProject, $usage, $events);
|
||||
|
||||
$response->setStatusCode(Response::STATUS_CODE_CREATED);
|
||||
$response->dynamic($user, Response::MODEL_USER);
|
||||
|
@ -276,7 +283,7 @@ App::post('/v1/users/scrypt')
|
|||
'length' => $passwordLength
|
||||
];
|
||||
|
||||
$user = createUser('scrypt', \json_encode($options), $userId, $email, $password, $name, $dbForProject, $usage, $events);
|
||||
$user = createUser('scrypt', \json_encode($options), $userId, $email, $password, null, $name, $dbForProject, $usage, $events);
|
||||
|
||||
$response->setStatusCode(Response::STATUS_CODE_CREATED);
|
||||
$response->dynamic($user, Response::MODEL_USER);
|
||||
|
@ -306,7 +313,7 @@ App::post('/v1/users/scrypt-modified')
|
|||
->inject('usage')
|
||||
->inject('events')
|
||||
->action(function (string $userId, string $email, string $password, string $passwordSalt, string $passwordSaltSeparator, string $passwordSignerKey, string $name, Response $response, Database $dbForProject, Stats $usage, Event $events) {
|
||||
$user = createUser('scryptMod', '{"signerKey":"' . $passwordSignerKey . '","saltSeparator":"' . $passwordSaltSeparator . '","salt":"' . $passwordSalt . '"}', $userId, $email, $password, $name, $dbForProject, $usage, $events);
|
||||
$user = createUser('scryptMod', '{"signerKey":"' . $passwordSignerKey . '","saltSeparator":"' . $passwordSaltSeparator . '","salt":"' . $passwordSalt . '"}', $userId, $email, $password, null, $name, $dbForProject, $usage, $events);
|
||||
|
||||
$response->setStatusCode(Response::STATUS_CODE_CREATED);
|
||||
$response->dynamic($user, Response::MODEL_USER);
|
||||
|
|
|
@ -264,11 +264,103 @@ trait UsersBase
|
|||
$this->assertEquals($response['body']['userId'], 'scrypt-modified');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests all optional parameters of createUser (email, phone, anonymous..)
|
||||
*
|
||||
* @depends testCreateUser
|
||||
*/
|
||||
public function testCreateUserTypes(array $data): void {
|
||||
/**
|
||||
* Test for SUCCESS
|
||||
*/
|
||||
|
||||
// Email + password
|
||||
$response = $this->client->call(Client::METHOD_POST, '/users', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getHeaders()), [
|
||||
'userId' => 'unique()',
|
||||
'email' => 'emailuser@appwrite.io',
|
||||
'password' => 'emailUserPassword',
|
||||
]);
|
||||
|
||||
$this->assertNotEmpty($response['body']['email']);
|
||||
$this->assertNotEmpty($response['body']['password']);
|
||||
$this->assertEmpty($response['body']['phone']);
|
||||
|
||||
// Phone
|
||||
$response = $this->client->call(Client::METHOD_POST, '/users', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getHeaders()), [
|
||||
'userId' => 'unique()',
|
||||
'phone' => '+123456789012',
|
||||
]);
|
||||
|
||||
$this->assertEmpty($response['body']['email']);
|
||||
$this->assertEmpty($response['body']['password']);
|
||||
$this->assertNotEmpty($response['body']['phone']);
|
||||
|
||||
// Anonymous
|
||||
$response = $this->client->call(Client::METHOD_POST, '/users', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getHeaders()), [
|
||||
'userId' => 'unique()',
|
||||
]);
|
||||
|
||||
$this->assertEmpty($response['body']['email']);
|
||||
$this->assertEmpty($response['body']['password']);
|
||||
$this->assertEmpty($response['body']['phone']);
|
||||
|
||||
// Email-only
|
||||
$response = $this->client->call(Client::METHOD_POST, '/users', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getHeaders()), [
|
||||
'userId' => 'unique()',
|
||||
'email' => 'emailonlyuser@appwrite.io',
|
||||
]);
|
||||
|
||||
$this->assertNotEmpty($response['body']['email']);
|
||||
$this->assertEmpty($response['body']['password']);
|
||||
$this->assertEmpty($response['body']['phone']);
|
||||
|
||||
// Password-only
|
||||
$response = $this->client->call(Client::METHOD_POST, '/users', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getHeaders()), [
|
||||
'userId' => 'unique()',
|
||||
'password' => 'passwordOnlyUser',
|
||||
]);
|
||||
|
||||
$this->assertEmpty($response['body']['email']);
|
||||
$this->assertNotEmpty($response['body']['password']);
|
||||
$this->assertEmpty($response['body']['phone']);
|
||||
|
||||
// Password and phone
|
||||
$response = $this->client->call(Client::METHOD_POST, '/users', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getHeaders()), [
|
||||
'userId' => 'unique()',
|
||||
'password' => 'passwordOnlyUser',
|
||||
'phone' => '+123456789013',
|
||||
]);
|
||||
|
||||
$this->assertEmpty($response['body']['email']);
|
||||
$this->assertNotEmpty($response['body']['password']);
|
||||
$this->assertNotEmpty($response['body']['phone']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreateUser
|
||||
*/
|
||||
public function testListUsers(array $data): void
|
||||
{
|
||||
$totalUsers = 15;
|
||||
|
||||
/**
|
||||
* Test for SUCCESS listUsers
|
||||
*/
|
||||
|
@ -280,7 +372,7 @@ trait UsersBase
|
|||
$this->assertEquals($response['headers']['status-code'], 200);
|
||||
$this->assertNotEmpty($response['body']);
|
||||
$this->assertNotEmpty($response['body']['users']);
|
||||
$this->assertCount(9, $response['body']['users']);
|
||||
$this->assertCount($totalUsers, $response['body']['users']);
|
||||
|
||||
$this->assertEquals($response['body']['users'][0]['$id'], $data['userId']);
|
||||
$this->assertEquals($response['body']['users'][1]['$id'], 'user1');
|
||||
|
@ -295,7 +387,7 @@ trait UsersBase
|
|||
$this->assertEquals($response['headers']['status-code'], 200);
|
||||
$this->assertNotEmpty($response['body']);
|
||||
$this->assertNotEmpty($response['body']['users']);
|
||||
$this->assertCount(8, $response['body']['users']);
|
||||
$this->assertCount($totalUsers - 1, $response['body']['users']);
|
||||
$this->assertEquals($response['body']['users'][0]['$id'], 'user1');
|
||||
|
||||
$response = $this->client->call(Client::METHOD_GET, '/users', array_merge([
|
||||
|
|
Loading…
Reference in a new issue