Clover icon

Magnolia REST Services 2.1.3

  1. Project Clover database Fri Oct 25 2019 12:35:42 CEST
  2. Package info.magnolia.rest.setup

File RestServicesModuleVersionHandlerTest.java

 

Code metrics

0
27
7
1
146
74
7
0.26
3.86
7
1

Classes

Class Line # Actions
RestServicesModuleVersionHandlerTest 63 27 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /**
2    * This file Copyright (c) 2013-2018 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.assertEquals;
37   
38    import info.magnolia.cms.security.MgnlRoleManager;
39    import info.magnolia.cms.security.SecuritySupport;
40    import info.magnolia.cms.security.SecuritySupportImpl;
41    import info.magnolia.context.MgnlContext;
42    import info.magnolia.jcr.util.NodeTypes;
43    import info.magnolia.jcr.util.NodeUtil;
44    import info.magnolia.module.ModuleVersionHandler;
45    import info.magnolia.module.ModuleVersionHandlerTestCase;
46    import info.magnolia.module.model.Version;
47    import info.magnolia.repository.RepositoryConstants;
48    import info.magnolia.rest.service.setup.RestServicesModuleVersionHandler;
49    import info.magnolia.test.ComponentsTestUtil;
50   
51    import java.util.Arrays;
52    import java.util.List;
53   
54    import javax.jcr.Node;
55    import javax.jcr.Session;
56   
57    import org.junit.Before;
58    import org.junit.Test;
59   
60    /**
61    * Test for {@link info.magnolia.rest.service.setup.RestServicesModuleVersionHandler}.
62    */
 
63    public class RestServicesModuleVersionHandlerTest extends ModuleVersionHandlerTestCase {
64   
65    private Session session;
66   
 
67  3 toggle @Override
68    protected String getModuleDescriptorPath() {
69  3 return "/META-INF/magnolia/rest-services.xml";
70    }
71   
 
72  3 toggle @Override
73    protected List<String> getModuleDescriptorPathsForTests() {
74  3 return Arrays.asList(
75    "/META-INF/magnolia/core.xml"
76    );
77    }
78   
 
79  3 toggle @Override
80    protected ModuleVersionHandler newModuleVersionHandlerForTests() {
81  3 return new RestServicesModuleVersionHandler();
82    }
83   
 
84  3 toggle @Override
85    @Before
86    public void setUp() throws Exception {
87  3 super.setUp();
88   
89  3 session = MgnlContext.getJCRSession(RepositoryConstants.CONFIG);
90   
91    // for AddPermissionTask
92  3 final SecuritySupportImpl securitySupport = new SecuritySupportImpl();
93  3 securitySupport.setRoleManager(new MgnlRoleManager());
94  3 ComponentsTestUtil.setInstance(SecuritySupport.class, securitySupport);
95    }
96   
 
97  1 toggle @Test
98    public void basicInstallAndCheckThatMethodsWereAdded() throws Exception {
99    // GIVEN
100  1 Node node = NodeUtil.createPath(session.getRootNode(), "/server/IPConfig/allow-all", NodeTypes.ContentNode.NAME);
101  1 node.setProperty("methods", "GET,POST");
102   
103  1 Session rolesSession = MgnlContext.getJCRSession(RepositoryConstants.USER_ROLES);
104  1 NodeUtil.createPath(rolesSession.getRootNode(), "/rest-editor", NodeTypes.Role.NAME);
105   
106    // WHEN
107  1 executeUpdatesAsIfTheCurrentlyInstalledVersionWas(null);
108   
109    // THEN
110  1 assertEquals("GET,POST,PUT,DELETE", node.getProperty("methods").getString());
111   
112  1 assertEquals(5, rolesSession.getNode("/rest-editor/acl_uri").getNodes().getSize());
113   
114  1 assertEquals("rest-editor", session.getProperty("/modules/rest-services/rest-endpoints/commands/enabledCommands/markAsDeleted/access/roles/rest").getString());
115  1 assertEquals("rest-editor", session.getProperty("/modules/rest-services/rest-endpoints/commands/enabledCommands/activate/access/roles/rest").getString());
116   
117    }
118   
 
119  1 toggle @Test
120    public void updateTo1_1CommandEndpointIsUpdatedToV2() throws Exception {
121    // GIVEN
122  1 setupConfigProperty("/modules/rest-services/rest-endpoints/commands", "implementationClass", "info.magnolia.rest.service.command.v1.CommandEndpoint");
123  1 setupConfigProperty("/modules/rest-services/rest-endpoints/commands/enabledCommands/activate/access/roles", "rest", "rest");
124  1 setupConfigProperty("/modules/rest-services/rest-endpoints/commands/enabledCommands/markAsDeleted/access/roles", "rest", "rest");
125   
126    // WHEN
127  1 executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("1.0"));
128   
129    // THEN
130  1 assertEquals("info.magnolia.rest.service.command.v2.CommandEndpoint", session.getProperty("/modules/rest-services/rest-endpoints/commands/implementationClass").getString());
131    }
132   
 
133  1 toggle @Test
134    public void updateTo2_0CommandEndpointRolesIsUpdatedToRestEditor() throws Exception {
135    // GIVEN
136  1 setupConfigProperty("/modules/rest-services/rest-endpoints/commands/enabledCommands/activate/access/roles", "rest", "rest");
137  1 setupConfigProperty("/modules/rest-services/rest-endpoints/commands/enabledCommands/markAsDeleted/access/roles", "rest", "rest");
138   
139    // WHEN
140  1 executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("1.1"));
141   
142    // THEN
143  1 assertEquals("rest-editor", session.getProperty("/modules/rest-services/rest-endpoints/commands/enabledCommands/markAsDeleted/access/roles/rest").getString());
144  1 assertEquals("rest-editor", session.getProperty("/modules/rest-services/rest-endpoints/commands/enabledCommands/activate/access/roles/rest").getString());
145    }
146    }