Skip to content

Commit af887aa

Browse files
committed
feat: children deletion tests
Signed-off-by: romanetar <roman_ag@hotmail.com>
1 parent b69c731 commit af887aa

10 files changed

Lines changed: 184 additions & 21 deletions

app/Models/OAuth2/ResourceServer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class ResourceServer extends BaseEntity
4949
/**
5050
* @var Api[]
5151
*/
52-
#[ORM\OneToMany(targetEntity: \Api::class, mappedBy: 'resource_server', cascade: ['persist'], fetch: 'EXTRA_LAZY')]
52+
#[ORM\OneToMany(targetEntity: \Api::class, mappedBy: 'resource_server', cascade: ['persist'], orphanRemoval: true, fetch: 'EXTRA_LAZY')]
5353
private $apis;
5454

5555
/**

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"mockery/mockery": "^1.6",
6464
"nunomaduro/collision": "^8.0",
6565
"phpunit/phpunit": "^11.0.1",
66-
"rector/rector": "*"
66+
"rector/rector": "^2.0"
6767
},
6868
"suggest":{
6969
"lib-openssl": "Required to use AES algorithms (except AES GCM)",

composer.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/unit/ApiEndpointMappingTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Models\OAuth2\Api;
1818
use Models\OAuth2\ApiEndpoint;
1919
use Models\OAuth2\ApiScope;
20+
use Models\OAuth2\ApiScopeGroup;
2021
use Tests\BrowserKitTestCase;
2122

2223
/**
@@ -33,11 +34,22 @@ public function testApiEndpointPersistence()
3334

3435
$api = EntityManager::getRepository(Api::class)->findAll()[0];
3536

37+
$group = new ApiScopeGroup();
38+
$group->setName($name);
39+
$group->setActive(true);
40+
$group->setDescription('test description');
41+
42+
EntityManager::persist($group);
43+
3644
$scope = new ApiScope();
3745
$scope->setName($scope_name);
3846
$scope->setShortDescription('test short description');
3947
$scope->setDescription('test description');
4048
$scope->setActive(true);
49+
50+
//Many-to-Many relation with ApiScopeGroup
51+
$scope->addToScopeGroup($group);
52+
4153
EntityManager::persist($scope);
4254

4355
$endpoint = new ApiEndpoint();
@@ -48,6 +60,8 @@ public function testApiEndpointPersistence()
4860
$endpoint->setAllowCors(true);
4961
$endpoint->setAllowCredentials(true);
5062
$endpoint->setApi($api);
63+
64+
//Many-to-Many relation with ApiScope
5165
$endpoint->addScope($scope);
5266

5367
EntityManager::persist($endpoint);
@@ -63,5 +77,18 @@ public function testApiEndpointPersistence()
6377
$this->assertEquals($name, $found_endpoint->getName());
6478
$this->assertEquals($route, $found_endpoint->getRoute());
6579
$this->assertEquals($scope_name, $found_scope->getName());
80+
$this->assertCount(1, $found_scope->getScopeGroups()->toArray());
81+
82+
//Children removal tests
83+
$endpoint = $repo->find($found_endpoint->getId());
84+
$former_scopes_count = count($endpoint->getScopes());
85+
$endpoint->removeScope($found_scope);
86+
87+
EntityManager::persist($endpoint);
88+
EntityManager::flush();
89+
EntityManager::clear();
90+
91+
$found_endpoint = $repo->find($found_endpoint->getId());
92+
$this->assertCount($former_scopes_count - 1, $found_endpoint->getScopes());
6693
}
6794
}

tests/unit/ApiScopeGroupMappingTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function testApiScopeGroupPersistence()
4040
$scope->setActive(true);
4141
EntityManager::persist($scope);
4242

43+
//Many-to-Many relation with ApiScope
4344
$group->addScope($scope);
4445

4546
EntityManager::persist($group);

tests/unit/BannedIPMappingTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* limitations under the License.
1414
**/
1515

16+
use Auth\User;
1617
use LaravelDoctrine\ORM\Facades\EntityManager;
1718
use Models\BannedIP;
1819
use Tests\BrowserKitTestCase;
@@ -26,11 +27,17 @@ class BannedIPMappingTest extends BrowserKitTestCase
2627
public function testBannedIPPersistence()
2728
{
2829
$ip = "127.0.0.1";
30+
31+
$user = EntityManager::getRepository(User::class)->findAll()[0];
32+
2933
$banned_ip = new BannedIP();
3034
$banned_ip->setIp("127.0.0.1");
31-
$banned_ip->setExceptionType("TestExceptionType");;
35+
$banned_ip->setExceptionType("TestExceptionType");
3236
$banned_ip->setHits(1);
3337

38+
//Many-to-one relation with User
39+
$banned_ip->setUser($user);
40+
3441
EntityManager::persist($banned_ip);
3542
EntityManager::flush();
3643
EntityManager::clear();
@@ -40,5 +47,6 @@ public function testBannedIPPersistence()
4047

4148
$this->assertInstanceOf(BannedIP::class, $found_banned_ip);
4249
$this->assertEquals($ip, $found_banned_ip->getIp());
50+
$this->assertEquals($user->getEmail(), $found_banned_ip->getUser()->getEmail());
4351
}
4452
}

