mirror of
https://github.com/Sylius/Sylius.git
synced 2026-07-05 20:57:12 +00:00
[API][Administrator] Update mapping
This commit is contained in:
parent
c2f83dcffa
commit
a47cf38bf4
7 changed files with 274 additions and 2 deletions
|
|
@ -34,9 +34,12 @@
|
|||
<attribute>sylius</attribute>
|
||||
<attribute>sylius_user_create</attribute>
|
||||
</attribute>
|
||||
<attribute name="normalization_context">
|
||||
<attribute name="denormalization_context">
|
||||
<attribute name="groups">admin:admin_user:create</attribute>
|
||||
</attribute>
|
||||
<attribute name="normalization_context">
|
||||
<attribute name="groups">admin:admin_user:read</attribute>
|
||||
</attribute>
|
||||
</collectionOperation>
|
||||
</collectionOperations>
|
||||
|
||||
|
|
@ -53,6 +56,9 @@
|
|||
<attribute name="denormalization_context">
|
||||
<attribute name="groups">admin:admin_user:update</attribute>
|
||||
</attribute>
|
||||
<attribute name="normalization_context">
|
||||
<attribute name="groups">admin:admin_user:read</attribute>
|
||||
</attribute>
|
||||
</itemOperation>
|
||||
|
||||
<itemOperation name="admin_delete">
|
||||
|
|
|
|||
|
|
@ -13,12 +13,16 @@ declare(strict_types=1);
|
|||
|
||||
namespace Sylius\Tests\Api\Admin;
|
||||
|
||||
use Sylius\Component\Core\Model\AdminUserInterface;
|
||||
use Sylius\Tests\Api\JsonApiTestCase;
|
||||
use Sylius\Tests\Api\Utils\AdminUserLoginTrait;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
final class AdminUsersTest extends JsonApiTestCase
|
||||
{
|
||||
use AdminUserLoginTrait;
|
||||
|
||||
/** @test */
|
||||
public function it_allows_admin_users_to_log_in(): void
|
||||
{
|
||||
|
|
@ -54,10 +58,141 @@ final class AdminUsersTest extends JsonApiTestCase
|
|||
self::CONTENT_TYPE_HEADER,
|
||||
json_encode([
|
||||
'email' => 'api@example.com',
|
||||
])
|
||||
], JSON_THROW_ON_ERROR),
|
||||
);
|
||||
|
||||
$response = $this->client->getResponse();
|
||||
$this->assertResponseCode($response, Response::HTTP_ACCEPTED);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_gets_administrators(): void
|
||||
{
|
||||
$this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'administrator.yaml']);
|
||||
$header = $this->getLoggedHeader();
|
||||
|
||||
$this->client->request('GET', '/api/v2/admin/administrators', [], [], $header,);
|
||||
|
||||
$this->assertResponse(
|
||||
$this->client->getResponse(),
|
||||
'admin/administrator/get_administrators_response',
|
||||
Response::HTTP_OK,
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_gets_an_administrator(): void
|
||||
{
|
||||
$fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'administrator.yaml']);
|
||||
$header = $this->getLoggedHeader();
|
||||
|
||||
/** @var AdminUserInterface $administrator */
|
||||
$administrator = $fixtures['admin_user_wilhelm'];
|
||||
|
||||
$this->client->request(
|
||||
'GET',
|
||||
sprintf('/api/v2/admin/administrators/%s', $administrator->getId()),
|
||||
[],
|
||||
[],
|
||||
$header,
|
||||
);
|
||||
|
||||
$this->assertResponse(
|
||||
$this->client->getResponse(),
|
||||
'admin/administrator/get_administrator_response',
|
||||
Response::HTTP_OK,
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_creates_an_administrator(): void
|
||||
{
|
||||
$this->loadFixturesFromFile('authentication/api_administrator.yaml');
|
||||
$header = $this->getLoggedHeader();
|
||||
|
||||
$this->client->request(
|
||||
'POST',
|
||||
'/api/v2/admin/administrators',
|
||||
[],
|
||||
[],
|
||||
$header,
|
||||
json_encode([
|
||||
'email' => 'j.api@test.com',
|
||||
'username' => 'johnApi',
|
||||
'plainPassword' => 'very-secure',
|
||||
'enabled' => true,
|
||||
'firstName' => 'John',
|
||||
'lastName' => 'Api',
|
||||
'localeCode' => 'ga_IE',
|
||||
], JSON_THROW_ON_ERROR),
|
||||
);
|
||||
|
||||
$this->assertResponse(
|
||||
$this->client->getResponse(),
|
||||
'admin/administrator/create_administrator_response',
|
||||
Response::HTTP_CREATED,
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_updates_an_administrator(): void
|
||||
{
|
||||
$fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'administrator.yaml']);
|
||||
$header = $this->getLoggedHeader();
|
||||
|
||||
/** @var AdminUserInterface $administrator */
|
||||
$administrator = $fixtures['admin_user_wilhelm'];
|
||||
|
||||
$this->client->request(
|
||||
'PUT',
|
||||
sprintf('/api/v2/admin/administrators/%s', $administrator->getId()),
|
||||
[],
|
||||
[],
|
||||
$header,
|
||||
json_encode([
|
||||
'email' => 'j.api@test.com',
|
||||
'username' => 'johnApi',
|
||||
'plainPassword' => 'very-secure',
|
||||
'enabled' => false,
|
||||
'firstName' => 'John',
|
||||
'lastName' => 'Api',
|
||||
'localeCode' => 'ga_IE',
|
||||
], JSON_THROW_ON_ERROR),
|
||||
);
|
||||
|
||||
$this->assertResponse(
|
||||
$this->client->getResponse(),
|
||||
'admin/administrator/put_administrator_response',
|
||||
Response::HTTP_OK,
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_deletes_an_administrator(): void
|
||||
{
|
||||
$fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'administrator.yaml']);
|
||||
$header = $this->getLoggedHeader();
|
||||
|
||||
/** @var AdminUserInterface $administrator */
|
||||
$administrator = $fixtures['admin_user_wilhelm'];
|
||||
|
||||
$this->client->request(
|
||||
'DELETE',
|
||||
sprintf('/api/v2/admin/administrators/%s', $administrator->getId()),
|
||||
[],
|
||||
[],
|
||||
$header
|
||||
);
|
||||
|
||||
$this->assertResponseCode($this->client->getResponse(), Response::HTTP_NO_CONTENT);
|
||||
}
|
||||
|
||||
private function getLoggedHeader(): array
|
||||
{
|
||||
$token = $this->logInAdminUser('api@example.com');
|
||||
$authorizationHeader = self::$kernel->getContainer()->getParameter('sylius.api.authorization_header');
|
||||
$header['HTTP_' . $authorizationHeader] = 'Bearer ' . $token;
|
||||
|
||||
return array_merge($header, self::CONTENT_TYPE_HEADER);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
8
tests/Api/DataFixtures/ORM/administrator.yaml
Normal file
8
tests/Api/DataFixtures/ORM/administrator.yaml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
Sylius\Component\Core\Model\AdminUser:
|
||||
admin_user_{rich, susie, wilhelm}:
|
||||
plainPassword: sylius
|
||||
roles: [ROLE_API_ACCESS]
|
||||
enabled: true
|
||||
email: "api_<current()>\\@admin.com"
|
||||
username: <current()>
|
||||
localeCode: en
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"@context": "\/api\/v2\/contexts\/Administrator",
|
||||
"@id": "\/api\/v2\/admin\/administrators\/@integer@",
|
||||
"@type": "Administrator",
|
||||
"firstName": "John",
|
||||
"lastName": "Api",
|
||||
"localeCode": "ga_IE",
|
||||
"avatar": null,
|
||||
"id": @integer@,
|
||||
"username": "johnApi",
|
||||
"lastLogin": null,
|
||||
"verifiedAt": null,
|
||||
"email": "j.api@test.com",
|
||||
"createdAt": @date@,
|
||||
"updatedAt": @date@,
|
||||
"enabled": true
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"@context": "\/api\/v2\/contexts\/Administrator",
|
||||
"@id": "\/api\/v2\/admin\/administrators\/@integer@",
|
||||
"@type": "Administrator",
|
||||
"firstName": null,
|
||||
"lastName": null,
|
||||
"localeCode": "en",
|
||||
"avatar": null,
|
||||
"id": @integer@,
|
||||
"username": "wilhelm",
|
||||
"lastLogin": null,
|
||||
"verifiedAt": null,
|
||||
"email": "api_wilhelm@admin.com",
|
||||
"createdAt": @date@,
|
||||
"updatedAt": @date@,
|
||||
"enabled": true
|
||||
}
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
{
|
||||
"@context": "\/api\/v2\/contexts\/Administrator",
|
||||
"@id": "\/api\/v2\/admin\/administrators",
|
||||
"@type": "hydra:Collection",
|
||||
"hydra:member": [
|
||||
{
|
||||
"@id": "\/api\/v2\/admin\/administrators\/@integer@",
|
||||
"@type": "Administrator",
|
||||
"firstName": null,
|
||||
"lastName": null,
|
||||
"localeCode": "en",
|
||||
"avatar": null,
|
||||
"id": @integer@,
|
||||
"username": "rich",
|
||||
"lastLogin": null,
|
||||
"verifiedAt": null,
|
||||
"email": "api_rich@admin.com",
|
||||
"createdAt": @date@,
|
||||
"updatedAt": @date@,
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"@id": "\/api\/v2\/admin\/administrators\/@integer@",
|
||||
"@type": "Administrator",
|
||||
"firstName": null,
|
||||
"lastName": null,
|
||||
"localeCode": "en",
|
||||
"avatar": null,
|
||||
"id": @integer@,
|
||||
"username": "susie",
|
||||
"lastLogin": null,
|
||||
"verifiedAt": null,
|
||||
"email": "api_susie@admin.com",
|
||||
"createdAt": @date@,
|
||||
"updatedAt": @date@,
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"@id": "\/api\/v2\/admin\/administrators\/@integer@",
|
||||
"@type": "Administrator",
|
||||
"firstName": null,
|
||||
"lastName": null,
|
||||
"localeCode": "en",
|
||||
"avatar": null,
|
||||
"id": @integer@,
|
||||
"username": "wilhelm",
|
||||
"lastLogin": null,
|
||||
"verifiedAt": null,
|
||||
"email": "api_wilhelm@admin.com",
|
||||
"createdAt": @date@,
|
||||
"updatedAt": @date@,
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"@id": "\/api\/v2\/admin\/administrators\/@integer@",
|
||||
"@type": "Administrator",
|
||||
"firstName": null,
|
||||
"lastName": null,
|
||||
"localeCode": "en",
|
||||
"avatar": null,
|
||||
"id": @integer@,
|
||||
"username": "sylius",
|
||||
"lastLogin": null,
|
||||
"verifiedAt": null,
|
||||
"email": "api@example.com",
|
||||
"createdAt": @date@,
|
||||
"updatedAt": @date@,
|
||||
"enabled": true
|
||||
}
|
||||
],
|
||||
"hydra:totalItems": 4
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"@context": "\/api\/v2\/contexts\/Administrator",
|
||||
"@id": "\/api\/v2\/admin\/administrators\/@integer@",
|
||||
"@type": "Administrator",
|
||||
"firstName": "John",
|
||||
"lastName": "Api",
|
||||
"localeCode": "ga_IE",
|
||||
"avatar": null,
|
||||
"id": @integer@,
|
||||
"username": "johnApi",
|
||||
"lastLogin": null,
|
||||
"verifiedAt": null,
|
||||
"email": "j.api@test.com",
|
||||
"createdAt": @date@,
|
||||
"updatedAt": @date@,
|
||||
"enabled": false
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue