diff --git a/lib/Controller/AccountApiController.php b/lib/Controller/AccountApiController.php index 7fb101d4b1..1e6fd9c0bb 100644 --- a/lib/Controller/AccountApiController.php +++ b/lib/Controller/AccountApiController.php @@ -37,9 +37,9 @@ public function __construct( } /** - * List all email accounts and their aliases of the user which is currently logged-in. + * List all email accounts and their aliases of the user which is currently logged-in * - * @return DataResponse|DataResponse + * @return DataResponse|DataResponse * * 200: Account list * 404: User was not logged in diff --git a/lib/ResponseDefinitions.php b/lib/ResponseDefinitions.php index c8e3bea835..9dfd9f7418 100644 --- a/lib/ResponseDefinitions.php +++ b/lib/ResponseDefinitions.php @@ -48,11 +48,11 @@ * @psalm-type MailAccountListResponse = array{ * id: int, * email: string, - * aliases: array { + * aliases: array{ * id: int, * email: string, - * name?: string, - * } + * name: ?string, + * }[] * } */ class ResponseDefinitions { diff --git a/tests/Unit/Controller/AccountApiControllerTest.php b/tests/Unit/Controller/AccountApiControllerTest.php new file mode 100644 index 0000000000..c560c8e4f5 --- /dev/null +++ b/tests/Unit/Controller/AccountApiControllerTest.php @@ -0,0 +1,168 @@ +request = $this->createMock(IRequest::class); + $this->accountService = $this->createMock(AccountService::class); + $this->aliasesService = $this->createMock(AliasesService::class); + + $this->controller = new AccountApiController( + 'mail', + $this->request, + self::USER_ID, + $this->accountService, + $this->aliasesService, + ); + } + + public function testListWithoutUser() { + $controller = new AccountApiController( + 'mail', + $this->request, + null, + $this->accountService, + $this->aliasesService, + ); + + $this->accountService->expects(self::never()) + ->method('findByUserId'); + + $this->aliasesService->expects(self::never()) + ->method('findAll'); + + $actual = $controller->list(); + $this->assertEquals(Http::STATUS_NOT_FOUND, $actual->getStatus()); + } + + public function testList() { + $mailAccount = new MailAccount(); + $mailAccount->setId(42); + $mailAccount->setEmail('foo@bar.com'); + + $account = new Account($mailAccount); + $this->accountService->expects(self::once()) + ->method('findByUserId') + ->with(self::USER_ID) + ->willReturn([$account]); + + $alias = new Alias(); + $alias->setId(10); + $alias->setName('Baz'); + $alias->setAlias('baz@bar.com'); + $this->aliasesService->expects(self::once()) + ->method('findAll') + ->with(42, self::USER_ID) + ->willReturn([$alias]); + + $actual = $this->controller->list(); + $this->assertEquals(Http::STATUS_OK, $actual->getStatus()); + $this->assertEquals([ + [ + 'id' => 42, + 'email' => 'foo@bar.com', + 'aliases' => [ + [ + 'id' => 10, + 'email' => 'baz@bar.com', + 'name' => 'Baz', + ], + ], + ] + ], $actual->getData()); + } + + public function testListWithAliasWithoutName() { + $mailAccount = new MailAccount(); + $mailAccount->setId(42); + $mailAccount->setEmail('foo@bar.com'); + + $account = new Account($mailAccount); + $this->accountService->expects(self::once()) + ->method('findByUserId') + ->with(self::USER_ID) + ->willReturn([$account]); + + $alias = new Alias(); + $alias->setId(10); + $alias->setName(null); + $alias->setAlias('baz@bar.com'); + $this->aliasesService->expects(self::once()) + ->method('findAll') + ->with(42, self::USER_ID) + ->willReturn([$alias]); + + $actual = $this->controller->list(); + $this->assertEquals(Http::STATUS_OK, $actual->getStatus()); + $this->assertEquals([ + [ + 'id' => 42, + 'email' => 'foo@bar.com', + 'aliases' => [ + [ + 'id' => 10, + 'email' => 'baz@bar.com', + 'name' => null, + ], + ], + ] + ], $actual->getData()); + } + + public function testListWithoutAliases() { + $mailAccount = new MailAccount(); + $mailAccount->setId(42); + $mailAccount->setEmail('foo@bar.com'); + + $account = new Account($mailAccount); + $this->accountService->expects(self::once()) + ->method('findByUserId') + ->with(self::USER_ID) + ->willReturn([$account]); + + $this->aliasesService->expects(self::once()) + ->method('findAll') + ->with(42, self::USER_ID) + ->willReturn([]); + + $actual = $this->controller->list(); + $this->assertEquals(Http::STATUS_OK, $actual->getStatus()); + $this->assertEquals([ + [ + 'id' => 42, + 'email' => 'foo@bar.com', + 'aliases' => [], + ] + ], $actual->getData()); + } + +}