23
23
import java .util .Set ;
24
24
import static org .hamcrest .CoreMatchers .*;
25
25
import static org .junit .Assert .assertNull ;
26
+ import static org .junit .Assert .assertNotNull ;
26
27
import static org .junit .Assert .assertThat ;
27
28
import static org .junit .Assert .assertTrue ;
28
29
import static org .junit .Assert .fail ;
56
57
import org .mockito .runners .MockitoJUnitRunner ;
57
58
import org .mockito .stubbing .Answer ;
58
59
import org .springframework .security .core .Authentication ;
59
- import org .springframework .security .oauth2 . provider . AuthorizationRequest ;
60
+ import org .springframework .security .core . GrantedAuthority ;
60
61
import org .springframework .security .oauth2 .provider .OAuth2Authentication ;
61
62
import org .springframework .security .oauth2 .provider .OAuth2Request ;
62
63
@@ -161,9 +162,9 @@ public void testExportRefreshTokens() throws IOException, ParseException {
161
162
JsonObject root = elem .getAsJsonObject ();
162
163
163
164
// make sure the root is there
164
- assertThat (root .has (MITREidDataService .MITREID_CONNECT_1_0 ), is (true ));
165
+ assertThat (root .has (MITREidDataService .MITREID_CONNECT_1_1 ), is (true ));
165
166
166
- JsonObject config = root .get (MITREidDataService .MITREID_CONNECT_1_0 ).getAsJsonObject ();
167
+ JsonObject config = root .get (MITREidDataService .MITREID_CONNECT_1_1 ).getAsJsonObject ();
167
168
168
169
// make sure all the root elements are there
169
170
assertThat (config .has (MITREidDataService .CLIENTS ), is (true ));
@@ -407,9 +408,9 @@ public void testExportAccessTokens() throws IOException, ParseException {
407
408
JsonObject root = elem .getAsJsonObject ();
408
409
409
410
// make sure the root is there
410
- assertThat (root .has (MITREidDataService .MITREID_CONNECT_1_0 ), is (true ));
411
+ assertThat (root .has (MITREidDataService .MITREID_CONNECT_1_1 ), is (true ));
411
412
412
- JsonObject config = root .get (MITREidDataService .MITREID_CONNECT_1_0 ).getAsJsonObject ();
413
+ JsonObject config = root .get (MITREidDataService .MITREID_CONNECT_1_1 ).getAsJsonObject ();
413
414
414
415
// make sure all the root elements are there
415
416
assertThat (config .has (MITREidDataService .CLIENTS ), is (true ));
@@ -657,9 +658,9 @@ public void testExportClients() throws IOException {
657
658
JsonObject root = elem .getAsJsonObject ();
658
659
659
660
// make sure the root is there
660
- assertThat (root .has (MITREidDataService .MITREID_CONNECT_1_0 ), is (true ));
661
+ assertThat (root .has (MITREidDataService .MITREID_CONNECT_1_1 ), is (true ));
661
662
662
- JsonObject config = root .get (MITREidDataService .MITREID_CONNECT_1_0 ).getAsJsonObject ();
663
+ JsonObject config = root .get (MITREidDataService .MITREID_CONNECT_1_1 ).getAsJsonObject ();
663
664
664
665
// make sure all the root elements are there
665
666
assertThat (config .has (MITREidDataService .CLIENTS ), is (true ));
@@ -828,9 +829,9 @@ public void testExportBlacklistedSites() throws IOException {
828
829
JsonObject root = elem .getAsJsonObject ();
829
830
830
831
// make sure the root is there
831
- assertThat (root .has (MITREidDataService .MITREID_CONNECT_1_0 ), is (true ));
832
+ assertThat (root .has (MITREidDataService .MITREID_CONNECT_1_1 ), is (true ));
832
833
833
- JsonObject config = root .get (MITREidDataService .MITREID_CONNECT_1_0 ).getAsJsonObject ();
834
+ JsonObject config = root .get (MITREidDataService .MITREID_CONNECT_1_1 ).getAsJsonObject ();
834
835
835
836
// make sure all the root elements are there
836
837
assertThat (config .has (MITREidDataService .CLIENTS ), is (true ));
@@ -969,9 +970,9 @@ public void testExportWhitelistedSites() throws IOException {
969
970
JsonObject root = elem .getAsJsonObject ();
970
971
971
972
// make sure the root is there
972
- assertThat (root .has (MITREidDataService .MITREID_CONNECT_1_0 ), is (true ));
973
+ assertThat (root .has (MITREidDataService .MITREID_CONNECT_1_1 ), is (true ));
973
974
974
- JsonObject config = root .get (MITREidDataService .MITREID_CONNECT_1_0 ).getAsJsonObject ();
975
+ JsonObject config = root .get (MITREidDataService .MITREID_CONNECT_1_1 ).getAsJsonObject ();
975
976
976
977
// make sure all the root elements are there
977
978
assertThat (config .has (MITREidDataService .CLIENTS ), is (true ));
@@ -1101,6 +1102,9 @@ public void testExportGrants() throws IOException {
1101
1102
WhitelistedSite mockWlSite1 = mock (WhitelistedSite .class );
1102
1103
when (mockWlSite1 .getId ()).thenReturn (1L );
1103
1104
1105
+ OAuth2AccessTokenEntity mockToken1 = mock (OAuth2AccessTokenEntity .class );
1106
+ when (mockToken1 .getId ()).thenReturn (1L );
1107
+
1104
1108
ApprovedSite site1 = new ApprovedSite ();
1105
1109
site1 .setId (1L );
1106
1110
site1 .setClientId ("foo" );
@@ -1109,6 +1113,7 @@ public void testExportGrants() throws IOException {
1109
1113
site1 .setUserId ("user1" );
1110
1114
site1 .setWhitelistedSite (mockWlSite1 );
1111
1115
site1 .setAllowedScopes (ImmutableSet .of ("openid" , "phone" ));
1116
+ site1 .setApprovedAccessTokens (ImmutableSet .of (mockToken1 ));
1112
1117
1113
1118
Date creationDate2 = DateUtil .utcToDate ("2014-09-11T18:49:44.090+0000" );
1114
1119
Date accessDate2 = DateUtil .utcToDate ("2014-09-11T20:49:44.090+0000" );
@@ -1147,9 +1152,9 @@ public void testExportGrants() throws IOException {
1147
1152
JsonObject root = elem .getAsJsonObject ();
1148
1153
1149
1154
// make sure the root is there
1150
- assertThat (root .has (MITREidDataService .MITREID_CONNECT_1_0 ), is (true ));
1155
+ assertThat (root .has (MITREidDataService .MITREID_CONNECT_1_1 ), is (true ));
1151
1156
1152
- JsonObject config = root .get (MITREidDataService .MITREID_CONNECT_1_0 ).getAsJsonObject ();
1157
+ JsonObject config = root .get (MITREidDataService .MITREID_CONNECT_1_1 ).getAsJsonObject ();
1153
1158
1154
1159
// make sure all the root elements are there
1155
1160
assertThat (config .has (MITREidDataService .CLIENTS ), is (true ));
@@ -1205,6 +1210,16 @@ public void testExportGrants() throws IOException {
1205
1210
assertNull (compare .getWhitelistedSite ());
1206
1211
} else {
1207
1212
assertThat (site .get ("whitelistedSiteId" ).getAsLong (), equalTo (compare .getWhitelistedSite ().getId ()));
1213
+ }
1214
+ if (site .get ("approvedAccessTokens" ).isJsonNull () || site .getAsJsonArray ("approvedAccessTokens" ) == null ) {
1215
+ assertTrue (compare .getApprovedAccessTokens () == null || compare .getApprovedAccessTokens ().isEmpty ());
1216
+ } else {
1217
+ assertNotNull (compare .getApprovedAccessTokens ());
1218
+ Set <String > tokenIds = new HashSet <String >();
1219
+ for (OAuth2AccessTokenEntity entity : compare .getApprovedAccessTokens ()) {
1220
+ tokenIds .add (entity .getId ().toString ());
1221
+ }
1222
+ assertThat (jsonArrayToStringSet (site .getAsJsonArray ("approvedAccessTokens" )), equalTo (tokenIds ));
1208
1223
}
1209
1224
checked .add (compare );
1210
1225
}
@@ -1221,6 +1236,9 @@ public void testImportGrants() throws IOException {
1221
1236
WhitelistedSite mockWlSite1 = mock (WhitelistedSite .class );
1222
1237
when (mockWlSite1 .getId ()).thenReturn (1L );
1223
1238
1239
+ OAuth2AccessTokenEntity mockToken1 = mock (OAuth2AccessTokenEntity .class );
1240
+ when (mockToken1 .getId ()).thenReturn (1L );
1241
+
1224
1242
ApprovedSite site1 = new ApprovedSite ();
1225
1243
site1 .setId (1L );
1226
1244
site1 .setClientId ("foo" );
@@ -1229,6 +1247,7 @@ public void testImportGrants() throws IOException {
1229
1247
site1 .setUserId ("user1" );
1230
1248
site1 .setWhitelistedSite (mockWlSite1 );
1231
1249
site1 .setAllowedScopes (ImmutableSet .of ("openid" , "phone" ));
1250
+ site1 .setApprovedAccessTokens (ImmutableSet .of (mockToken1 ));
1232
1251
1233
1252
Date creationDate2 = DateUtil .utcToDate ("2014-09-11T18:49:44.090+0000" );
1234
1253
Date accessDate2 = DateUtil .utcToDate ("2014-09-11T20:49:44.090+0000" );
@@ -1254,7 +1273,8 @@ public void testImportGrants() throws IOException {
1254
1273
"\" " + MITREidDataService .GRANTS + "\" : [" +
1255
1274
1256
1275
"{\" id\" :1,\" clientId\" :\" foo\" ,\" creationDate\" :\" 2014-09-10T22:49:44.090+0000\" ,\" accessDate\" :\" 2014-09-10T23:49:44.090+0000\" ,"
1257
- + "\" userId\" :\" user1\" ,\" whitelistedSiteId\" :null,\" allowedScopes\" :[\" openid\" ,\" phone\" ], \" whitelistedSiteId\" :1}," +
1276
+ + "\" userId\" :\" user1\" ,\" whitelistedSiteId\" :null,\" allowedScopes\" :[\" openid\" ,\" phone\" ], \" whitelistedSiteId\" :1,"
1277
+ + "\" approvedAccessTokens\" :[1]}," +
1258
1278
"{\" id\" :2,\" clientId\" :\" bar\" ,\" creationDate\" :\" 2014-09-11T18:49:44.090+0000\" ,\" accessDate\" :\" 2014-09-11T20:49:44.090+0000\" ,"
1259
1279
+ "\" timeoutDate\" :\" 2014-10-01T20:49:44.090+0000\" ,\" userId\" :\" user2\" ,"
1260
1280
+ "\" allowedScopes\" :[\" openid\" ,\" offline_access\" ,\" email\" ,\" profile\" ]}" +
@@ -1295,10 +1315,19 @@ public WhitelistedSite answer(InvocationOnMock invocation) throws Throwable {
1295
1315
return _site ;
1296
1316
}
1297
1317
});
1298
-
1318
+ when (tokenRepository .getAccessTokenById (isNull (Long .class ))).thenAnswer (new Answer <OAuth2AccessTokenEntity >() {
1319
+ Long id = 2L ;
1320
+ @ Override
1321
+ public OAuth2AccessTokenEntity answer (InvocationOnMock invocation ) throws Throwable {
1322
+ OAuth2AccessTokenEntity _token = mock (OAuth2AccessTokenEntity .class );
1323
+ when (_token .getId ()).thenReturn (id ++);
1324
+ return _token ;
1325
+ }
1326
+ });
1327
+
1299
1328
dataService .importData (reader );
1300
- //2 for sites, 1 more for updating whitelistedSite ref on #2
1301
- verify (approvedSiteRepository , times (3 )).save (capturedApprovedSites .capture ());
1329
+ //2 for sites, 1 for updating access token ref on #1, 1 more for updating whitelistedSite ref on #2
1330
+ verify (approvedSiteRepository , times (4 )).save (capturedApprovedSites .capture ());
1302
1331
1303
1332
List <ApprovedSite > savedSites = new ArrayList (fakeDb .values ());
1304
1333
@@ -1310,30 +1339,34 @@ public WhitelistedSite answer(InvocationOnMock invocation) throws Throwable {
1310
1339
assertThat (savedSites .get (0 ).getAllowedScopes (), equalTo (site1 .getAllowedScopes ()));
1311
1340
assertThat (savedSites .get (0 ).getIsWhitelisted (), equalTo (site1 .getIsWhitelisted ()));
1312
1341
assertThat (savedSites .get (0 ).getTimeoutDate (), equalTo (site1 .getTimeoutDate ()));
1342
+ assertThat (savedSites .get (0 ).getApprovedAccessTokens ().size (), equalTo (site1 .getApprovedAccessTokens ().size ()));
1313
1343
1314
1344
assertThat (savedSites .get (1 ).getClientId (), equalTo (site2 .getClientId ()));
1315
1345
assertThat (savedSites .get (1 ).getAccessDate (), equalTo (site2 .getAccessDate ()));
1316
1346
assertThat (savedSites .get (1 ).getCreationDate (), equalTo (site2 .getCreationDate ()));
1317
1347
assertThat (savedSites .get (1 ).getAllowedScopes (), equalTo (site2 .getAllowedScopes ()));
1318
1348
assertThat (savedSites .get (1 ).getTimeoutDate (), equalTo (site2 .getTimeoutDate ()));
1319
1349
assertThat (savedSites .get (1 ).getIsWhitelisted (), equalTo (site2 .getIsWhitelisted ()));
1350
+ assertThat (savedSites .get (1 ).getApprovedAccessTokens (), equalTo (site2 .getApprovedAccessTokens ())); //both should be null or empty
1320
1351
}
1321
1352
1322
1353
@ Test
1323
1354
public void testExportAuthenticationHolders () throws IOException {
1324
- OAuth2Request mockRequest1 = mock (OAuth2Request .class );
1325
- when (mockRequest1 .getRequestParameters ()).thenReturn (new HashMap <String , String >());
1326
- Authentication mockAuth1 = null ;
1327
- OAuth2Authentication auth1 = new OAuth2Authentication (mockRequest1 , mockAuth1 );
1355
+ OAuth2Request req1 = new OAuth2Request (new HashMap <String , String >(), "client1" , new ArrayList <GrantedAuthority >(),
1356
+ true , new HashSet <String >(), new HashSet <String >(), "http://foo.com" ,
1357
+ new HashSet <String >(), null );
1358
+ Authentication mockAuth1 = mock (Authentication .class , withSettings ().serializable ());
1359
+ OAuth2Authentication auth1 = new OAuth2Authentication (req1 , mockAuth1 );
1328
1360
1329
1361
AuthenticationHolderEntity holder1 = new AuthenticationHolderEntity ();
1330
1362
holder1 .setId (1L );
1331
1363
holder1 .setAuthentication (auth1 );
1332
1364
1333
- OAuth2Request mockRequest2 = mock (OAuth2Request .class );
1334
- when (mockRequest2 .getRequestParameters ()).thenReturn (new HashMap <String , String >());
1335
- Authentication mockAuth2 = null ;
1336
- OAuth2Authentication auth2 = new OAuth2Authentication (mockRequest2 , mockAuth2 );
1365
+ OAuth2Request req2 = new OAuth2Request (new HashMap <String , String >(), "client2" , new ArrayList <GrantedAuthority >(),
1366
+ true , new HashSet <String >(), new HashSet <String >(), "http://bar.com" ,
1367
+ new HashSet <String >(), null );
1368
+ Authentication mockAuth2 = mock (Authentication .class , withSettings ().serializable ());
1369
+ OAuth2Authentication auth2 = new OAuth2Authentication (req2 , mockAuth2 );
1337
1370
1338
1371
AuthenticationHolderEntity holder2 = new AuthenticationHolderEntity ();
1339
1372
holder2 .setId (2L );
@@ -1363,9 +1396,9 @@ public void testExportAuthenticationHolders() throws IOException {
1363
1396
JsonObject root = elem .getAsJsonObject ();
1364
1397
1365
1398
// make sure the root is there
1366
- assertThat (root .has (MITREidDataService .MITREID_CONNECT_1_0 ), is (true ));
1399
+ assertThat (root .has (MITREidDataService .MITREID_CONNECT_1_1 ), is (true ));
1367
1400
1368
- JsonObject config = root .get (MITREidDataService .MITREID_CONNECT_1_0 ).getAsJsonObject ();
1401
+ JsonObject config = root .get (MITREidDataService .MITREID_CONNECT_1_1 ).getAsJsonObject ();
1369
1402
1370
1403
// make sure all the root elements are there
1371
1404
assertThat (config .has (MITREidDataService .CLIENTS ), is (true ));
@@ -1418,19 +1451,21 @@ public void testExportAuthenticationHolders() throws IOException {
1418
1451
1419
1452
@ Test
1420
1453
public void testImportAuthenticationHolders () throws IOException {
1421
- OAuth2Request mockRequest1 = mock (OAuth2Request .class );
1422
- when (mockRequest1 .getRequestParameters ()).thenReturn (new HashMap <String , String >());
1423
- Authentication mockAuth1 = null ;
1424
- OAuth2Authentication auth1 = new OAuth2Authentication (mockRequest1 , mockAuth1 );
1454
+ OAuth2Request req1 = new OAuth2Request (new HashMap <String , String >(), "client1" , new ArrayList <GrantedAuthority >(),
1455
+ true , new HashSet <String >(), new HashSet <String >(), "http://foo.com" ,
1456
+ new HashSet <String >(), null );
1457
+ Authentication mockAuth1 = mock (Authentication .class , withSettings ().serializable ());
1458
+ OAuth2Authentication auth1 = new OAuth2Authentication (req1 , mockAuth1 );
1425
1459
1426
1460
AuthenticationHolderEntity holder1 = new AuthenticationHolderEntity ();
1427
1461
holder1 .setId (1L );
1428
1462
holder1 .setAuthentication (auth1 );
1429
1463
1430
- OAuth2Request mockRequest2 = mock (OAuth2Request .class );
1431
- when (mockRequest2 .getRequestParameters ()).thenReturn (new HashMap <String , String >());
1432
- Authentication mockAuth2 = null ;
1433
- OAuth2Authentication auth2 = new OAuth2Authentication (mockRequest2 , mockAuth2 );
1464
+ OAuth2Request req2 = new OAuth2Request (new HashMap <String , String >(), "client2" , new ArrayList <GrantedAuthority >(),
1465
+ true , new HashSet <String >(), new HashSet <String >(), "http://bar.com" ,
1466
+ new HashSet <String >(), null );
1467
+ Authentication mockAuth2 = mock (Authentication .class , withSettings ().serializable ());
1468
+ OAuth2Authentication auth2 = new OAuth2Authentication (req2 , mockAuth2 );
1434
1469
1435
1470
AuthenticationHolderEntity holder2 = new AuthenticationHolderEntity ();
1436
1471
holder2 .setId (2L );
@@ -1530,9 +1565,9 @@ public void testExportSystemScopes() throws IOException {
1530
1565
JsonObject root = elem .getAsJsonObject ();
1531
1566
1532
1567
// make sure the root is there
1533
- assertThat (root .has (MITREidDataService .MITREID_CONNECT_1_0 ), is (true ));
1568
+ assertThat (root .has (MITREidDataService .MITREID_CONNECT_1_1 ), is (true ));
1534
1569
1535
- JsonObject config = root .get (MITREidDataService .MITREID_CONNECT_1_0 ).getAsJsonObject ();
1570
+ JsonObject config = root .get (MITREidDataService .MITREID_CONNECT_1_1 ).getAsJsonObject ();
1536
1571
1537
1572
// make sure all the root elements are there
1538
1573
assertThat (config .has (MITREidDataService .CLIENTS ), is (true ));
0 commit comments