removed document existence check for the document id

updated labels
added tests for upserts
This commit is contained in:
ArnabChatterjee20k
2025-05-19 13:56:11 +05:30
parent 330c298c58
commit 08478b8d39
2 changed files with 67 additions and 12 deletions
+2 -12
View File
@@ -4204,7 +4204,7 @@ App::patch('/v1/databases/:databaseId/collections/:collectionId/documents/:docum
App::put('/v1/databases/:databaseId/collections/:collectionId/documents/:documentId')
->alias('/v1/database/collections/:collectionId/documents/:documentId')
->desc('Create or update document')
->desc('upsert document')
->groups(['api', 'database'])
->label('event', 'databases.[databaseId].collections.[collectionId].documents.[documentId].update')
->label('scope', 'documents.write')
@@ -4217,7 +4217,7 @@ App::put('/v1/databases/:databaseId/collections/:collectionId/documents/:documen
->label('sdk', new Method(
namespace: 'databases',
group: 'documents',
name: 'updateDocument',
name: 'upsertDocument',
description: '/docs/references/databases/update-document.md',
auth: [AuthType::SESSION, AuthType::KEY, AuthType::JWT],
responses: [
@@ -4258,12 +4258,6 @@ App::put('/v1/databases/:databaseId/collections/:collectionId/documents/:documen
throw new Exception(Exception::COLLECTION_NOT_FOUND);
}
// Read permission should not be required for update
$document = Authorization::skip(fn () => $dbForProject->getDocument('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), $documentId));
if ($document->isEmpty()) {
throw new Exception(Exception::DOCUMENT_NOT_FOUND);
}
// Map aggregate permissions into the multiple permissions they represent.
$permissions = Permission::aggregate($permissions, [
Database::PERMISSION_READ,
@@ -4292,10 +4286,6 @@ App::put('/v1/databases/:databaseId/collections/:collectionId/documents/:documen
}
}
if (\is_null($permissions)) {
$permissions = $document->getPermissions() ?? [];
}
$data['$id'] = $documentId;
$data['$permissions'] = $permissions;
$newDocument = new Document($data);
@@ -14,6 +14,8 @@ use Utopia\Database\Query;
use Utopia\Database\Validator\Datetime as DatetimeValidator;
use Utopia\Validator\JSON;
use function PHPUnit\Framework\assertEquals;
trait DatabasesBase
{
public function testCreateDatabase(): array
@@ -1683,6 +1685,69 @@ trait DatabasesBase
return $data;
}
/**
* @depends testCreateIndexes
*/
public function testUpsertDocument(array $data): void
{
$databaseId = $data['databaseId'];
$documentId = ID::unique();
$document = $this->client->call(Client::METHOD_PUT, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/documents/' . $documentId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'title' => 'Thor: Ragnarok',
'releaseYear' => 2000
],
'permissions' => [
Permission::read(Role::users()),
Permission::update(Role::users()),
Permission::delete(Role::users()),
],
]);
$this->assertEquals(200, $document['headers']['status-code']);
$document = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/documents/' . $documentId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals('Thor: Ragnarok', $document['body']['title']);
$document = $this->client->call(Client::METHOD_PUT, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/documents/' . $documentId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'title' => 'Thor: Love and Thunder',
'releaseYear' => 2000
],
'permissions' => [
Permission::read(Role::users()),
Permission::update(Role::users()),
Permission::delete(Role::users()),
],
]);
$this->assertEquals(200, $document['headers']['status-code']);
$this->assertEquals('Thor: Love and Thunder', $document['body']['title']);
$document = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/documents/' . $documentId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals('Thor: Love and Thunder', $document['body']['title']);
$document = $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/documents/' . $documentId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
}
/**
* @depends testCreateDocument
*/