/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.camunda.bpm.engine.test.api.identity;

import static org.camunda.bpm.engine.authorization.Authorization.*;
import static org.camunda.bpm.engine.authorization.Resources.*;
import static org.camunda.bpm.engine.authorization.Permissions.*;

import java.util.List;

import org.camunda.bpm.engine.authorization.Authorization;
import org.camunda.bpm.engine.impl.cfg.auth.DefaultAuthorizationProvider;
import org.camunda.bpm.engine.impl.test.PluggableProcessEngineTestCase;

/**
 * <p>Test authorizations provided by {@link DefaultAuthorizationProvider}</p>
 * 
 * @author Daniel Meyer  
 *
 */
public class DefaultAuthorizationProviderTest extends PluggableProcessEngineTestCase {

  protected void setUp() throws Exception {
    // we are jonny
    identityService.setAuthenticatedUserId("jonny");
    // make sure we can do stuff:
    Authorization jonnyIsGod = authorizationService.createNewAuthorization(AUTH_TYPE_GRANT);
    jonnyIsGod.setUserId("jonny");
    jonnyIsGod.setResource(USER);
    jonnyIsGod.setResourceId(ANY);
    jonnyIsGod.addPermission(ALL);
    authorizationService.saveAuthorization(jonnyIsGod);        
    
    jonnyIsGod = authorizationService.createNewAuthorization(AUTH_TYPE_GRANT);
    jonnyIsGod.setUserId("jonny");
    jonnyIsGod.setResource(GROUP);
    jonnyIsGod.setResourceId(ANY);
    jonnyIsGod.addPermission(ALL);
    authorizationService.saveAuthorization(jonnyIsGod);
    
    jonnyIsGod = authorizationService.createNewAuthorization(AUTH_TYPE_GRANT);
    jonnyIsGod.setUserId("jonny");
    jonnyIsGod.setResource(AUTHORIZATION);
    jonnyIsGod.setResourceId(ANY);
    jonnyIsGod.addPermission(ALL);
    authorizationService.saveAuthorization(jonnyIsGod);
        
    // enable authorizations
    processEngineConfiguration.setAuthorizationEnabled(true);    
    super.setUp();
  }
  
  protected void tearDown() throws Exception {
    List<Authorization> jonnysAuths = authorizationService.createAuthorizationQuery().userIdIn("jonny").list();
    for (Authorization authorization : jonnysAuths) {
      authorizationService.deleteAuthorization(authorization.getId());
    }
    processEngineConfiguration.setAuthorizationEnabled(false);
    super.tearDown();
  }
  
  public void testCreateUser() {
    // initially there are no authorizations for jonny2:
    assertEquals(0, authorizationService.createAuthorizationQuery().userIdIn("jonny2").count());
    
    // create new user
    identityService.saveUser(identityService.newUser("jonny2"));
    
    // now there is an authorization for jonny2 which grants him ALL permissions on himself
    Authorization authorization = authorizationService.createAuthorizationQuery().userIdIn("jonny2").singleResult();
    assertNotNull(authorization);
    assertEquals(AUTH_TYPE_GRANT, authorization.getAuthorizationType());
    assertEquals(USER.resourceType(), authorization.getResourceType());
    assertEquals("jonny2", authorization.getResourceId());
    assertTrue(authorization.isPermissionGranted(ALL));
    
    // delete the user
    identityService.deleteUser("jonny2");
    
    // the authorization is deleted as well:
    assertEquals(0, authorizationService.createAuthorizationQuery().userIdIn("jonny2").count());
  }
  
  public void testCreateGroup() {
    // initially there are no authorizations for group "sales":
    assertEquals(0, authorizationService.createAuthorizationQuery().groupIdIn("sales").count());
    
    // create new group
    identityService.saveGroup(identityService.newGroup("sales"));
    
    // now there is an authorization for sales which grants all members READ permissions
    Authorization authorization = authorizationService.createAuthorizationQuery().groupIdIn("sales").singleResult();
    assertNotNull(authorization);
    assertEquals(AUTH_TYPE_GRANT, authorization.getAuthorizationType());
    assertEquals(GROUP.resourceType(), authorization.getResourceType());
    assertEquals("sales", authorization.getResourceId());
    assertTrue(authorization.isPermissionGranted(READ));
    
    // delete the group
    identityService.deleteGroup("sales");
    
    // the authorization is deleted as well:
    assertEquals(0, authorizationService.createAuthorizationQuery().groupIdIn("sales").count());
  }
  
}

@@ -173,18 +173,17 @@
 se at%0A *
- 
 %0A
+
  *      
@@ -227,17 +227,16 @@
 E-2.0%0A *
- 
 %0A * Unle
@@ -1118,17 +1118,16 @@
 %7D%3C/p%3E%0A *
- 
 %0A * @aut
@@ -1142,18 +1142,16 @@
 el Meyer
-  
 %0A *%0A */%0A
@@ -1676,29 +1676,17 @@
 yIsGod);
-        %0A    
+%0A
 %0A    jon
@@ -1953,21 +1953,17 @@
 IsGod);%0A
-    
 %0A
+
     jonn
@@ -2241,17 +2241,9 @@
 d);%0A
-        
 %0A
+
     
@@ -2328,20 +2328,16 @@
 d(true);
-    
 %0A    sup
@@ -2352,19 +2352,17 @@
 ();%0A  %7D%0A
-  
 %0A
+
   protec
@@ -2396,24 +2396,87 @@
 Exception %7B%0A
+    processEngineConfiguration.setAuthorizationEnabled(false);%0A
     List%3CAut
@@ -2710,71 +2710,8 @@
   %7D%0A
-    processEngineConfiguration.setAuthorizationEnabled(false);%0A
     
@@ -2728,27 +2728,25 @@
 Down();%0A  %7D%0A
-  
 %0A
+
   public voi
@@ -2912,37 +2912,33 @@
 ny2%22).count());%0A
-    
 %0A
+
     // create ne
@@ -3005,29 +3005,25 @@
 %22jonny2%22));%0A
-    
 %0A
+
     // now t
@@ -3509,29 +3509,25 @@
 nted(ALL));%0A
-    
 %0A
+
     // delet
@@ -3575,29 +3575,25 @@
 (%22jonny2%22);%0A
-    
 %0A
+
     // the a
@@ -3726,19 +3726,17 @@
 ));%0A  %7D%0A
-  
 %0A
+
   public
@@ -3918,29 +3918,25 @@
 ).count());%0A
-    
 %0A
+
     // creat
@@ -4009,29 +4009,25 @@
 (%22sales%22));%0A
-    
 %0A
+
     // now t
@@ -4515,21 +4515,17 @@
 READ));%0A
-    
 %0A
+
     // d
@@ -4586,13 +4586,9 @@
 %22);%0A
-    
 %0A
+
     
@@ -4729,13 +4729,11 @@
 ));%0A  %7D%0A
-  
 %0A%7D%0A

disable authorization before deleting auths