urn_perun_user_facility_attribute_def_def_shell extends UserFacilityAttributesModuleAbstract implements UserFacilityAttributesModuleImplApi { @Override public void checkAttributeSemantics(PerunSessionImpl session, User user, Facility facility, Attribute attribute) throws WrongReferenceAttributeValueException, WrongAttributeAssignmentException { String shell = attribute.valueAsString(); if (shell == null) return; List<String> allowedShells = allShellsAtSpecifiedFacility(session, facility, user); if (allowedShells.isEmpty()) { throw new WrongReferenceAttributeValueException(attribute, null, user, facility, "There are no shells available at associated facilities"); } if (!allowedShells.contains(shell)) { throw new WrongReferenceAttributeValueException(attribute, null, user, facility, "Such shell is not allowed at specified facility for the user."); } }  @Override void checkAttributeSyntax(PerunSessionImpl session, User user, Facility facility, Attribute attribute); @Override void checkAttributeSemantics(PerunSessionImpl session, User user, Facility facility, Attribute attribute); @Override List<String> getDependencies(); @Override AttributeDefinition getAttributeDefinition();  }
@Test public void testCheckAttributeSemantics() throws Exception { System.out.println("testCheckAttributeSemantics()"); Attribute attributeToCheck = new Attribute(); attributeToCheck.setValue("/bin/bash"); when(session.getPerunBl().getUsersManagerBl().getAllowedResources(any(PerunSession.class), any(Facility.class), any(User.class))).thenReturn(new ArrayList<Resource>(){{add(resource);}}); when(session.getPerunBl().getFacilitiesManagerBl().getAssignedResources(any(PerunSession.class), any(Facility.class))).thenReturn(new ArrayList<Resource>(){{add(resource);}}); when(session.getPerunBl().getAttributesManagerBl().getAttribute(any(PerunSession.class), any(Resource.class), eq(AttributesManager.NS_RESOURCE_ATTR_DEF + ":shells"))).thenReturn(listOfShells); classInstance.checkAttributeSemantics(session, user, facility, attributeToCheck); }
@Test(expected=WrongReferenceAttributeValueException.class) public void testCheckAttributeSemanticsOfUnknownUser() throws Exception{ System.out.println("testCheckAttributeSemanticsOfUnknownUser()"); when(session.getPerunBl().getUsersManagerBl().getAllowedResources(any(PerunSession.class), any(Facility.class), any(User.class))).thenReturn(new ArrayList<>()); when(session.getPerunBl().getAttributesManagerBl().getAttribute(any(PerunSession.class), any(Resource.class), anyString())).thenReturn(listOfShells); Attribute atr = new Attribute(); atr.setValue(("/bin/bash")); classInstance.checkAttributeSemantics(session, user, facility, atr); }
@Test(expected=WrongReferenceAttributeValueException.class) public void testCheckAttributeSemanticsWrongShellFormat() throws Exception { System.out.println("testCheckAttributeSemanticsWrongShellFormat()"); Attribute attributeToCheck = new Attribute(); attributeToCheck.setValue("/bin/\n/bash"); when(session.getPerunBl().getUsersManagerBl().getAllowedResources(any(PerunSession.class), any(Facility.class), any(User.class))).thenReturn(new ArrayList<Resource>(){{add(resource);}}); when(session.getPerunBl().getFacilitiesManagerBl().getAssignedResources(any(PerunSession.class), any(Facility.class))).thenReturn(new ArrayList<Resource>(){{add(resource);}}); when(session.getPerunBl().getAttributesManagerBl().getAttribute(any(PerunSession.class), any(Resource.class), anyString())).thenReturn(listOfShells); classInstance.checkAttributeSemantics(session, user, facility, attributeToCheck); fail("Wrong shell format should have thrown an exception"); }
@Test(expected=WrongReferenceAttributeValueException.class) public void testCheckAttributeSemanticsWrongShellFormatInvalidCharacter() throws Exception { System.out.println("testCheckAttributeSemanticsWrongShellFormatInvalidCharacter()"); Attribute attributeToCheck = new Attribute(); attributeToCheck.setValue("/"); when(session.getPerunBl().getUsersManagerBl().getAllowedResources(any(PerunSession.class), any(Facility.class), any(User.class))).thenReturn(new ArrayList<Resource>(){{add(resource);}}); when(session.getPerunBl().getFacilitiesManagerBl().getAssignedResources(any(PerunSession.class), any(Facility.class))).thenReturn(new ArrayList<Resource>(){{add(resource);}}); when(session.getPerunBl().getAttributesManagerBl().getAttribute(any(PerunSession.class), any(Resource.class), anyString())).thenReturn(listOfShells); classInstance.checkAttributeSemantics(session, user, facility, attributeToCheck); fail("Wrong shell format should have thrown an exception"); }
@Test(expected=WrongReferenceAttributeValueException.class) public void testCheckAttributeSemanticsWrongShellFormatShellIsDirectory() throws Exception { System.out.println("testCheckAttributeSemanticsWrongShellFormatShellIsDirectory()"); when(session.getPerunBl().getUsersManagerBl().getAllowedResources(any(PerunSession.class), any(Facility.class), any(User.class))).thenReturn(new ArrayList<Resource>(){{add(resource);}}); when(session.getPerunBl().getFacilitiesManagerBl().getAssignedResources(any(PerunSession.class), any(Facility.class))).thenReturn(new ArrayList<Resource>(){{add(resource);}}); when(session.getPerunBl().getAttributesManagerBl().getAttribute(any(PerunSession.class), any(Resource.class), anyString())).thenReturn(listOfShells); Attribute attributeToCheck = new Attribute(); attributeToCheck.setValue("/bin/bash/"); classInstance.checkAttributeSemantics(session, user, facility, attributeToCheck); fail("Wrong shell format should have thrown an exception"); }