|
| 1 | +package cz.metacentrum.perun.core.impl; |
| 2 | + |
| 3 | +import cz.metacentrum.perun.core.AbstractPerunIntegrationTest; |
| 4 | +import cz.metacentrum.perun.core.api.Attribute; |
| 5 | +import cz.metacentrum.perun.core.api.BeansUtils; |
| 6 | +import cz.metacentrum.perun.core.api.CoreConfig; |
| 7 | +import cz.metacentrum.perun.core.api.User; |
| 8 | +import cz.metacentrum.perun.core.api.exceptions.AttributeDefinitionExistsException; |
| 9 | +import cz.metacentrum.perun.core.api.exceptions.InternalErrorException; |
| 10 | +import cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException; |
| 11 | +import cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException; |
| 12 | +import cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException; |
| 13 | +import org.junit.Before; |
| 14 | +import org.junit.Test; |
| 15 | + |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.Collections; |
| 18 | +import java.util.HashSet; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import static org.junit.Assert.assertNotNull; |
| 22 | +import static org.junit.Assert.assertThrows; |
| 23 | + |
| 24 | +public class DefaultBlockedLoginCheckerTest extends AbstractPerunIntegrationTest { |
| 25 | + private final static String CLASS_NAME = "DefaultBlockedLoginChecker."; |
| 26 | + private final static String LOGIN = "testLogin"; |
| 27 | + |
| 28 | + private User user; |
| 29 | + private Attribute attr; |
| 30 | + |
| 31 | + DefaultBlockedLoginChecker defaultBlockedLoginChecker; |
| 32 | + |
| 33 | + @Before |
| 34 | + public void setUp() throws Exception { |
| 35 | + defaultBlockedLoginChecker = new DefaultBlockedLoginChecker(perun); |
| 36 | + |
| 37 | + setUser(); |
| 38 | + setLoginNamespaceAttribute(); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void defaultBlockedLoginAlreadyUsed() { |
| 43 | + System.out.println(CLASS_NAME + "defaultBlockedLoginIsAlreadyUsed"); |
| 44 | + |
| 45 | + List<String> originalAdmins = BeansUtils.getCoreConfig().getAdmins(); |
| 46 | + try { |
| 47 | + // configure admins to contain one login - testLogin |
| 48 | + BeansUtils.getCoreConfig().setAdmins(Collections.singletonList(LOGIN)); |
| 49 | + assertThrows(InternalErrorException.class, () -> defaultBlockedLoginChecker.checkDefaultBlockedLogins()); |
| 50 | + } finally { |
| 51 | + // set admins back to the original admins |
| 52 | + BeansUtils.getCoreConfig().setAdmins(originalAdmins); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void defaultBlockedLoginAreNotUsed() { |
| 58 | + System.out.println(CLASS_NAME + "defaultBlockedLoginAreNotUsed"); |
| 59 | + |
| 60 | + CoreConfig originalConfig = BeansUtils.getCoreConfig(); |
| 61 | + try { |
| 62 | + // set new core config |
| 63 | + CoreConfig cfNew = new CoreConfig(); |
| 64 | + |
| 65 | + cfNew.setAdmins(new ArrayList<>()); |
| 66 | + cfNew.setEnginePrincipals(new ArrayList<>()); |
| 67 | + cfNew.setNotificationPrincipals(new ArrayList<>()); |
| 68 | + cfNew.setDontLookupUsers(new HashSet<>()); |
| 69 | + cfNew.setRegistrarPrincipals(new ArrayList<>()); |
| 70 | + cfNew.setRpcPrincipal(null); |
| 71 | + cfNew.setInstanceId("test"); |
| 72 | + |
| 73 | + BeansUtils.setConfig(cfNew); |
| 74 | + |
| 75 | + defaultBlockedLoginChecker.checkDefaultBlockedLogins(); |
| 76 | + } finally { |
| 77 | + // set core config back to original |
| 78 | + BeansUtils.setConfig(originalConfig); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private void setUser() { |
| 83 | + user = new User(); |
| 84 | + user.setFirstName("Joe"); |
| 85 | + user.setLastName("Doe"); |
| 86 | + user = perun.getUsersManagerBl().createUser(sess, user); |
| 87 | + assertNotNull(user); |
| 88 | + } |
| 89 | + |
| 90 | + private void setLoginNamespaceAttribute() throws AttributeDefinitionExistsException, WrongAttributeAssignmentException, WrongReferenceAttributeValueException, WrongAttributeValueException { |
| 91 | + attr = new Attribute(); |
| 92 | + attr.setNamespace("urn:perun:user:attribute-def:def"); |
| 93 | + attr.setFriendlyName("login-namespace:META-login"); |
| 94 | + attr.setType(String.class.getName()); |
| 95 | + attr.setValue(LOGIN); |
| 96 | + |
| 97 | + assertNotNull("unable to create login namespace attribute", perun.getAttributesManagerBl().createAttribute(sess, attr)); |
| 98 | + |
| 99 | + perun.getAttributesManagerBl().setAttribute(sess, user, attr); |
| 100 | + } |
| 101 | +} |
0 commit comments