1
0
Fork 0
mirror of synced 2024-06-02 19:04:49 +12:00

Merge branch 'feat-database-indexing' into feat-custom-id

This commit is contained in:
Damodar Lohani 2021-08-04 12:27:19 +05:45
commit 201d7114ec
20 changed files with 785 additions and 259 deletions

View file

@ -156,7 +156,7 @@ App::post('/v1/account/sessions')
$email = \strtolower($email);
$protocol = $request->getProtocol();
$profile = $dbForInternal->findFirst('users', [new Query('email', Query::TYPE_EQUAL, [$email])], 1); // Get user by email address
$profile = $dbForInternal->findOne('users', [new Query('email', Query::TYPE_EQUAL, [$email])]); // Get user by email address
if (!$profile || !Auth::passwordVerify($password, $profile->getAttribute('password'))) {
$audits
@ -442,16 +442,16 @@ App::get('/v1/account/sessions/oauth2/:provider/redirect')
}
}
$user = ($user->isEmpty()) ? $dbForInternal->findFirst('sessions', [ // Get user by provider id
$user = ($user->isEmpty()) ? $dbForInternal->findOne('sessions', [ // Get user by provider id
new Query('provider', QUERY::TYPE_EQUAL, [$provider]),
new Query('providerUid', QUERY::TYPE_EQUAL, [$oauth2ID]),
], 1) : $user;
]) : $user;
if ($user === false || $user->isEmpty()) { // No user logged in or with OAuth2 provider ID, create new one or connect with account with same email
$name = $oauth2->getUserName($accessToken);
$email = $oauth2->getUserEmail($accessToken);
$user = $dbForInternal->findFirst('users', [new Query('email', Query::TYPE_EQUAL, [$email])], 1); // Get user by email address
$user = $dbForInternal->findOne('users', [new Query('email', Query::TYPE_EQUAL, [$email])]); // Get user by email address
if ($user === false || $user->isEmpty()) { // Last option -> create the user, generate random password
$limit = $project->getAttribute('usersAuthLimit', 0);
@ -1074,7 +1074,7 @@ App::patch('/v1/account/email')
}
$email = \strtolower($email);
$profile = $dbForInternal->findFirst('users', [new Query('email', Query::TYPE_EQUAL, [\strtolower($email)])], 1); // Get user by email address
$profile = $dbForInternal->findOne('users', [new Query('email', Query::TYPE_EQUAL, [\strtolower($email)])]); // Get user by email address
if ($profile) {
throw new Exception('User already registered', 400);
@ -1379,7 +1379,7 @@ App::post('/v1/account/recovery')
$isAppUser = Auth::isAppUser(Authorization::$roles);
$email = \strtolower($email);
$profile = $dbForInternal->findFirst('users', [new Query('email', Query::TYPE_EQUAL, [$email])], 1); // Get user by email address
$profile = $dbForInternal->findOne('users', [new Query('email', Query::TYPE_EQUAL, [$email])]); // Get user by email address
if (!$profile) {
throw new Exception('User not found', 404);

View file

@ -4,11 +4,11 @@ use Appwrite\Database\Validator\CustomId;
use Utopia\App;
use Utopia\Exception;
use Utopia\Validator\Boolean;
use Utopia\Validator\FloatValidator;
use Utopia\Validator\Integer;
use Utopia\Validator\Numeric;
use Utopia\Validator\Range;
use Utopia\Validator\WhiteList;
use Utopia\Validator\Wildcard;
use Utopia\Validator\Text;
use Utopia\Validator\ArrayList;
use Utopia\Validator\JSON;
@ -16,6 +16,7 @@ use Utopia\Database\Validator\Key;
use Utopia\Database\Validator\Permissions;
use Utopia\Database\Validator\QueryValidator;
use Utopia\Database\Validator\Queries as QueriesValidator;
use Utopia\Database\Validator\Structure;
use Utopia\Database\Validator\UID;
use Utopia\Database\Exception\Authorization as AuthorizationException;
use Utopia\Database\Exception\Structure as StructureException;
@ -24,6 +25,101 @@ use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Query;
$attributesCallback = function ($attribute, $response, $dbForExternal, $database, $audits) {
/** @var Utopia\Database\Document $document*/
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForExternal*/
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $audits */
$collectionId = $attribute->getCollection();
$attributeId = $attribute->getId();
$type = $attribute->getAttribute('type', '');
$size = $attribute->getAttribute('size', 0);
$required = $attribute->getAttribute('required', true);
$default = $attribute->getAttribute('default', null);
$min = $attribute->getAttribute('min', null);
$max = $attribute->getAttribute('max', null);
$signed = $attribute->getAttribute('signed', true); // integers are signed by default
$array = $attribute->getAttribute('array', false);
$format = $attribute->getAttribute('format', null);
$filters = $attribute->getAttribute('filters', []); // filters are hidden from the endpoint
$collection = $dbForExternal->getCollection($collectionId);
if ($collection->isEmpty()) {
throw new Exception('Collection not found', 404);
}
// TODO@kodumbeats how to depend on $size for Text validator length
// Ensure attribute default is within required size
if ($size > 0 && !\is_null($default)) {
$validator = new Text($size);
if (!$validator->isValid($default)) {
throw new Exception('Length of default attribute exceeds attribute size', 400);
}
}
if (!\is_null($format)) {
$name = \json_decode($format, true)['name'];
if (!Structure::hasFormat($name, $type)) {
throw new Exception("Format {$name} not available for {$type} attributes.", 400);
}
}
if (!is_null($min) || !is_null($max)) { // Add range validator if either $min or $max is provided
switch ($type) {
case Database::VAR_INTEGER:
$min = (is_null($min)) ? -INF : \intval($min);
$max = (is_null($max)) ? INF : \intval($max);
$format = 'int-range';
break;
case Database::VAR_FLOAT:
$min = (is_null($min)) ? -INF : \floatval($min);
$max = (is_null($max)) ? INF : \floatval($max);
$format = 'float-range';
break;
default:
throw new Exception("Format range not available for {$type} attributes.", 400);
}
}
$success = $dbForExternal->addAttributeInQueue($collectionId, $attributeId, $type, $size, $required, $default, $signed, $array, $format, $filters);
// Database->addAttributeInQueue() does not return a document
// So we need to create one for the response
//
// TODO@kodumbeats should $signed and $filters be part of the response model?
$attribute = new Document([
'$collection' => $collectionId,
'$id' => $attributeId,
'type' => $type,
'size' => $size,
'required' => $required,
'default' => $default,
'min' => $min,
'max' => $max,
'signed' => $signed,
'array' => $array,
'format' => $format,
'filters' => $filters,
]);
$database
->setParam('type', CREATE_TYPE_ATTRIBUTE)
->setParam('document', $attribute)
;
$audits
->setParam('event', 'database.attributes.create')
->setParam('resource', 'database/attributes/'.$attribute->getId())
->setParam('data', $attribute)
;
$response->setStatusCode(Response::STATUS_CODE_CREATED);
$response->dynamic($attribute, Response::MODEL_ATTRIBUTE);
};
App::post('/v1/database/collections')
->desc('Create Collection')
->groups(['api', 'database'])
@ -226,79 +322,289 @@ App::delete('/v1/database/collections/:collectionId')
$response->noContent();
});
App::post('/v1/database/collections/:collectionId/attributes')
->desc('Create Attribute')
App::post('/v1/database/collections/:collectionId/attributes/string')
->desc('Create String Attribute')
->groups(['api', 'database'])
->label('event', 'database.attributes.create')
->label('scope', 'attributes.write')
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
->label('sdk.namespace', 'database')
->label('sdk.method', 'createAttribute')
->label('sdk.description', '/docs/references/database/create-attribute.md')
->label('sdk.method', 'createStringAttribute')
->label('sdk.description', '/docs/references/database/create-attribute-string.md')
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_ATTRIBUTE)
->param('collectionId', '', new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
// TODO@kodumbeats attributeId
->param('id', '', new Key(), 'Attribute ID.')
// TODO@kodumbeats whitelist (allowlist)
->param('type', null, new Text(8), 'Attribute type.')
// TODO@kodumbeats hide size for ints/floats/bools
->param('size', null, new Integer(), 'Attribute size for text attributes, in number of characters. For integers, floats, or bools, use 0.')
->param('attributeId', '', new Key(), 'Attribute ID.')
->param('size', null, new Integer(), 'Attribute size for text attributes, in number of characters.')
->param('required', null, new Boolean(), 'Is attribute required?')
->param('default', null, new Wildcard(), 'Default value for attribute when not provided. Cannot be set when attribute is required.', true)
->param('default', null, new Text(0), 'Default value for attribute when not provided. Cannot be set when attribute is required.', true)
->param('array', false, new Boolean(), 'Is attribute an array?', true)
->inject('response')
->inject('dbForExternal')
->inject('database')
->inject('audits')
->action(function ($collectionId, $id, $type, $size, $required, $default, $array, $response, $dbForExternal, $database, $audits) {
->action(function ($collectionId, $attributeId, $size, $required, $default, $array, $response, $dbForExternal, $database, $audits) use ($attributesCallback) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForExternal*/
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $audits */
$collection = $dbForExternal->getCollection($collectionId);
if ($collection->isEmpty()) {
throw new Exception('Collection not found', 404);
}
// integers are signed by default, and filters are hidden from the endpoint.
$signed = true;
$filters = [];
$success = $dbForExternal->addAttributeInQueue($collectionId, $id, $type, $size, $required, $default, $signed, $array, $filters);
// Database->addAttributeInQueue() does not return a document
// So we need to create one for the response
//
// TODO@kodumbeats should $signed and $filters be part of the response model?
$attribute = new Document([
return $attributesCallback(new Document([
'$collection' => $collectionId,
'$id' => $id,
'type' => $type,
'$id' => $attributeId,
'type' => Database::VAR_STRING,
'size' => $size,
'required' => $required,
'default' => $default,
'signed' => $signed,
'array' => $array,
'filters' => $filters
]);
]), $response, $dbForExternal, $database, $audits);
});
$database
->setParam('type', CREATE_TYPE_ATTRIBUTE)
->setParam('document', $attribute)
;
App::post('/v1/database/collections/:collectionId/attributes/email')
->desc('Create Email Attribute')
->groups(['api', 'database'])
->label('event', 'database.attributes.create')
->label('scope', 'attributes.write')
->label('sdk.namespace', 'database')
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
->label('sdk.method', 'createEmailAttribute')
->label('sdk.description', '/docs/references/database/create-attribute-email.md')
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_ATTRIBUTE)
->param('collectionId', '', new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
->param('attributeId', '', new Key(), 'Attribute ID.')
->param('required', null, new Boolean(), 'Is attribute required?')
->param('default', null, new Text(0), 'Default value for attribute when not provided. Cannot be set when attribute is required.', true)
->param('array', false, new Boolean(), 'Is attribute an array?', true)
->inject('response')
->inject('dbForExternal')
->inject('database')
->inject('audits')
->action(function ($collectionId, $attributeId, $required, $default, $array, $response, $dbForExternal, $database, $audits) use ($attributesCallback) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForExternal*/
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $audits */
$audits
->setParam('event', 'database.attributes.create')
->setParam('resource', 'database/attributes/'.$attribute->getId())
->setParam('data', $attribute)
;
return $attributesCallback(new Document([
'$collection' => $collectionId,
'$id' => $attributeId,
'type' => Database::VAR_STRING,
'size' => 254,
'required' => $required,
'default' => $default,
'array' => $array,
'format' => \json_encode(['name'=>'email']),
]), $response, $dbForExternal, $database, $audits);
});
$response->setStatusCode(Response::STATUS_CODE_CREATED);
$response->dynamic($attribute, Response::MODEL_ATTRIBUTE);
App::post('/v1/database/collections/:collectionId/attributes/ip')
->desc('Create IP Address Attribute')
->groups(['api', 'database'])
->label('event', 'database.attributes.create')
->label('scope', 'attributes.write')
->label('sdk.namespace', 'database')
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
->label('sdk.method', 'createIpAttribute')
->label('sdk.description', '/docs/references/database/create-attribute-ip.md')
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_ATTRIBUTE)
->param('collectionId', '', new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
->param('attributeId', '', new Key(), 'Attribute ID.')
->param('required', null, new Boolean(), 'Is attribute required?')
->param('default', null, new Text(0), 'Default value for attribute when not provided. Cannot be set when attribute is required.', true)
->param('array', false, new Boolean(), 'Is attribute an array?', true)
->inject('response')
->inject('dbForExternal')
->inject('database')
->inject('audits')
->action(function ($collectionId, $attributeId, $required, $default, $array, $response, $dbForExternal, $database, $audits) use ($attributesCallback) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForExternal*/
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $audits */
return $attributesCallback(new Document([
'$collection' => $collectionId,
'$id' => $attributeId,
'type' => Database::VAR_STRING,
'size' => 39,
'required' => $required,
'default' => $default,
'array' => $array,
'format' => \json_encode(['name'=>'ip']),
]), $response, $dbForExternal, $database, $audits);
});
App::post('/v1/database/collections/:collectionId/attributes/url')
->desc('Create IP Address Attribute')
->groups(['api', 'database'])
->label('event', 'database.attributes.create')
->label('scope', 'attributes.write')
->label('sdk.namespace', 'database')
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
->label('sdk.method', 'createUrlAttribute')
->label('sdk.description', '/docs/references/database/create-attribute-url.md')
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_ATTRIBUTE)
->param('collectionId', '', new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
->param('attributeId', '', new Key(), 'Attribute ID.')
->param('size', null, new Integer(), 'Attribute size for text attributes, in number of characters.')
->param('required', null, new Boolean(), 'Is attribute required?')
->param('default', null, new Text(0), 'Default value for attribute when not provided. Cannot be set when attribute is required.', true)
->param('array', false, new Boolean(), 'Is attribute an array?', true)
->inject('response')
->inject('dbForExternal')
->inject('database')
->inject('audits')
->action(function ($collectionId, $attributeId, $size, $required, $default, $array, $response, $dbForExternal, $database, $audits) use ($attributesCallback) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForExternal*/
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $audits */
return $attributesCallback(new Document([
'$collection' => $collectionId,
'$id' => $attributeId,
'type' => Database::VAR_STRING,
'size' => $size,
'required' => $required,
'default' => $default,
'array' => $array,
'format' => \json_encode(['name'=>'url']),
]), $response, $dbForExternal, $database, $audits);
});
App::post('/v1/database/collections/:collectionId/attributes/integer')
->desc('Create Integer Attribute')
->groups(['api', 'database'])
->label('event', 'database.attributes.create')
->label('scope', 'attributes.write')
->label('sdk.namespace', 'database')
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
->label('sdk.method', 'createIntegerAttribute')
->label('sdk.description', '/docs/references/database/create-attribute-integer.md')
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_ATTRIBUTE)
->param('collectionId', '', new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
->param('attributeId', '', new Key(), 'Attribute ID.')
->param('required', null, new Boolean(), 'Is attribute required?')
->param('min', null, new Integer(), 'Minimum value to enforce on new documents', true)
->param('max', null, new Integer(), 'Maximum value to enforce on new documents', true)
->param('default', null, new Integer(), 'Default value for attribute when not provided. Cannot be set when attribute is required.', true)
->param('array', false, new Boolean(), 'Is attribute an array?', true)
->inject('response')
->inject('dbForExternal')
->inject('database')
->inject('audits')
->action(function ($collectionId, $attributeId, $required, $min, $max, $default, $array, $response, $dbForExternal, $database, $audits) use ($attributesCallback) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForExternal*/
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $audits */
return $attributesCallback(new Document([
'$collection' => $collectionId,
'$id' => $attributeId,
'type' => Database::VAR_INTEGER,
'size' => 0,
'required' => $required,
'default' => $default,
'array' => $array,
'format' => \json_encode([
'name'=>'int-range',
'min' => $min,
'max' => $max,
]),
]), $response, $dbForExternal, $database, $audits);
});
App::post('/v1/database/collections/:collectionId/attributes/float')
->desc('Create Float Attribute')
->groups(['api', 'database'])
->label('event', 'database.attributes.create')
->label('scope', 'attributes.write')
->label('sdk.namespace', 'database')
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
->label('sdk.method', 'createFloatAttribute')
->label('sdk.description', '/docs/references/database/create-attribute-float.md')
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_ATTRIBUTE)
->param('collectionId', '', new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
->param('attributeId', '', new Key(), 'Attribute ID.')
->param('required', null, new Boolean(), 'Is attribute required?')
->param('min', null, new FloatValidator(), 'Minimum value to enforce on new documents', true)
->param('max', null, new FloatValidator(), 'Maximum value to enforce on new documents', true)
->param('default', null, new FloatValidator(), 'Default value for attribute when not provided. Cannot be set when attribute is required.', true)
->param('array', false, new Boolean(), 'Is attribute an array?', true)
->inject('response')
->inject('dbForExternal')
->inject('database')
->inject('audits')
->action(function ($collectionId, $attributeId, $required, $min, $max, $default, $array, $response, $dbForExternal, $database, $audits) use ($attributesCallback) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForExternal*/
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $audits */
return $attributesCallback(new Document([
'$collection' => $collectionId,
'$id' => $attributeId,
'type' => Database::VAR_FLOAT,
'required' => $required,
'size' => 0,
'default' => $default,
'array' => $array,
'format' => \json_encode([
'name'=>'float-range',
'min' => $min,
'max' => $max,
]),
]), $response, $dbForExternal, $database, $audits);
});
App::post('/v1/database/collections/:collectionId/attributes/boolean')
->desc('Create Boolean Attribute')
->groups(['api', 'database'])
->label('event', 'database.attributes.create')
->label('scope', 'attributes.write')
->label('sdk.namespace', 'database')
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
->label('sdk.method', 'createBooleanAttribute')
->label('sdk.description', '/docs/references/database/create-attribute-boolean.md')
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_ATTRIBUTE)
->param('collectionId', '', new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
->param('attributeId', '', new Key(), 'Attribute ID.')
->param('required', null, new Boolean(), 'Is attribute required?')
->param('default', null, new Boolean(), 'Default value for attribute when not provided. Cannot be set when attribute is required.', true)
->param('array', false, new Boolean(), 'Is attribute an array?', true)
->inject('response')
->inject('dbForExternal')
->inject('database')
->inject('audits')
->action(function ($collectionId, $attributeId, $required, $default, $array, $response, $dbForExternal, $database, $audits) use ($attributesCallback) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForExternal*/
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $audits */
return $attributesCallback(new Document([
'$collection' => $collectionId,
'$id' => $attributeId,
'type' => Database::VAR_BOOLEAN,
'size' => 0,
'required' => $required,
'default' => $default,
'array' => $array,
]), $response, $dbForExternal, $database, $audits);
});
App::get('/v1/database/collections/:collectionId/attributes')
@ -424,6 +730,9 @@ App::delete('/v1/database/collections/:collectionId/attributes/:attributeId')
'collectionId' => $collectionId,
])]);
$type = $attribute->getAttribute('type', '');
$format = $attribute->getAttribute('format', '');
$database
->setParam('type', DELETE_TYPE_ATTRIBUTE)
->setParam('document', $attribute)
@ -458,9 +767,7 @@ App::post('/v1/database/collections/:collectionId/indexes')
->param('id', null, new Key(), 'Index ID.')
->param('type', null, new WhiteList([Database::INDEX_KEY, Database::INDEX_FULLTEXT, Database::INDEX_UNIQUE, Database::INDEX_SPATIAL, Database::INDEX_ARRAY]), 'Index type.')
->param('attributes', null, new ArrayList(new Key()), 'Array of attributes to index.')
// TODO@kodumbeats debug below
->param('orders', [], new ArrayList(new WhiteList(['ASC', 'DESC'], false, Database::VAR_STRING)), 'Array of index orders.', true)
// ->param('orders', [], new ArrayList(new Text(4)), 'Array of index orders.', true)
->inject('response')
->inject('dbForExternal')
->inject('database')

