Skip to content

Commit

Permalink
feat(organizations): add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fschmtt committed Dec 30, 2024
1 parent bdef932 commit b5f3c13
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 10 deletions.
6 changes: 6 additions & 0 deletions src/Representation/Realm.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@
* @method self withWebAuthnPolicySignatureAlgorithms(?array $value)
* @method self withWebAuthnPolicyUserVerificationRequirement(?string $value)
*
* @method OrganizationCollection|null getOrganizations()
* @method self withOrganizations(?OrganizationCollection $organizations)
*
* @method bool|null getOrganizationsEnabled()
* @method self withOrganizationsEnabled(?bool $organizationsEnabled)
*
* @codeCoverageIgnore
*/
class Realm extends Representation
Expand Down
3 changes: 1 addition & 2 deletions src/Resource/Realms.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public function import(Realm $realm): Realm
new Command(
'/admin/realms',
Method::POST,
[],
$realm,
payload: $realm,
),
);

Expand Down
10 changes: 5 additions & 5 deletions tests/Integration/IntegrationTestBehaviour.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@

trait IntegrationTestBehaviour
{
private ?Keycloak $keycloak = null;
private static ?Keycloak $keycloak = null;

public function getKeycloak(): Keycloak
public static function getKeycloak(): Keycloak
{
if (!$this->keycloak) {
$this->keycloak = new Keycloak(
if (!self::$keycloak) {
self::$keycloak = new Keycloak(
$_SERVER['KEYCLOAK_BASE_URL'] ?? 'http://keycloak:8080',
'admin',
'admin',
);
}

return $this->keycloak;
return self::$keycloak;
}

protected function skipIfKeycloakVersionIsLessThan(string $version): void
Expand Down
6 changes: 3 additions & 3 deletions tests/Integration/KeycloakTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ class KeycloakTest extends TestCase
public function testFetchesKeycloakVersionBeforeResourceIsAccessedForTheFirstTime(): void
{
$reflection = new ReflectionClass($this->getKeycloak());
$version = $reflection->getProperty('version')->getValue($this->keycloak);
$version = $reflection->getProperty('version')->getValue(static::getKeycloak());

static::assertNull($version);

$this->keycloak->realms();
static::getKeycloak()->realms();

$version = $reflection->getProperty('version')->getValue($this->keycloak);
$version = $reflection->getProperty('version')->getValue(static::getKeycloak());
static::assertIsString($version);
}
}
71 changes: 71 additions & 0 deletions tests/Integration/Resource/OrganizationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace Fschmtt\Keycloak\Test\Integration\Resource;

use Fschmtt\Keycloak\Collection\OrganizationDomainCollection;
use Fschmtt\Keycloak\Representation\Organization;
use Fschmtt\Keycloak\Representation\OrganizationDomain;
use Fschmtt\Keycloak\Representation\Realm;
use Fschmtt\Keycloak\Test\Integration\IntegrationTestBehaviour;
use PHPUnit\Framework\Attributes\AfterClass;
use PHPUnit\Framework\Attributes\BeforeClass;
use PHPUnit\Framework\TestCase;

class OrganizationsTest extends TestCase
{
use IntegrationTestBehaviour;
private const REALM = 'organizations-tests';

#[BeforeClass]
public static function createRealmWithOrganizationsEnabled(): void
{
static::getKeycloak()->realms()->import(new Realm(realm: self::REALM, organizationsEnabled: true));
}

#[AfterClass]
public static function deleteRealmWithOrganizationsEnabled(): void
{
static::getKeycloak()->realms()->delete(self::REALM);
}

public function testOrganizations(): void
{
// No organizations exist yet in realm
$organizations = $this->getKeycloak()->organizations()->all(self::REALM);
static::assertCount(0, $organizations);

// Create a new organization in realm
$createdOrganization = new Organization(
name: 'created-organization',
domains: new OrganizationDomainCollection([
new OrganizationDomain('foo.bar', true),
new OrganizationDomain('bar.foo', false),
]),
);
$this->getKeycloak()->organizations()->create(self::REALM, $createdOrganization);

$organizations = $this->getKeycloak()->organizations()->all(self::REALM);
static::assertCount(1, $organizations);
static::assertSame($createdOrganization->getName(), $organizations->first()->getName());

// Get newly created organization
$organization = static::getKeycloak()->organizations()->get(self::REALM, $organizations->first()->getId());
static::assertSame($createdOrganization->getName(), $organization->getName());

// Invite user to newly created organization
static::getKeycloak()->organizations()->inviteUser(
self::REALM,
$organizations->first()->getId(),
'[email protected]',
'John',
'Doe',
);

// Delete newly created organization
static::getKeycloak()->organizations()->delete(self::REALM, $organizations->first()->getId());
$organizations = $this->getKeycloak()->organizations()->all(self::REALM);
static::assertCount(0, $organizations);
}
}

0 comments on commit b5f3c13

Please sign in to comment.