Clover icon

Magnolia REST Integration 2.0-rc1

  1. Project Clover database Mon Oct 30 2017 16:27:09 CET
  2. Package info.magnolia.rest.setup

File RemoveRoleFromUserTaskTest.java

 

Code metrics

0
25
4
1
120
67
4
0.16
6.25
4
1

Classes

Class Line # Actions
RemoveRoleFromUserTaskTest 63 25 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /**
2    * This file Copyright (c) 2017 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.rest.setup;
35   
36    import static org.junit.Assert.*;
37    import static org.mockito.Mockito.mock;
38   
39    import info.magnolia.cms.security.MgnlRoleManager;
40    import info.magnolia.cms.security.MgnlUserManager;
41    import info.magnolia.cms.security.SecuritySupport;
42    import info.magnolia.cms.security.SecuritySupportImpl;
43    import info.magnolia.cms.security.User;
44    import info.magnolia.context.MgnlContext;
45    import info.magnolia.init.MagnoliaConfigurationProperties;
46    import info.magnolia.jcr.util.NodeNameHelper;
47    import info.magnolia.jcr.util.NodeTypes;
48    import info.magnolia.module.delta.TaskExecutionException;
49    import info.magnolia.objectfactory.Components;
50    import info.magnolia.repository.RepositoryConstants;
51    import info.magnolia.test.ComponentsTestUtil;
52    import info.magnolia.test.RepositoryTestCase;
53   
54    import javax.jcr.AccessDeniedException;
55    import javax.jcr.Session;
56   
57    import org.junit.After;
58    import org.junit.Before;
59    import org.junit.Test;
60   
61    import com.google.common.collect.ImmutableMap;
62   
 
63    public class RemoveRoleFromUserTaskTest extends RepositoryTestCase {
64   
65    private MgnlUserManager userManager;
66    private MgnlRoleManager roleManager;
67   
 
68  1 toggle @Before
69    @Override
70    public void setUp() throws Exception {
71  1 super.setUp();
72  1 Session session = MgnlContext.getJCRSession(RepositoryConstants.USERS);
73  1 session.getRootNode().addNode("admin");
74  1 Session sessionRoles = MgnlContext.getJCRSession(RepositoryConstants.USER_ROLES);
75  1 sessionRoles.getRootNode().addNode("testFolder", NodeTypes.Folder.NAME);
76  1 NodeNameHelper nodeNameHelper = new NodeNameHelper(mock(MagnoliaConfigurationProperties.class));
77  1 userManager = new MgnlUserManager(nodeNameHelper);
78  1 roleManager = new MgnlRoleManager(nodeNameHelper) {
 
79  1 toggle @Override
80    protected void validateRoleName(String name) {
81    //do nothing please.
82    }
83    };
84  1 userManager.setRealmName("admin");
85  1 ComponentsTestUtil.setImplementation(SecuritySupport.class, SecuritySupportImpl.class);
86  1 SecuritySupportImpl securitySupport = (SecuritySupportImpl) Components.getComponent(SecuritySupport.class);
87  1 securitySupport.setRoleManager(roleManager);
88  1 securitySupport.setUserManagers(ImmutableMap.of("admin", userManager));
89    }
90   
 
91  1 toggle @After
92    @Override
93    public void tearDown() throws Exception {
94  1 super.tearDown();
95  1 ComponentsTestUtil.clear();
96  1 MgnlContext.setInstance(null);
97    }
98   
 
99  1 toggle @Test
100    public void removeRoleFromUser() throws AccessDeniedException, TaskExecutionException {
101    // GIVEN
102  1 User user = userManager.createUser("test", "test");
103  1 roleManager.createRole("rest-editor");
104  1 userManager.addRole(user, "rest-editor");
105   
106    // We need to get the updated instance of the user (see MAGNOLIA-7028)
107  1 user = userManager.getUser("test");
108   
109  1 assertTrue(user.hasRole("rest-editor"));
110  1 RemoveRoleFromUserTask task = new RemoveRoleFromUserTask("Test Task", "test", "rest-editor");
111    // WHEN
112  1 task.execute(null);
113   
114    // We need to get the updated instance of the user (see MAGNOLIA-7028)
115  1 user = userManager.getUser("test");
116   
117    // THEN
118  1 assertFalse(user.hasRole("rest-editor"));
119    }
120    }