View file

@ -276,7 +276,7 @@ App::post('/v1/teams/:teamId/memberships')
throw new Exception('Team not found', 404);
}
$invitee = $dbForInternal->findFirst('users', [new Query('email', Query::TYPE_EQUAL, [$email])], 1); // Get user by email address
$invitee = $dbForInternal->findOne('users', [new Query('email', Query::TYPE_EQUAL, [$email])]); // Get user by email address
if (empty($invitee)) { // Create new user if no user with same email found

View file

@ -44,9 +44,9 @@ App::init(function ($utopia, $request, $response, $console, $project, $dbForCons
} else {
Authorization::disable();
$certificate = $dbForConsole->findFirst('certificates', [
$certificate = $dbForConsole->findOne('certificates', [
new Query('domain', QUERY::TYPE_EQUAL, [$domain->get()])
], /*limit*/ 1);
]);
if (empty($certificate)) {
$certificate = new Document([

View file

@ -25,6 +25,9 @@ use Appwrite\Database\Adapter\MySQL as MySQLAdapter;
use Appwrite\Database\Adapter\Redis as RedisAdapter;
use Appwrite\Database\Document;
use Appwrite\Event\Event;
use Appwrite\Network\Validator\Email;
use Appwrite\Network\Validator\IP;
use Appwrite\Network\Validator\URL;
use Appwrite\OpenSSL\OpenSSL;
use Utopia\App;
use Utopia\View;
@ -38,7 +41,9 @@ use Utopia\Cache\Cache;
use Utopia\Database\Adapter\MariaDB;
use Utopia\Database\Document as Document2;
use Utopia\Database\Database as Database2;
use Utopia\Database\Validator\Structure;
use Utopia\Database\Validator\Authorization;
use Utopia\Validator\Range;
use Swoole\Database\PDOConfig;
use Swoole\Database\PDOPool;
use Swoole\Database\RedisConfig;
@ -184,6 +189,38 @@ Database2::addFilter('encrypt',
}
);
Structure::addFormat('email', function() {
return new Email();
}, Database2::VAR_STRING);
Structure::addFormat('ip', function() {
return new IP();
}, Database2::VAR_STRING);
Structure::addFormat('url', function() {
return new URL();
}, Database2::VAR_STRING);
Structure::addFormat('int-range', function($attribute) {
// Format encoded as json string containing name and relevant options
// E.g. Range: $format = json_encode(['name'=>$name, 'min'=>$min, 'max'=>$max]);
$format = json_decode($attribute['format'], true);
$min = $format['min'] ?? -INF;
$max = $format['max'] ?? INF;
$type = $attribute['type'];
return new Range($min, $max, $type);
}, Database2::VAR_INTEGER);
Structure::addFormat('float-range', function($attribute) {
// Format encoded as json string containing name and relevant options
// E.g. Range: $format = json_encode(['name'=>$name, 'min'=>$min, 'max'=>$max]);
$format = json_decode($attribute['format'], true);
$min = $format['min'] ?? -INF;
$max = $format['max'] ?? INF;
$type = $attribute['type'] ?? '';
return new Range($min, $max, $type);
}, Database2::VAR_FLOAT);
/*
* Registry
*/

View file

@ -78,9 +78,9 @@ class CertificatesV1 extends Worker
}
}
$certificate = $dbForConsole->findFirst('certificates', [
$certificate = $dbForConsole->findOne('certificates', [
new Query('domain', QUERY::TYPE_EQUAL, [$domain->get()])
], /*limit*/ 1);
]);
// $condition = ($certificate
// && $certificate instanceof Document

View file

@ -71,9 +71,10 @@ class DatabaseV1 extends Worker
$default = $attribute->getAttribute('default', null);
$signed = $attribute->getAttribute('signed', true);
$array = $attribute->getAttribute('array', false);
$format = $attribute->getAttribute('format', null);
$filters = $attribute->getAttribute('filters', []);
$success = $dbForExternal->createAttribute($collectionId, $id, $type, $size, $required, $default, $signed, $array, $filters);
$success = $dbForExternal->createAttribute($collectionId, $id, $type, $size, $required, $default, $signed, $array, $format, $filters);
if ($success) {
$removed = $dbForExternal->removeAttributeInQueue($collectionId, $id);
}

View file

@ -38,14 +38,14 @@
"appwrite/php-clamav": "1.1.*",
"appwrite/php-runtimes": "0.4.*",
"utopia-php/framework": "0.16.*",
"utopia-php/framework": "0.17.*",
"utopia-php/abuse": "0.6.*",
"utopia-php/analytics": "0.2.*",
"utopia-php/audit": "0.6.*",
"utopia-php/cache": "0.4.*",
"utopia-php/cli": "0.11.*",
"utopia-php/config": "0.2.*",
"utopia-php/database": "0.5.*",
"utopia-php/database": "0.6.*",
"utopia-php/locale": "0.3.*",
"utopia-php/registry": "0.5.*",
"utopia-php/preloader": "0.2.*",
@ -62,6 +62,7 @@
"adhocore/jwt": "1.1.2",
"slickdeals/statsd": "3.1.0"
},
"repositories": [],
"require-dev": {
"appwrite/sdk-generator": "0.13.0",
"swoole/ide-helper": "4.6.7",

54
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "6e0b87a4505ba02e444efa70b6f68ace",
"content-hash": "f9727be2725e573a92b2d1aeeb77b421",
"packages": [
{
"name": "adhocore/jwt",
@ -1666,22 +1666,22 @@
},
{
"name": "utopia-php/abuse",
"version": "0.6.0",
"version": "0.6.1",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/abuse.git",
"reference": "f568f97467eca66450af6fba18038ddeb8e8d05a"
"reference": "c9078aa3a87750d66060f0ed7642e03e5815da17"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/abuse/zipball/f568f97467eca66450af6fba18038ddeb8e8d05a",
"reference": "f568f97467eca66450af6fba18038ddeb8e8d05a",
"url": "https://api.github.com/repos/utopia-php/abuse/zipball/c9078aa3a87750d66060f0ed7642e03e5815da17",
"reference": "c9078aa3a87750d66060f0ed7642e03e5815da17",
"shasum": ""
},
"require": {
"ext-pdo": "*",
"php": ">=7.4",
"utopia-php/database": "0.5.*"
"utopia-php/database": "0.6.*"
},
"require-dev": {
"phpunit/phpunit": "^9.4",
@ -1713,9 +1713,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/abuse/issues",
"source": "https://github.com/utopia-php/abuse/tree/0.6.0"
"source": "https://github.com/utopia-php/abuse/tree/0.6.1"
},
"time": "2021-08-01T11:16:50+00:00"
"time": "2021-08-03T19:31:07+00:00"
},
{
"name": "utopia-php/analytics",
@ -1774,22 +1774,22 @@
},
{
"name": "utopia-php/audit",
"version": "0.6.0",
"version": "0.6.1",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/audit.git",
"reference": "f81bbe0bf64d74c9668ca7ab90d0b6eb3df4d47e"
"reference": "971dcd5c88309656df31ac20f326d3ac8b555594"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/audit/zipball/f81bbe0bf64d74c9668ca7ab90d0b6eb3df4d47e",
"reference": "f81bbe0bf64d74c9668ca7ab90d0b6eb3df4d47e",
"url": "https://api.github.com/repos/utopia-php/audit/zipball/971dcd5c88309656df31ac20f326d3ac8b555594",
"reference": "971dcd5c88309656df31ac20f326d3ac8b555594",
"shasum": ""
},
"require": {
"ext-pdo": "*",
"php": ">=7.4",
"utopia-php/database": "0.5.*"
"utopia-php/database": "0.6.*"
},
"require-dev": {
"phpunit/phpunit": "^9.3",
@ -1821,9 +1821,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/audit/issues",
"source": "https://github.com/utopia-php/audit/tree/0.6.0"
"source": "https://github.com/utopia-php/audit/tree/0.6.1"
},
"time": "2021-08-01T11:14:31+00:00"
"time": "2021-08-03T19:29:34+00:00"
},
{
"name": "utopia-php/cache",
@ -1984,16 +1984,16 @@
},
{
"name": "utopia-php/database",
"version": "0.5.0",
"version": "0.6.0",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/database.git",
"reference": "e050e51060df72eff3af9fc24fc95a41ca9a2096"
"reference": "561adc215fce3bd3b8c3ebb971ca354fb1526f26"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/database/zipball/e050e51060df72eff3af9fc24fc95a41ca9a2096",
"reference": "e050e51060df72eff3af9fc24fc95a41ca9a2096",
"url": "https://api.github.com/repos/utopia-php/database/zipball/561adc215fce3bd3b8c3ebb971ca354fb1526f26",
"reference": "561adc215fce3bd3b8c3ebb971ca354fb1526f26",
"shasum": ""
},
"require": {
@ -2041,9 +2041,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/database/issues",
"source": "https://github.com/utopia-php/database/tree/0.5.0"
"source": "https://github.com/utopia-php/database/tree/0.6.0"
},
"time": "2021-07-03T16:49:44+00:00"
"time": "2021-08-03T15:13:48+00:00"
},
{
"name": "utopia-php/domains",
@ -2101,16 +2101,16 @@
},
{
"name": "utopia-php/framework",
"version": "0.16.2",
"version": "0.17.3",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/framework.git",
"reference": "df02354a670df366b92e2e927fbf128be9a8e64e"
"reference": "0274f6b3e49db2af0d702edf278ec7504dc99878"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/framework/zipball/df02354a670df366b92e2e927fbf128be9a8e64e",
"reference": "df02354a670df366b92e2e927fbf128be9a8e64e",
"url": "https://api.github.com/repos/utopia-php/framework/zipball/0274f6b3e49db2af0d702edf278ec7504dc99878",
"reference": "0274f6b3e49db2af0d702edf278ec7504dc99878",
"shasum": ""
},
"require": {
@ -2144,9 +2144,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/framework/issues",
"source": "https://github.com/utopia-php/framework/tree/0.16.2"
"source": "https://github.com/utopia-php/framework/tree/0.17.3"
},
"time": "2021-07-20T10:24:56+00:00"
"time": "2021-08-03T13:57:01+00:00"
},
{
"name": "utopia-php/image",

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -634,13 +634,13 @@ input[type=checkbox], input[type=radio] {
position: absolute;
top: 0;
.func-end(0);
.func-border-start(1px);
border: solid 1px var(--config-color-fade-light);
height: ~"calc(100% - 2px)";
width: 50px;
line-height: 50px;
text-align: center;
background: var(--config-color-background-focus);
margin: 1px;
margin: 0;
border-radius: 0 10px 10px 0;
}
}

