Skip to content
Permalink
Browse files Browse the repository at this point in the history
Add email validation and testing for booking
Signed-off-by: Anna Larch <anna@nextcloud.com>
  • Loading branch information
miaulalala authored and backportbot[bot] committed Mar 16, 2022
1 parent 58f0823 commit 7b70edf
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 11 deletions.
30 changes: 20 additions & 10 deletions lib/Controller/BookingController.php
Expand Up @@ -43,6 +43,7 @@
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\Exception;
use OCP\IRequest;
use OCP\Mail\IMailer;
use Psr\Log\LoggerInterface;

class BookingController extends Controller {
Expand All @@ -65,14 +66,18 @@ class BookingController extends Controller {
/** @var LoggerInterface */
private $logger;

public function __construct(string $appName,
IRequest $request,
ITimeFactory $timeFactory,
IInitialState $initialState,
BookingService $bookingService,
/** @var IMailer */
private $mailer;

public function __construct(string $appName,
IRequest $request,
ITimeFactory $timeFactory,
IInitialState $initialState,
BookingService $bookingService,
AppointmentConfigService $appointmentConfigService,
URLGenerator $urlGenerator,
LoggerInterface $logger) {
URLGenerator $urlGenerator,
LoggerInterface $logger,
IMailer $mailer) {
parent::__construct($appName, $request);

$this->bookingService = $bookingService;
Expand All @@ -81,6 +86,7 @@ public function __construct(string $appName,
$this->initialState = $initialState;
$this->urlGenerator = $urlGenerator;
$this->logger = $logger;
$this->mailer = $mailer;
}

/**
Expand Down Expand Up @@ -154,13 +160,17 @@ public function getBookableSlots(int $appointmentConfigId,
* @param string $timeZone
* @return JsonResponse
*/
public function bookSlot(int $appointmentConfigId,
int $start,
int $end,
public function bookSlot(int $appointmentConfigId,
int $start,
int $end,
string $displayName,
string $email,
string $description,
string $timeZone): JsonResponse {
if (!$this->mailer->validateMailAddress($email)) {
return JsonResponse::fail('Invalid email address', Http::STATUS_UNPROCESSABLE_ENTITY);
}

if ($start > $end) {
return JsonResponse::fail('Invalid time range', Http::STATUS_UNPROCESSABLE_ENTITY);
}
Expand Down
120 changes: 119 additions & 1 deletion tests/php/unit/Controller/BookingControllerTest.php
Expand Up @@ -27,8 +27,12 @@
use ChristophWurst\Nextcloud\Testing\TestCase;
use DateTimeZone;
use Exception;
use InvalidArgumentException;
use OC\URLGenerator;
use OCA\Calendar\Db\AppointmentConfig;
use OCA\Calendar\Db\Booking;
use OCA\Calendar\Exception\NoSlotFoundException;
use OCA\Calendar\Exception\ServiceException;
use OCA\Calendar\Service\Appointments\AppointmentConfigService;
use OCA\Calendar\Service\Appointments\BookingService;
use OCP\AppFramework\Services\IInitialState;
Expand All @@ -38,6 +42,7 @@
use OCP\IInitialStateService;
use OCP\IRequest;
use OCP\IUser;
use OCP\Mail\IMailer;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Safe\DateTimeImmutable;
Expand Down Expand Up @@ -80,6 +85,9 @@ class BookingControllerTest extends TestCase {
/** @var mixed|MockObject|LoggerInterface */
private $logger;

/** @var IMailer|MockObject */
private $mailer;

protected function setUp():void {
parent::setUp();

Expand All @@ -95,6 +103,7 @@ protected function setUp():void {
$this->apptService = $this->createMock(AppointmentConfigService::class);
$this->urlGenerator = $this->createMock(URLGenerator::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->mailer = $this->createMock(IMailer::class);
$this->controller = new BookingController(
$this->appName,
$this->request,
Expand All @@ -103,7 +112,8 @@ protected function setUp():void {
$this->bookingService,
$this->apptService,
$this->urlGenerator,
$this->logger
$this->logger,
$this->mailer
);
}

Expand Down Expand Up @@ -171,4 +181,112 @@ public function testGetBookableSlotsDatesInPast(): void {

$this->controller->getBookableSlots($apptConfg->getId(), $start,'Europe/Berlin');
}

public function testBook(): void {
$email = 'penny@stardewvalley.edu';
$config = new AppointmentConfig();

$this->mailer->expects(self::once())
->method('validateMailAddress')
->with($email)
->willReturn(true);
$this->apptService->expects(self::once())
->method('findById')
->willReturn($config);
$this->bookingService->expects(self::once())
->method('book')
->with($config, 1, 1, 'Hook/Neverland', 'Test', $email, 'Test')
->willReturn(new Booking());

$this->controller->bookSlot(1, 1, 1, 'Test', $email, 'Test', 'Hook/Neverland');
}


public function testBookInvalidTimeZone(): void {
$email = 'penny@stardewvalley.edu';
$config = new AppointmentConfig();

$this->mailer->expects(self::once())
->method('validateMailAddress')
->with($email)
->willReturn(true);
$this->apptService->expects(self::once())
->method('findById')
->willReturn($config);
$this->bookingService->expects(self::once())
->method('book')
->with($config, 1, 1, 'Hook/Neverland', 'Test', $email, 'Test')
->willThrowException(new InvalidArgumentException());

$this->controller->bookSlot(1, 1, 1, 'Test', $email, 'Test', 'Hook/Neverland');
}

public function testBookInvalidSlot(): void {
$email = 'penny@stardewvalley.edu';
$config = new AppointmentConfig();

$this->mailer->expects(self::once())
->method('validateMailAddress')
->with($email)
->willReturn(true);
$this->apptService->expects(self::once())
->method('findById')
->willReturn($config);
$this->bookingService->expects(self::once())
->method('book')
->with($config, 1, 1, 'Europe/Berlin', 'Test', $email, 'Test')
->willThrowException(new NoSlotFoundException());

$this->controller->bookSlot(1, 1, 1, 'Test', $email, 'Test', 'Europe/Berlin');
}

public function testBookInvalidBooking(): void {
$email = 'penny@stardewvalley.edu';
$config = new AppointmentConfig();

$this->mailer->expects(self::once())
->method('validateMailAddress')
->with($email)
->willReturn(true);
$this->apptService->expects(self::once())
->method('findById')
->willReturn($config);
$this->bookingService->expects(self::once())
->method('book')
->with($config, 1, 1, 'Europe/Berlin', 'Test', $email, 'Test')
->willThrowException(new ServiceException());

$this->controller->bookSlot(1, 1, 1, 'Test', $email, 'Test', 'Europe/Berlin');
}

public function testBookInvalidId(): void {
$email = 'penny@stardewvalley.edu';
$this->mailer->expects(self::once())
->method('validateMailAddress')
->with($email)
->willReturn(true);
$this->apptService->expects(self::once())
->method('findById')
->willThrowException(new ServiceException());
$this->bookingService->expects(self::never())
->method('book');

$this->controller->bookSlot(1, 1, 1, 'Test', $email, 'Test', 'Europe/Berlin');
}


public function testBookInvalidEmail(): void {
$email = 'testing-abcdef';

$this->mailer->expects(self::once())
->method('validateMailAddress')
->with($email)
->willReturn(false);
$this->apptService->expects(self::never())
->method('findById');
$this->bookingService->expects(self::never())
->method('book');

$this->controller->bookSlot(1, 1, 1, 'Test', $email, 'Test', 'Europe/Berlin');
}
}

0 comments on commit 7b70edf

Please sign in to comment.