Skip to content

Commit d18d325

Browse files
arielakjricher
authored andcommitted
Better method of creating test AuthenticationHolderEntity, added some more testing to testImport/ExportGrants
Conflicts: openid-connect-server/src/test/java/org/mitre/openid/connect/service/impl/TestMITREidDataService_1_1.java
1 parent ff28e1a commit d18d325

File tree

2 files changed

+76
-56
lines changed

2 files changed

+76
-56
lines changed

openid-connect-server/src/test/java/org/mitre/openid/connect/service/impl/TestMITREidDataService_1_0.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -650,19 +650,13 @@ public WhitelistedSite answer(InvocationOnMock invocation) throws Throwable {
650650

651651
@Test
652652
public void testImportAuthenticationHolders() throws IOException {
653-
OAuth2Request mockRequest1 = mock(OAuth2Request.class);
654-
when(mockRequest1.getRequestParameters()).thenReturn(new HashMap<String, String>());
655-
Authentication mockAuth1 = null;
656-
OAuth2Authentication auth1 = new OAuth2Authentication(mockRequest1, mockAuth1);
653+
OAuth2Authentication auth1 = mock(OAuth2Authentication.class, withSettings().serializable());
657654

658655
AuthenticationHolderEntity holder1 = new AuthenticationHolderEntity();
659656
holder1.setId(1L);
660657
holder1.setAuthentication(auth1);
661658

662-
OAuth2Request mockRequest2 = mock(OAuth2Request.class);
663-
when(mockRequest2.getRequestParameters()).thenReturn(new HashMap<String, String>());
664-
Authentication mockAuth2 = null;
665-
OAuth2Authentication auth2 = new OAuth2Authentication(mockRequest2, mockAuth2);
659+
OAuth2Authentication auth2 = mock(OAuth2Authentication.class, withSettings().serializable());
666660

667661
AuthenticationHolderEntity holder2 = new AuthenticationHolderEntity();
668662
holder2.setId(2L);
@@ -708,8 +702,8 @@ public AuthenticationHolderEntity answer(InvocationOnMock invocation) throws Thr
708702
List<AuthenticationHolderEntity> savedAuthHolders = capturedAuthHolders.getAllValues();
709703

710704
assertThat(savedAuthHolders.size(), is(2));
711-
assertThat(savedAuthHolders.get(0).getAuthentication().getName(), equalTo(holder1.getAuthentication().getName()));
712-
assertThat(savedAuthHolders.get(1).getAuthentication().getName(), equalTo(holder2.getAuthentication().getName()));
705+
assertThat(savedAuthHolders.get(0).getAuthentication().getDetails(), equalTo(holder1.getAuthentication().getDetails()));
706+
assertThat(savedAuthHolders.get(1).getAuthentication().getDetails(), equalTo(holder2.getAuthentication().getDetails()));
713707
}
714708

715709
@Test
@@ -784,13 +778,4 @@ public void testImportSystemScopes() throws IOException {
784778
assertThat(savedScopes.get(2).isAllowDynReg(), equalTo(scope3.isAllowDynReg()));
785779

786780
}
787-
788-
private Set<String> jsonArrayToStringSet(JsonArray a) {
789-
Set<String> s = new HashSet<String>();
790-
for (JsonElement jsonElement : a) {
791-
s.add(jsonElement.getAsString());
792-
}
793-
return s;
794-
}
795-
796781
}

openid-connect-server/src/test/java/org/mitre/openid/connect/service/impl/TestMITREidDataService_1_1.java

Lines changed: 72 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Set;
2424
import static org.hamcrest.CoreMatchers.*;
2525
import static org.junit.Assert.assertNull;
26+
import static org.junit.Assert.assertNotNull;
2627
import static org.junit.Assert.assertThat;
2728
import static org.junit.Assert.assertTrue;
2829
import static org.junit.Assert.fail;
@@ -56,7 +57,7 @@
5657
import org.mockito.runners.MockitoJUnitRunner;
5758
import org.mockito.stubbing.Answer;
5859
import org.springframework.security.core.Authentication;
59-
import org.springframework.security.oauth2.provider.AuthorizationRequest;
60+
import org.springframework.security.core.GrantedAuthority;
6061
import org.springframework.security.oauth2.provider.OAuth2Authentication;
6162
import org.springframework.security.oauth2.provider.OAuth2Request;
6263