tests/unit/ClientMappingTest.php

Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@
1313
* limitations under the License.
1414
**/
1515

16+
use DateTimeZone;
17+
use jwa\JSONWebSignatureAndEncryptionAlgorithms;
18+
use jwk\JSONWebKeyPublicKeyUseValues;
19+
use jwk\JSONWebKeyTypes;
1620
use LaravelDoctrine\ORM\Facades\EntityManager;
21+
use Models\OAuth2\Api;
22+
use Models\OAuth2\ApiScope;
1723
use Models\OAuth2\Client;
24+
use Models\OAuth2\ClientPublicKey;
25+
use Models\OAuth2\OAuth2OTP;
1826
use Models\OAuth2\ResourceServer;
1927
use Tests\BrowserKitTestCase;
2028
use Auth\User;
@@ -25,14 +33,35 @@
2533
*/
2634
class ClientMappingTest extends BrowserKitTestCase
2735
{
36+
static $client_public_key_1 = <<<PPK
37+
-----BEGIN PUBLIC KEY-----
38+
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAkjiUI6n3Fq140AipaLxN
39+
IPCzEItQFcY8G5Xd17u7InM3H542+34PdBpwR66miQUgJK+rtfaot/v4QPj4/0Bn
40+
Yc78BhI0Mp3tVEH95jjIrhDMZoRFfSQsAhiom5NTP1B5XiiyRjzkO1+7a29JST5t
41+
IQUIS2U345DMWyf3GNlC1cBAfgI+PrRo3gLby/iW5EF/Mqq0ZUIOuggZ7r8kU2aU
42+
hXILFx2w9V/y90DwruJdzZ0TesbsFit2nM3Axie7HX2wIpbl2hyvvhX/AxZ0NPud
43+
Vh58wNogsKOMUN6guU+RzL5L6vF+QjfzBCtOE+CRmUD60E0LdQHzElBcF0tbc2cj
44+
2YelZ0Dp+4NEBDjCNsSv//5hHacUxxXQdwwotLUV85iErEZgcGyMNnTMsw7JIh39
45+
UBgOEmQgfpfOUlH+/5WmRO+kskvPCACz1SR8gzAKz9Nu9r3UyE+gWaZzM2+CpQ1s
46+
zEd94MIapHxJw9vHogL7sNkjmZ34Y9eQmoCVevqDVpYEdTtLsg9H49+pEndQHI6l
47+
GAB7QlsPLN8A17L2l3p68BFcYkSZR4GuXAyQguq3KzWYDZ9PjWAV5lhVg6K3GaV7
48+
fvn2pKCk4P5Y5hZt08fholt3k/5Gc82CP6rfgQFi7HnpBJKRauoIdsvUPvXZYTLl
49+
TaE5jLBAwxm+wF6Ue/nRPJMCAwEAAQ==
50+
-----END PUBLIC KEY-----
51+
PPK;
52+
2853
public function testClientPersistence()
2954
{
3055
$app_description = 'test app description';
3156
$host = 'https://www.openstack.org';
57+
$otp_value = 'test_otp_value';
3258

3359
$client_repo = EntityManager::getRepository(Client::class);
3460
$client = $client_repo->findAll()[0];
3561

62+
$former_scopes_count = count($client->getClientScopes());
63+
$former_pks_count = count($client->getPublicKeys($otp_value));
64+
3665
$user_repo = EntityManager::getRepository(User::class);
3766
$user = $user_repo->findAll()[0];
3867
$admin_user1 = $user_repo->findAll()[1];
@@ -47,16 +76,50 @@ public function testClientPersistence()
4776

4877
$client->setAppDescription($app_description);
4978

50-
//Many-to-one mapping test
79+
//Many-to-one user mapping test
5180
$client->setEditedBy($user);
5281

53-
//One-to-one mapping test
82+
//One-to-one resource server mapping test
5483
$client->setResourceServer($rs);
5584

56-
//Many-to-many mapping test
85+
//Many-to-many admin mapping test
5786
$client->addAdminUser($admin_user1);
5887
$client->addAdminUser($admin_user2);
5988

89+
//One-to-many public key mapping test
90+
$now = new \DateTime('now', new DateTimeZone('UTC'));
91+
$to = new \DateTime('now', new DateTimeZone('UTC'));
92+
$to->add(new \DateInterval('P31D'));
93+
94+
$pk = ClientPublicKey::buildFromPEM(
95+
'public_key_1',
96+
JSONWebKeyTypes::RSA,
97+
JSONWebKeyPublicKeyUseValues::Encryption,
98+
self::$client_public_key_1,
99+
JSONWebSignatureAndEncryptionAlgorithms::RSA_OAEP_256,
100+
true,
101+
$now,
102+
$to
103+
);
104+
$client->addPublicKey($pk);
105+
106+
//Many-to-many scope mapping test
107+
$api = EntityManager::getRepository(Api::class)->findAll()[0];
108+
$scope = new ApiScope();
109+
$scope->setName('test_scope_name');
110+
$scope->setShortDescription('test short description');
111+
$scope->setDescription('test description');
112+
$scope->setActive(true);
113+
$scope->setApi($api);
114+
EntityManager::persist($scope);
115+
116+
$client->addScope($scope);
117+
118+
$otp_grant = new OAuth2OTP(6, 120);
119+
$otp_grant->setValue($otp_value);
120+
121+
$client->addOTPGrant($otp_grant);
122+
60123
EntityManager::persist($client);
61124
EntityManager::flush();
62125
EntityManager::clear();
@@ -65,7 +128,28 @@ public function testClientPersistence()
65128

66129
$this->assertEquals($app_description, $found_client->getApplicationDescription());
67130
$this->assertEquals($user->getEmail(), $found_client->getEditedByNice());
68-
$this->assertCount(2, $client->getAdminUsers()->toArray());
131+
$this->assertCount(2, $found_client->getAdminUsers()->toArray());
132+
$this->assertCount($former_scopes_count + 1, $found_client->getClientScopes());
69133
$this->assertEquals($host, $found_client->getResourceServer()->getHost());
134+
$this->assertTrue($found_client->hasOTP($otp_value));
135+
$this->assertCount($former_pks_count + 1, $found_client->getPublicKeys($otp_value));
136+
137+
//Children removal tests
138+
139+
$client = $client_repo->find($client->getId());
140+
$otp_grant = $client->getOTPByValue($otp_value);
141+
$client->removeOTPGrant($otp_grant);
142+
$client->removeAllAdminUsers();
143+
$client->removeAllScopes();
144+
145+
EntityManager::persist($client);
146+
EntityManager::flush();
147+
EntityManager::clear();
148+
149+
$found_client = $client_repo->find($client->getId());
150+
151+
$this->assertFalse($found_client->hasOTP($otp_value));
152+
$this->assertEmpty($found_client->getAdminUsers()->toArray());
153+
$this->assertEmpty($found_client->getClientScopes());
70154
}
71155
}

