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 RestIntegrationModuleVersionHandlerTest.java

 

Code metrics

0
65
7
1
214
126
7
0.11
9.29
7
1

Classes

Class Line # Actions
RestIntegrationModuleVersionHandlerTest 74 65 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 3 tests. .

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 info.magnolia.test.hamcrest.NodeMatchers.*;
37    import static org.hamcrest.CoreMatchers.allOf;
38    import static org.junit.Assert.*;
39    import static org.mockito.Mockito.mock;
40   
41    import info.magnolia.cms.security.MgnlGroupManager;
42    import info.magnolia.cms.security.MgnlRoleManager;
43    import info.magnolia.cms.security.MgnlUserManager;
44    import info.magnolia.cms.security.Realm;
45    import info.magnolia.cms.security.SecuritySupport;
46    import info.magnolia.cms.security.SecuritySupportImpl;
47    import info.magnolia.cms.security.User;
48    import info.magnolia.context.MgnlContext;
49    import info.magnolia.init.MagnoliaConfigurationProperties;
50    import info.magnolia.jcr.util.NodeNameHelper;
51    import info.magnolia.jcr.util.NodeTypes;
52    import info.magnolia.jcr.util.NodeUtil;
53    import info.magnolia.jcr.util.PropertyUtil;
54    import info.magnolia.module.ModuleManagementException;
55    import info.magnolia.module.ModuleVersionHandler;
56    import info.magnolia.module.ModuleVersionHandlerTestCase;
57    import info.magnolia.module.model.Version;
58    import info.magnolia.repository.RepositoryConstants;
59    import info.magnolia.test.ComponentsTestUtil;
60   
61    import java.util.Arrays;
62    import java.util.List;
63   
64    import javax.jcr.Node;
65    import javax.jcr.RepositoryException;
66    import javax.jcr.Session;
67   
68    import org.junit.Before;
69    import org.junit.Test;
70   
71    /**
72    * Tests for the module version handler ({@link RestIntegrationModuleVersionHandler}).
73    */
 
