-
Notifications
You must be signed in to change notification settings - Fork 264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ocs): list accounts and aliases of current user #10735
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Mail\Controller; | ||
|
||
use OCA\Mail\Account; | ||
use OCA\Mail\Db\Alias; | ||
use OCA\Mail\ResponseDefinitions; | ||
use OCA\Mail\Service\AccountService; | ||
use OCA\Mail\Service\AliasesService; | ||
use OCP\AppFramework\Http; | ||
use OCP\AppFramework\Http\Attribute\ApiRoute; | ||
use OCP\AppFramework\Http\Attribute\NoAdminRequired; | ||
use OCP\AppFramework\Http\Attribute\NoCSRFRequired; | ||
use OCP\AppFramework\Http\DataResponse; | ||
use OCP\AppFramework\OCSController; | ||
use OCP\IRequest; | ||
|
||
/** | ||
* @psalm-import-type MailAccountListResponse from ResponseDefinitions | ||
*/ | ||
class AccountApiController extends OCSController { | ||
public function __construct( | ||
string $appName, | ||
IRequest $request, | ||
private readonly ?string $userId, | ||
private readonly AccountService $accountService, | ||
private readonly AliasesService $aliasesService, | ||
) { | ||
parent::__construct($appName, $request); | ||
} | ||
|
||
/** | ||
* List all email accounts and their aliases of the user which is currently logged-in | ||
* | ||
* @return DataResponse<Http::STATUS_OK, MailAccountListResponse[], array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{}, array{}> | ||
* | ||
* 200: Account list | ||
* 404: User was not logged in | ||
*/ | ||
#[ApiRoute(verb: 'GET', url: '/account/list')] | ||
#[NoAdminRequired] | ||
#[NoCSRFRequired] | ||
public function list(): DataResponse { | ||
$userId = $this->userId; | ||
if ($userId === null) { | ||
return new DataResponse([], Http::STATUS_NOT_FOUND); | ||
} | ||
|
||
$accounts = $this->accountService->findByUserId($userId); | ||
return new DataResponse(array_map(function (Account $account) use ($userId) { | ||
$aliases = $this->aliasesService->findAll($account->getId(), $userId); | ||
return [ | ||
'id' => $account->getId(), | ||
'email' => $account->getEmail(), | ||
'aliases' => array_map(static fn (Alias $alias) => [ | ||
'id' => $alias->getId(), | ||
'email' => $alias->getAlias(), | ||
'name' => $alias->getName(), | ||
], $aliases), | ||
]; | ||
}, $accounts)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Unit\Controller; | ||
|
||
use ChristophWurst\Nextcloud\Testing\TestCase; | ||
use OCA\Mail\Account; | ||
use OCA\Mail\Controller\AccountApiController; | ||
use OCA\Mail\Db\Alias; | ||
use OCA\Mail\Db\MailAccount; | ||
use OCA\Mail\Service\AccountService; | ||
use OCA\Mail\Service\AliasesService; | ||
use OCP\AppFramework\Http; | ||
use OCP\IRequest; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
|
||
class AccountApiControllerTest extends TestCase { | ||
private const USER_ID = 'user'; | ||
|
||
private AccountApiController $controller; | ||
|
||
private IRequest&MockObject $request; | ||
private AccountService&MockObject $accountService; | ||
private AliasesService&MockObject $aliasesService; | ||
|
||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->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('[email protected]'); | ||
|
||
$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('[email protected]'); | ||
$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' => '[email protected]', | ||
'aliases' => [ | ||
[ | ||
'id' => 10, | ||
'email' => '[email protected]', | ||
'name' => 'Baz', | ||
], | ||
], | ||
] | ||
], $actual->getData()); | ||
} | ||
|
||
public function testListWithAliasWithoutName() { | ||
$mailAccount = new MailAccount(); | ||
$mailAccount->setId(42); | ||
$mailAccount->setEmail('[email protected]'); | ||
|
||
$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('[email protected]'); | ||
$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' => '[email protected]', | ||
'aliases' => [ | ||
[ | ||
'id' => 10, | ||
'email' => '[email protected]', | ||
'name' => null, | ||
], | ||
], | ||
] | ||
], $actual->getData()); | ||
} | ||
|
||
public function testListWithoutAliases() { | ||
$mailAccount = new MailAccount(); | ||
$mailAccount->setId(42); | ||
$mailAccount->setEmail('[email protected]'); | ||
|
||
$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' => '[email protected]', | ||
'aliases' => [], | ||
] | ||
], $actual->getData()); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not strictly related but I noticed that the DB schema allows null values but the entity does not. Otherwise, I can't write a unit test for null values of the name column.