From 6dbc3966b2fd44cdb20aa73f1c1a1e647bb5060b Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Mon, 22 Feb 2021 12:27:31 +0100 Subject: [PATCH] adds migration and migration tests --- src/Appwrite/Migration/Version/V07.php | 70 +++++++++++++++++++++++ tests/unit/Migration/MigrationV07Test.php | 69 ++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 src/Appwrite/Migration/Version/V07.php create mode 100644 tests/unit/Migration/MigrationV07Test.php diff --git a/src/Appwrite/Migration/Version/V07.php b/src/Appwrite/Migration/Version/V07.php new file mode 100644 index 0000000000..0b97a83f80 --- /dev/null +++ b/src/Appwrite/Migration/Version/V07.php @@ -0,0 +1,70 @@ +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; + } +} diff --git a/tests/unit/Migration/MigrationV07Test.php b/tests/unit/Migration/MigrationV07Test.php new file mode 100644 index 0000000000..ca73c3229d --- /dev/null +++ b/tests/unit/Migration/MigrationV07Test.php @@ -0,0 +1,69 @@ +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']); + + } +}