add: add sharing groups api tests, extend openapi spec

pull/80/head
Luciano Righetti 2022-01-14 14:43:21 +01:00
parent 2d05f9228d
commit fa7316db3f
7 changed files with 523 additions and 1 deletions

View File

@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace App\Test\Fixture;
use Cake\TestSuite\Fixture\TestFixture;
class SharingGroupsFixture extends TestFixture
{
public $connection = 'test';
public const SHARING_GROUP_A_ID = 1;
public const SHARING_GROUP_B_ID = 2;
public function init(): void
{
$faker = \Faker\Factory::create();
$this->records = [
[
'id' => self::SHARING_GROUP_A_ID,
'uuid' => $faker->uuid(),
'name' => 'Sharing Group A',
'releasability' => 'Sharing Group A',
'description' => 'Sharing Group A description',
'organisation_id' => OrganisationsFixture::ORGANISATION_A_ID,
'user_id' => UsersFixture::USER_ADMIN_ID,
'active' => true,
'local' => true,
'created' => $faker->dateTime()->getTimestamp(),
'modified' => $faker->dateTime()->getTimestamp()
],
[
'id' => self::SHARING_GROUP_B_ID,
'uuid' => $faker->uuid(),
'name' => 'Sharing Group B',
'releasability' => 'Sharing Group B',
'description' => 'Sharing Group B description',
'organisation_id' => OrganisationsFixture::ORGANISATION_B_ID,
'user_id' => UsersFixture::USER_ADMIN_ID,
'active' => true,
'local' => true,
'created' => $faker->dateTime()->getTimestamp(),
'modified' => $faker->dateTime()->getTimestamp()
],
];
parent::init();
}
}

View File

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Api\Users;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
use App\Test\Fixture\AuthKeysFixture;
use App\Test\Fixture\IndividualsFixture;
use App\Test\Helper\ApiTestTrait;
class ViewIndividualApiTest extends TestCase
{
use IntegrationTestTrait;
use ApiTestTrait;
protected const ENDPOINT = '/api/v1/individuals/view';
protected $fixtures = [
'app.Organisations',
'app.Individuals',
'app.Roles',
'app.Users',
'app.AuthKeys'
];
public function setUp(): void
{
parent::setUp();
$this->initializeValidator(APP . '../webroot/docs/openapi.yaml');
}
public function testViewIndividualById(): void
{
$this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY);
$url = sprintf('%s/%d', self::ENDPOINT, IndividualsFixture::INDIVIDUAL_ADMIN_ID);
$this->get($url);
$this->assertResponseOk();
$this->assertResponseContains(sprintf('"id": %d', IndividualsFixture::INDIVIDUAL_ADMIN_ID));
// TODO: $this->assertRequestMatchesOpenApiSpec();
$this->assertResponseMatchesOpenApiSpec($url);
}
}

View File

@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Api\Users;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
use App\Test\Fixture\AuthKeysFixture;
use App\Test\Fixture\SharingGroupsFixture;
use App\Test\Helper\ApiTestTrait;
class DeleteSharingGroupApiTest extends TestCase
{
use IntegrationTestTrait;
use ApiTestTrait;
protected const ENDPOINT = '/api/v1/sharingGroups/delete';
protected $fixtures = [
'app.Organisations',
'app.Individuals',
'app.Roles',
'app.Users',
'app.AuthKeys',
'app.SharingGroups'
];
public function setUp(): void
{
parent::setUp();
$this->initializeValidator(APP . '../webroot/docs/openapi.yaml');
}
public function testDeleteSharingGroup(): void
{
$this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY);
$url = sprintf('%s/%d', self::ENDPOINT, SharingGroupsFixture::SHARING_GROUP_A_ID);
$this->delete($url);
$this->assertResponseOk();
$this->assertDbRecordNotExists('SharingGroups', ['id' => SharingGroupsFixture::SHARING_GROUP_A_ID]);
//TODO: $this->assertRequestMatchesOpenApiSpec();
$this->assertResponseMatchesOpenApiSpec($url, 'delete');
$this->addWarning('TODO: CRUDComponent::delete() sets some view variables, does not take into account `isRest()`, fix it.');
}
public function testDeleteSharingGroupNotAllowedAsRegularUser(): void
{
$this->setAuthToken(AuthKeysFixture::REGULAR_USER_API_KEY);
$url = sprintf('%s/%d', self::ENDPOINT, SharingGroupsFixture::SHARING_GROUP_A_ID);
$this->delete($url);
$this->assertResponseCode(405);
$this->assertDbRecordExists('SharingGroups', ['id' => SharingGroupsFixture::SHARING_GROUP_A_ID]);
//TODO: $this->assertRequestMatchesOpenApiSpec();
$this->assertResponseMatchesOpenApiSpec($url, 'delete');
$this->addWarning('TODO: CRUDComponent::delete() sets some view variables, does not take into account `isRest()`, fix it.');
}
}

