New file |
0,0 → 1,154 |
package pt.estgp.estgweb.domain.test; |
|
import com.owlike.genson.TransformationException; |
import junit.framework.TestCase; |
import org.json.JSONException; |
import pt.estgp.estgweb.Globals; |
import pt.estgp.estgweb.domain.*; |
import pt.estgp.estgweb.services.pageContent.PageContentAccessPolicyAcl; |
import pt.estgp.estgweb.services.pageContent.PageContentService; |
|
import java.io.IOException; |
import java.util.ArrayList; |
import java.util.Arrays; |
|
/** |
* Created by jorgemachado on 04/01/17. |
*/ |
public class TestPageContentAclSimple extends TestCase |
{ |
|
/******************************************** |
* |
* |
* MAIS TESTES NO PACOTE DO SERVICO PAGECONTENT |
* |
* |
********************************************/ |
|
|
public static UserSessionImpl uSessionJonhDoe ; |
public static UserSessionImpl uSessionTeacher ; |
public static UserSessionImpl uSessionStudent ; |
|
static |
{ |
uSessionJonhDoe = new UserSessionImpl(); |
uSessionJonhDoe.setUser(new UserImpl()); |
uSessionJonhDoe.getUser().setUsername("jonhdoe"); |
uSessionJonhDoe.setCookie("jonhdoeCookie"); |
|
uSessionTeacher = new UserSessionImpl(); |
uSessionTeacher.setUser(new TeacherImpl()); |
uSessionTeacher.getUser().setUsername("teacher name"); |
uSessionTeacher.setCookie("uSessionTeacherCookie"); |
|
uSessionStudent = new UserSessionImpl(); |
uSessionStudent.setUser(new StudentImpl()); |
uSessionStudent.getUser().setUsername("student name"); |
uSessionStudent.setCookie("studentCookie"); |
} |
|
public static PageContentImpl.Acl aclAnonimaRead; |
public static PageContentImpl.Acl aclAnonimaDenyRead; |
public static PageContentImpl.Acl aclUser_authenticaded_Read; |
public static PageContentImpl.Acl aclUser_authenticaded_DenyRead; |
public static PageContentImpl.Acl aclUser_jonhdoe_DenyRead; |
public static PageContentImpl.Acl aclUser_jonhdoe_Read; |
public static PageContentImpl.Acl aclUser_teacherRole_DenyRead; |
public static PageContentImpl.Acl aclUser_teacherRole_Read; |
|
static |
{ |
aclAnonimaRead = new PageContentImpl.Acl(); |
aclAnonimaRead.setTypeEnum(PageContentImpl.Acl.Type.ANONYMOUS); |
aclAnonimaRead.setOperationEnum(PageContentImpl.Acl.Operation.READ_CONTENTS); |
aclAnonimaRead.setAccessEnum(PageContentImpl.Acl.Access.PERMIT); |
|
aclAnonimaDenyRead = new PageContentImpl.Acl(); |
aclAnonimaDenyRead.setTypeEnum(PageContentImpl.Acl.Type.ANONYMOUS); |
aclAnonimaDenyRead.setOperationEnum(PageContentImpl.Acl.Operation.READ_CONTENTS); |
aclAnonimaDenyRead.setAccessEnum(PageContentImpl.Acl.Access.DENY); |
|
|
aclUser_authenticaded_Read = new PageContentImpl.Acl(); |
aclUser_authenticaded_Read.setTypeEnum(PageContentImpl.Acl.Type.AUTHENTICATED); |
aclUser_authenticaded_Read.setOperationEnum(PageContentImpl.Acl.Operation.READ_CONTENTS); |
aclUser_authenticaded_Read.setAccessEnum(PageContentImpl.Acl.Access.PERMIT); |
|
aclUser_authenticaded_DenyRead = new PageContentImpl.Acl(); |
aclUser_authenticaded_DenyRead.setTypeEnum(PageContentImpl.Acl.Type.AUTHENTICATED); |
aclUser_authenticaded_DenyRead.setOperationEnum(PageContentImpl.Acl.Operation.READ_CONTENTS); |
aclUser_authenticaded_DenyRead.setAccessEnum(PageContentImpl.Acl.Access.DENY); |
|
|
aclUser_jonhdoe_DenyRead = new PageContentImpl.Acl(); |
aclUser_jonhdoe_DenyRead.setTypeEnum(PageContentImpl.Acl.Type.USER); |
aclUser_jonhdoe_DenyRead.setOperationEnum(PageContentImpl.Acl.Operation.READ_CONTENTS); |
aclUser_jonhdoe_DenyRead.setAccessEnum(PageContentImpl.Acl.Access.DENY); |
aclUser_jonhdoe_DenyRead.setId("jonhdoe"); |
|
aclUser_jonhdoe_Read = new PageContentImpl.Acl(); |
aclUser_jonhdoe_Read.setTypeEnum(PageContentImpl.Acl.Type.USER); |
aclUser_jonhdoe_Read.setOperationEnum(PageContentImpl.Acl.Operation.READ_CONTENTS); |
aclUser_jonhdoe_Read.setAccessEnum(PageContentImpl.Acl.Access.PERMIT); |
aclUser_jonhdoe_Read.setId("jonhdoe"); |
|
|
aclUser_teacherRole_DenyRead = new PageContentImpl.Acl(); |
aclUser_teacherRole_DenyRead.setTypeEnum(PageContentImpl.Acl.Type.ROLE); |
aclUser_teacherRole_DenyRead.setOperationEnum(PageContentImpl.Acl.Operation.READ_CONTENTS); |
aclUser_teacherRole_DenyRead.setAccessEnum(PageContentImpl.Acl.Access.DENY); |
aclUser_teacherRole_DenyRead.setId("teacher"); |
|
aclUser_teacherRole_Read = new PageContentImpl.Acl(); |
aclUser_teacherRole_Read.setTypeEnum(PageContentImpl.Acl.Type.ROLE); |
aclUser_teacherRole_Read.setOperationEnum(PageContentImpl.Acl.Operation.READ_CONTENTS); |
aclUser_teacherRole_Read.setAccessEnum(PageContentImpl.Acl.Access.PERMIT); |
aclUser_teacherRole_Read.setId("teacher"); |
} |
|
@Override |
protected void setUp() throws Exception { |
super.setUp(); |
|
|
|
//to check if baco auto gives role teacher and student to these users respectively |
assertTrue(uSessionTeacher.getUser().hasRole(Globals.TEACHER_ROLE)); |
assertTrue(uSessionStudent.getUser().hasRole(Globals.STUDENT_ROLE)); |
|
assertFalse(uSessionJonhDoe.getUser().hasRole(Globals.TEACHER_ROLE)); |
assertFalse(uSessionJonhDoe.getUser().hasRole(Globals.STUDENT_ROLE)); |
|
|
|
} |
|
public void testAclUser() throws IOException, TransformationException, JSONException |
{ |
PageSection psAnonimousDenyAuthenticatedRead = new PageSectionImpl(); |
psAnonimousDenyAuthenticatedRead.setAccessPolicyClass(PageContentAccessPolicyAcl.class.getName()); |
psAnonimousDenyAuthenticatedRead.setAclList(getArrayList(new PageContentImpl.Acl[]{aclAnonimaDenyRead, aclUser_authenticaded_Read})); |
|
//clearences not initialized |
assertFalse(psAnonimousDenyAuthenticatedRead.isClear(uSessionJonhDoe, PageContentImpl.Acl.Operation.READ_CONTENTS)); |
new PageContentService().computeUserSessionClearancesTree(psAnonimousDenyAuthenticatedRead,uSessionJonhDoe); |
//clearences initialized |
assertTrue(psAnonimousDenyAuthenticatedRead.isClear(uSessionJonhDoe, PageContentImpl.Acl.Operation.READ_CONTENTS)); |
//clearences initialized with other guy |
assertFalse(psAnonimousDenyAuthenticatedRead.isClear(uSessionStudent, PageContentImpl.Acl.Operation.READ_CONTENTS)); |
|
|
assertFalse(psAnonimousDenyAuthenticatedRead.isClear(uSessionJonhDoe, PageContentImpl.Acl.Operation.ADD_FILES)); |
|
assertFalse(psAnonimousDenyAuthenticatedRead.isClear(uSessionJonhDoe, PageContentImpl.Acl.Operation.ADD_MODULES)); |
assertFalse(psAnonimousDenyAuthenticatedRead.isClear(uSessionJonhDoe, PageContentImpl.Acl.Operation.ADD_SECTIONS)); |
assertFalse(psAnonimousDenyAuthenticatedRead.isClear(uSessionJonhDoe, PageContentImpl.Acl.Operation.ADD_URLS)); |
} |
|
public static ArrayList<PageContentImpl.Acl> getArrayList(PageContentImpl.Acl[] acls) |
{ |
return new ArrayList<PageContentImpl.Acl>(Arrays.asList(acls)); |
} |
} |