| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.platform.wiki.creationjob.script; |
| 21 |
|
|
| 22 |
|
import javax.inject.Provider; |
| 23 |
|
|
| 24 |
|
import org.junit.Before; |
| 25 |
|
import org.junit.Rule; |
| 26 |
|
import org.junit.Test; |
| 27 |
|
import org.xwiki.context.Execution; |
| 28 |
|
import org.xwiki.context.ExecutionContext; |
| 29 |
|
import org.xwiki.extension.ExtensionId; |
| 30 |
|
import org.xwiki.extension.distribution.internal.DistributionManager; |
| 31 |
|
import org.xwiki.job.Job; |
| 32 |
|
import org.xwiki.job.event.status.JobStatus; |
| 33 |
|
import org.xwiki.model.reference.DocumentReference; |
| 34 |
|
import org.xwiki.model.reference.WikiReference; |
| 35 |
|
import org.xwiki.platform.wiki.creationjob.WikiCreationRequest; |
| 36 |
|
import org.xwiki.platform.wiki.creationjob.WikiCreator; |
| 37 |
|
import org.xwiki.security.authorization.AccessDeniedException; |
| 38 |
|
import org.xwiki.security.authorization.AuthorizationManager; |
| 39 |
|
import org.xwiki.security.authorization.Right; |
| 40 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
| 41 |
|
import org.xwiki.wiki.descriptor.WikiDescriptorManager; |
| 42 |
|
|
| 43 |
|
import com.xpn.xwiki.XWiki; |
| 44 |
|
import com.xpn.xwiki.XWikiContext; |
| 45 |
|
|
| 46 |
|
import static org.junit.Assert.assertEquals; |
| 47 |
|
import static org.junit.Assert.assertNotNull; |
| 48 |
|
import static org.junit.Assert.assertNull; |
| 49 |
|
import static org.mockito.ArgumentMatchers.any; |
| 50 |
|
import static org.mockito.ArgumentMatchers.eq; |
| 51 |
|
import static org.mockito.Mockito.doThrow; |
| 52 |
|
import static org.mockito.Mockito.mock; |
| 53 |
|
import static org.mockito.Mockito.verify; |
| 54 |
|
import static org.mockito.Mockito.when; |
| 55 |
|
|
| 56 |
|
|
| 57 |
|
@version |
| 58 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (48) |
Complexity: 6 |
Complexity Density: 0.14 |
|
| 59 |
|
public class WikiCreationJobScriptServicesTest |
| 60 |
|
{ |
| 61 |
|
@Rule |
| 62 |
|
public MockitoComponentMockingRule<WikiCreationJobScriptServices> mocker = |
| 63 |
|
new MockitoComponentMockingRule<>(WikiCreationJobScriptServices.class); |
| 64 |
|
|
| 65 |
|
private WikiCreator wikiCreator; |
| 66 |
|
|
| 67 |
|
private Execution execution; |
| 68 |
|
|
| 69 |
|
private AuthorizationManager authorizationManager; |
| 70 |
|
|
| 71 |
|
private WikiDescriptorManager wikiDescriptorManager; |
| 72 |
|
|
| 73 |
|
private DistributionManager distributionManager; |
| 74 |
|
|
| 75 |
|
private Provider<XWikiContext> xcontextProvider; |
| 76 |
|
|
| 77 |
|
private XWikiContext xcontext; |
| 78 |
|
|
| 79 |
|
private XWiki xwiki; |
| 80 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 1 |
Complexity Density: 0.07 |
|
| 81 |
5 |
@Before... |
| 82 |
|
public void setUp() throws Exception |
| 83 |
|
{ |
| 84 |
5 |
wikiCreator = mocker.getInstance(WikiCreator.class); |
| 85 |
5 |
execution = mocker.getInstance(Execution.class); |
| 86 |
5 |
authorizationManager = mocker.getInstance(AuthorizationManager.class); |
| 87 |
5 |
wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class); |
| 88 |
5 |
distributionManager = mocker.getInstance(DistributionManager.class); |
| 89 |
5 |
xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER); |
| 90 |
5 |
xcontext = mock(XWikiContext.class); |
| 91 |
5 |
when(xcontextProvider.get()).thenReturn(xcontext); |
| 92 |
5 |
xwiki = mock(XWiki.class); |
| 93 |
5 |
when(xcontext.getWiki()).thenReturn(xwiki); |
| 94 |
|
|
| 95 |
5 |
when(wikiDescriptorManager.getMainWikiId()).thenReturn("mainWikiId"); |
| 96 |
|
|
| 97 |
5 |
ExecutionContext executionContext = new ExecutionContext(); |
| 98 |
5 |
when(execution.getContext()).thenReturn(executionContext); |
| 99 |
|
|
| 100 |
5 |
ExtensionId extensionId = new ExtensionId("authorized-extension", "1.0"); |
| 101 |
5 |
when(distributionManager.getWikiUIExtensionId()).thenReturn(extensionId); |
| 102 |
|
} |
| 103 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 104 |
1 |
@Test... |
| 105 |
|
public void createWiki() throws Exception |
| 106 |
|
{ |
| 107 |
1 |
Job job = mock(Job.class); |
| 108 |
1 |
when(wikiCreator.createWiki(any(WikiCreationRequest.class))).thenReturn(job); |
| 109 |
|
|
| 110 |
1 |
WikiCreationRequest wikiCreationRequest = new WikiCreationRequest(); |
| 111 |
1 |
wikiCreationRequest.setExtensionId("authorized-extension", "1.0"); |
| 112 |
1 |
assertEquals(job, mocker.getComponentUnderTest().createWiki(wikiCreationRequest)); |
| 113 |
1 |
assertNull(mocker.getComponentUnderTest().getLastError()); |
| 114 |
|
} |
| 115 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
| 116 |
1 |
@Test... |
| 117 |
|
public void createWikiWhenExtensionIsNotAuthorized() throws Exception |
| 118 |
|
{ |
| 119 |
1 |
WikiCreationRequest wikiCreationRequest = new WikiCreationRequest(); |
| 120 |
1 |
wikiCreationRequest.setExtensionId("badExtension", "version"); |
| 121 |
|
|
| 122 |
1 |
assertNull(mocker.getComponentUnderTest().createWiki(wikiCreationRequest)); |
| 123 |
1 |
Exception lastError = mocker.getComponentUnderTest().getLastError(); |
| 124 |
1 |
assertNotNull(lastError); |
| 125 |
1 |
assertEquals("The extension [badExtension-version] is not authorized.", lastError.getMessage()); |
| 126 |
1 |
verify(mocker.getMockedLogger()).warn("Failed to create a new wiki.", lastError); |
| 127 |
|
} |
| 128 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
| 129 |
1 |
@Test... |
| 130 |
|
public void createWikiWhenNoCreateWikiRight() throws Exception |
| 131 |
|
{ |
| 132 |
1 |
DocumentReference currentUser = new DocumentReference("xwiki", "XWiki", "User"); |
| 133 |
1 |
when(xcontext.getUserReference()).thenReturn(currentUser); |
| 134 |
1 |
AccessDeniedException exception = |
| 135 |
|
new AccessDeniedException(Right.CREATE_WIKI, currentUser, new WikiReference("mainWikiId")); |
| 136 |
1 |
doThrow(exception).when(authorizationManager).checkAccess(eq(Right.CREATE_WIKI), eq(currentUser), |
| 137 |
|
eq(new WikiReference("mainWikiId"))); |
| 138 |
|
|
| 139 |
1 |
WikiCreationRequest wikiCreationRequest = new WikiCreationRequest(); |
| 140 |
1 |
wikiCreationRequest.setExtensionId("authorized-extension", "1.0"); |
| 141 |
|
|
| 142 |
1 |
assertNull(mocker.getComponentUnderTest().createWiki(wikiCreationRequest)); |
| 143 |
1 |
Exception lastError = mocker.getComponentUnderTest().getLastError(); |
| 144 |
1 |
assertNotNull(lastError); |
| 145 |
1 |
assertEquals(exception, lastError); |
| 146 |
|
} |
| 147 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 148 |
1 |
@Test... |
| 149 |
|
public void getJobStatus() throws Exception |
| 150 |
|
{ |
| 151 |
1 |
JobStatus jobStatus = mock(JobStatus.class); |
| 152 |
1 |
when(wikiCreator.getJobStatus("wikiId")).thenReturn(jobStatus); |
| 153 |
1 |
assertEquals(jobStatus, mocker.getComponentUnderTest().getJobStatus("wikiId")); |
| 154 |
|
} |
| 155 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 156 |
1 |
@Test... |
| 157 |
|
public void newWikiCreationRequest() throws Exception |
| 158 |
|
{ |
| 159 |
1 |
assertNotNull(mocker.getComponentUnderTest().newWikiCreationRequest()); |
| 160 |
|
} |
| 161 |
|
} |