summaryrefslogtreecommitdiffstats
path: root/apps/settings
diff options
context:
space:
mode:
authorPytal <24800714+Pytal@users.noreply.github.com>2022-04-06 11:42:08 -0700
committerGitHub <noreply@github.com>2022-04-06 11:42:08 -0700
commit9644b7e505dc90a1e683f77ad38dc6dc4e90fa2f (patch)
tree29b3a328b63d3c8b4438666ee4f92491c4d1abb5 /apps/settings
parent9f34d176de8a72f155aa8a5e7c238684da430ca7 (diff)
parent8aa9ea74021bbefb80a355a17238caf4855aed8a (diff)
downloadnextcloud-server-9644b7e505dc90a1e683f77ad38dc6dc4e90fa2f.tar.gz
nextcloud-server-9644b7e505dc90a1e683f77ad38dc6dc4e90fa2f.zip
Merge pull request #31852 from nextcloud/test-account-migrator
Diffstat (limited to 'apps/settings')
-rw-r--r--apps/settings/tests/UserMigration/AccountMigratorTest.php120
-rw-r--r--apps/settings/tests/UserMigration/assets/account-complex.json1
-rw-r--r--apps/settings/tests/UserMigration/assets/account.json1
3 files changed, 122 insertions, 0 deletions
diff --git a/apps/settings/tests/UserMigration/AccountMigratorTest.php b/apps/settings/tests/UserMigration/AccountMigratorTest.php
new file mode 100644
index 00000000000..c668bb9e6af
--- /dev/null
+++ b/apps/settings/tests/UserMigration/AccountMigratorTest.php
@@ -0,0 +1,120 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright 2022 Christopher Ng <chrng8@gmail.com>
+ *
+ * @author Christopher Ng <chrng8@gmail.com>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\Settings\Tests\UserMigration;
+
+use OCA\Settings\AppInfo\Application;
+use OCA\Settings\UserMigration\AccountMigrator;
+use OCP\Accounts\IAccountManager;
+use OCP\AppFramework\App;
+use OCP\IUserManager;
+use OCP\UserMigration\IExportDestination;
+use OCP\UserMigration\IImportSource;
+use PHPUnit\Framework\MockObject\MockObject;
+use Sabre\VObject\UUIDUtil;
+use Symfony\Component\Console\Output\OutputInterface;
+use Test\TestCase;
+
+/**
+ * @group DB
+ */
+class AccountMigratorTest extends TestCase {
+
+ private IUserManager $userManager;
+
+ private AccountMigrator $migrator;
+
+ /** @var IImportSource|MockObject */
+ private $importSource;
+
+ /** @var IExportDestination|MockObject */
+ private $exportDestination;
+
+ /** @var OutputInterface|MockObject */
+ private $output;
+
+ private const ASSETS_DIR = __DIR__ . '/assets/';
+
+ private const REGEX_ACCOUNT_FILE = '/' . Application::APP_ID . '\/' . '[a-z]+\.json' . '/';
+
+ protected function setUp(): void {
+ $app = new App(Application::APP_ID);
+ $container = $app->getContainer();
+
+ $this->userManager = $container->get(IUserManager::class);
+ $this->migrator = $container->get(AccountMigrator::class);
+
+ $this->importSource = $this->createMock(IImportSource::class);
+ $this->exportDestination = $this->createMock(IExportDestination::class);
+ $this->output = $this->createMock(OutputInterface::class);
+ }
+
+ public function dataImportExportAccount(): array {
+ return array_map(
+ fn (string $filename) => [
+ UUIDUtil::getUUID(),
+ json_decode(file_get_contents(self::ASSETS_DIR . $filename), true, 512, JSON_THROW_ON_ERROR),
+ ],
+ array_diff(
+ scandir(self::ASSETS_DIR),
+ // Exclude current and parent directories
+ ['.', '..'],
+ ),
+ );
+ }
+
+ /**
+ * @dataProvider dataImportExportAccount
+ */
+ public function testImportExportAccount(string $userId, array $importData): void {
+ $user = $this->userManager->createUser($userId, 'topsecretpassword');
+ $exportData = $importData;
+ // Verification status of email will be set to in progress on import so we set the export data to reflect that
+ $exportData[IAccountManager::PROPERTY_EMAIL]['verified'] = IAccountManager::VERIFICATION_IN_PROGRESS;
+
+ $this->importSource
+ ->expects($this->once())
+ ->method('getMigratorVersion')
+ ->with(AccountMigrator::class)
+ ->willReturn(1);
+
+ $this->importSource
+ ->expects($this->once())
+ ->method('getFileContents')
+ ->with($this->matchesRegularExpression(self::REGEX_ACCOUNT_FILE))
+ ->willReturn(json_encode($importData));
+
+ $this->migrator->import($user, $this->importSource, $this->output);
+
+ $this->exportDestination
+ ->expects($this->once())
+ ->method('addFileContents')
+ ->with($this->matchesRegularExpression(self::REGEX_ACCOUNT_FILE), json_encode($exportData))
+ ->willReturn(true);
+
+ $this->migrator->export($user, $this->exportDestination, $this->output);
+ }
+}
diff --git a/apps/settings/tests/UserMigration/assets/account-complex.json b/apps/settings/tests/UserMigration/assets/account-complex.json
new file mode 100644
index 00000000000..4199b13b704
--- /dev/null
+++ b/apps/settings/tests/UserMigration/assets/account-complex.json
@@ -0,0 +1 @@
+{"displayname":{"name":"displayname","value":"Steve Smith","scope":"v2-local","verified":"0","verificationData":""},"address":{"name":"address","value":"123 Water St","scope":"v2-local","verified":"0","verificationData":""},"website":{"name":"website","value":"https:\/\/example.org","scope":"v2-local","verified":"0","verificationData":""},"email":{"name":"email","value":"steve@example.org","scope":"v2-federated","verified":"0","verificationData":""},"avatar":{"name":"avatar","value":"","scope":"v2-local","verified":"0","verificationData":""},"phone":{"name":"phone","value":"+12178515387","scope":"v2-private","verified":"0","verificationData":""},"twitter":{"name":"twitter","value":"steve","scope":"v2-federated","verified":"0","verificationData":""},"organisation":{"name":"organisation","value":"Mytery Machine","scope":"v2-private","verified":"0","verificationData":""},"role":{"name":"role","value":"Manager","scope":"v2-private","verified":"0","verificationData":""},"headline":{"name":"headline","value":"I am Steve","scope":"v2-local","verified":"0","verificationData":""},"biography":{"name":"biography","value":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris porttitor ullamcorper dictum. Sed fermentum ut ligula scelerisque semper. Aliquam interdum convallis tellus eu dapibus. Integer in justo sollicitudin, hendrerit ligula sit amet, blandit sem.\n\nSuspendisse consectetur ultrices accumsan. Quisque sagittis bibendum lectus ut placerat. Mauris tincidunt ornare neque, et pulvinar tortor porttitor eu.","scope":"v2-local","verified":"0","verificationData":""},"profile_enabled":{"name":"profile_enabled","value":"1","scope":"v2-local","verified":"0","verificationData":""},"additional_mail":[{"name":"additional_mail","value":"steve@example.com","scope":"v2-published","verified":"0","verificationData":""},{"name":"additional_mail","value":"steve@earth.world","scope":"v2-local","verified":"0","verificationData":""}]} \ No newline at end of file
diff --git a/apps/settings/tests/UserMigration/assets/account.json b/apps/settings/tests/UserMigration/assets/account.json
new file mode 100644
index 00000000000..7f53f28c4d3
--- /dev/null
+++ b/apps/settings/tests/UserMigration/assets/account.json
@@ -0,0 +1 @@
+{"displayname":{"name":"displayname","value":"Emma Jones","scope":"v2-federated","verified":"0","verificationData":""},"address":{"name":"address","value":"920 Grass St","scope":"v2-local","verified":"0","verificationData":""},"website":{"name":"website","value":"","scope":"v2-local","verified":"0","verificationData":""},"email":{"name":"email","value":"","scope":"v2-federated","verified":"0","verificationData":""},"avatar":{"name":"avatar","value":"","scope":"v2-federated","verified":"0","verificationData":""},"phone":{"name":"phone","value":"","scope":"v2-local","verified":"0","verificationData":""},"twitter":{"name":"twitter","value":"","scope":"v2-local","verified":"0","verificationData":""},"organisation":{"name":"organisation","value":"","scope":"v2-local","verified":"0","verificationData":""},"role":{"name":"role","value":"","scope":"v2-local","verified":"0","verificationData":""},"headline":{"name":"headline","value":"","scope":"v2-local","verified":"0","verificationData":""},"biography":{"name":"biography","value":"","scope":"v2-local","verified":"0","verificationData":""},"profile_enabled":{"name":"profile_enabled","value":"1","scope":"v2-local","verified":"0","verificationData":""},"additional_mail":[]} \ No newline at end of file