tests/unit/OAuth2OTPMappingTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@
1616
use LaravelDoctrine\ORM\Facades\EntityManager;
1717
use Models\OAuth2\Client;
1818
use Models\OAuth2\OAuth2OTP;
19-
use OAuth2\OAuth2Protocol;
2019
use Tests\BrowserKitTestCase;
21-
use models\oauth2\UserConsent;
22-
use Auth\User;
23-
use Utils\Services\IdentifierGenerator;
2420

2521
/**
2622
* Class OAuth2OTPMappingTest
@@ -37,12 +33,16 @@ public function testOAuth2OTPPersistence()
3733
$otp->setConnection($connection);
3834
$otp->setSend('1');
3935
$otp->setValue('test value');
40-
$otp->setNonce('test nonce');;
36+
$otp->setNonce('test nonce');
4137
$otp->setRedirectUrl('https://www.openstack.org/');
4238
$otp->setScope('openid email');
4339
$otp->setEmail($email);
4440
$otp->setPhoneNumber('1234567890');
4541

42+
//Many-to-one client mapping test
43+
$client = EntityManager::getRepository(Client::class)->findAll()[0];
44+
$otp->setClient($client);
45+
4646
EntityManager::persist($otp);
4747
EntityManager::flush();
4848
EntityManager::clear();
@@ -53,5 +53,6 @@ public function testOAuth2OTPPersistence()
5353
$this->assertInstanceOf(OAuth2OTP::class, $found_otp);
5454
$this->assertEquals($connection, $found_otp->getConnection());
5555
$this->assertEquals($email, $found_otp->getEmail());
56+
$this->assertEquals($client->getApplicationName(), $found_otp->getClient()->getApplicationName());
5657
}
5758
}

tests/unit/ResourceServerMappingTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ public function testClientPersistence()
3838
$rs->setIps('127.0.0.1');
3939
$rs->setActive(true);
4040

41+
//One-to-one client mapping test
4142
$rs->setClient($client);
43+
44+
//One-to-many api mapping test
4245
$rs->addApi($api);
4346

4447
EntityManager::persist($rs);
@@ -50,6 +53,21 @@ public function testClientPersistence()
5053

5154
$this->assertCount(1, $found_rs->getApis()->toArray());
5255
$this->assertEquals($host, $found_rs->getHost());
53-
$this->assertEquals($client->getApplicationName(), $found_rs->getClient()->getApplicationName());;
56+
$this->assertEquals($client->getApplicationName(), $found_rs->getClient()->getApplicationName());
57+
58+
//Children removal tests
59+
$rs = $rs_repo->find($rs->getId());
60+
$apis = $rs->getApis();
61+
62+
foreach ($apis as $api) {
63+
$rs->removeApi($api);
64+
}
65+
66+
EntityManager::persist($rs);
67+
EntityManager::flush();
68+
EntityManager::clear();
69+
70+
$found_rs = $rs_repo->find($rs->getId());
71+
$this->assertEmpty($found_rs->getApis()->toArray());
5472
}
5573
}

0 commit comments

Comments
 (0)