Skip to content

Commit

Permalink
Added attributes tests cases
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec committed Apr 22, 2022
1 parent 1525977 commit 47adcf1
Show file tree
Hide file tree
Showing 6 changed files with 394 additions and 0 deletions.
59 changes: 59 additions & 0 deletions tests/cases/Unit/Controllers/DeviceAttributesV1ControllerTest.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php declare(strict_types = 1);

namespace Tests\Cases;

use FastyBird\WebServer\Http;
use Fig\Http\Message\RequestMethodInterface;
use IPub\SlimRouter;
use React\Http\Message\ServerRequest;
use Tester\Assert;
use Tests\Tools;

require_once __DIR__ . '/../../../bootstrap.php';
require_once __DIR__ . '/../DbTestCase.php';

/**
* @testCase
*/
final class DeviceAttributesV1ControllerTest extends DbTestCase
{

/**
* @param string $url
* @param string|null $token
* @param int $statusCode
* @param string $fixture
*
* @dataProvider ./../../../fixtures/Controllers/deviceAttributesRead.php
*/
public function testRead(string $url, ?string $token, int $statusCode, string $fixture): void
{
/** @var SlimRouter\Routing\IRouter $router */
$router = $this->getContainer()->getByType(SlimRouter\Routing\IRouter::class);

$headers = [];

if ($token !== null) {
$headers['authorization'] = $token;
}

$request = new ServerRequest(
RequestMethodInterface::METHOD_GET,
$url,
$headers
);

$response = $router->handle($request);

Tools\JsonAssert::assertFixtureMatch(
$fixture,
(string) $response->getBody()
);
Assert::same($statusCode, $response->getStatusCode());
Assert::type(Http\Response::class, $response);
}

}

$test_case = new DeviceAttributesV1ControllerTest();
$test_case->run();
112 changes: 112 additions & 0 deletions tests/fixtures/Controllers/deviceAttributesRead.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php declare(strict_types = 1);

use Fig\Http\Message\StatusCodeInterface;

const VALID_TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJjb20uZmFzdHliaXJkLmF1dGgtbW9kdWxlIiwianRpIjoiMjQ3MTBlOTYtYTZmYi00ZmM3LWFh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ItNThlZTRiZTUzMjdkIiwiaWF0IjoxNTg1NzQyNDAwLCJleHAiOjE1ODU3NDk2MDAsInVzZXIiOiI1ZTc5ZWZiZi1iZDBkLTViN2MtNDZlZi1iZmJkZWZiZmJkMzQiLCJyb2xlcyI6WyJ1c2VyIl19.jELVcZGRa5_-Jcpoo3Jfho08vQT2IobtoEQPhxN2tzw';