74    public class RestIntegrationModuleVersionHandlerTest extends ModuleVersionHandlerTestCase {
75   
76    private SecuritySupportImpl securitySupport;
77   
 
78  3 toggle @Override
79    protected String getModuleDescriptorPath() {
80  3 return "/META-INF/magnolia/rest-integration.xml";
81    }
82   
 
83  3 toggle @Override
84    protected List<String> getModuleDescriptorPathsForTests() {
85  3 return Arrays.asList(
86    "/META-INF/magnolia/core.xml"
87    );
88    }
89   
 
90  3 toggle @Override
91    protected ModuleVersionHandler newModuleVersionHandlerForTests() {
92  3 return new RestIntegrationModuleVersionHandler();
93    }
94   
 
95  3 toggle @Override
96    @Before
97    public void setUp() throws Exception {
98  3 super.setUp();
99   
100  3 Session session = MgnlContext.getJCRSession(RepositoryConstants.CONFIG);
101   
102    // Creating servlet node
103  3 Node server = NodeUtil.createPath(session.getRootNode(), "/server", NodeTypes.ContentNode.NAME);
104  3 server.setProperty("admin", "");
105  3 NodeUtil.createPath(server, "/filters/servlets", NodeTypes.ContentNode.NAME);
106   
107  3 Session userSession = MgnlContext.getJCRSession(RepositoryConstants.USERS);
108  3 NodeUtil.createPath(userSession.getRootNode(), "/system", NodeTypes.Folder.NAME);
109   
110  3 Session roleSession = MgnlContext.getJCRSession(RepositoryConstants.USER_ROLES);
111  3 NodeUtil.createPath(roleSession.getRootNode(), "/security-base", NodeTypes.Role.NAME);
112   
113  3 NodeNameHelper nodeNameHelper = new NodeNameHelper(mock(MagnoliaConfigurationProperties.class));
114   
115  3 MgnlUserManager mgnlUserManager = new MgnlUserManager(nodeNameHelper);
116  3 mgnlUserManager.setRealmName(Realm.REALM_ALL.getName());
117  3 mgnlUserManager.setAllowCrossRealmDuplicateNames(true); // To prevent check whether user already exists
118   
119    // To prevent: WARN info.magnolia.module.InstallContextImpl: > User "superuser" not found, can't add him/her to the "publishers" group.
120  3 mgnlUserManager.createUser("/system", "superuser", "superuser");
121  3 mgnlUserManager.createUser("/system", "anonymous", "");
122   
123    // Security Support
124  3 securitySupport = new SecuritySupportImpl();
125  3 securitySupport.addUserManager(Realm.REALM_ALL.getName(), mgnlUserManager);
126  3 securitySupport.setRoleManager(new MgnlRoleManager(nodeNameHelper));
127  3 securitySupport.setGroupManager(new MgnlGroupManager(nodeNameHelper));
128  3 ComponentsTestUtil.setInstance(SecuritySupport.class, securitySupport);
129    }
130   
 
131  1 toggle @Test
132    public void basicInstallSuperuserHasRoleRest() throws Exception {
133    // GIVEN
134    // Create the role (which is bootstrapped in the MVH)
135  1 Session rolesSession = MgnlContext.getJCRSession(RepositoryConstants.USER_ROLES);
136  1 User superuser = securitySupport.getUserManager().getUser("superuser");
137  1 User anonymous = securitySupport.getUserManager().getUser("anonymous");
138   
139  1 assertFalse(superuser.hasRole("rest-admin"));
140  1 assertFalse(anonymous.hasRole("rest-anonymous"));
141   
142    // WHEN
143  1 executeUpdatesAsIfTheCurrentlyInstalledVersionWas(null);
144   
145    // We need to get the updated instance of the user (see MAGNOLIA-7028)
146  1 superuser = securitySupport.getUserManager().getUser("superuser");
147  1 anonymous = securitySupport.getUserManager().getUser("anonymous");
148   
149    // THEN
150  1 assertThat(rolesSession.getNode("/rest-editor"), nodeName("rest-editor"));
151  1 assertThat(rolesSession.getNode("/rest-admin"), nodeName("rest-admin"));
152  1 assertThat(rolesSession.getNode("/rest-anonymous"), nodeName("rest-anonymous"));
153   
154  1 assertTrue(superuser.hasRole("rest-admin"));
155  1 assertTrue(anonymous.hasRole("rest-anonymous"));
156    }
157   
 
158  1 toggle @Test
159    public void updateFrom123ChangeRestEditorRoleToRestAdminRole() throws RepositoryException, ModuleManagementException {
160    // GIVEN
161  1 Session rolesSession = MgnlContext.getJCRSession(RepositoryConstants.USER_ROLES);
162  1 NodeUtil.createPath(rolesSession.getRootNode(), "/rest", NodeTypes.Role.NAME);
163  1 User superuser = securitySupport.getUserManager().getUser("superuser");
164  1 User anonymous = securitySupport.getUserManager().getUser("anonymous");
165   
166  1 assertFalse(superuser.hasRole("rest-admin"));
167  1 assertFalse(anonymous.hasRole("rest-anonymous"));
168   
169    // WHEN
170  1 executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("1.2.3"));
171   
172    // We need to get the updated instance of the user (see MAGNOLIA-7028)
173  1 superuser = securitySupport.getUserManager().getUser("superuser");
174  1 anonymous = securitySupport.getUserManager().getUser("anonymous");
175   
176    // THEN
177  1 assertThat(rolesSession.getNode("/rest-editor"), nodeName("rest-editor"));
178  1 assertThat(rolesSession.getNode("/rest-admin"), nodeName("rest-admin"));
179  1 assertThat(rolesSession.getNode("/rest-anonymous"), nodeName("rest-anonymous"));
180   
181  1 assertTrue(superuser.hasRole("rest-admin"));
182  1 assertFalse(superuser.hasRole("rest-editor"));
183  1 assertFalse(superuser.hasRole("rest-anonymous"));
184  1 assertTrue(anonymous.hasRole("rest-anonymous"));
185  1 assertFalse(anonymous.hasRole("rest-admin"));
186    }
187   
 
188  1 toggle @Test
189    public void updateFrom12UpdatesRestEditorRole() throws Exception {
190    // GIVEN
191  1 Session rolesSession = MgnlContext.getJCRSession(RepositoryConstants.USER_ROLES);
192  1 Node restNode = NodeUtil.createPath(rolesSession.getRootNode(), "/rest", NodeTypes.Role.NAME);
193  1 PropertyUtil.setProperty(restNode, "description", "test");
194  1 PropertyUtil.setProperty(restNode, "title", "test");
195   
196  1 Node aclRole = NodeUtil.createPath(restNode, "acl_userroles/0", NodeTypes.ContentNode.NAME);
197  1 Node aclUri = NodeUtil.createPath(restNode, "acl_uri/0", NodeTypes.ContentNode.NAME);
198   
199  1 aclRole.setProperty("path", "/rest");
200  1 aclUri.setProperty("path", "/.rest*");
201   
202    // WHEN
203  1 executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("1.2.3"));
204   
205    // THEN
206  1 assertThat(restNode, allOf(
207    nodeName("rest-editor"),
208    hasProperty("description", "This role grants GET/POST permissions to REST services APIs (nodes, properties), for a limited set of workspaces."),
209    hasProperty("title", "REST Editor")));
210   
211  1 assertThat(aclRole, hasProperty("path", "/rest-editor"));
212  1 assertThat(aclUri, hasProperty("path", "/.rest/*"));
213    }
214    }