package org.minimalj.security;

import java.util.Collections;

import org.junit.Assert;
import org.junit.Test;
import org.minimalj.backend.repository.ReadEntityTransaction;
import org.minimalj.security.permissiontest.TestEntityA;
import org.minimalj.security.permissiontest.TestEntityB;
import org.minimalj.security.permissiontest.TestEntityBView;
import org.minimalj.security.permissiontest.pkgrole.TestEntityG;
import org.minimalj.transaction.TransactionAnnotations;

public class SecurityTest {

	@Test
	public void testEntityWithoutRole() throws Exception {
		Assert.assertNull("ReadTransaction without Role annotation to class or package should need no role",
				TransactionAnnotations.getRoles(new ReadEntityTransaction<>(TestEntityA.class, 1)));
	}

	@Test
	public void testEntityWithSingleRole() throws Exception {
		Subject subject = new Subject();
		subject.getRoles().add("RoleA");
		Subject.setCurrent(subject);
		try {
			Authorization.check(new ReadEntityTransaction<>(TestEntityB.class, 1));
			Assert.fail("RoleA should not allow access to TestEntityB");
		} catch (Exception e) {
			// expected
		}
		subject.getRoles().add("RoleB");
		Authorization.check(new ReadEntityTransaction<>(TestEntityB.class, 1));
	}

	@Test
	public void testEntityView() throws Exception {
		Subject subject = new Subject();
		subject.getRoles().add("RoleA");
		Subject.setCurrent(subject);
		Authorization.check(new ReadEntityTransaction<>(TestEntityBView.class, 1));
		subject.getRoles().remove("RoleA");
		subject.getRoles().add("RoleB");
		try {
			Authorization.check(new ReadEntityTransaction<>(TestEntityBView.class, 1));
			Assert.fail("RoleB should not allow access to TestEntityBView");
		} catch (Exception e) {
			// expected
		}
	}

	//
	
	public void testNotAuthorized() {
		Assert.assertFalse(Authorization.isAllowed(Collections.emptyList(), new ReadEntityTransaction<TestEntityG>(TestEntityG.class, 1)));
	}

	@Test
	public void testGranted() {
		Assert.assertTrue(Authorization.isAllowed(Collections.singletonList("pkgRole"), new ReadEntityTransaction<TestEntityG>(TestEntityG.class, 1)));
	}

	@Test
	public void testGrantedReadOnPackage() {
		Assert.assertTrue(Authorization.isAllowed(Collections.singletonList("pkgRole"), new ReadEntityTransaction<TestEntityG>(TestEntityG.class, 1)));
	}

}

@@ -403,17 +403,17 @@
 stEntity
-G
+H
 ;%0Aimport
@@ -1868,35 +1868,24 @@
 Transaction%3C
-TestEntityG
 %3E(TestEntity
@@ -1876,33 +1876,33 @@
 ion%3C%3E(TestEntity
-G
+H
 .class, 1)));%0A%09%7D
@@ -1927,21 +1927,24 @@
 oid test
-Grant
+Authoriz
 ed() %7B%0A%09
@@ -2005,35 +2005,37 @@
 .singletonList(%22
-pkg
+Class
 Role%22), new Read
@@ -2048,35 +2048,24 @@
 Transaction%3C
-TestEntityG
 %3E(TestEntity
@@ -2056,33 +2056,33 @@
 ion%3C%3E(TestEntity
-G
+H
 .class, 1)));%0A%09%7D
@@ -2111,28 +2111,39 @@
 test
-GrantedReadOnPackage
+AuthorizationOnPackageOverruled
 () %7B
@@ -2150,35 +2150,36 @@
 %0A%09%09Assert.assert
-Tru
+Fals
 e(Authorization.
@@ -2252,27 +2252,16 @@
 saction%3C
-TestEntityG
 %3E(TestEn
@@ -2268,9 +2268,9 @@
 tity
-G
+H
 .cla

make the test more meaningful