Skip to content

Commit

Permalink
Add integration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
redblom committed Jun 18, 2024
1 parent df986d4 commit ceb94fb
Show file tree
Hide file tree
Showing 34 changed files with 3,031 additions and 38 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: PHPUnit

on: [push]

jobs:
integration-tests:
runs-on:
ubuntu-latest
env:
# the current branch
BRANCH: ${{ github.ref_name }}

steps:
- name: Checkout
# not compatible with actions/checkout@v4
uses: actions/checkout@v3

# the docker compose setup
- name: docker-compose-setup
uses: cloudposse/github-action-docker-compose-test-run@main
with:
file: tests/docker/docker-compose-github.yaml
service: integration-tests
command: /tmp/tests/tests.sh
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<category>integration</category>
<bugs>https://github.com/sara-nl/nc-invitation/issues</bugs>
<dependencies>
<nextcloud min-version="28" max-version="28"/>
<nextcloud min-version="29" max-version="29"/>
</dependencies>
<navigations>
<navigation>
Expand Down
5 changes: 4 additions & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
'note' => ['url' => '/notes'],
'note_api' => ['url' => '/api/0.1/notes']
],
'ocs' => [
['root' => '/invitation', 'name' => 'ocs#find_by_token', 'url' => '/invitations/{token}', 'verb' => 'GET'],
],
'routes' => [
// bespoke API - invitation
['name' => 'invitation#index', 'url' => '/index', 'verb' => 'GET'],
// ['name' => 'invitation#index', 'url' => '/index', 'verb' => 'GET'],
['name' => 'invitation#find_by_token', 'url' => '/invitations/{token}', 'verb' => 'GET'],
['name' => 'invitation#find', 'url' => '/invitations', 'verb' => 'GET'],

Expand Down
2 changes: 2 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Application extends App implements IBootstrap
{
public const APP_ID = 'invitation';

public const CONFIG_ALLOW_SHARING_WITH_INVITED_USERS_ONLY = 'allow_sharing_with_invited_users_only';

public function __construct()
{
parent::__construct(self::APP_ID);
Expand Down
41 changes: 21 additions & 20 deletions lib/Controller/InvitationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
namespace OCA\Invitation\Controller;

use Exception;
use OCA\Invitation\AppInfo\InvitationApp;
use OCA\Invitation\AppInfo\AppError;
use OCA\Invitation\AppInfo\Application;
use OCA\Invitation\Db\Schema;
use OCA\Invitation\Federation\Invitation;
use OCA\Invitation\Service\InvitationService;
Expand All @@ -18,7 +18,6 @@
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\ILogger;
use OCP\IRequest;
use Psr\Log\LoggerInterface;
Expand All @@ -33,21 +32,21 @@ public function __construct(
InvitationService $service,
LoggerInterface $logger
) {
parent::__construct(InvitationApp::APP_NAME, $request);
parent::__construct(Application::APP_ID, $request);
$this->service = $service;
$this->logger = $logger;
}

/**
*
* @NoAdminRequired
* @NoCSRFRequired
* @return TemplateResponse
*/
public function index(): TemplateResponse
{
return new TemplateResponse($this->appName, 'invitation.index');
}
// /**
// *
// * @NoAdminRequired
// * @NoCSRFRequired
// * @return TemplateResponse
// */
// public function index(): TemplateResponse
// {
// return new TemplateResponse($this->appName, 'invitation.main');
// }

/**
* Removes the notification that is associated with the invitation with specified token.
Expand All @@ -62,12 +61,12 @@ private function removeInvitationNotification(string $token): void
$manager = \OC::$server->getNotificationManager();
$notification = $manager->createNotification();
$notification
->setApp(InvitationApp::APP_NAME)
->setApp(Application::APP_ID)
->setUser(\OC::$server->getUserSession()->getUser()->getUID())
->setObject(MeshRegistryService::PARAM_NAME_TOKEN, $token);
$manager->markProcessed($notification);
} catch (Exception $e) {
$this->logger->error('Remove notification failed: ' . $e->getMessage() . ' Trace: ' . $e->getTraceAsString(), ['app' => InvitationApp::APP_NAME]);
$this->logger->error('Remove notification failed: ' . $e->getMessage() . ' Trace: ' . $e->getTraceAsString(), ['app' => Application::APP_ID]);
throw $e;
}
}
Expand Down Expand Up @@ -98,6 +97,7 @@ private function verifiedInviteAcceptedResponse(array $response): bool
/**
* example url: https://rd-1.nl/apps/invitation/invitations?status=open,accepted
*
* @PublicPage
* @NoAdminRequired
* @NoCSRFRequired
*/
Expand All @@ -111,7 +111,7 @@ public function find(string $status = null): DataResponse
}

if (empty($fieldsAndValues)) {
$this->logger->error("findAll() - missing query parameter.", ['app' => InvitationApp::APP_NAME]);
$this->logger->error("findAll() - missing query parameter.", ['app' => Application::APP_ID]);
return new DataResponse(
[
'success' => false,
Expand All @@ -130,7 +130,7 @@ public function find(string $status = null): DataResponse
Http::STATUS_OK
);
} catch (Exception $e) {
$this->logger->error('invitations not found for fields: ' . print_r($status, true) . 'Error: ' . $e->getMessage() . ' Trace: ' . $e->getTraceAsString(), ['app' => InvitationApp::APP_NAME]);
$this->logger->error('invitations not found for fields: ' . print_r($status, true) . 'Error: ' . $e->getMessage() . ' Trace: ' . $e->getTraceAsString(), ['app' => Application::APP_ID]);
return new DataResponse(
[
'success' => false,
Expand All @@ -143,12 +143,13 @@ public function find(string $status = null): DataResponse

/**
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function findByToken(string $token = null): DataResponse
{
if (!isset($token)) {
$this->logger->error("findByToken() - missing parameter 'token'.", ['app' => InvitationApp::APP_NAME]);
$this->logger->error("findByToken() - missing parameter 'token'.", ['app' => Application::APP_ID]);
return new DataResponse(
[
'success' => false,
Expand All @@ -167,7 +168,7 @@ public function findByToken(string $token = null): DataResponse
Http::STATUS_OK,
);
} catch (NotFoundException $e) {
$this->logger->error("invitation not found for token '$token'. Error: " . $e->getMessage() . ' Trace: ' . $e->getTraceAsString(), ['app' => InvitationApp::APP_NAME]);
$this->logger->error("invitation not found for token '$token'. Error: " . $e->getMessage() . ' Trace: ' . $e->getTraceAsString(), ['app' => Application::APP_ID]);
return new DataResponse(
[
'success' => false,
Expand All @@ -176,7 +177,7 @@ public function findByToken(string $token = null): DataResponse
Http::STATUS_NOT_FOUND,
);
} catch (Exception $e) {
$this->logger->error("invitation not found for token '$token'. Error: " . $e->getMessage() . ' Trace: ' . $e->getTraceAsString(), ['app' => InvitationApp::APP_NAME]);
$this->logger->error("invitation not found for token '$token'. Error: " . $e->getMessage() . ' Trace: ' . $e->getTraceAsString(), ['app' => Application::APP_ID]);
return new DataResponse(
[
'success' => false,
Expand Down
17 changes: 9 additions & 8 deletions lib/Federation/InvitationMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace OCA\Invitation\Federation;

use Exception;
use OCA\Invitation\AppInfo\InvitationApp;
use OCA\Invitation\AppInfo\Application;
use OCA\Invitation\Db\Schema;
use OCA\Invitation\Federation\Invitation;
use OCA\Invitation\Federation\VInvitation;
Expand Down Expand Up @@ -55,15 +55,16 @@ public function findByToken(string $token)
try {
$qb = $this->db->getQueryBuilder();
$qb->automaticTablePrefix(false);
$result = $qb->select('*')
$qb->select('*')
->from(Schema::VIEW_INVITATIONS, 'i')
->where($qb->expr()->eq('i.token', $qb->createNamedParameter($token)))
->executeQuery()->fetch();
->where($qb->expr()->eq('i.token', $qb->createNamedParameter($token)));
$this->logger->debug($qb->getSQL(), ['app' => Application::APP_ID]);
$result = $qb->executeQuery()->fetch();
if (is_array($result) && count($result) > 0) {
return $this->getVInvitation($result);
}
} catch (Exception $e) {
$this->logger->error($e->getMessage() . ' Trace: ' . $e->getTraceAsString(), ['app' => InvitationApp::APP_NAME]);
$this->logger->error($e->getMessage() . ' Trace: ' . $e->getTraceAsString(), ['app' => Application::APP_ID]);
throw new NotFoundException($e->getMessage());
}
throw new NotFoundException("Invitation not found for token $token");
Expand Down Expand Up @@ -141,7 +142,7 @@ public function updateInvitation(array $fieldsAndValues, string $userCloudID = '
}
}
} catch (Exception $e) {
$this->logger->error('updateInvitation failed with error: ' . $e->getMessage() . ' Trace: ' . $e->getTraceAsString(), ['app' => InvitationApp::APP_NAME]);
$this->logger->error('updateInvitation failed with error: ' . $e->getMessage() . ' Trace: ' . $e->getTraceAsString(), ['app' => Application::APP_ID]);
}
return false;
}
Expand All @@ -162,7 +163,7 @@ public function deleteForStatus(array $statuses): void
$qb->setParameter(Schema::INVITATION_STATUS, $statuses, IQueryBuilder::PARAM_STR_ARRAY);
$qb->executeQuery();
} catch (Exception $e) {
$this->logger->error($e->getMessage() . ' Trace: ' . $e->getTraceAsString(), ['app' => InvitationApp::APP_NAME]);
$this->logger->error($e->getMessage() . ' Trace: ' . $e->getTraceAsString(), ['app' => Application::APP_ID]);
throw new Exception('An error occurred trying to delete invitations.');
}
}
Expand Down Expand Up @@ -198,7 +199,7 @@ private function getVInvitation(array $associativeArray): VInvitation
$invitation->setRemoteUserProviderName($associativeArray[Schema::VINVITATION_REMOTE_USER_PROVIDER_NAME]);
return $invitation;
}
$this->logger->error('Unable to create a new Invitation from associative array: ' . print_r($associativeArray, true), ['app' => InvitationApp::APP_NAME]);
$this->logger->error('Unable to create a new Invitation from associative array: ' . print_r($associativeArray, true), ['app' => Application::APP_ID]);
return null;
}

Expand Down
16 changes: 8 additions & 8 deletions lib/Service/InvitationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace OCA\Invitation\Service;

use Exception;
use OCA\Invitation\AppInfo\InvitationApp;
use OCA\Invitation\AppInfo\Application;
use OCA\Invitation\Db\Schema;
use OCA\Invitation\Federation\Invitation;
use OCA\Invitation\Federation\InvitationMapper;
Expand Down Expand Up @@ -42,13 +42,13 @@ public function find(int $id): VInvitation
if ($this->userSession->getUser()->getCloudId() === $invitation->getUserCloudID()) {
return $invitation;
}
$this->logger->debug("User with cloud id '" . $this->userSession->getUser()->getCloudId() . "' is not authorized to access invitation with id '$id'.", ['app' => InvitationApp::APP_NAME]);
$this->logger->debug("User with cloud id '" . $this->userSession->getUser()->getCloudId() . "' is not authorized to access invitation with id '$id'.", ['app' => Application::APP_ID]);
throw new NotFoundException("Invitation with id=$id not found.");
} catch (NotFoundException $e) {
$this->logger->debug($e->getMessage() . ' Trace: ' . $e->getTraceAsString(), ['app' => InvitationApp::APP_NAME]);
$this->logger->debug($e->getMessage() . ' Trace: ' . $e->getTraceAsString(), ['app' => Application::APP_ID]);
throw new NotFoundException("Invitation with id=$id not found.");
} catch (Exception $e) {
$this->logger->error($e->getMessage() . ' Trace: ' . $e->getTraceAsString(), ['app' => InvitationApp::APP_NAME]);
$this->logger->error($e->getMessage() . ' Trace: ' . $e->getTraceAsString(), ['app' => Application::APP_ID]);
throw new NotFoundException("Invitation with id=$id not found.");
}
}
Expand All @@ -68,7 +68,7 @@ public function findByToken(string $token, bool $loginRequired = true): VInvitat
try {
$invitation = $this->mapper->findByToken($token);
} catch (NotFoundException $e) {
$this->logger->error("Invitation not found for token '$token'.", ['app' => InvitationApp::APP_NAME]);
$this->logger->error("Invitation not found for token '$token'.", ['app' => Application::APP_ID]);
throw new NotFoundException("An exception occurred trying to retrieve the invitation with token '$token'.");
}
if ($loginRequired == true && $this->userSession->getUser() == null) {
Expand All @@ -95,7 +95,7 @@ public function findAll(array $criteria): array
try {
return $this->mapper->findAll($criteria);
} catch (Exception $e) {
$this->logger->error('findAll failed with error: ' . $e->getMessage() . ' Trace: ' . $e->getTraceAsString(), ['app' => InvitationApp::APP_NAME]);
$this->logger->error('findAll failed with error: ' . $e->getMessage() . ' Trace: ' . $e->getTraceAsString(), ['app' => Application::APP_ID]);
throw new ServiceException('Failed to find all invitations for the specified criteria.');
}
}
Expand All @@ -112,7 +112,7 @@ public function insert(Invitation $invitation): Invitation
try {
return $this->mapper->insert($invitation);
} catch (Exception $e) {
$this->logger->error('Message: ' . $e->getMessage() . ' Trace: ' . $e->getTraceAsString(), ['app' => InvitationApp::APP_NAME]);
$this->logger->error('Message: ' . $e->getMessage() . ' Trace: ' . $e->getTraceAsString(), ['app' => Application::APP_ID]);
throw new ServiceException('Error inserting the invitation.');
}
}
Expand All @@ -128,7 +128,7 @@ public function update(array $fieldsAndValues, bool $loginRequired = true): bool
{
if ($loginRequired === true) {
if ($this->userSession->getUser() == null) {
$this->logger->debug('Unable to update invitation, unauthenticated.', ['app' => InvitationApp::APP_NAME]);
$this->logger->debug('Unable to update invitation, unauthenticated.', ['app' => Application::APP_ID]);
return false;
}
return $this->mapper->updateInvitation($fieldsAndValues, $this->userSession->getUser()->getCloudId());
Expand Down
Loading

0 comments on commit ceb94fb

Please sign in to comment.