View File

@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Api\Users;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
use App\Test\Fixture\AuthKeysFixture;
use App\Test\Fixture\OrganisationsFixture;
use App\Test\Fixture\SharingGroupsFixture;
use App\Test\Helper\ApiTestTrait;
class EditSharingGroupApiTest extends TestCase
{
use IntegrationTestTrait;
use ApiTestTrait;
protected const ENDPOINT = '/api/v1/sharingGroups/edit';
protected $fixtures = [
'app.Organisations',
'app.Individuals',
'app.Roles',
'app.Users',
'app.AuthKeys',
'app.SharingGroups'
];
public function setUp(): void
{
parent::setUp();
$this->initializeValidator(APP . '../webroot/docs/openapi.yaml');
}
public function testEditSharingGroup(): void
{
$this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY);
$url = sprintf('%s/%d', self::ENDPOINT, SharingGroupsFixture::SHARING_GROUP_A_ID);
$this->put(
$url,
[
'name' => 'Test Sharing Group 4321',
]
);
$this->assertResponseOk();
$this->assertDbRecordExists(
'SharingGroups',
[
'id' => SharingGroupsFixture::SHARING_GROUP_A_ID,
'name' => 'Test Sharing Group 4321',
]
);
//TODO: $this->assertRequestMatchesOpenApiSpec();
$this->assertResponseMatchesOpenApiSpec($url, 'put');
}
public function testEditSharingGroupNotAllowedAsRegularUser(): void
{
$this->setAuthToken(AuthKeysFixture::REGULAR_USER_API_KEY);
$url = sprintf('%s/%d', self::ENDPOINT, SharingGroupsFixture::SHARING_GROUP_B_ID);
$this->put(
$url,
[
'name' => 'Test Sharing Group 1234'
]
);
$this->assertResponseCode(405);
$this->assertDbRecordNotExists(
'SharingGroups',
[
'id' => SharingGroupsFixture::SHARING_GROUP_B_ID,
'name' => 'Test Sharing Group 1234'
]
);
//TODO: $this->assertRequestMatchesOpenApiSpec();
$this->assertResponseMatchesOpenApiSpec($url, 'put');
}
}

View File

@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Api\Users;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
use App\Test\Fixture\AuthKeysFixture;
use App\Test\Fixture\SharingGroupsFixture;
use App\Test\Helper\ApiTestTrait;
class IndexSharingGroupsApiTest extends TestCase
{
use IntegrationTestTrait;
use ApiTestTrait;
protected const ENDPOINT = '/api/v1/sharingGroups/index';
protected $fixtures = [
'app.Organisations',
'app.Individuals',
'app.Roles',
'app.Users',
'app.AuthKeys',
'app.SharingGroups'
];
public function setUp(): void
{
parent::setUp();
$this->initializeValidator(APP . '../webroot/docs/openapi.yaml');
}
public function testIndexSharingGroups(): void
{
$this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY);
$this->get(self::ENDPOINT);
$this->assertResponseOk();
$this->assertResponseContains(sprintf('"id": %d', SharingGroupsFixture::SHARING_GROUP_A_ID));
$this->assertResponseContains(sprintf('"id": %d', SharingGroupsFixture::SHARING_GROUP_B_ID));
// TODO: $this->assertRequestMatchesOpenApiSpec();
$this->assertResponseMatchesOpenApiSpec(self::ENDPOINT);
}
}

View File