return [
// Valid responses
//////////////////
'readAll' => [
'/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes',
'Bearer ' . VALID_TOKEN,
StatusCodeInterface::STATUS_OK,
__DIR__ . '/responses/device.attributes.index.json',
],
'readAllPaging' => [
'/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes?page[offset]=1&page[limit]=1',
'Bearer ' . VALID_TOKEN,
StatusCodeInterface::STATUS_OK,
__DIR__ . '/responses/device.attributes.index.paging.json',
],
'readOne' => [
'/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes/0e771233-fd53-43dd-bd24-cda3303f902e',
'Bearer ' . VALID_TOKEN,
StatusCodeInterface::STATUS_OK,
__DIR__ . '/responses/device.attributes.read.json',
],
'readRelationshipsDevice' => [
'/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes/0e771233-fd53-43dd-bd24-cda3303f902e/relationships/device',
'Bearer ' . VALID_TOKEN,
StatusCodeInterface::STATUS_OK,
__DIR__ . '/responses/device.attributes.relationships.device.json',
],

// Invalid responses
////////////////////
'readOneUnknown' => [
'/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes/28bc0d38-2f7c-4a71-aa74-27b102f8dfc4',
'Bearer ' . VALID_TOKEN,
StatusCodeInterface::STATUS_NOT_FOUND,
__DIR__ . '/responses/generic/notFound.json',
],
'readRelationshipsDeviceUnknownAttribute' => [
'/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes/28bc0d38-2f7c-4a71-aa74-27b102f8dfc4/relationships/device',
'Bearer ' . VALID_TOKEN,
StatusCodeInterface::STATUS_NOT_FOUND,
__DIR__ . '/responses/generic/notFound.json',
],
'readRelationshipsUnknownDevice' => [
'/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009af/attributes/0e771233-fd53-43dd-bd24-cda3303f902e/relationships/device',
'Bearer ' . VALID_TOKEN,
StatusCodeInterface::STATUS_NOT_FOUND,
__DIR__ . '/responses/generic/notFound.json',
],
'readRelationshipsUnknown' => [
'/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes/0e771233-fd53-43dd-bd24-cda3303f902e/relationships/unknown',
'Bearer ' . VALID_TOKEN,
StatusCodeInterface::STATUS_NOT_FOUND,
__DIR__ . '/responses/generic/relation.unknown.json',
],
'readAllMissingToken' => [
'/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes',
null,
StatusCodeInterface::STATUS_FORBIDDEN,
__DIR__ . '/responses/generic/forbidden.json',
],
'readOneMissingToken' => [
'/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes/0e771233-fd53-43dd-bd24-cda3303f902e',
null,
StatusCodeInterface::STATUS_FORBIDDEN,
__DIR__ . '/responses/generic/forbidden.json',
],
'readAllEmptyToken' => [
'/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes',
'',
StatusCodeInterface::STATUS_FORBIDDEN,
__DIR__ . '/responses/generic/forbidden.json',
],
'readOneEmptyToken' => [
'/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes/0e771233-fd53-43dd-bd24-cda3303f902e',
'',
StatusCodeInterface::STATUS_FORBIDDEN,
__DIR__ . '/responses/generic/forbidden.json',
],
'readAllInvalidToken' => [
'/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes',
'Bearer ' . INVALID_TOKEN,
StatusCodeInterface::STATUS_UNAUTHORIZED,
__DIR__ . '/responses/generic/unauthorized.json',
],
'readOneInvalidToken' => [
'/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes/0e771233-fd53-43dd-bd24-cda3303f902e',
'Bearer ' . INVALID_TOKEN,
StatusCodeInterface::STATUS_UNAUTHORIZED,
__DIR__ . '/responses/generic/unauthorized.json',
],
'readAllExpiredToken' => [
'/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes',
'Bearer ' . EXPIRED_TOKEN,
StatusCodeInterface::STATUS_UNAUTHORIZED,
__DIR__ . '/responses/generic/unauthorized.json',
],
'readOneExpiredToken' => [
'/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes/0e771233-fd53-43dd-bd24-cda3303f902e',
'Bearer ' . EXPIRED_TOKEN,
StatusCodeInterface::STATUS_UNAUTHORIZED,
__DIR__ . '/responses/generic/unauthorized.json',
],
];
129 changes: 129 additions & 0 deletions tests/fixtures/Controllers/responses/device.attributes.index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
{
"meta" : {
"author" : "FastyBird team",
"copyright" : "FastyBird s.r.o"
},
"jsonapi" : {
"version" : "1.1"
},
"links" : {
"self" : "/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes"
},
"data" : [
{
"type" : "com.fastybird.devices-module/attribute/device",
"id" : "03164a6d-9628-460c-95cc-90e6216332d9",
"attributes" : {
"identifier" : "hardware_manufacturer",
"name" : null,
"content" : "itead"
},
"relationships" : {
"device" : {
"links" : {
"related" : "/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa"
},
"data" : {
"type" : "com.fastybird.devices-module/device/blank",
"id" : "69786d15-fd0c-4d9f-9378-33287c2009fa"
}
}
},
"links" : {
"self" : "/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes/03164a6d-9628-460c-95cc-90e6216332d9"
}
},
{
"type" : "com.fastybird.devices-module/attribute/device",
"id" : "06599b74-0236-4a98-99c8-c459a3cdb6a4",
"attributes" : {
"identifier" : "hardware_model",
"name" : null,
"content" : "sonoff_basic"
},
"relationships" : {
"device" : {
"links" : {
"related" : "/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa"
},
"data" : {
"type" : "com.fastybird.devices-module/device/blank",
"id" : "69786d15-fd0c-4d9f-9378-33287c2009fa"
}
}
},
"links" : {
"self" : "/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes/06599b74-0236-4a98-99c8-c459a3cdb6a4"
}
},
{
"type" : "com.fastybird.devices-module/attribute/device",
"id" : "090df4f2-5f23-4118-a2bd-6f0646cf2a70",
"attributes" : {
"identifier" : "hardware_version",
"name" : null,
"content" : "rev1"
},
"relationships" : {
"device" : {
"links" : {
"related" : "/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa"
},
"data" : {
"type" : "com.fastybird.devices-module/device/blank",
"id" : "69786d15-fd0c-4d9f-9378-33287c2009fa"
}
}
},
"links" : {
"self" : "/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes/090df4f2-5f23-4118-a2bd-6f0646cf2a70"
}
},
{
"type" : "com.fastybird.devices-module/attribute/device",
"id" : "0e771233-fd53-43dd-bd24-cda3303f902e",
"attributes" : {
"identifier" : "hardware_mac_address",
"name" : null,
"content" : "80:7d:3a:3d:be:6d"
},
"relationships" : {
"device" : {
"links" : {
"related" : "/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa"
},
"data" : {
"type" : "com.fastybird.devices-module/device/blank",
"id" : "69786d15-fd0c-4d9f-9378-33287c2009fa"
}
}
},
"links" : {
"self" : "/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes/0e771233-fd53-43dd-bd24-cda3303f902e"
}
},
{
"type" : "com.fastybird.devices-module/attribute/device",
"id" : "0eb39dae-ef88-4bb5-a9ea-94b0b788101f",
"attributes" : {
"identifier" : "firmware_manufacturer",
"name" : null,
"content" : "fastybird"
},
"relationships" : {
"device" : {
"links" : {
"related" : "/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa"
},
"data" : {
"type" : "com.fastybird.devices-module/device/blank",
"id" : "69786d15-fd0c-4d9f-9378-33287c2009fa"
}
}
},
"links" : {
"self" : "/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes/0eb39dae-ef88-4bb5-a9ea-94b0b788101f"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"meta" : {
"author" : "FastyBird team",
"copyright" : "FastyBird s.r.o",
"totalCount" : 5
},
"jsonapi" : {
"version" : "1.1"
},
"links" : {
"self" : "/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes?page%5Boffset%5D=1&page%5Blimit%5D=1",
"first" : "/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes?page%5Boffset%5D=0&page%5Blimit%5D=1",
"prev" : "/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes?page%5Boffset%5D=0&page%5Blimit%5D=1",
"next" : "/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes?page%5Boffset%5D=2&page%5Blimit%5D=1",
"last" : "/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes?page%5Boffset%5D=4&page%5Blimit%5D=1"
},
"data" : [
{
"type" : "com.fastybird.devices-module/attribute/device",
"id" : "06599b74-0236-4a98-99c8-c459a3cdb6a4",
"attributes" : {
"identifier" : "hardware_model",
"name" : null,
"content" : "sonoff_basic"
},
"relationships" : {
"device" : {
"links" : {
"related" : "/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa"
},
"data" : {
"type" : "com.fastybird.devices-module/device/blank",
"id" : "69786d15-fd0c-4d9f-9378-33287c2009fa"
}
}
},
"links" : {
"self" : "/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes/06599b74-0236-4a98-99c8-c459a3cdb6a4"
}
}
]
}
35 changes: 35 additions & 0 deletions tests/fixtures/Controllers/responses/device.attributes.read.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"meta" : {
"author" : "FastyBird team",
"copyright" : "FastyBird s.r.o"
},
"jsonapi" : {
"version" : "1.1"
},
"links" : {
"self" : "/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes/0e771233-fd53-43dd-bd24-cda3303f902e"
},
"data" : {
"type" : "com.fastybird.devices-module/attribute/device",
"id" : "0e771233-fd53-43dd-bd24-cda3303f902e",
"attributes" : {
"identifier" : "hardware_mac_address",
"name" : null,
"content" : "80:7d:3a:3d:be:6d"
},
"relationships" : {
"device" : {
"links" : {
"related" : "/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa"
},
"data" : {
"type" : "com.fastybird.devices-module/device/blank",
"id" : "69786d15-fd0c-4d9f-9378-33287c2009fa"
}
}
},
"links" : {
"self" : "/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes/0e771233-fd53-43dd-bd24-cda3303f902e"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"meta" : {
"author" : "FastyBird team",
"copyright" : "FastyBird s.r.o"
},
"jsonapi" : {
"version" : "1.1"
},
"links" : {
"self" : "/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa/attributes/0e771233-fd53-43dd-bd24-cda3303f902e/relationships/device",
"related" : "/v1/devices/69786d15-fd0c-4d9f-9378-33287c2009fa"
},
"data" : {
"type" : "com.fastybird.devices-module/device/blank",
"id" : "69786d15-fd0c-4d9f-9378-33287c2009fa"
}
}

0 comments on commit 47adcf1

Please sign in to comment.