UserProfileHandler implements LoggableResource { protected void cleanupUserProfile(final JsonObject profile, final Handler<JsonObject> profileCallback) { log.debug("About to remove 'not allowed' properties from user profile"); JsonObject profileCopy = profile.copy(); Set<String> profileFieldNames = profileCopy.fieldNames(); profileFieldNames.stream().filter(fieldName -> !userProfileConfiguration.isAllowedProfileProperty(fieldName)).forEach(fieldName -> { log.debug("Removing property '" + fieldName + "' from user profile"); profile.remove(fieldName); }); profileCallback.handle(profile); } UserProfileHandler(Vertx vertx, ResourceStorage storage, UserProfileConfiguration userProfileConfiguration); @Deprecated  UserProfileHandler(Vertx vertx, ResourceStorage storage, LoggingResourceManager loggingResourceManager, UserProfileConfiguration userProfileConfiguration); @Override void enableResourceLogging(boolean resourceLoggingEnabled); boolean isUserProfileRequest(HttpServerRequest request); void handle(final HttpServerRequest request);  }
@Test public void testCleanupProfileDefaultAttributes() { Vertx vertx = mock(Vertx.class); when(vertx.eventBus()).thenReturn(mock(EventBus.class)); ResourceStorage resourceStorage = mock(ResourceStorage.class); UserProfileConfiguration userProfileConfiguration = UserProfileConfiguration.create() .userProfileUriPattern("/users/v1/([^/]+)/profile") .addAllowedProfileProperties("username", "personalNumber", "fullname", "mail", "department", "lang", "addAttr1", "addAttr2", "addAttr3") .rolePattern("^z-gateleen[-_](.*)$") .build(); UserProfileHandler userProfileHandler = new UserProfileHandler(vertx, resourceStorage, userProfileConfiguration); JsonObject profile = new JsonObject(); profile.put("personalNumber", "04146251"); profile.put("username", "john"); profile.put("mail", "john.doe@swisspush.org"); profile.put("department", "sales"); profile.put("lang", "de"); profile.put("additionalAttribute", "addVal"); userProfileHandler.cleanupUserProfile(profile, updatedProfile -> { assertThat(updatedProfile.getString("personalNumber"), is("04146251")); assertThat(updatedProfile.getString("username"), is("john")); assertThat(updatedProfile.getString("mail"), is("john.doe@swisspush.org")); assertThat(updatedProfile.getString("department"), is("sales")); assertThat(updatedProfile.getString("lang"), is("de")); assertThat(updatedProfile.getString("additionalAttribute"), is(nullValue())); }); }
@Test public void testCleanupProfileAdditionalAttributes() { Vertx vertx = mock(Vertx.class); when(vertx.eventBus()).thenReturn(mock(EventBus.class)); ResourceStorage resourceStorage = mock(ResourceStorage.class); UserProfileConfiguration userProfileConfiguration = UserProfileConfiguration.create() .userProfileUriPattern("/users/v1/([^/]+)/profile") .addAllowedProfileProperties("username", "personalNumber", "fullname", "mail", "department", "lang", "addAttr1", "addAttr2", "addAttr3") .rolePattern("^z-gateleen[-_](.*)$") .build(); UserProfileHandler userProfileHandler = new UserProfileHandler(vertx, resourceStorage, userProfileConfiguration); JsonObject profile = new JsonObject(); profile.put("personalNumber", "04146251"); profile.put("username", "john"); profile.put("mail", "john.doe@swisspush.org"); profile.put("department", "sales"); profile.put("lang", "de"); profile.put("addAttr1", "addVal1"); profile.put("addAttr2", "addVal2"); profile.put("addAttr3", "addVal3"); profile.put("addAttr4", "addVal4"); userProfileHandler.cleanupUserProfile(profile, updatedProfile -> { assertThat(updatedProfile.getString("personalNumber"), is("04146251")); assertThat(updatedProfile.getString("username"), is("john")); assertThat(updatedProfile.getString("mail"), is("john.doe@swisspush.org")); assertThat(updatedProfile.getString("department"), is("sales")); assertThat(updatedProfile.getString("lang"), is("de")); assertThat(updatedProfile.getString("addAttr1"), is("addVal1")); assertThat(updatedProfile.getString("addAttr2"), is("addVal2")); assertThat(updatedProfile.getString("addAttr3"), is("addVal3")); assertThat(updatedProfile.getString("addAttr4"), is(nullValue())); }); }