WebUtils { public static boolean isAuthorizedToRunContainerTests(@Nonnull Session session) { DockerCloudUtils.requireNonNull(session, "Session cannot be null."); Optional<SUser> user = retrieveUserFromWSSession(session); return user.map(WebUtils::isAuthorizedToRunContainerTests).orElse(false); } private  WebUtils(); @Nonnull static Optional<HttpSession> retrieveHttpSessionFromWSSession(@Nonnull Session session); @Nonnull static Optional<SUser> retrieveUserFromWSSession(@Nonnull Session session); static boolean isAuthorizedToRunContainerTests(@Nonnull Session session); static boolean isAuthorizedToRunContainerTests(@Nullable SUser user);  }
@Test public void isAuthorizedToRunContainerTests() { TestSUser user = new TestSUser(); user.addProjectPermission("Foo", Permission.START_STOP_CLOUD_AGENT). addProjectPermission("Foo", Permission.MANAGE_AGENT_CLOUDS); assertThat(WebUtils.isAuthorizedToRunContainerTests(user)).isTrue(); }
@Test public void isAuthorizedToRunContainerTestsUnauthorized() { assertThat(WebUtils.isAuthorizedToRunContainerTests((SUser) null)).isFalse(); TestSUser user = new TestSUser(); user.addProjectPermission("Foo", Permission.START_STOP_CLOUD_AGENT); assertThat(WebUtils.isAuthorizedToRunContainerTests(user)).isFalse(); user = new TestSUser(); user.addProjectPermission("Foo", Permission.MANAGE_AGENT_CLOUDS); assertThat(WebUtils.isAuthorizedToRunContainerTests(user)).isFalse(); }
@Test public void isAuthorizedToRunContainerTestsFromSession() { TestSession session = new TestSession(); TestHttpSession httpSession = new TestHttpSession(); TestSUser user = new TestSUser(); session.getUserProperties().put(HttpSession.class.getName(), httpSession); httpSession.setAttribute(WebUtils.DEFAULT_USER_KEY, user); user.addProjectPermission("Foo", Permission.START_STOP_CLOUD_AGENT). addProjectPermission("Foo", Permission.MANAGE_AGENT_CLOUDS); assertThat(WebUtils.isAuthorizedToRunContainerTests(session)).isTrue(); }
@Test public void isAuthorizedToRunContainerTestsFromSessionNoUserInSession() { TestSession session = new TestSession(); TestHttpSession httpSession = new TestHttpSession(); session.getUserProperties().put(HttpSession.class.getName(), httpSession); assertThat(WebUtils.isAuthorizedToRunContainerTests(session)).isFalse(); }
@Test public void isAuthorizedToRunContainerTestsFromSessionUserNotAuthorized() { TestSession session = new TestSession(); TestHttpSession httpSession = new TestHttpSession(); TestSUser user = new TestSUser(); session.getUserProperties().put(HttpSession.class.getName(), httpSession); httpSession.setAttribute(WebUtils.DEFAULT_USER_KEY, user); assertThat(WebUtils.isAuthorizedToRunContainerTests(session)).isFalse(); }