View file

@ -653,7 +653,7 @@
.provider {
width: 50px;
height: 50px;
background: #f5f5f5;
background: var(--config-color-background-focus);
color: #868686;
line-height: 50px;
text-align: center;

View file

@ -39,7 +39,6 @@ use Appwrite\Utopia\Response\Model\Platform;
use Appwrite\Utopia\Response\Model\Project;
use Appwrite\Utopia\Response\Model\Rule;
use Appwrite\Utopia\Response\Model\Tag;
use Appwrite\Utopia\Response\Model\Task;
use Appwrite\Utopia\Response\Model\Token;
use Appwrite\Utopia\Response\Model\Webhook;
use Appwrite\Utopia\Response\Model\Preferences;
@ -59,7 +58,6 @@ class Response extends SwooleResponse
const MODEL_ERROR = 'error';
const MODEL_ERROR_DEV = 'errorDev';
const MODEL_BASE_LIST = 'baseList';
const MODEL_PERMISSIONS = 'permissions';
// Database
const MODEL_COLLECTION = 'collection';
@ -68,7 +66,6 @@ class Response extends SwooleResponse
const MODEL_ATTRIBUTE_LIST = 'attributeList';
const MODEL_INDEX = 'index';
const MODEL_INDEX_LIST = 'indexList';
const MODEL_RULE = 'rule';
const MODEL_DOCUMENT = 'document';
const MODEL_DOCUMENT_LIST = 'documentList';
@ -125,6 +122,10 @@ class Response extends SwooleResponse
const MODEL_DOMAIN = 'domain';
const MODEL_DOMAIN_LIST = 'domainList';
// Deprecated
const MODEL_PERMISSIONS = 'permissions';
const MODEL_RULE = 'rule';
// Tests (keep last)
const MODEL_MOCK = 'mock';
@ -176,12 +177,10 @@ class Response extends SwooleResponse
->setModel(new BaseList('Currencies List', self::MODEL_CURRENCY_LIST, 'currencies', self::MODEL_CURRENCY))
->setModel(new BaseList('Phones List', self::MODEL_PHONE_LIST, 'phones', self::MODEL_PHONE))
// Entities
->setModel(new Permissions())
->setModel(new Collection())
->setModel(new Attribute())
->setModel(new Index())
->setModel(new ModelDocument())
->setModel(new Rule())
->setModel(new Log())
->setModel(new User())
->setModel(new Preferences())

View file

@ -1,49 +0,0 @@
<?php
namespace Appwrite\Utopia\Response\Model;
use Appwrite\Utopia\Response;
use Appwrite\Utopia\Response\Model;
class Permissions extends Model
{
public function __construct()
{
$this
->addRule('read', [
'type' => self::TYPE_STRING,
'description' => 'Read permissions.',
'default' => [],
'example' => 'user:5e5ea5c16897e',
'array' => true,
])
->addRule('write', [
'type' => self::TYPE_STRING,
'description' => 'Write permissions.',
'default' => [],
'example' => 'user:5e5ea5c16897e',
'array' => true,
])
;
}
/**
* Get Name
*
* @return string
*/
public function getName():string
{
return 'Permissions';
}
/**
* Get Collection
*
* @return string
*/
public function getType():string
{
return Response::MODEL_PERMISSIONS;
}
}

View file

@ -1,89 +0,0 @@
<?php
namespace Appwrite\Utopia\Response\Model;
use Appwrite\Utopia\Response;
use Appwrite\Utopia\Response\Model;
class Rule extends Model
{
public function __construct()
{
$this
->addRule('$id', [
'type' => self::TYPE_STRING,
'description' => 'Rule ID.',
'default' => '',
'example' => '5e5ea5c16897e',
])
->addRule('$collection', [ // TODO remove this from public response
'type' => self::TYPE_STRING,
'description' => 'Rule Collection.',
'example' => '5e5e66c16897e',
])
->addRule('type', [
'type' => self::TYPE_STRING,
'description' => 'Rule type. Possible values: ',
'default' => '',
'example' => 'title',
])
->addRule('key', [
'type' => self::TYPE_STRING,
'description' => 'Rule key.',
'default' => '',
'example' => 'title',
])
->addRule('label', [
'type' => self::TYPE_STRING,
'description' => 'Rule label.',
'default' => '',
'example' => 'Title',
])
->addRule('default', [ // TODO should be of mixed types
'type' => self::TYPE_STRING,
'description' => 'Rule default value.',
'default' => '',
'example' => 'Movie Name',
])
->addRule('array', [
'type' => self::TYPE_BOOLEAN,
'description' => 'Is array?',
'default' => false,
'example' => false,
])
->addRule('required', [
'type' => self::TYPE_BOOLEAN,
'description' => 'Is required?',
'default' => false,
'example' => true,
])
->addRule('list', [
'type' => self::TYPE_STRING,
'description' => 'List of allowed values',
'array' => true,
'default' => [],
'example' => '5e5ea5c168099',
])
;
}
/**
* Get Name
*
* @return string
*/
public function getName():string
{
return 'Rule';
}
/**
* Get Collection
*
* @return string
*/
public function getType():string
{
return Response::MODEL_RULE;
}
}

View file

@ -33,35 +33,32 @@ trait DatabaseBase
*/
public function testCreateAttributes(array $data): array
{
$title = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/attributes', array_merge([
$title = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/attributes/string', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'id' => 'title',
'type' => 'string',
'attributeId' => 'title',
'size' => 256,
'required' => true,
]);
$releaseYear = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/attributes', array_merge([
$releaseYear = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/attributes/integer', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'id' => 'releaseYear',
'type' => 'integer',
'attributeId' => 'releaseYear',
'size' => 0,
'required' => true,
]);
$actors = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/attributes', array_merge([
$actors = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/attributes/string', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'id' => 'actors',
'type' => 'string',
'attributeId' => 'actors',
'size' => 256,
'required' => false,
'default' => null,
@ -521,6 +518,332 @@ trait DatabaseBase
return $data;
}
public function testInvalidDocumentStructure()
{
$collection = $this->client->call(Client::METHOD_POST, '/database/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'name' => 'invalidDocumentStructure',
'read' => ['role:all'],
'write' => ['role:all'],
]);
$this->assertEquals(201, $collection['headers']['status-code']);
$this->assertEquals('invalidDocumentStructure', $collection['body']['name']);
$collectionId = $collection['body']['$id'];
$email = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/email', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'email',
'required' => false,
]);
$ip = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/ip', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'ip',
'required' => false,
]);
$url = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/url', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'url',
'size' => 256,
'required' => false,
]);
$range = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/integer', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'range',
'required' => false,
'min' => 1,
'max' => 10,
]);
// TODO@kodumbeats min and max are rounded in error message
$floatRange = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/float', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'floatRange',
'required' => false,
'min' => 1.1,
'max' => 1.4,
]);
// TODO@kodumbeats float validator rejects 0.0 and 1.0 as floats
// $probability = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/float', array_merge([
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// 'x-appwrite-key' => $this->getProject()['apiKey']
// ]), [
// 'attributeId' => 'probability',
// 'required' => false,
// 'min' => \floatval(0.0),
// 'max' => \floatval(1.0),
// ]);
$upperBound = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/integer', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'upperBound',
'required' => false,
'max' => 10,
]);
$lowerBound = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/integer', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'lowerBound',
'required' => false,
'min' => 5,
]);
/**
* Test for failure
*/
// TODO@kodumbeats troubleshoot
// $invalidRange = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/integer', array_merge([
// 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'],
// 'x-appwrite-key' => $this->getProject()['apiKey']
// ]), [
// 'attributeId' => 'invalidRange',
// 'required' => false,
// 'min' => 4,
// 'max' => 3,
// ]);
$this->assertEquals(201, $email['headers']['status-code']);
$this->assertEquals(201, $ip['headers']['status-code']);
$this->assertEquals(201, $url['headers']['status-code']);
$this->assertEquals(201, $range['headers']['status-code']);
$this->assertEquals(201, $floatRange['headers']['status-code']);
$this->assertEquals(201, $upperBound['headers']['status-code']);
$this->assertEquals(201, $lowerBound['headers']['status-code']);
// $this->assertEquals(400, $invalidRange['headers']['status-code']);
// $this->assertEquals('Minimum value must be lesser than maximum value', $invalidRange['body']['message']);
// wait for worker to add attributes
sleep(10);
$collection = $this->client->call(Client::METHOD_GET, '/database/collections/' . $collectionId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), []);
$this->assertCount(7, $collection['body']['attributes']);
$this->assertCount(0, $collection['body']['attributesInQueue']);
/**
* Test for successful validation
*/
$goodEmail = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'email' => 'user@example.com',
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$goodIp = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'ip' => '1.1.1.1',
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$goodUrl = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'url' => 'http://www.example.com',
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$goodRange = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'range' => 3,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$goodFloatRange = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'floatRange' => 1.4,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$notTooHigh = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'upperBound' => 8,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$notTooLow = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'lowerBound' => 8,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
// var_dump($notTooLow);
$this->assertEquals(201, $goodEmail['headers']['status-code']);
$this->assertEquals(201, $goodIp['headers']['status-code']);
$this->assertEquals(201, $goodUrl['headers']['status-code']);
$this->assertEquals(201, $goodRange['headers']['status-code']);
$this->assertEquals(201, $goodFloatRange['headers']['status-code']);
$this->assertEquals(201, $notTooHigh['headers']['status-code']);
$this->assertEquals(201, $notTooLow['headers']['status-code']);
/*
* Test that custom validators reject documents
*/
$badEmail = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'email' => 'user@@example.com',
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$badIp = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'ip' => '1.1.1.1.1',
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$badUrl = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'url' => 'example...com',
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$badRange = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'range' => 11,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$badFloatRange = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'floatRange' => 2.5,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$tooHigh = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'upperBound' => 11,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$tooLow = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'lowerBound' => 3,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
$this->assertEquals(400, $badEmail['headers']['status-code']);
$this->assertEquals(400, $badIp['headers']['status-code']);
$this->assertEquals(400, $badUrl['headers']['status-code']);
$this->assertEquals(400, $badRange['headers']['status-code']);
$this->assertEquals(400, $badFloatRange['headers']['status-code']);
$this->assertEquals(400, $tooHigh['headers']['status-code']);
$this->assertEquals(400, $tooLow['headers']['status-code']);
$this->assertEquals('Invalid document structure: Attribute "email" has invalid format. Value must be a valid email address', $badEmail['body']['message']);
$this->assertEquals('Invalid document structure: Attribute "ip" has invalid format. Value must be a valid IP address', $badIp['body']['message']);
$this->assertEquals('Invalid document structure: Attribute "url" has invalid format. Value must be a valid URL', $badUrl['body']['message']);
$this->assertEquals('Invalid document structure: Attribute "range" has invalid format. Value must be a valid range between 1 and 10', $badRange['body']['message']);
$this->assertEquals('Invalid document structure: Attribute "floatRange" has invalid format. Value must be a valid range between 1 and 1', $badFloatRange['body']['message']);
$this->assertEquals('Invalid document structure: Attribute "upperBound" has invalid format. Value must be a valid range between inf and 10', $tooHigh['body']['message']);
$this->assertEquals('Invalid document structure: Attribute "lowerBound" has invalid format. Value must be a valid range between 5 and inf', $tooLow['body']['message']);
}
/**
* @depends testDeleteDocument
*/

