// Default value should be 1, so it should return 0
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(0, counter);
// Now add extended information with allowed requests 2
ExtendedInformation ei = new ExtendedInformation();
int allowedrequests = 2;
ei.setCustomData(ExtendedInformation.CUSTOM_REQUESTCOUNTER, String.valueOf(allowedrequests));
user = new UserDataVO(username, "C=SE,O=AnaTom,CN="+username, caid, null, null, SecConst.USER_INVALID, SecConst.EMPTY_ENDENTITYPROFILE, SecConst.CERTPROFILE_FIXED_ENDUSER, SecConst.TOKEN_SOFT_PEM, 0, ei);
boolean thrown = false;
try {
userAdminSession.changeUser(admin, user, false);
} catch (UserDoesntFullfillEndEntityProfile e) {
thrown = true;
}
// This requires "Enable end entity profile limitations" to be checked in admin GUI->System configuration
assertTrue(thrown);
// decrease the value, since we use the empty end entity profile, the counter will not be used
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(0, counter);
// Test that it works correctly with end entity profiles using the counter
int pid = 0;
try {
EndEntityProfile profile = new EndEntityProfile();
profile.addField(DnComponents.ORGANIZATION);
profile.addField(DnComponents.COUNTRY);
profile.addField(DnComponents.COMMONNAME);
profile.setValue(EndEntityProfile.AVAILCAS,0,""+caid);
profile.setUse(EndEntityProfile.ALLOWEDREQUESTS, 0, false);
endEntityProfileSession.addEndEntityProfile(admin, "TESTREQUESTCOUNTER", profile);
pid = endEntityProfileSession.getEndEntityProfileId(admin, "TESTREQUESTCOUNTER");
} catch (EndEntityProfileExistsException pee) {
assertTrue("Can not create end entity profile", false);
}
// Now add extended information with allowed requests 2
ei = new ExtendedInformation();
allowedrequests = 2;
ei.setCustomData(ExtendedInformation.CUSTOM_REQUESTCOUNTER, String.valueOf(allowedrequests));
user = new UserDataVO(username, "C=SE,O=AnaTom,CN="+username, caid, null, null, SecConst.USER_INVALID, pid, SecConst.CERTPROFILE_FIXED_ENDUSER, SecConst.TOKEN_SOFT_PEM, 0, ei);
thrown = false;
try {
userAdminSession.changeUser(admin, user, false);
} catch (UserDoesntFullfillEndEntityProfile e) {
thrown = true;
}
assertTrue(thrown);
// decrease the value
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(0, counter);
// decrease the value again, default value when the counter is not used is 0
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(0, counter);
// Now allow the counter
EndEntityProfile ep = endEntityProfileSession.getEndEntityProfile(admin, pid);
ep.setUse(EndEntityProfile.ALLOWEDREQUESTS, 0, true);
ep.setValue(EndEntityProfile.ALLOWEDREQUESTS,0,"2");
endEntityProfileSession.changeEndEntityProfile(admin, "TESTREQUESTCOUNTER", ep);
// This time changeUser will be ok
userAdminSession.changeUser(admin, user, false);
// decrease the value
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(1, counter);
// decrease the value again
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(0, counter);
// decrease the value again
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(-1, counter);
// decrease the value again
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(-1, counter);
// Now disallow the counter, it will be deleted from the user
ep = endEntityProfileSession.getEndEntityProfile(admin, pid);
ep.setUse(EndEntityProfile.ALLOWEDREQUESTS, 0, false);
endEntityProfileSession.changeEndEntityProfile(admin, "TESTREQUESTCOUNTER", ep);
ei = user.getExtendedinformation();
ei.setCustomData(ExtendedInformation.CUSTOM_REQUESTCOUNTER, null);
user.setExtendedinformation(ei);
userAdminSession.changeUser(admin, user, false);
// decrease the value
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(0, counter);
// allow the counter
ep = endEntityProfileSession.getEndEntityProfile(admin, pid);
ep.setUse(EndEntityProfile.ALLOWEDREQUESTS, 0, true);
ep.setValue(EndEntityProfile.ALLOWEDREQUESTS,0,"2");
endEntityProfileSession.changeEndEntityProfile(admin, "TESTREQUESTCOUNTER", ep);
ei = user.getExtendedinformation();
ei.setCustomData(ExtendedInformation.CUSTOM_REQUESTCOUNTER, "0");
user.setExtendedinformation(ei);
user.setStatus(UserDataConstants.STATUS_NEW);
userAdminSession.changeUser(admin, user, false);
// decrease the value
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(1, counter);
// decrease the value again
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(0, counter);
// decrease the value again
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(-1, counter);
// test setuserstatus it will re-set the counter
userAdminSession.setUserStatus(admin, user.getUsername(), UserDataConstants.STATUS_GENERATED);
ep = endEntityProfileSession.getEndEntityProfile(admin, pid);
ep.setUse(EndEntityProfile.ALLOWEDREQUESTS, 0, true);
ep.setValue(EndEntityProfile.ALLOWEDREQUESTS,0,"3");
endEntityProfileSession.changeEndEntityProfile(admin, "TESTREQUESTCOUNTER", ep);
userAdminSession.setUserStatus(admin, user.getUsername(), UserDataConstants.STATUS_NEW);
// decrease the value
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(2, counter);
// decrease the value again
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(1, counter);
// test setuserstatus again it will not re-set the counter if it is already new
userAdminSession.setUserStatus(admin, user.getUsername(), UserDataConstants.STATUS_NEW);
assertEquals(1, counter);
// decrease the value again
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(0, counter); // sets status to generated
// decrease the value again
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(-1, counter);
// test setuserstatus again it will re-set the counter since status is generated
ep = endEntityProfileSession.getEndEntityProfile(admin, pid);
ep.setUse(EndEntityProfile.ALLOWEDREQUESTS, 0, true);
ep.setValue(EndEntityProfile.ALLOWEDREQUESTS,0,"3");
endEntityProfileSession.changeEndEntityProfile(admin, "TESTREQUESTCOUNTER", ep);
userAdminSession.setUserStatus(admin, user.getUsername(), UserDataConstants.STATUS_NEW);
// decrease the value
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(2, counter);
// Also changeUser to new from something else will re-set status, if ei value is 0
userAdminSession.setUserStatus(admin, user.getUsername(), UserDataConstants.STATUS_GENERATED);
ei.setCustomData(ExtendedInformation.CUSTOM_REQUESTCOUNTER, "0");
user.setExtendedinformation(ei);
user.setStatus(UserDataConstants.STATUS_NEW);
userAdminSession.changeUser(admin, user, false);
// decrease the value
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(2, counter);
// Test set and re-set logic
// The profile has 3 as default value, if I change user with status to generated and value 2 it should be set as that
UserDataVO user1 = userAdminSession.findUser(admin, user.getUsername());
ei = new ExtendedInformation();
allowedrequests = 2;
ei.setCustomData(ExtendedInformation.CUSTOM_REQUESTCOUNTER, String.valueOf(allowedrequests));
user1.setExtendedinformation(ei);
user1.setStatus(UserDataConstants.STATUS_GENERATED);
userAdminSession.changeUser(admin, user1, false);
user1 = userAdminSession.findUser(admin, user.getUsername());
ei = user1.getExtendedinformation();
String value = ei.getCustomData(ExtendedInformation.CUSTOM_REQUESTCOUNTER);
assertEquals("2", value);
// If I change user with status to new and value 1 it should be set as that
ei = new ExtendedInformation();
allowedrequests = 1;
ei.setCustomData(ExtendedInformation.CUSTOM_REQUESTCOUNTER, String.valueOf(allowedrequests));
user1.setExtendedinformation(ei);
user1.setStatus(UserDataConstants.STATUS_NEW);
userAdminSession.changeUser(admin, user1, false);
user1 = userAdminSession.findUser(admin, user.getUsername());
ei = user1.getExtendedinformation();
value = ei.getCustomData(ExtendedInformation.CUSTOM_REQUESTCOUNTER);
assertEquals("1", value);
// If I set status to new again, with noting changed, nothing should change
userAdminSession.setUserStatus(admin, user.getUsername(), UserDataConstants.STATUS_NEW);
user1 = userAdminSession.findUser(admin, user.getUsername());
ei = user1.getExtendedinformation();
value = ei.getCustomData(ExtendedInformation.CUSTOM_REQUESTCOUNTER);
assertEquals("1", value);
// The same when I change the user
user1.setStatus(UserDataConstants.STATUS_NEW);
userAdminSession.changeUser(admin, user1, false);
user1 = userAdminSession.findUser(admin, user.getUsername());
ei = user1.getExtendedinformation();
value = ei.getCustomData(ExtendedInformation.CUSTOM_REQUESTCOUNTER);
assertEquals("1", value);
// If I change the status to generated, nothing should happen
userAdminSession.setUserStatus(admin, user.getUsername(), UserDataConstants.STATUS_GENERATED);
user1 = userAdminSession.findUser(admin, user.getUsername());
ei = user1.getExtendedinformation();
value = ei.getCustomData(ExtendedInformation.CUSTOM_REQUESTCOUNTER);
assertEquals("1", value);
// If I change the status to new from generated the default value should be used
userAdminSession.setUserStatus(admin, user.getUsername(), UserDataConstants.STATUS_NEW);
user1 = userAdminSession.findUser(admin, user.getUsername());
ei = user1.getExtendedinformation();
value = ei.getCustomData(ExtendedInformation.CUSTOM_REQUESTCOUNTER);
assertEquals("3", value);
// It should be possible to simply set the value to 0
ei = new ExtendedInformation();
allowedrequests = 0;
ei.setCustomData(ExtendedInformation.CUSTOM_REQUESTCOUNTER, String.valueOf(allowedrequests));
user1.setExtendedinformation(ei);
user1.setStatus(UserDataConstants.STATUS_GENERATED);
userAdminSession.changeUser(admin, user1, false);
user1 = userAdminSession.findUser(admin, user.getUsername());
ei = user1.getExtendedinformation();
value = ei.getCustomData(ExtendedInformation.CUSTOM_REQUESTCOUNTER);
assertEquals("0", value);
// Changing again to new, with 0 passed in will set the default value
user1.setStatus(UserDataConstants.STATUS_NEW);
userAdminSession.changeUser(admin, user1, false);
user1 = userAdminSession.findUser(admin, user.getUsername());
ei = user1.getExtendedinformation();
value = ei.getCustomData(ExtendedInformation.CUSTOM_REQUESTCOUNTER);
assertEquals("3", value);
// Set back to 0
user1.setStatus(UserDataConstants.STATUS_GENERATED);
ei.setCustomData(ExtendedInformation.CUSTOM_REQUESTCOUNTER, "0");
user1.setExtendedinformation(ei);
userAdminSession.changeUser(admin, user1, false);
user1 = userAdminSession.findUser(admin, user.getUsername());
ei = user1.getExtendedinformation();
value = ei.getCustomData(ExtendedInformation.CUSTOM_REQUESTCOUNTER);
assertEquals("0", value);
// Setting with null value will always remove the request counter (the whole extendedinformatin actually)
user1.setExtendedinformation(null);
user1.setStatus(UserDataConstants.STATUS_NEW);
userAdminSession.changeUser(admin, user1, false);