adds migration and migration tests
This commit is contained in:
parent
19b85d7ae6
commit
6dbc3966b2
70
src/Appwrite/Migration/Version/V07.php
Normal file
70
src/Appwrite/Migration/Version/V07.php
Normal file
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Migration\Version;
|
||||
|
||||
use Appwrite\Migration\Migration;
|
||||
use Utopia\Config\Config;
|
||||
use Utopia\CLI\Console;
|
||||
use Appwrite\Auth\Auth;
|
||||
use Appwrite\Database\Database;
|
||||
use Appwrite\Database\Document;
|
||||
|
||||
class V07 extends Migration
|
||||
{
|
||||
public function execute(): void
|
||||
{
|
||||
$db = $this->db;
|
||||
$project = $this->project;
|
||||
Console::log('Migrating project: ' . $project->getAttribute('name') . ' (' . $project->getId() . ')');
|
||||
|
||||
$this->forEachDocument([$this, 'fixDocument']);
|
||||
}
|
||||
|
||||
protected function fixDocument(Document $document)
|
||||
{
|
||||
$providers = Config::getParam('providers');
|
||||
|
||||
switch ($document->getAttribute('$collection')) {
|
||||
case Database::SYSTEM_COLLECTION_USERS:
|
||||
foreach ($providers as $key => $provider) {
|
||||
/**
|
||||
* Remove deprecated OAuth2 properties in the Users Documents.
|
||||
*/
|
||||
if (!empty($document->getAttribute('oauth2' . \ucfirst($key)))) {
|
||||
$document->removeAttribute('oauth2' . \ucfirst($key));
|
||||
}
|
||||
|
||||
if (!empty($document->getAttribute('oauth2' . \ucfirst($key) . 'AccessToken'))) {
|
||||
$document->removeAttribute('oauth2' . \ucfirst($key) . 'AccessToken');
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidate all Login Tokens, since they can't be migrated to the new structure.
|
||||
* Reason for it is the missing distinction between E-Mail and OAuth2 tokens.
|
||||
*/
|
||||
$tokens = array_filter($document->getAttribute('tokens', []), function($token) {
|
||||
return ($token->getAttribute('type') != Auth::TOKEN_TYPE_LOGIN);
|
||||
});
|
||||
|
||||
$document->setAttribute('tokens', array_values($tokens));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
foreach ($document as &$attr) { // Handle child documents
|
||||
if ($attr instanceof Document) {
|
||||
$attr = $this->fixDocument($attr);
|
||||
}
|
||||
|
||||
if (\is_array($attr)) {
|
||||
foreach ($attr as &$child) {
|
||||
if ($child instanceof Document) {
|
||||
$child = $this->fixDocument($child);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $document;
|
||||
}
|
||||
}
|
69
tests/unit/Migration/MigrationV07Test.php
Normal file
69
tests/unit/Migration/MigrationV07Test.php
Normal file
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Tests;
|
||||
|
||||
use ReflectionClass;
|
||||
use Appwrite\Migration\Version\V07;
|
||||
use Appwrite\Database\Database;
|
||||
use Appwrite\Database\Document;
|
||||
use Appwrite\Auth\Auth;
|
||||
use Utopia\Config\Config;
|
||||
|
||||
class MigrationV07Test extends MigrationTest
|
||||
{
|
||||
public function setUp(): void
|
||||
{
|
||||
Config::load('providers', __DIR__ . '/../../../app/config/providers.php');
|
||||
|
||||
$this->pdo = new \PDO('sqlite::memory:');
|
||||
$this->migration = new V07($this->pdo);
|
||||
$reflector = new ReflectionClass('Appwrite\Migration\Version\V07');
|
||||
$this->method = $reflector->getMethod('fixDocument');
|
||||
$this->method->setAccessible(true);
|
||||
}
|
||||
|
||||
public function testMigration()
|
||||
{
|
||||
$document = $this->fixDocument(new Document([
|
||||
'$id' => 'unique',
|
||||
'$collection' => Database::SYSTEM_COLLECTION_USERS,
|
||||
'oauth2Github' => 123,
|
||||
'oauth2GithubAccessToken' => 456,
|
||||
'tokens' => [
|
||||
new Document([
|
||||
'$collection' => Database::SYSTEM_COLLECTION_TOKENS,
|
||||
'userId' => 'unique',
|
||||
'type' => Auth::TOKEN_TYPE_LOGIN,
|
||||
'secret' => 'login',
|
||||
]),
|
||||
new Document([
|
||||
'$collection' => Database::SYSTEM_COLLECTION_TOKENS,
|
||||
'userId' => 'unique',
|
||||
'type' => Auth::TOKEN_TYPE_INVITE,
|
||||
'secret' => 'invite',
|
||||
]),
|
||||
new Document([
|
||||
'$collection' => Database::SYSTEM_COLLECTION_TOKENS,
|
||||
'userId' => 'unique',
|
||||
'type' => Auth::TOKEN_TYPE_RECOVERY,
|
||||
'secret' => 'recovery',
|
||||
]),
|
||||
new Document([
|
||||
'$collection' => Database::SYSTEM_COLLECTION_TOKENS,
|
||||
'userId' => 'unique',
|
||||
'type' => Auth::TOKEN_TYPE_VERIFICATION,
|
||||
'secret' => 'verification',
|
||||
]),
|
||||
]
|
||||
]));
|
||||
|
||||
$this->assertEquals($document->getAttribute('oauth2Github', null), null);
|
||||
$this->assertEquals($document->getAttribute('oauth2GithubAccessToken', null), null);
|
||||
|
||||
$this->assertCount(3, $document->getAttribute('tokens', []));
|
||||
$this->assertEquals(Auth::TOKEN_TYPE_INVITE, $document->getAttribute('tokens', [])[0]['type']);
|
||||
$this->assertEquals(Auth::TOKEN_TYPE_RECOVERY, $document->getAttribute('tokens', [])[1]['type']);
|
||||
$this->assertEquals(Auth::TOKEN_TYPE_VERIFICATION, $document->getAttribute('tokens', [])[2]['type']);
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue