use of org.xwiki.security.authorization.AuthorizationManager in project xwiki-platform by xwiki.
the class SolrQueryExecutorTest method filterResponse.
@Test
public void filterResponse() throws Exception {
ParameterizedType resolverType = new DefaultParameterizedType(null, DocumentReferenceResolver.class, SolrDocument.class);
DocumentReferenceResolver<SolrDocument> resolver = this.componentManager.getInstance(resolverType);
AuthorizationManager authorizationManager = this.componentManager.getInstance(AuthorizationManager.class);
DocumentReference currentUserReference = new DocumentReference("xwiki", "XWiki", "currentuser");
this.oldCore.getXWikiContext().setUserReference(currentUserReference);
DocumentReference currentAuthorReference = new DocumentReference("xwiki", "XWiki", "currentauthor");
XWikiDocument currentDocument = new XWikiDocument(currentAuthorReference);
currentDocument.setContentAuthorReference(currentAuthorReference);
this.oldCore.getXWikiContext().setDoc(currentDocument);
DocumentReference aliceReference = new DocumentReference("wiki", "Users", "Alice");
when(authorizationManager.hasAccess(Right.VIEW, currentAuthorReference, aliceReference)).thenReturn(true);
SolrDocument alice = new SolrDocument();
when(resolver.resolve(alice)).thenReturn(aliceReference);
DocumentReference bobReference = new DocumentReference("wiki", "Users", "Bob");
when(authorizationManager.hasAccess(Right.VIEW, currentUserReference, bobReference)).thenReturn(true);
when(authorizationManager.hasAccess(Right.VIEW, currentAuthorReference, bobReference)).thenReturn(true);
SolrDocument bob = new SolrDocument();
when(resolver.resolve(bob)).thenReturn(bobReference);
DocumentReference carolReference = new DocumentReference("wiki", "Users", "Carol");
when(authorizationManager.hasAccess(Right.VIEW, currentUserReference, carolReference)).thenReturn(true);
SolrDocument carol = new SolrDocument();
when(resolver.resolve(carol)).thenReturn(carolReference);
SolrDocumentList sourceResults = new SolrDocumentList();
sourceResults.addAll(Arrays.asList(alice, bob, carol));
sourceResults.setNumFound(3);
QueryResponse response = mock(QueryResponse.class);
when(this.solr.query(any(SolrParams.class))).thenReturn(response);
DefaultQuery query = new DefaultQuery("", null);
// No right check
when(response.getResults()).thenReturn((SolrDocumentList) sourceResults.clone());
SolrDocumentList results = ((QueryResponse) this.componentManager.getComponentUnderTest().execute(query).get(0)).getResults();
assertEquals(Arrays.asList(alice, bob, carol), results);
// Check current user right
query.checkCurrentUser(true);
when(response.getResults()).thenReturn((SolrDocumentList) sourceResults.clone());
results = ((QueryResponse) this.componentManager.getComponentUnderTest().execute(query).get(0)).getResults();
assertEquals(Arrays.asList(bob, carol), results);
// Check both current user and author rights
query.checkCurrentAuthor(true);
when(response.getResults()).thenReturn((SolrDocumentList) sourceResults.clone());
results = ((QueryResponse) this.componentManager.getComponentUnderTest().execute(query).get(0)).getResults();
assertEquals(Arrays.asList(bob), results);
// Check current author right
query.checkCurrentUser(false);
when(response.getResults()).thenReturn((SolrDocumentList) sourceResults.clone());
results = ((QueryResponse) this.componentManager.getComponentUnderTest().execute(query).get(0)).getResults();
assertEquals(Arrays.asList(alice, bob), results);
}
use of org.xwiki.security.authorization.AuthorizationManager in project xwiki-platform by xwiki.
the class IntegrationTests method initialize.
@RenderingTestSuite.Initialized
public void initialize(MockitoComponentManager componentManager) throws Exception {
ModelContext modelContext = componentManager.registerMockComponent(ModelContext.class);
when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("currentWiki"));
// Document Access Bridge mock
DocumentAccessBridge dab = componentManager.registerMockComponent(DocumentAccessBridge.class);
DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
DocumentReference currentDocumentReference = new DocumentReference("currentwiki", "currentspace", "currentpage");
DocumentModelBridge document = Mockito.mock(DocumentModelBridge.class);
when(dab.getDocumentURL(new DocumentReference("currentWiki", "space", "page"), "temp", null, null)).thenReturn("temppath");
when(dab.getCurrentDocumentReference()).thenReturn(currentDocumentReference);
when(dab.exists(documentReference)).thenReturn(true);
when(dab.getDocumentInstance(documentReference)).thenReturn(document);
when(dab.getCurrentUserReference()).thenReturn(null);
DocumentDisplayer displayer = componentManager.registerMockComponent(DocumentDisplayer.class);
Parser parser = componentManager.getInstance(Parser.class, Syntax.XWIKI_2_0.toIdString());
final XDOM xdom = parser.parse(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(WIKI_CONTENT_FILE)));
when(displayer.display(eq(document), any(DocumentDisplayerParameters.class))).thenReturn(xdom);
AuthorizationManager authorizationManager = componentManager.registerMockComponent(AuthorizationManager.class);
when(authorizationManager.hasAccess(Right.VIEW, null, documentReference)).thenReturn(true);
componentManager.registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "current");
componentManager.registerMockComponent(SpaceReferenceResolver.TYPE_STRING, "current");
}
use of org.xwiki.security.authorization.AuthorizationManager in project xwiki-platform by xwiki.
the class DocumentTitleDisplayerTest method whenSettingTheContextDocumentTheContextWikiIsAlsoSet.
@Test
public void whenSettingTheContextDocumentTheContextWikiIsAlsoSet() throws Exception {
EntityReferenceProvider defaultEntityReferenceProvider = this.mocker.getInstance(EntityReferenceProvider.class);
when(defaultEntityReferenceProvider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(new EntityReference("Page", EntityType.DOCUMENT));
DocumentModelBridge document = mock(DocumentModelBridge.class);
DocumentReference documentReference = new DocumentReference("wiki", Arrays.asList("Space"), "Page");
when(document.getDocumentReference()).thenReturn(documentReference);
when(document.getTitle()).thenReturn("title");
XDOM titleXDOM = new XDOM(Arrays.asList(new WordBlock("title")));
Parser plainTextParser = this.mocker.getInstance(Parser.class, "plain/1.0");
when(plainTextParser.parse(any(StringReader.class))).thenReturn(titleXDOM);
ModelContext modelContext = this.mocker.getInstance(ModelContext.class);
WikiReference currentWikiReference = new WikiReference("currentWiki");
when(modelContext.getCurrentEntityReference()).thenReturn(currentWikiReference);
AuthorizationManager authorizationManager = this.mocker.getInstance(AuthorizationManager.class);
when(authorizationManager.hasAccess(eq(Right.SCRIPT), any(), any())).thenReturn(true);
DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
DocumentDisplayerParameters params = new DocumentDisplayerParameters();
params.setTitleDisplayed(true);
params.setExecutionContextIsolated(true);
this.mocker.getComponentUnderTest().display(document, params);
// Check that the context is set.
verify(dab).pushDocumentInContext(any(), eq(documentReference));
verify(modelContext).setCurrentEntityReference(documentReference.getWikiReference());
// Check that the context is restored.
verify(dab).popDocumentFromContext(any());
verify(modelContext).setCurrentEntityReference(currentWikiReference);
}
use of org.xwiki.security.authorization.AuthorizationManager in project xwiki-platform by xwiki.
the class DefaultNotificationManagerTest method setUp.
@Before
public void setUp() throws Exception {
eventStream = mocker.getInstance(EventStream.class);
queryGenerator = mocker.getInstance(QueryGenerator.class);
documentAccessBridge = mocker.getInstance(DocumentAccessBridge.class);
documentReferenceResolver = mocker.getInstance(DocumentReferenceResolver.TYPE_STRING);
notificationPreferenceManager = mocker.getInstance(NotificationPreferenceManager.class);
authorizationManager = mocker.getInstance(AuthorizationManager.class);
startDate = new Date(10);
when(documentReferenceResolver.resolve("xwiki:XWiki.UserA")).thenReturn(userReference);
query = mock(Query.class);
when(queryGenerator.generateQuery(any(DocumentReference.class), any(NotificationFormat.class), nullable(Date.class), nullable(Date.class), nullable(List.class))).thenReturn(query);
NotificationPreference pref1 = mock(NotificationPreference.class);
when(pref1.getProperties()).thenReturn(Collections.singletonMap(NotificationPreferenceProperty.EVENT_TYPE, "create"));
when(pref1.isNotificationEnabled()).thenReturn(true);
when(notificationPreferenceManager.getAllPreferences(userReference)).thenReturn(Arrays.asList(pref1));
}
use of org.xwiki.security.authorization.AuthorizationManager in project xwiki-platform by xwiki.
the class WikiTemplateManagerScriptTest method setUp.
@Before
public void setUp() throws Exception {
wikiTemplateManager = mocker.getInstance(WikiTemplateManager.class);
wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
authorizationManager = mocker.getInstance(AuthorizationManager.class);
entityReferenceSerializer = mocker.getInstance(new DefaultParameterizedType(null, EntityReferenceSerializer.class, String.class));
xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
xcontext = mock(XWikiContext.class);
when(xcontextProvider.get()).thenReturn(xcontext);
execution = mocker.getInstance(Execution.class);
executionContext = new ExecutionContext();
when(execution.getContext()).thenReturn(executionContext);
currentUserRef = new DocumentReference("mainWiki", "XWiki", "User");
when(xcontext.getUserReference()).thenReturn(currentUserRef);
currentDoc = mock(XWikiDocument.class);
when(xcontext.getDoc()).thenReturn(currentDoc);
when(xcontext.getMainXWiki()).thenReturn("mainWiki");
when(entityReferenceSerializer.serialize(currentUserRef)).thenReturn("mainWiki:XWiki.User");
}
Aggregations