use of org.apache.qpid.server.security.auth.manager.SimpleAuthenticationManager in project qpid-broker-j by apache.
the class BrokerImplTest method testPurgeUser.
public void testPurgeUser() throws Exception {
final String testUsername = "testUser";
final String testPassword = "testPassword";
// setup broker
Map<String, Object> brokerAttributes = new HashMap<>();
brokerAttributes.put("name", "Broker");
brokerAttributes.put(Broker.MODEL_VERSION, BrokerModel.MODEL_VERSION);
brokerAttributes.put(Broker.DURABLE, true);
_brokerImpl = new BrokerImpl(brokerAttributes, _systemConfig);
_brokerImpl.open();
// setup auth provider with testuser
final Map<String, Object> authProviderAttributes = new HashMap<>();
authProviderAttributes.put(ConfiguredObject.NAME, "testAuthProvider");
authProviderAttributes.put(ConfiguredObject.TYPE, "Simple");
SimpleAuthenticationManager authenticationProvider = new SimpleAuthenticationManager(authProviderAttributes, _brokerImpl);
authenticationProvider.create();
authenticationProvider.addUser(testUsername, testPassword);
// setup preference owned by testuser
final Map<String, Object> preferenceAttributes = new HashMap<>();
UUID preferenceId = UUID.randomUUID();
preferenceAttributes.put(Preference.ID_ATTRIBUTE, preferenceId);
preferenceAttributes.put(Preference.NAME_ATTRIBUTE, "testPref");
preferenceAttributes.put(Preference.TYPE_ATTRIBUTE, "X-testPrefType");
preferenceAttributes.put(Preference.VALUE_ATTRIBUTE, Collections.EMPTY_MAP);
Subject testUserSubject = new Subject();
testUserSubject.getPrincipals().add(new AuthenticatedPrincipal(new UsernamePrincipal(testUsername, authenticationProvider)));
testUserSubject.setReadOnly();
final Collection<Preference> preferences = Collections.singleton(PreferenceFactory.fromAttributes(_brokerImpl, preferenceAttributes));
Subject.doAs(testUserSubject, new PrivilegedAction<Void>() {
@Override
public Void run() {
try {
_brokerImpl.getUserPreferences().updateOrAppend(preferences).get(10, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
e.printStackTrace();
fail("Failed to put preference:");
}
return null;
}
});
// test pre-conditions
Collection<Preference> preferencesBeforePurge = getPreferencesAs(testUserSubject);
assertEquals("Unexpected number of preferences before userPurge", 1, preferencesBeforePurge.size());
assertEquals("Unexpected preference before userPurge", preferenceId, preferencesBeforePurge.iterator().next().getId());
assertTrue("User was not valid before userPurge", authenticationProvider.getUsers().containsKey(testUsername));
_brokerImpl.purgeUser(authenticationProvider, testUsername);
// test post-conditions
Collection<Preference> preferencesAfterPurge = getPreferencesAs(testUserSubject);
assertEquals("Preferences were not deleted during userPurge", Collections.EMPTY_SET, preferencesAfterPurge);
assertEquals("User was not deleted from authentication Provider", Collections.EMPTY_MAP, authenticationProvider.getUsers());
verify(_preferenceStore).replace(Collections.singleton(preferenceId), Collections.EMPTY_SET);
}
Aggregations