Skip to content

Commit

Permalink
Tests for roles
Browse files Browse the repository at this point in the history
  • Loading branch information
diegosm committed Apr 5, 2020
1 parent 985dd4b commit b8c8ac1
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 15 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ $keycloakAdmin = KeycloakAdminFactory::create(
To do list (Not ordered by priority)
------
1. Use parameters to search users on list method.
2. Review and make tests for all exceptions.
3. Make realm extra endpoints (i.e. clear sessions).
4. Make documentation.
5. Change User access from array to a Class.
2. Create role composites
3. Review and make tests for all exceptions.
4. Make realm extra endpoints (i.e. clear sessions).
5. Make documentation.
6. Change User access from array to a Class.
69 changes: 58 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 118 additions & 0 deletions tests/Roles/RoleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

declare(strict_types=1);

namespace Tests\Roles;

use GuzzleHttp\Psr7\Response;
use KeycloakAdmin\Roles\Role;
use KeycloakAdmin\Roles\RoleCollection;
use KeycloakAdmin\Roles\RoleManager;
use Tests\BaseTest;

class RoleTest extends BaseTest
{
public function testItCanListUsers()
{
$client = $this->createStubClient('Roles/list.json');

$manager = $this->createRoleManager($client);

$result = $manager->list();

$this->assertInstanceOf(RoleCollection::class, $result);

$this->assertEquals($result->getRoles()[0]->getId(), 'd99e9834-fc42-4041-a38c-72caebcb857a');
}

public function testItCanSaveRole()
{
$client = $this->createStubClient('', 201);

$manager = $this->createRoleManager($client);

$json = '{"username": "diego@email.com"}';

$result = $manager->createFromJson($json)->save();

$this->assertInstanceOf(Role::class, $result);
}

public function testItCanShowRole()
{
$client = $this->createStubClient('Roles/show.json', 200);

$manager = $this->createRoleManager($client);

$result = $manager->show('fb2f0f35-ca93-48dc-96fc-4a29c8f2f734');

$this->assertInstanceOf(Role::class, $result);

$this->assertEquals('lalala-lalalala-lalalala', $result->getId());
}

public function testItCanUpdateRole()
{
$json = '{
"name": "manage-something",
"description": "La descripcion",
"composite": false,
"clientRole": true,
"containerId": "Oque é isso",
"id": "lalala-lalalala-lalalala"
}';

$updateResponse = new Response(
204,
[],
file_get_contents('tests/Stubs/Roles/update.json')
);

$showResponse = new Response(
200,
[],
$json
);

$client = $this->getMockBuilder(\GuzzleHttp\Client::class)->getMock();
$client->expects($this->at(0))
->method('request')
->willReturn($updateResponse);

$client->expects($this->at(1))
->method('request')
->willReturn($showResponse);

$manager = $this->createRoleManager($client);

$result = $manager->createFromJson($json)->update();
$this->assertEquals("manage-something", $result->getName());
}

public function testItCanDeleteRole()
{
$client = $this->createStubClient('', 204);

$manager = $this->createRoleManager($client);

$this->assertNull(
$manager->delete('fdca49c1-80c8-4492-9c97-e808202f87dd')
);
}

protected function createRoleManager($clientHTTP = null) : RoleManager
{
if (null === $clientHTTP) {
$clientHTTP = $this->createStubClient();
}

return new RoleManager(
$clientHTTP,
$this->keycloakAdminConfig,
$this->serializer,
$this->keycloakAuth,
'master',
'id'
);
}
}
8 changes: 8 additions & 0 deletions tests/Stubs/Roles/show.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "manage-something",
"description": "La descripcion",
"composite": false,
"clientRole": true,
"containerId": "Oque é isso",
"id": "lalala-lalalala-lalalala"
}
8 changes: 8 additions & 0 deletions tests/Stubs/Roles/update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "manage-something",
"description": "La descripcion",
"composite": false,
"clientRole": true,
"containerId": "Oque é isso",
"id": "lalala-lalalala-lalalala"
}

0 comments on commit b8c8ac1

Please sign in to comment.