Skip to content

Commit

Permalink
Jans ext uid test (#10346)
Browse files Browse the repository at this point in the history
* feat(jans-orm): add jansExtUid attributes persistence test

Signed-off-by: Yuriy Movchan <[email protected]>

* feat(jans-orm): add jansExtUid attributes persistence test

Signed-off-by: Yuriy Movchan <[email protected]>

* feat(jans-orm): add jansExtUid attributes persistence test

Signed-off-by: Yuriy Movchan <[email protected]>

* feat(jans-orm): add jansExtUid attributes persistence test

Signed-off-by: Yuriy Movchan <[email protected]>

---------

Signed-off-by: Yuriy Movchan <[email protected]>
Co-authored-by: YuriyZ <[email protected]>
  • Loading branch information
yurem and yuriyz authored Dec 6, 2024
1 parent 019e5ae commit 30f33c5
Show file tree
Hide file tree
Showing 7 changed files with 322 additions and 19 deletions.
2 changes: 1 addition & 1 deletion jans-auth-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
<version>3.5.2</version>
<configuration>
<argLine>@{argLine}</argLine>

Expand Down
57 changes: 52 additions & 5 deletions jans-auth-server/server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,15 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Xms1024m -Xmx2048m -XX:MaxMetaspaceSize=512m -XX:+DisableExplicitGC
<properties>
<configurationParameters>
junit.jupiter.conditions.deactivate = *
junit.jupiter.extensions.autodetection.enabled = true
junit.jupiter.testinstance.lifecycle.default = per_class
junit.jupiter.execution.parallel.enabled = true
</configurationParameters>
</properties>
<argLine>-Xms1024m -Xmx2048m -XX:MaxMetaspaceSize=512m -XX:+DisableExplicitGC
-Djans.base=${project.build.directory}
-Dlog.base=${project.build.directory}
-Dgluu.disable.scheduler=true
Expand Down Expand Up @@ -571,6 +579,12 @@
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mvel</groupId>
Expand All @@ -591,13 +605,46 @@
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
<version>6.14.3</version> <!-- we need 6.14.3 due to Arquillian -->
</dependency>
<!-- Test -->
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-junit5</artifactId>
<scope>test</scope>
</dependency>
<!-- JUnit Jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<!-- Mockito -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>3.5.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>javax.xml.bind</groupId>
Expand Down Expand Up @@ -773,7 +820,7 @@
<artifactId>jakarta.ejb-api</artifactId>
</dependency>


<!-- Test: Embedded Jetty -->
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
Expand Down Expand Up @@ -826,7 +873,7 @@
</manifest>
<manifestEntries>
<Implementation-Build>${buildNumber}</Implementation-Build>
<!--suppress UnresolvedMavenProperty -->
<!--suppress UnresolvedMavenProperty -->
<Build-Branch>${git.branch}</Build-Branch>
</manifestEntries>
</archive>
Expand Down Expand Up @@ -1051,7 +1098,7 @@
</repository>
</repositories>
<dependencies>
<!--
<!--
<dependency>
<groupId>com.eatthepath</groupId>
<artifactId>pushy</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;

import javax.inject.Named;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
Expand All @@ -23,9 +22,7 @@
* @author Yuriy Z
*/
@RequestScoped
@Named
public class AuthorizationChallengeValidator {

@Inject
private Logger log;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
/*
* Janssen Project software is available under the Apache License (2004). See http://www.apache.org/licenses/ for full text.
*
* Copyright (c) 2020, Janssen Project
*/

package io.jans.as.server.comp.db;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.jboss.weld.junit5.ExplicitParamInjection;
import org.jboss.weld.junit5.auto.AddBeanClasses;
import org.jboss.weld.junit5.auto.EnableAutoWeld;
import org.jboss.weld.junit5.auto.WeldJunit5AutoExtension;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;

import io.jans.as.common.model.common.User;
import io.jans.as.common.service.common.ApplicationFactory;
import io.jans.orm.PersistenceEntryManager;
import io.jans.orm.PersistenceEntryManagerFactory;
import io.jans.orm.model.PersistenceConfiguration;
import io.jans.orm.service.PersistanceFactoryService;
import io.jans.orm.util.properties.FileConfiguration;
import io.jans.service.EncryptionService;
import io.jans.service.UserAuthenticatorService;
import io.jans.util.security.SecurityProviderUtility;
import io.jans.util.security.StringEncrypter;
import io.jans.util.security.StringEncrypter.EncryptionException;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Default;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Inject;

/**
* @author Yuriy Movchan Date: 05/12/2024
*/
@ExtendWith(WeldJunit5AutoExtension.class)
@EnableAutoWeld
@TestMethodOrder(OrderAnnotation.class)

@AddBeanClasses(io.jans.service.util.Resources.class)

@AddBeanClasses(value = { io.jans.orm.service.PersistanceFactoryService.class,
io.jans.orm.ldap.impl.LdapEntryManagerFactory.class, io.jans.orm.sql.impl.SqlEntryManagerFactory.class,
io.jans.orm.couchbase.impl.CouchbaseEntryManagerFactory.class, io.jans.orm.cloud.spanner.impl.SpannerEntryManagerFactory.class})

@ExplicitParamInjection
public class UserJansExtUidAttributeTest {

static {
System.setProperty("jans.base", "target");
}

@Inject
Logger log;

@Inject
PersistenceEntryManager persistenceEntryManager;

@Inject
UserAuthenticatorService userAuthenticatorService;

private static String userId, userDn;

@BeforeAll
public static void init() {
SecurityProviderUtility.installBCProvider();

long currentTimeMillis = System.currentTimeMillis();
userId = "sample_user_" + currentTimeMillis;
userDn = String.format("inum=%s,ou=people,o=jans", currentTimeMillis);
}

@Produces
@ApplicationScoped
public StringEncrypter getStringEncrypter() throws EncryptionException {
FileConfiguration cryptoConfiguration = new FileConfiguration(".\\target\\conf\\salt");
String encodeSalt = cryptoConfiguration.getString("encodeSalt");

return StringEncrypter.instance(encodeSalt);
}

@Produces
@ApplicationScoped
public PersistenceEntryManager createPersistenceEntryManager(@Default PersistanceFactoryService persistanceFactoryService, @Default EncryptionService encryptionService) {
PersistenceConfiguration persistenceConfiguration = persistanceFactoryService.loadPersistenceConfiguration("jans.properties");
FileConfiguration persistenceConfig = persistenceConfiguration.getConfiguration();
Properties connectionProperties = encryptionService.decryptAllProperties(persistenceConfig.getProperties());

PersistenceEntryManagerFactory persistenceEntryManagerFactory = persistanceFactoryService.getPersistenceEntryManagerFactory(persistenceConfiguration);
PersistenceEntryManager persistenceEntryManager = persistenceEntryManagerFactory.createEntryManager(connectionProperties);
log.info("Created {}: {} with operation service: {}",
ApplicationFactory.PERSISTENCE_ENTRY_MANAGER_NAME, persistenceEntryManager,
persistenceEntryManager.getOperationService());


return persistenceEntryManager;
}

@Test
@Order(1)
public void testUserPersist() {
User user = new User();
user.setDn(userDn);
user.setUserId(userId);
persistenceEntryManager.persist(user);
}

@Test
@Order(2)
public void findUserAfterPersist() {
User user = persistenceEntryManager.find(User.class, userDn);

assertNotNull(user);
assertEquals(user.getUserId(), userId);
}

@Test
@Order(3)
public void addUserLegacyAuthenticators() {
User user = persistenceEntryManager.find(User.class, userDn);
assertNotNull(user);

assertNull(user.getAttributeValues("jansExtUid"));

List<String> jansExtUids = new ArrayList<>();
jansExtUids.add("type1:id1");
jansExtUids.add("type2:id2");

user.setAttribute("jansExtUid", jansExtUids);
persistenceEntryManager.merge(user);

assertEquals(user.getUserId(), userId);
}

@Test
@Order(4)
public void checkUserLegacyAuthenticators() {
User user = persistenceEntryManager.find(User.class, userDn);
assertNotNull(user);

// Access as custom attribute
List<String> jansExtUids = user.getAttributeValues("jansExtUid");
assertNotNull(jansExtUids);

assertEquals(jansExtUids.size(), 2);
assertEquals(jansExtUids.get(0), "type1:id1");
assertEquals(jansExtUids.get(1), "type2:id2");

// Access as generic attribute
assertNotNull(user.getExternalUid());
assertEquals(user.getExternalUid().length, 2);
assertEquals(user.getExternalUid()[0], "type1:id1");
assertEquals(user.getExternalUid()[1], "type2:id2");
}

@Test
@Order(5)
public void removeUserLegacyAuthenticators() {
User user = persistenceEntryManager.find(User.class, userDn);

// Access as custom attribute
user.removeAttribute("jansExtUid");
persistenceEntryManager.merge(user);
}

@Test
@Order(6)
public void checkAfterUserLegacyAuthenticatorsRemoval() {
User user = persistenceEntryManager.find(User.class, userDn);
assertNotNull(user);

// Access as custom attribute
List<String> jansExtUids = user.getAttributeValues("jansExtUid");
assertNull(jansExtUids);
}

@Test
@Order(7)
// Explicetly specify that attribute is multivalued
public void addUserLegacyAuthenticators2() {
User user = persistenceEntryManager.find(User.class, userDn);
assertNotNull(user);

assertNull(user.getAttributeValues("jansExtUid"));

List<Object> jansExtUids = new ArrayList<>();
jansExtUids.add("type1:id1");
jansExtUids.add("type2:id2");

user.setAttribute("jansExtUid", jansExtUids, true);
persistenceEntryManager.merge(user);

assertEquals(user.getUserId(), userId);
}

@Test
@Order(8)
public void checkUserLegacyAuthenticators2() {
User user = persistenceEntryManager.find(User.class, userDn);
assertNotNull(user);

// Access as custom attribute
List<String> jansExtUids = user.getAttributeValues("jansExtUid");
assertNotNull(jansExtUids);

assertEquals(jansExtUids.size(), 2);
assertEquals(jansExtUids.get(0), "type1:id1");
assertEquals(jansExtUids.get(1), "type2:id2");

// Access as generic attribute
assertNotNull(user.getExternalUid());
assertEquals(user.getExternalUid().length, 2);
assertEquals(user.getExternalUid()[0], "type1:id1");
assertEquals(user.getExternalUid()[1], "type2:id2");
}

@Test
@Order(9)
public void removeUserLegacyAuthenticators2() {
User user = persistenceEntryManager.find(User.class, userDn);

// Access as custom attribute
user.setAttribute("jansExtUid", (Object) null);
persistenceEntryManager.merge(user);
}

@Test
@Order(10)
public void checkAfterUserLegacyAuthenticatorsRemoval2() {
User user = persistenceEntryManager.find(User.class, userDn);
assertNotNull(user);

// Access as custom attribute
List<String> jansExtUids = user.getAttributeValues("jansExtUid");
assertNull(jansExtUids);

}

}
Loading

0 comments on commit 30f33c5

Please sign in to comment.