View file

@ -34,24 +34,22 @@ class DatabaseCustomServerTest extends Scope
$this->assertEquals($actors['headers']['status-code'], 201);
$this->assertEquals($actors['body']['name'], 'Actors');
$firstName = $this->client->call(Client::METHOD_POST, '/database/collections/' . $actors['body']['$id'] . '/attributes', array_merge([
$firstName = $this->client->call(Client::METHOD_POST, '/database/collections/' . $actors['body']['$id'] . '/attributes/string', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'id' => 'firstName',
'type' => 'string',
'attributeId' => 'firstName',
'size' => 256,
'required' => true,
]);
$lastName = $this->client->call(Client::METHOD_POST, '/database/collections/' . $actors['body']['$id'] . '/attributes', array_merge([
$lastName = $this->client->call(Client::METHOD_POST, '/database/collections/' . $actors['body']['$id'] . '/attributes/string', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'id' => 'lastName',
'type' => 'string',
'attributeId' => 'lastName',
'size' => 256,
'required' => true,
]);

View file

@ -51,24 +51,22 @@ trait WebhooksBase
*/
public function testCreateAttributes(array $data): array
{
$firstName = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['actorsId'] . '/attributes', array_merge([
$firstName = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['actorsId'] . '/attributes/string', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'id' => 'firstName',
'type' => 'string',
'attributeId' => 'firstName',
'size' => 256,
'required' => true,
]);
$lastName = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['actorsId'] . '/attributes', array_merge([
$lastName = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['actorsId'] . '/attributes/string', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'id' => 'lastName',
'type' => 'string',
'attributeId' => 'lastName',
'size' => 256,
'required' => true,
]);

View file

@ -64,7 +64,7 @@ class WebhooksCustomServerTest extends Scope
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'id' => 'fullname',
'type' => 'text',
'type' => 'key',
'attributes' => ['lastName', 'firstName'],
'orders' => ['ASC', 'ASC'],
]);