@@ -161,9 +162,9 @@ public void testExportRefreshTokens() throws IOException, ParseException {
161162
JsonObject root = elem.getAsJsonObject();
162163

163164
// 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));
165166

166-
JsonObject config = root.get(MITREidDataService.MITREID_CONNECT_1_0).getAsJsonObject();
167+
JsonObject config = root.get(MITREidDataService.MITREID_CONNECT_1_1).getAsJsonObject();
167168

168169
// make sure all the root elements are there
169170
assertThat(config.has(MITREidDataService.CLIENTS), is(true));
@@ -407,9 +408,9 @@ public void testExportAccessTokens() throws IOException, ParseException {
407408
JsonObject root = elem.getAsJsonObject();
408409

409410
// 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));
411412

412-
JsonObject config = root.get(MITREidDataService.MITREID_CONNECT_1_0).getAsJsonObject();
413+
JsonObject config = root.get(MITREidDataService.MITREID_CONNECT_1_1).getAsJsonObject();
413414

414415
// make sure all the root elements are there
415416
assertThat(config.has(MITREidDataService.CLIENTS), is(true));
@@ -657,9 +658,9 @@ public void testExportClients() throws IOException {
657658
JsonObject root = elem.getAsJsonObject();
658659

659660
// 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));
661662

662-
JsonObject config = root.get(MITREidDataService.MITREID_CONNECT_1_0).getAsJsonObject();
663+
JsonObject config = root.get(MITREidDataService.MITREID_CONNECT_1_1).getAsJsonObject();
663664

664665
// make sure all the root elements are there
665666
assertThat(config.has(MITREidDataService.CLIENTS), is(true));
@@ -828,9 +829,9 @@ public void testExportBlacklistedSites() throws IOException {
828829
JsonObject root = elem.getAsJsonObject();
829830

830831
// 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));
832833

833-
JsonObject config = root.get(MITREidDataService.MITREID_CONNECT_1_0).getAsJsonObject();
834+
JsonObject config = root.get(MITREidDataService.MITREID_CONNECT_1_1).getAsJsonObject();
834835

835836
// make sure all the root elements are there
836837
assertThat(config.has(MITREidDataService.CLIENTS), is(true));
@@ -969,9 +970,9 @@ public void testExportWhitelistedSites() throws IOException {
969970
JsonObject root = elem.getAsJsonObject();
970971

971972
// 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));
973974

974-
JsonObject config = root.get(MITREidDataService.MITREID_CONNECT_1_0).getAsJsonObject();
975+
JsonObject config = root.get(MITREidDataService.MITREID_CONNECT_1_1).getAsJsonObject();
975976

976977
// make sure all the root elements are there
977978
assertThat(config.has(MITREidDataService.CLIENTS), is(true));
@@ -1101,6 +1102,9 @@ public void testExportGrants() throws IOException {
11011102
WhitelistedSite mockWlSite1 = mock(WhitelistedSite.class);
11021103
when(mockWlSite1.getId()).thenReturn(1L);
11031104

1105+
OAuth2AccessTokenEntity mockToken1 = mock(OAuth2AccessTokenEntity.class);
1106+
when(mockToken1.getId()).thenReturn(1L);
1107+
11041108
ApprovedSite site1 = new ApprovedSite();
11051109
site1.setId(1L);
11061110
site1.setClientId("foo");
@@ -1109,6 +1113,7 @@ public void testExportGrants() throws IOException {
11091113
site1.setUserId("user1");
11101114
site1.setWhitelistedSite(mockWlSite1);
11111115
site1.setAllowedScopes(ImmutableSet.of("openid", "phone"));
1116+
site1.setApprovedAccessTokens(ImmutableSet.of(mockToken1));
11121117

11131118
Date creationDate2 = DateUtil.utcToDate("2014-09-11T18:49:44.090+0000");
11141119
Date accessDate2 = DateUtil.utcToDate("2014-09-11T20:49:44.090+0000");
@@ -1147,9 +1152,9 @@ public void testExportGrants() throws IOException {
11471152
JsonObject root = elem.getAsJsonObject();
11481153

11491154
// 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));
11511156

1152-
JsonObject config = root.get(MITREidDataService.MITREID_CONNECT_1_0).getAsJsonObject();
1157+
JsonObject config = root.get(MITREidDataService.MITREID_CONNECT_1_1).getAsJsonObject();
11531158

11541159
// make sure all the root elements are there
11551160
assertThat(config.has(MITREidDataService.CLIENTS), is(true));
@@ -1205,6 +1210,16 @@ public void testExportGrants() throws IOException {
12051210
assertNull(compare.getWhitelistedSite());
12061211
} else {
12071212
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));
12081223
}
12091224
checked.add(compare);
12101225
}
@@ -1221,6 +1236,9 @@ public void testImportGrants() throws IOException {
12211236
WhitelistedSite mockWlSite1 = mock(WhitelistedSite.class);
12221237
when(mockWlSite1.getId()).thenReturn(1L);
12231238

1239+
OAuth2AccessTokenEntity mockToken1 = mock(OAuth2AccessTokenEntity.class);
1240+
when(mockToken1.getId()).thenReturn(1L);
1241+
12241242
ApprovedSite site1 = new ApprovedSite();
12251243
site1.setId(1L);
12261244
site1.setClientId("foo");
@@ -1229,6 +1247,7 @@ public void testImportGrants() throws IOException {
12291247
site1.setUserId("user1");
12301248
site1.setWhitelistedSite(mockWlSite1);
12311249
site1.setAllowedScopes(ImmutableSet.of("openid", "phone"));
1250+
site1.setApprovedAccessTokens(ImmutableSet.of(mockToken1));
12321251

12331252
Date creationDate2 = DateUtil.utcToDate("2014-09-11T18:49:44.090+0000");
12341253
Date accessDate2 = DateUtil.utcToDate("2014-09-11T20:49:44.090+0000");
@@ -1254,7 +1273,8 @@ public void testImportGrants() throws IOException {
12541273
"\"" + MITREidDataService.GRANTS + "\": [" +
12551274

12561275
"{\"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]}," +
12581278
"{\"id\":2,\"clientId\":\"bar\",\"creationDate\":\"2014-09-11T18:49:44.090+0000\",\"accessDate\":\"2014-09-11T20:49:44.090+0000\","
12591279
+ "\"timeoutDate\":\"2014-10-01T20:49:44.090+0000\",\"userId\":\"user2\","
12601280
+ "\"allowedScopes\":[\"openid\",\"offline_access\",\"email\",\"profile\"]}" +
@@ -1295,10 +1315,19 @@ public WhitelistedSite answer(InvocationOnMock invocation) throws Throwable {
12951315
return _site;
12961316
}
12971317
});
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+
12991328
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());
13021331

13031332
List<ApprovedSite> savedSites = new ArrayList(fakeDb.values());
13041333

@@ -1310,30 +1339,34 @@ public WhitelistedSite answer(InvocationOnMock invocation) throws Throwable {
13101339
assertThat(savedSites.get(0).getAllowedScopes(), equalTo(site1.getAllowedScopes()));
13111340
assertThat(savedSites.get(0).getIsWhitelisted(), equalTo(site1.getIsWhitelisted()));
13121341
assertThat(savedSites.get(0).getTimeoutDate(), equalTo(site1.getTimeoutDate()));
1342+
assertThat(savedSites.get(0).getApprovedAccessTokens().size(), equalTo(site1.getApprovedAccessTokens().size()));
13131343

13141344
assertThat(savedSites.get(1).getClientId(), equalTo(site2.getClientId()));
13151345
assertThat(savedSites.get(1).getAccessDate(), equalTo(site2.getAccessDate()));
13161346
assertThat(savedSites.get(1).getCreationDate(), equalTo(site2.getCreationDate()));
13171347
assertThat(savedSites.get(1).getAllowedScopes(), equalTo(site2.getAllowedScopes()));
13181348
assertThat(savedSites.get(1).getTimeoutDate(), equalTo(site2.getTimeoutDate()));
13191349
assertThat(savedSites.get(1).getIsWhitelisted(), equalTo(site2.getIsWhitelisted()));
1350+
assertThat(savedSites.get(1).getApprovedAccessTokens(), equalTo(site2.getApprovedAccessTokens())); //both should be null or empty
13201351
}
13211352

13221353
@Test
13231354
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);
13281360

13291361
AuthenticationHolderEntity holder1 = new AuthenticationHolderEntity();
13301362
holder1.setId(1L);
13311363
holder1.setAuthentication(auth1);
13321364

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);
13371370

13381371
AuthenticationHolderEntity holder2 = new AuthenticationHolderEntity();
13391372
holder2.setId(2L);
@@ -1363,9 +1396,9 @@ public void testExportAuthenticationHolders() throws IOException {
13631396
JsonObject root = elem.getAsJsonObject();
13641397

13651398
// 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));
13671400

1368-
JsonObject config = root.get(MITREidDataService.MITREID_CONNECT_1_0).getAsJsonObject();
1401+
JsonObject config = root.get(MITREidDataService.MITREID_CONNECT_1_1).getAsJsonObject();
13691402

13701403
// make sure all the root elements are there
13711404
assertThat(config.has(MITREidDataService.CLIENTS), is(true));
@@ -1418,19 +1451,21 @@ public void testExportAuthenticationHolders() throws IOException {
14181451

14191452
@Test
14201453
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);
14251459

14261460
AuthenticationHolderEntity holder1 = new AuthenticationHolderEntity();
14271461
holder1.setId(1L);
14281462
holder1.setAuthentication(auth1);
14291463

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);
14341469

14351470
AuthenticationHolderEntity holder2 = new AuthenticationHolderEntity();
14361471
holder2.setId(2L);
@@ -1530,9 +1565,9 @@ public void testExportSystemScopes() throws IOException {
15301565
JsonObject root = elem.getAsJsonObject();
15311566

15321567
// 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));
15341569

1535-
JsonObject config = root.get(MITREidDataService.MITREID_CONNECT_1_0).getAsJsonObject();
1570+
JsonObject config = root.get(MITREidDataService.MITREID_CONNECT_1_1).getAsJsonObject();
15361571

15371572
// make sure all the root elements are there
15381573
assertThat(config.has(MITREidDataService.CLIENTS), is(true));

0 commit comments

Comments
 (0)