@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Api\Users;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
use App\Test\Fixture\AuthKeysFixture;
use App\Test\Fixture\SharingGroupsFixture;
use App\Test\Helper\ApiTestTrait;
class ViewSharingGroupApiTest extends TestCase
{
use IntegrationTestTrait;
use ApiTestTrait;
protected const ENDPOINT = '/api/v1/sharingGroups/view';
protected $fixtures = [
'app.Organisations',
'app.Individuals',
'app.Roles',
'app.Users',
'app.AuthKeys',
'app.SharingGroups'
];
public function setUp(): void
{
parent::setUp();
$this->initializeValidator(APP . '../webroot/docs/openapi.yaml');
}
public function testVieSharingGroupById(): void
{
$this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY);
$url = sprintf('%s/%d', self::ENDPOINT, SharingGroupsFixture::SHARING_GROUP_A_ID);
$this->get($url);
$this->assertResponseOk();
$this->assertResponseContains('"name": "Sharing Group A"');
// TODO: $this->assertRequestMatchesOpenApiSpec();
$this->assertResponseMatchesOpenApiSpec($url);
}
}

View File

@ -19,6 +19,8 @@ tags:
description: "Tags can be attached to entity to quickly classify them, allowing further filtering and searches." description: "Tags can be attached to entity to quickly classify them, allowing further filtering and searches."
- name: Inbox - name: Inbox
description: "Inbox messages represent A list of requests to be manually processed." description: "Inbox messages represent A list of requests to be manually processed."
- name: SharingGroups
description: "Sharing groups are distribution lists usable by tools that can exchange information with a list of trusted partners. Create recurring or ad hoc sharing groups and share them with the members of the sharing group."
paths: paths:
/api/v1/individuals/index: /api/v1/individuals/index:
@ -39,6 +41,24 @@ paths:
default: default:
$ref: "#/components/responses/ApiErrorResponse" $ref: "#/components/responses/ApiErrorResponse"
/api/v1/individuals/view/{individualId}:
get:
summary: "Get individual by ID"
operationId: getIndividualById
tags:
- Individuals
parameters:
- $ref: "#/components/parameters/individualId"
responses:
"200":
$ref: "#/components/responses/IndividualResponse"
"403":
$ref: "#/components/responses/UnauthorizedApiErrorResponse"
"405":
$ref: "#/components/responses/MethodNotAllowedApiErrorResponse"
default:
$ref: "#/components/responses/ApiErrorResponse"
/api/v1/individuals/add: /api/v1/individuals/add:
post: post:
summary: "Add individual" summary: "Add individual"
@ -131,7 +151,7 @@ paths:
/api/v1/users/view/{userId}: /api/v1/users/view/{userId}:
get: get:
summary: "Get information of a user by id" summary: "Get information of a user by ID"
operationId: viewUserById operationId: viewUserById
tags: tags:
- Users - Users
@ -407,6 +427,80 @@ paths:
default: default:
$ref: "#/components/responses/ApiErrorResponse" $ref: "#/components/responses/ApiErrorResponse"
/api/v1/sharingGroups/index:
get:
summary: "Get a sharing groups list"
operationId: getSharingGroups
tags:
- SharingGroups
parameters:
- $ref: "#/components/parameters/quickFilter"
responses:
"200":
$ref: "#/components/responses/SharingGroupListResponse"
"403":
$ref: "#/components/responses/UnauthorizedApiErrorResponse"
"405":
$ref: "#/components/responses/MethodNotAllowedApiErrorResponse"
default:
$ref: "#/components/responses/ApiErrorResponse"
/api/v1/sharingGroups/view/{sharingGroupId}:
get:
summary: "Get sharing group by ID"
operationId: getSharingGroupById
tags:
- SharingGroups
parameters:
- $ref: "#/components/parameters/sharingGroupId"
responses:
"200":
$ref: "#/components/responses/SharingGroupResponse"
"403":
$ref: "#/components/responses/UnauthorizedApiErrorResponse"
"405":
$ref: "#/components/responses/MethodNotAllowedApiErrorResponse"
default:
$ref: "#/components/responses/ApiErrorResponse"
/api/v1/sharingGroups/delete/{sharingGroupId}:
delete:
summary: "Delete sharing group by ID"
operationId: deleteSharingGroupById
tags:
- SharingGroups
parameters:
- $ref: "#/components/parameters/sharingGroupId"
responses:
"200":
$ref: "#/components/responses/SharingGroupResponse"
"403":
$ref: "#/components/responses/UnauthorizedApiErrorResponse"
"405":
$ref: "#/components/responses/MethodNotAllowedApiErrorResponse"
default:
$ref: "#/components/responses/ApiErrorResponse"
/api/v1/sharingGroups/edit/{sharingGroupId}:
put:
summary: "Edit sharing group"
operationId: editSharingGroup
tags:
- SharingGroups
parameters:
- $ref: "#/components/parameters/sharingGroupId"
requestBody:
$ref: "#/components/requestBodies/EditSharingGroupRequest"
responses:
"200":
$ref: "#/components/responses/SharingGroupResponse"
"403":
$ref: "#/components/responses/UnauthorizedApiErrorResponse"
"405":
$ref: "#/components/responses/MethodNotAllowedApiErrorResponse"
default:
$ref: "#/components/responses/ApiErrorResponse"
components: components:
schemas: schemas:
# General # General
@ -763,6 +857,55 @@ components:
- $ref: "#/components/schemas/UserRegistrationInbox" - $ref: "#/components/schemas/UserRegistrationInbox"
- $ref: "#/components/schemas/IncomingConnectionRequestInbox" - $ref: "#/components/schemas/IncomingConnectionRequestInbox"
# SharingGroups
SharingGroupName:
type: string
SharingGroupReleasability:
type: string
SharingGroupDescription:
type: string
SharingGroup:
type: object
properties:
id:
$ref: "#/components/schemas/ID"
uuid:
$ref: "#/components/schemas/UUID"
name:
$ref: "#/components/schemas/SharingGroupName"
releasability:
$ref: "#/components/schemas/SharingGroupReleasability"
description:
$ref: "#/components/schemas/SharingGroupDescription"
organisation_id:
$ref: "#/components/schemas/ID"
user_id:
$ref: "#/components/schemas/ID"
active:
type: boolean
local:
type: boolean
sharing_group_orgs:
type: array
items:
$ref: "#/components/schemas/Organisation"
user:
$ref: "#/components/schemas/User"
organisation:
$ref: "#/components/schemas/Organisation"
created:
$ref: "#/components/schemas/DateTime"
modified:
$ref: "#/components/schemas/DateTime"
SharingGroupList:
type: array
items:
$ref: "#/components/schemas/SharingGroup"
# Errors # Errors
ApiError: ApiError:
type: object type: object
@ -856,6 +999,14 @@ components:
schema: schema:
$ref: "#/components/schemas/ID" $ref: "#/components/schemas/ID"
sharingGroupId:
name: sharingGroupId
in: path
description: "Numeric ID of the Sharing Group"
required: true
schema:
$ref: "#/components/schemas/ID"
quickFilter: quickFilter:
name: quickFilter name: quickFilter
in: query in: query
@ -1038,6 +1189,31 @@ components:
password: password:
type: string type: string
# SharingGroups
EditSharingGroupRequest:
required: true
content:
application/json:
schema:
type: object
properties:
uuid:
$ref: "#/components/schemas/UUID"
name:
$ref: "#/components/schemas/SharingGroupName"
releasability:
$ref: "#/components/schemas/SharingGroupReleasability"
description:
$ref: "#/components/schemas/SharingGroupDescription"
organisation_id:
$ref: "#/components/schemas/ID"
user_id:
$ref: "#/components/schemas/ID"
active:
type: boolean
local:
type: boolean
responses: responses:
# Individuals # Individuals
IndividualResponse: IndividualResponse:
@ -1144,6 +1320,22 @@ components:
type: array type: array
items: items:
type: object type: object
# TODO: describe
# SharingGroups
SharingGroupResponse:
description: "Sharing group response"
content:
application/json:
schema:
$ref: "#/components/schemas/SharingGroup"
SharingGroupListResponse:
description: "Sharing groups list response"
content:
application/json:
schema:
$ref: "#/components/schemas/SharingGroupList"
# Errors # Errors
ApiErrorResponse: ApiErrorResponse: