CCM NG/ccm-cms: Extended ContentTypeRepositoryTest to check if annotations for authorisation work.

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4366 8810af33-2d31-482b-a856-94f89814c4df
pull/2/head
jensp 2016-10-09 07:49:25 +00:00
parent 93ae630f41
commit 0cccf9dec4
5 changed files with 90 additions and 20 deletions

View File

@ -25,11 +25,13 @@ import static org.librecms.CmsConstants.*;
import org.libreccm.core.CcmObject; import org.libreccm.core.CcmObject;
import org.libreccm.l10n.LocalizedString; import org.libreccm.l10n.LocalizedString;
import org.libreccm.security.InheritsPermissions;
import org.libreccm.workflow.WorkflowTemplate; import org.libreccm.workflow.WorkflowTemplate;
import org.librecms.lifecycle.LifecycleDefinition; import org.librecms.lifecycle.LifecycleDefinition;
import java.io.Serializable; import java.io.Serializable;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import javax.persistence.AssociationOverride; import javax.persistence.AssociationOverride;
import javax.persistence.Column; import javax.persistence.Column;
@ -69,7 +71,8 @@ import javax.persistence.Table;
+ "WHERE i.contentType = :type" + "WHERE i.contentType = :type"
) )
}) })
public class ContentType extends CcmObject implements Serializable { public class ContentType extends CcmObject implements InheritsPermissions,
Serializable {
private static final long serialVersionUID = -2708659750560382851L; private static final long serialVersionUID = -2708659750560382851L;
@ -191,6 +194,11 @@ public class ContentType extends CcmObject implements Serializable {
this.defaultWorkflow = defaultWorkflow; this.defaultWorkflow = defaultWorkflow;
} }
@Override
public Optional<CcmObject> getParent() {
return Optional.of(contentSection);
}
@Override @Override
public int hashCode() { public int hashCode() {
int hash = super.hashCode(); int hash = super.hashCode();

View File

@ -24,10 +24,14 @@ import javax.persistence.OneToOne;
import javax.persistence.Table; import javax.persistence.Table;
import org.libreccm.categorization.Category; import org.libreccm.categorization.Category;
import org.libreccm.core.CcmObject;
import org.libreccm.security.InheritsPermissions;
import java.io.Serializable;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.persistence.Column; import javax.persistence.Column;
@ -55,7 +59,8 @@ import static org.librecms.CmsConstants.*;
name = "Folder.findByName", name = "Folder.findByName",
query = "SELECT f FROM Folder f WHERE f.name = :name") query = "SELECT f FROM Folder f WHERE f.name = :name")
}) })
public class Folder extends Category { public class Folder extends Category implements InheritsPermissions,
Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@ -88,6 +93,15 @@ public class Folder extends Category {
this.type = type; this.type = type;
} }
@Override
public Optional<CcmObject> getParent() {
if (getParentFolder() == null) {
return Optional.of(section);
} else {
return Optional.of(getParentFolder());
}
}
/** /**
* A convenient method for getting all sub folders of folder. * A convenient method for getting all sub folders of folder.
* *

View File

@ -20,6 +20,7 @@ package org.librecms.contentsection;
import static org.libreccm.testutils.DependenciesHelpers.*; import static org.libreccm.testutils.DependenciesHelpers.*;
import org.apache.shiro.authz.UnauthorizedException;
import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.ShouldThrowException; import org.jboss.arquillian.container.test.api.ShouldThrowException;
import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.junit.Arquillian;
@ -31,7 +32,6 @@ import org.jboss.arquillian.persistence.UsingDataSet;
import org.jboss.arquillian.transaction.api.annotation.TransactionMode; import org.jboss.arquillian.transaction.api.annotation.TransactionMode;
import org.jboss.arquillian.transaction.api.annotation.Transactional; import org.jboss.arquillian.transaction.api.annotation.Transactional;
import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive; import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.After; import org.junit.After;
import org.junit.AfterClass; import org.junit.AfterClass;
@ -39,6 +39,7 @@ import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.libreccm.security.Shiro;
import org.libreccm.tests.categories.IntegrationTest; import org.libreccm.tests.categories.IntegrationTest;
import org.librecms.contenttypes.Article; import org.librecms.contenttypes.Article;
import org.librecms.contenttypes.News; import org.librecms.contenttypes.News;
@ -69,6 +70,9 @@ public class ContentTypeRepositoryTest {
@Inject @Inject
private ContentSectionRepository contentSectionRepo; private ContentSectionRepository contentSectionRepo;
@Inject
private Shiro shiro;
public ContentTypeRepositoryTest() { public ContentTypeRepositoryTest() {
} }
@ -139,8 +143,10 @@ public class ContentTypeRepositoryTest {
.addAsLibraries(getCcmCoreDependencies()) .addAsLibraries(getCcmCoreDependencies())
.addAsResource("test-persistence.xml", .addAsResource("test-persistence.xml",
"META-INF/persistence.xml") "META-INF/persistence.xml")
.addAsWebInfResource("test-web.xml", "WEB-INF/web.xml") .addAsWebInfResource("test-web.xml", "web.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "WEB-INF/beans.xml"); .addAsResource("configs/shiro.ini", "shiro.ini")
.addAsResource("META-INF/beans.xml", "META-INF/beans.xml");
//.addAsWebInfResource(EmptyAsset.INSTANCE, "WEB-INF/beans.xml");
} }
/** /**
@ -151,6 +157,7 @@ public class ContentTypeRepositoryTest {
public void checkInjection() { public void checkInjection() {
assertThat(contentTypeRepo, is(not(nullValue()))); assertThat(contentTypeRepo, is(not(nullValue())));
assertThat(contentSectionRepo, is(not(nullValue()))); assertThat(contentSectionRepo, is(not(nullValue())));
assertThat(shiro, is(not(nullValue())));
} }
/** /**
@ -369,6 +376,26 @@ public class ContentTypeRepositoryTest {
.findByContentSectionAndClass(section, News.class); .findByContentSectionAndClass(section, News.class);
assertThat(newsType.isPresent(), is(true)); assertThat(newsType.isPresent(), is(true));
shiro.getSystemUser()
.execute(() -> contentTypeRepo.delete(newsType.get()));
}
/**
* Verifies that an unused content type can be deleted.
*/
@Test(expected = UnauthorizedException.class)
@InSequence(2000)
@UsingDataSet("datasets/org/librecms/contentsection/"
+ "ContentTypeRepositoryTest/data.xml")
@ShouldMatchDataSet("datasets/org/librecms/contentsection/"
+ "ContentTypeRepositoryTest/data.xml")
@ShouldThrowException(UnauthorizedException.class)
public void deleteUnusedContentTypeUnauthorized() {
final ContentSection section = contentSectionRepo.findById(-1001L);
final Optional<ContentType> newsType = contentTypeRepo
.findByContentSectionAndClass(section, News.class);
assertThat(newsType.isPresent(), is(true));
contentTypeRepo.delete(newsType.get()); contentTypeRepo.delete(newsType.get());
} }
@ -377,7 +404,7 @@ public class ContentTypeRepositoryTest {
* Verifies that content types which are in use can't be deleted. * Verifies that content types which are in use can't be deleted.
*/ */
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
@InSequence(2000) @InSequence(2200)
@UsingDataSet("datasets/org/librecms/contentsection/" @UsingDataSet("datasets/org/librecms/contentsection/"
+ "ContentTypeRepositoryTest/data.xml") + "ContentTypeRepositoryTest/data.xml")
@ShouldMatchDataSet("datasets/org/librecms/contentsection/" @ShouldMatchDataSet("datasets/org/librecms/contentsection/"
@ -389,7 +416,8 @@ public class ContentTypeRepositoryTest {
.findByContentSectionAndClass(section, Article.class); .findByContentSectionAndClass(section, Article.class);
assertThat(articleType.isPresent(), is(true)); assertThat(articleType.isPresent(), is(true));
contentTypeRepo.delete(articleType.get()); shiro.getSystemUser()
.execute(() -> contentTypeRepo.delete(articleType.get()));
} }
} }

