Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/mail.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/Controller/AliasesController.php21
-rw-r--r--lib/Db/AliasMapper.php4
-rw-r--r--lib/Service/AliasesService.php37
-rw-r--r--tests/Unit/Controller/AliasesControllerTest.php163
-rw-r--r--tests/Unit/Service/AliasesServiceTest.php102
5 files changed, 211 insertions, 116 deletions
diff --git a/lib/Controller/AliasesController.php b/lib/Controller/AliasesController.php
index ca9d61589..b7475affa 100644
--- a/lib/Controller/AliasesController.php
+++ b/lib/Controller/AliasesController.php
@@ -23,30 +23,29 @@ declare(strict_types=1);
namespace OCA\Mail\Controller;
+use OCA\Mail\Exception\ClientException;
use OCA\Mail\Exception\NotImplemented;
use OCA\Mail\Service\AliasesService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
-use OCP\IUser;
-use OCP\IUserSession;
class AliasesController extends Controller {
/** @var AliasesService */
private $aliasService;
- /** @var IUser */
- private $currentUser;
+ /** @var string */
+ private $currentUserId;
public function __construct(string $appName,
IRequest $request,
AliasesService $aliasesService,
- IUserSession $userSession) {
+ string $UserId) {
parent::__construct($appName, $request);
$this->aliasService = $aliasesService;
- $this->currentUser = $userSession->getUser();
+ $this->currentUserId = $UserId;
}
/**
@@ -58,7 +57,7 @@ class AliasesController extends Controller {
* @return JSONResponse
*/
public function index(int $accountId): JSONResponse {
- return new JSONResponse($this->aliasService->findAll($accountId, $this->currentUser->getUID()));
+ return new JSONResponse($this->aliasService->findAll($accountId, $this->currentUserId));
}
/**
@@ -85,7 +84,7 @@ class AliasesController extends Controller {
* @return JSONResponse
*/
public function destroy(int $id): JSONResponse {
- return new JSONResponse($this->aliasService->delete($id, $this->currentUser->getUID()));
+ return new JSONResponse($this->aliasService->delete($id, $this->currentUserId));
}
/**
@@ -97,8 +96,12 @@ class AliasesController extends Controller {
* @param string $aliasName
*
* @return JSONResponse
+ * @throws ClientException
*/
public function create(int $accountId, string $alias, string $aliasName): JSONResponse {
- return new JSONResponse($this->aliasService->create($accountId, $alias, $aliasName), Http::STATUS_CREATED);
+ return new JSONResponse(
+ $this->aliasService->create($this->currentUserId, $accountId, $alias, $aliasName),
+ Http::STATUS_CREATED
+ );
}
}
diff --git a/lib/Db/AliasMapper.php b/lib/Db/AliasMapper.php
index 8f84796f4..61f99511b 100644
--- a/lib/Db/AliasMapper.php
+++ b/lib/Db/AliasMapper.php
@@ -14,6 +14,8 @@ declare(strict_types=1);
namespace OCA\Mail\Db;
+use OCP\AppFramework\Db\DoesNotExistException;
+use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
@@ -36,6 +38,8 @@ class AliasMapper extends QBMapper {
* @param string $currentUserId
*
* @return Alias
+ * @throws DoesNotExistException
+ * @throws MultipleObjectsReturnedException
*/
public function find(int $aliasId, string $currentUserId): Alias {
$qb = $this->db->getQueryBuilder();
diff --git a/lib/Service/AliasesService.php b/lib/Service/AliasesService.php
index 218f13044..933303187 100644
--- a/lib/Service/AliasesService.php
+++ b/lib/Service/AliasesService.php
@@ -25,14 +25,21 @@ namespace OCA\Mail\Service;
use OCA\Mail\Db\Alias;
use OCA\Mail\Db\AliasMapper;
+use OCA\Mail\Db\MailAccountMapper;
+use OCA\Mail\Exception\ClientException;
+use OCP\AppFramework\Db\DoesNotExistException;
class AliasesService {
/** @var AliasMapper */
- private $mapper;
+ private $aliasMapper;
- public function __construct(AliasMapper $mapper) {
- $this->mapper = $mapper;
+ /** @var MailAccountMapper */
+ private $mailAccountMapper;
+
+ public function __construct(AliasMapper $aliasMapper, MailAccountMapper $mailAccountMapper) {
+ $this->aliasMapper = $aliasMapper;
+ $this->mailAccountMapper = $mailAccountMapper;
}
/**
@@ -41,7 +48,7 @@ class AliasesService {
* @return Alias[]
*/
public function findAll(int $accountId, string $currentUserId): array {
- return $this->mapper->findAll($accountId, $currentUserId);
+ return $this->aliasMapper->findAll($accountId, $currentUserId);
}
/**
@@ -50,21 +57,31 @@ class AliasesService {
* @return Alias
*/
public function find(int $aliasId, string $currentUserId): Alias {
- return $this->mapper->find($aliasId, $currentUserId);
+ return $this->aliasMapper->find($aliasId, $currentUserId);
}
/**
+ * @param string $currentUserId
* @param int $accountId
* @param string $alias
* @param string $aliasName
+ *
* @return Alias
+ * @throws ClientException
*/
- public function create(int $accountId, string $alias, string $aliasName) {
+ public function create(string $currentUserId, int $accountId, string $alias, string $aliasName): Alias {
+ try {
+ $this->mailAccountMapper->find($currentUserId, $accountId);
+ } catch (DoesNotExistException $e) {
+ throw new ClientException("Account $accountId does not exist or no permission to access it");
+ }
+
$aliasEntity = new Alias();
$aliasEntity->setAccountId($accountId);
$aliasEntity->setAlias($alias);
$aliasEntity->setName($aliasName);
- return $this->mapper->insert($aliasEntity);
+
+ return $this->aliasMapper->insert($aliasEntity);
}
/**
@@ -73,8 +90,8 @@ class AliasesService {
* @return Alias
*/
public function delete(int $aliasId, string $currentUserId): Alias {
- $alias = $this->mapper->find($aliasId, $currentUserId);
- $this->mapper->delete($alias);
+ $alias = $this->aliasMapper->find($aliasId, $currentUserId);
+ $this->aliasMapper->delete($alias);
return $alias;
}
@@ -87,6 +104,6 @@ class AliasesService {
* @return void
*/
public function deleteAll($accountId): void {
- $this->mapper->deleteAll($accountId);
+ $this->aliasMapper->deleteAll($accountId);
}
}
diff --git a/tests/Unit/Controller/AliasesControllerTest.php b/tests/Unit/Controller/AliasesControllerTest.php
index 342de1e48..90b883dca 100644
--- a/tests/Unit/Controller/AliasesControllerTest.php
+++ b/tests/Unit/Controller/AliasesControllerTest.php
@@ -24,6 +24,12 @@ namespace OCA\Mail\Tests\Unit\Controller;
use ChristophWurst\Nextcloud\Testing\TestCase;
use OCA\Mail\Controller\AliasesController;
use OCA\Mail\Db\Alias;
+use OCA\Mail\Db\AliasMapper;
+use OCA\Mail\Db\MailAccountMapper;
+use OCA\Mail\Exception\ClientException;
+use OCA\Mail\Exception\NotImplemented;
+use OCA\Mail\Service\AliasesService;
+use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
@@ -31,90 +37,127 @@ class AliasesControllerTest extends TestCase {
private $controller;
private $appName = 'mail';
private $request;
- private $aliasService;
private $userId = 'user12345';
- private $userSession;
- private $user;
private $alias;
+ /** @var AliasMapper */
+ private $aliasMapper;
+
+ /** @var MailAccountMapper */
+ private $mailAccountMapper;
+
+ /** @var AliasesService */
+ private $aliasService;
+
public function setUp(): void {
parent::setUp();
$this->request = $this->getMockBuilder('OCP\IRequest')
->getMock();
- $this->aliasService = $this->getMockBuilder('OCA\Mail\Service\AliasesService')
- ->disableOriginalConstructor()
- ->getMock();
- $this->userSession = $this->getMockBuilder('OCP\IUserSession')
- ->getMock();
- $this->user = $this->getMockBuilder('OCP\IUser')
- ->getMock();
+
$this->alias = $this->getMockBuilder('\OCA\Mail\Db\Alias')
->disableOriginalConstructor()
->getMock();
- $this->userSession->expects($this->once())
- ->method('getUser')
- ->will($this->returnValue($this->user));
- $this->controller = new AliasesController($this->appName, $this->request, $this->aliasService, $this->userSession);
+ $this->aliasMapper = $this->createMock(AliasMapper::class);
+ $this->mailAccountMapper = $this->createMock(MailAccountMapper::class);
+
+ $this->aliasService = new AliasesService($this->aliasMapper, $this->mailAccountMapper);
+ $this->controller = new AliasesController($this->appName, $this->request, $this->aliasService, $this->userId);
}
- public function testIndex() {
- $accountId = 123;
- $this->user->expects($this->once())
- ->method('getUID')
- ->will($this->returnValue($this->userId));
- $this->aliasService->expects($this->once())
+ public function testIndex(): void {
+ $alias = new Alias();
+ $alias->setId(100);
+ $alias->setAccountId(200);
+ $alias->setName('Jane Doe');
+ $alias->setAlias('jane@doe.com');
+
+ $this->aliasMapper->expects($this->once())
->method('findAll')
- ->with($accountId, $this->userId)
- ->will($this->returnValue([$this->alias]));
+ ->with($alias->getAccountId(), $this->userId)
+ ->willReturn([$alias]);
- $response = $this->controller->index($accountId);
+ $expectedResponse = new JSONResponse([$alias]);
+ $response = $this->controller->index($alias->getAccountId());
- $expectedResponse = new JSONResponse([
- $this->alias
- ]);
$this->assertEquals($expectedResponse, $response);
}
- public function testDestroy() {
- $aliasId = 123;
- $alias = $this->createMock(Alias::class);
- $this->user->expects($this->once())
- ->method('getUID')
- ->will($this->returnValue($this->userId));
- $this->aliasService->expects($this->once())
- ->method('delete')
- ->with($this->equalTo($aliasId), $this->equalTo($this->userId))
- ->will($this->returnValue($alias));
+ public function testShow(): void {
+ $this->expectException(NotImplemented::class);
+ $this->controller->show();
+ }
+
+ public function testUpdate(): void {
+ $this->expectException(NotImplemented::class);
+ $this->controller->update();
+ }
+
+ public function testDestroy(): void {
+ $alias = new Alias();
+ $alias->setId(101);
+ $alias->setAccountId(200);
+ $alias->setName('Jane Doe');
+ $alias->setAlias('jane@doe.com');
- $response = $this->controller->destroy($aliasId);
+ $this->aliasMapper->expects($this->once())
+ ->method('find')
+ ->with($alias->getId(), $this->userId)
+ ->willReturn($alias);
+
+ $this->aliasMapper->expects($this->once())
+ ->method('delete')
+ ->with($alias);
$expectedResponse = new JSONResponse($alias);
+ $response = $this->controller->destroy($alias->getId());
+
$this->assertEquals($expectedResponse, $response);
}
- public function testCreate() {
- $accountId = 123;
- $alias = "alias@marvel.com";
- $aliasName = "Peter Parker";
- $this->aliasService->expects($this->once())
- ->method('create')
- ->with($this->equalTo($accountId), $this->equalTo($alias), $this->equalTo($aliasName))
- ->will($this->returnValue([
- 'accountId' => $accountId,
- 'name' => $aliasName,
- 'alias' => $alias,
- 'id' => 123
- ]));
-
- $response = $this->controller->create($accountId, $alias, $aliasName);
-
- $expected = new JSONResponse([
- 'accountId' => $accountId,
- 'name' => $aliasName,
- 'alias' => $alias,
- 'id' => 123
- ], Http::STATUS_CREATED);
- $this->assertEquals($expected, $response);
+ public function testCreate(): void {
+ $alias = new Alias();
+ $alias->setId(102);
+ $alias->setAccountId(200);
+ $alias->setName('Jane Doe');
+ $alias->setAlias('jane@doe.com');
+
+ $this->mailAccountMapper->expects($this->once())
+ ->method('find');
+
+ $this->aliasMapper->expects($this->once())
+ ->method('insert')
+ ->willReturn($alias);
+
+ $expectedResponse = new JSONResponse(
+ $alias,
+ Http::STATUS_CREATED
+ );
+ $response = $this->controller->create(
+ $alias->getAccountId(),
+ $alias->getAlias(),
+ $alias->getName()
+ );
+
+ $this->assertEquals($expectedResponse, $response);
+ }
+
+ public function testCreateForbiddenAccountId(): void {
+ $this->expectException(ClientException::class);
+
+ $entity = new Alias();
+ $entity->setAccountId(200);
+ $entity->setAlias('jane@doe.com');
+ $entity->setName('Jane Doe');
+
+ $this->mailAccountMapper->expects($this->once())
+ ->method('find')
+ ->willThrowException(new DoesNotExistException('Account does not exist'));
+
+ $this->controller->create(
+ $entity->getAccountId(),
+ $entity->getAlias(),
+ $entity->getName()
+ );
}
}
diff --git a/tests/Unit/Service/AliasesServiceTest.php b/tests/Unit/Service/AliasesServiceTest.php
index 98e8064e1..4ae737a0f 100644
--- a/tests/Unit/Service/AliasesServiceTest.php
+++ b/tests/Unit/Service/AliasesServiceTest.php
@@ -24,35 +24,44 @@ namespace OCA\Mail\Tests\Unit\Service;
use ChristophWurst\Nextcloud\Testing\TestCase;
use OCA\Mail\Db\Alias;
use OCA\Mail\Db\AliasMapper;
+use OCA\Mail\Db\MailAccountMapper;
+use OCA\Mail\Exception\ClientException;
use OCA\Mail\Service\AliasesService;
-use PHPUnit_Framework_MockObject_MockObject;
+use OCP\AppFramework\Db\DoesNotExistException;
class AliasesServiceTest extends TestCase {
- /** @var AliasesService|PHPUnit_Framework_MockObject_MockObject */
+ /** @var AliasesService */
private $service;
/** @var string */
private $user = 'herbert';
- /** @var AliasMapper|PHPUnit_Framework_MockObject_MockObject */
- private $mapper;
+ /** @var AliasMapper */
+ private $aliasMapper;
- /** @var Alias|PHPUnit_Framework_MockObject_MockObject */
+ /** @var MailAccountMapper */
+ private $mailAccountMapper;
+
+ /** @var Alias */
private $alias;
protected function setUp(): void {
parent::setUp();
- $this->mapper = $this->createMock(AliasMapper::class);
+ $this->aliasMapper = $this->createMock(AliasMapper::class);
+ $this->mailAccountMapper = $this->createMock(MailAccountMapper::class);
$this->alias = $this->createMock(Alias::class);
- $this->service = new AliasesService($this->mapper);
+ $this->service = new AliasesService(
+ $this->aliasMapper,
+ $this->mailAccountMapper
+ );
}
public function testFindAll() {
$accountId = 123;
- $this->mapper->expects($this->once())
+ $this->aliasMapper->expects($this->once())
->method('findAll')
->with($accountId, $this->user)
->will($this->returnValue([$this->alias]));
@@ -67,7 +76,7 @@ class AliasesServiceTest extends TestCase {
public function testFind() {
$aliasId = 123;
- $this->mapper->expects($this->once())
+ $this->aliasMapper->expects($this->once())
->method('find')
->with($aliasId, $this->user)
->will($this->returnValue($this->alias));
@@ -78,43 +87,62 @@ class AliasesServiceTest extends TestCase {
$this->assertEquals($expected, $actual);
}
- public function testCreate() {
- $accountId = 123;
- $alias = "alias@marvel.com";
- $aliasName = "alias";
- $aliasEntity = new Alias();
- $aliasEntity->setAccountId($accountId);
- $aliasEntity->setAlias($alias);
- $aliasEntity->setName($aliasName);
- $this->mapper->expects($this->once())
+ public function testCreate(): void {
+ $entity = new Alias();
+ $entity->setAccountId(200);
+ $entity->setAlias('jane@doe.com');
+ $entity->setName('Jane Doe');
+
+ $this->mailAccountMapper->expects($this->once())
+ ->method('find');
+
+ $this->aliasMapper->expects($this->once())
->method('insert')
- ->with($aliasEntity)
- ->will($this->returnValue($aliasEntity));
-
- $result = $this->service->create($accountId, $alias, $aliasName);
-
- $this->assertEquals(
- [
- 'accountId' => $aliasEntity->getAccountId(),
- 'name' => $aliasEntity->getName(),
- 'alias' => $aliasEntity->getAlias(),
- 'id' => $aliasEntity->getId()
- ], [
- 'accountId' => $result->getAccountId(),
- 'name' => $result->getName(),
- 'alias' => $result->getAlias(),
- 'id' => $result->getId()
- ]
+ ->willReturnCallback(static function (Alias $alias) {
+ $alias->setId(100);
+ return $alias;
+ });
+
+ $result = $this->service->create(
+ 300,
+ $entity->getAccountId(),
+ $entity->getAlias(),
+ $entity->getName()
+ );
+
+ $this->assertEquals(100, $result->getId());
+ $this->assertEquals($entity->getAccountId(), $result->getAccountId());
+ $this->assertEquals($entity->getAlias(), $result->getAlias());
+ $this->assertEquals($entity->getName(), $result->getName());
+ }
+
+ public function testCreateForbiddenAccountId(): void {
+ $this->expectException(ClientException::class);
+
+ $entity = new Alias();
+ $entity->setAccountId(200);
+ $entity->setAlias('jane@doe.com');
+ $entity->setName('Jane Doe');
+
+ $this->mailAccountMapper->expects($this->once())
+ ->method('find')
+ ->willThrowException(new DoesNotExistException('Account does not exist'));
+
+ $this->service->create(
+ 300,
+ $entity->getAccountId(),
+ $entity->getAlias(),
+ $entity->getName()
);
}
public function testDelete() {
$aliasId = 123;
- $this->mapper->expects($this->once())
+ $this->aliasMapper->expects($this->once())
->method('find')
->with($aliasId, $this->user)
->will($this->returnValue($this->alias));
- $this->mapper->expects($this->once())
+ $this->aliasMapper->expects($this->once())
->method('delete')
->with($this->alias);