1313 * limitations under the License.
1414 **/
1515
16+ use DateTimeZone ;
17+ use jwa \JSONWebSignatureAndEncryptionAlgorithms ;
18+ use jwk \JSONWebKeyPublicKeyUseValues ;
19+ use jwk \JSONWebKeyTypes ;
1620use LaravelDoctrine \ORM \Facades \EntityManager ;
21+ use Models \OAuth2 \Api ;
22+ use Models \OAuth2 \ApiScope ;
1723use Models \OAuth2 \Client ;
24+ use Models \OAuth2 \ClientPublicKey ;
25+ use Models \OAuth2 \OAuth2OTP ;
1826use Models \OAuth2 \ResourceServer ;
1927use Tests \BrowserKitTestCase ;
2028use Auth \User ;
2533 */
2634class 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}
0 commit comments