View File

@ -146,6 +146,18 @@
category_index="false" category_index="false"
type="folder" /> type="folder" />
<ccm_core.parties party_id="-8001"
name = "public-user" />
<ccm_core.users party_id="-8001"
given_name="public"
family_name="user"
email_address="public-user@localhost"
banned="false"
bouncing="false"
verified="true"
password_reset_required="false" />
<ccm_core.ccm_roles role_id="-6001" <ccm_core.ccm_roles role_id="-6001"
name="info_alert_recipient" /> name="info_alert_recipient" />
<ccm_core.ccm_roles role_id="-6002" <ccm_core.ccm_roles role_id="-6002"

View File

@ -274,10 +274,12 @@ public class OneTimeAuthManagerTest {
final User user = userRepository.findByName("jdoe"); final User user = userRepository.findByName("jdoe");
shiro.getSystemUser().execute( shiro.getSystemUser().execute(
() -> assertThat( () -> {
oneTimeAuthManager.validTokenExistsForUser( assertThat(
user, OneTimeAuthTokenPurpose.EMAIL_VERIFICATION), oneTimeAuthManager.validTokenExistsForUser(
is(true))); user, OneTimeAuthTokenPurpose.EMAIL_VERIFICATION),
is(true));
});
} }
@Test @Test
@ -288,10 +290,12 @@ public class OneTimeAuthManagerTest {
final User user = userRepository.findByName("mmuster"); final User user = userRepository.findByName("mmuster");
shiro.getSystemUser().execute( shiro.getSystemUser().execute(
() -> assertThat( () -> {
oneTimeAuthManager.validTokenExistsForUser( assertThat(
user, OneTimeAuthTokenPurpose.EMAIL_VERIFICATION), oneTimeAuthManager.validTokenExistsForUser(
is(false))); user, OneTimeAuthTokenPurpose.EMAIL_VERIFICATION),
is(false));
});
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
@ -338,8 +342,10 @@ public class OneTimeAuthManagerTest {
}); });
assertThat(result, is(not(empty()))); assertThat(result, is(not(empty())));
shiro.getSystemUser().execute( shiro.getSystemUser().execute(
() -> assertThat(oneTimeAuthManager.isValid(result.get(0)), () -> {
is(true))); assertThat(oneTimeAuthManager.isValid(result.get(0)),
is(true));
});
} }
@Test @Test
@ -363,7 +369,9 @@ public class OneTimeAuthManagerTest {
token.setValidUntil(Date.from(date.toInstant(ZoneOffset.UTC))); token.setValidUntil(Date.from(date.toInstant(ZoneOffset.UTC)));
shiro.getSystemUser().execute( shiro.getSystemUser().execute(
() -> assertThat(oneTimeAuthManager.isValid(token), is(false))); () -> {
assertThat(oneTimeAuthManager.isValid(token), is(false));
});
} }