PageUtils { public static void hideUploadButtonWhenNeeded(final Page page, final Session session, final UserManager userManager, final Boolean lockdownUploadProjects) { final User user = session.getUser(); if (lockdownUploadProjects && !UserUtils.hasPermissionforAction(userManager, user, Permission.Type.UPLOADPROJECTS)) { page.add("hideUploadProject", true); } } private  PageUtils(); static void hideUploadButtonWhenNeeded(final Page page, final Session session,
      final UserManager userManager,
      final Boolean lockdownUploadProjects);  }
@Test public void testUploadButtonisHiddenWhenGlobalPropertyIsSet() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException, ClassNotFoundException, InstantiationException, IOException { final HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); final HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); final User user = TestUtils.getTestUser(); final Session session = new Session("fake-session-id", user, "127.0.0.1"); final VelocityEngine velocityEngine = new VelocityEngine(); velocityEngine.init("src/test/resources/velocity.properties"); final Page page = new Page(httpServletRequest, httpServletResponse, velocityEngine, "azkaban/webapp/servlet/velocity/permissionspage.vm"); final UserManager userManager = TestUtils.createTestXmlUserManager(); PageUtils.hideUploadButtonWhenNeeded(page, session, userManager, true); final Field velocityContextField = Page.class.getDeclaredField("context"); velocityContextField.setAccessible(true); final VelocityContext velocityContext = (VelocityContext) velocityContextField.get(page); assertThat((boolean) velocityContext.get("hideUploadProject")).isTrue(); }
@Test public void testUploadButtonIsVisibleToAllWhenGlobalPropertyIsNotSet() throws NoSuchFieldException, IllegalAccessException { final HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); final HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); final User user = TestUtils.getTestUser(); final Session session = new Session("fake-session-id", user, "127.0.0.1"); final VelocityEngine velocityEngine = new VelocityEngine(); velocityEngine.init("src/test/resources/velocity.properties"); final Page page = new Page(httpServletRequest, httpServletResponse, velocityEngine, "azkaban/webapp/servlet/velocity/permissionspage.vm"); final UserManager userManager = TestUtils.createTestXmlUserManager(); PageUtils.hideUploadButtonWhenNeeded(page, session, userManager, false); final Field velocityContextField = Page.class.getDeclaredField("context"); velocityContextField.setAccessible(true); final VelocityContext velocityContext = (VelocityContext) velocityContextField.get(page); assertThat(velocityContext.containsKey("hideUploadProject")).isFalse(); }
@Test public void testUploadButtonIsEnabledForAdminUser() throws NoSuchFieldException, IllegalAccessException, UserManagerException { final HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); final HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); final UserManager userManager = TestUtils.createTestXmlUserManager(); final User testAdmin = userManager.getUser("testAdmin", "testAdmin"); final Session session = new Session("fake-session-id", testAdmin, "127.0.0.1"); final VelocityEngine velocityEngine = new VelocityEngine(); velocityEngine.init("src/test/resources/velocity.properties"); final Page page = new Page(httpServletRequest, httpServletResponse, velocityEngine, "azkaban/webapp/servlet/velocity/permissionspage.vm"); PageUtils.hideUploadButtonWhenNeeded(page, session, userManager, false); final Field velocityContextField = Page.class.getDeclaredField("context"); velocityContextField.setAccessible(true); final VelocityContext velocityContext = (VelocityContext) velocityContextField.get(page); assertThat(velocityContext.get("hideUploadProject")).isNull(); }