Skip to content

Commit

Permalink
Basic ReportWebService tests
Browse files Browse the repository at this point in the history
  • Loading branch information
semancik committed Mar 26, 2019
1 parent 49a35cc commit 042f12a
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 19 deletions.
6 changes: 5 additions & 1 deletion model/report-impl/pom.xml
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<!--
~ Copyright (c) 2010-2017 Evolveum
~ Copyright (c) 2010-2019 Evolveum
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -133,6 +133,10 @@
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down
Expand Up @@ -23,6 +23,8 @@
import javax.xml.namespace.QName;

import com.evolveum.midpoint.xml.ns._public.common.common_3.SelectorQualifiedGetOptionsType;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.interceptor.Fault;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -200,6 +202,9 @@ public ObjectListType processReport(String reportOid, String query, RemoteReport
}

private PrismObject<ReportType> authorizeReportProcessing(String reportOid, Task task, OperationResult result) throws ObjectNotFoundException, SchemaException, SecurityViolationException, CommunicationException, ConfigurationException, ExpressionEvaluationException {
if (StringUtils.isBlank(reportOid)) {
throw new SchemaException("No report OID specified");
}
PrismObject<ReportType> report = reportService.getReportDefinition(reportOid, task, result);
// TODO TODO TODO: authorization
return report;
Expand Down
Expand Up @@ -37,6 +37,24 @@
public class AbstractReportIntegrationTest extends AbstractModelIntegrationTest {

protected final static File TEST_DIR_COMMON = new File("src/test/resources/common");
protected final static File EXPORT_DIR = new File("target/midpoint-home/export");

protected final static File TEST_REPOSTS_DIR = new File("src/test/resources/reports");

protected final static File REPORT_USER_LIST_FILE = new File(TEST_REPOSTS_DIR, "report-user-list.xml");
protected final static String REPORT_USER_LIST_OID = "00000000-0000-0000-0000-000000000110";

protected final static File REPORT_USER_LIST_EXPRESSIONS_CSV_FILE = new File(TEST_REPOSTS_DIR, "report-user-list-expressions-csv.xml");
protected final static String REPORT_USER_LIST_EXPRESSIONS_CSV_OID = "8fa48180-4f17-11e9-9eed-3fb4721a135e";

protected final static File REPORT_USER_LIST_EXPRESSIONS_POISONOUS_QUERY_CSV_FILE = new File(TEST_REPOSTS_DIR, "report-user-list-expressions-poisonous-query-csv.xml");
protected final static String REPORT_USER_LIST_EXPRESSIONS_POISONOUS_QUERY_CSV_OID = "5c5af02a-4fe9-11e9-bb07-7b4e52fe05cd";

protected final static File REPORT_USER_LIST_EXPRESSIONS_POISONOUS_FIELD_CSV_FILE = new File(TEST_REPOSTS_DIR, "report-user-list-expressions-poisonous-field-csv.xml");
protected final static String REPORT_USER_LIST_EXPRESSIONS_POISONOUS_FIELD_CSV_OID = "76c58132-4fe9-11e9-86fe-ff36d221f673";

protected final static File REPORT_USER_LIST_SCRIPT_FILE = new File(TEST_REPOSTS_DIR, "report-user-list-script.xml");
protected final static String REPORT_USER_LIST_SCRIPT_OID = "222bf2b8-c89b-11e7-bf36-ebd4e4d45a80";

protected final static File USER_JACK_FILE = new File(TEST_DIR_COMMON, "user-jack.xml");
protected final static String USER_JACK_OID = "c0c010c0-d34d-b33f-f00d-111111111111";
Expand Down
Expand Up @@ -56,24 +56,6 @@
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
public class TestReport extends AbstractReportIntegrationTest {

protected final static File TEST_DIR = new File("src/test/resources/reports");
protected final static File EXPORT_DIR = new File("target/midpoint-home/export");

protected final static File REPORT_USER_LIST_FILE = new File(TEST_DIR, "report-user-list.xml");
protected final static String REPORT_USER_LIST_OID = "00000000-0000-0000-0000-000000000110";

protected final static File REPORT_USER_LIST_EXPRESSIONS_CSV_FILE = new File(TEST_DIR, "report-user-list-expressions-csv.xml");
protected final static String REPORT_USER_LIST_EXPRESSIONS_CSV_OID = "8fa48180-4f17-11e9-9eed-3fb4721a135e";

protected final static File REPORT_USER_LIST_EXPRESSIONS_POISONOUS_QUERY_CSV_FILE = new File(TEST_DIR, "report-user-list-expressions-poisonous-query-csv.xml");
protected final static String REPORT_USER_LIST_EXPRESSIONS_POISONOUS_QUERY_CSV_OID = "5c5af02a-4fe9-11e9-bb07-7b4e52fe05cd";

protected final static File REPORT_USER_LIST_EXPRESSIONS_POISONOUS_FIELD_CSV_FILE = new File(TEST_DIR, "report-user-list-expressions-poisonous-field-csv.xml");
protected final static String REPORT_USER_LIST_EXPRESSIONS_POISONOUS_FIELD_CSV_OID = "76c58132-4fe9-11e9-86fe-ff36d221f673";

protected final static File REPORT_USER_LIST_SCRIPT_FILE = new File(TEST_DIR, "report-user-list-script.xml");
protected final static String REPORT_USER_LIST_SCRIPT_OID = "222bf2b8-c89b-11e7-bf36-ebd4e4d45a80";

@Override
public void initSystem(Task initTask, OperationResult initResult) throws Exception {
super.initSystem(initTask, initResult);
Expand Down
@@ -0,0 +1,174 @@
/**
* Copyright (c) 2010-2019 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.report;

import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertNotNull;
import static org.testng.AssertJUnit.assertNull;
import static org.testng.AssertJUnit.assertTrue;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

import org.apache.cxf.interceptor.Fault;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.Test;

import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.report.impl.ReportWebService;
import com.evolveum.midpoint.schema.SearchResultList;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.test.util.MidPointTestConstants;
import com.evolveum.midpoint.test.util.TestUtil;
import com.evolveum.midpoint.util.exception.CommunicationException;
import com.evolveum.midpoint.util.exception.ConfigurationException;
import com.evolveum.midpoint.util.exception.ExpressionEvaluationException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SecurityViolationException;
import com.evolveum.midpoint.xml.ns._public.common.api_types_3.ObjectListType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ReportType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
import com.evolveum.midpoint.xml.ns._public.report.report_3.RemoteReportParametersType;

/**
* Basic report tests.
*/
@ContextConfiguration(locations = { "classpath:ctx-report-test-main.xml" })
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
public class TestReportWebService extends AbstractReportIntegrationTest {

@Autowired protected ReportWebService reportWebService;

@Override
public void initSystem(Task initTask, OperationResult initResult) throws Exception {
super.initSystem(initTask, initResult);

repoAddObjectFromFile(REPORT_USER_LIST_EXPRESSIONS_CSV_FILE, ReportType.class, initResult);
repoAddObjectFromFile(REPORT_USER_LIST_EXPRESSIONS_POISONOUS_QUERY_CSV_FILE, ReportType.class, initResult);
repoAddObjectFromFile(REPORT_USER_LIST_EXPRESSIONS_POISONOUS_FIELD_CSV_FILE, ReportType.class, initResult);

// Let's make this more interesting by adding a couple of users
importObjectsFromFileNotRaw(USERS_MONKEY_ISLAND_FILE, initTask, initResult);
}

@Test
public void test000Sanity() throws Exception {
final String TEST_NAME = "test000Sanity";
displayTestTitle(TEST_NAME);

assertNotNull("No web service", reportWebService);
}

@Test
public void test100ProcessReportUserList() throws Exception {
final String TEST_NAME = "test100ProcessReportUserList";
displayTestTitle(TEST_NAME);

String query = createAllQueryString(UserType.class);
RemoteReportParametersType parameters = createReportParameters();

// WHEN
displayWhen(TEST_NAME);
ObjectListType userList = reportWebService.processReport(REPORT_USER_LIST_EXPRESSIONS_CSV_OID, query, parameters, null);

// THEN
displayThen(TEST_NAME);
display("Returned user list ("+userList.getObject().size()+" objects)", userList);

assertUserList(userList);
}

@Test
public void test110ProcessReportUserListNoReportOid() throws Exception {
final String TEST_NAME = "test110ProcessReportUserListNoReportOid";
displayTestTitle(TEST_NAME);

String query = createAllQueryString(UserType.class);
RemoteReportParametersType parameters = createReportParameters();

try {

// WHEN
displayWhen(TEST_NAME);
reportWebService.processReport(null, query, parameters, null);

assertNotReached();

} catch (Fault f) {
// THEN
displayThen(TEST_NAME);
display("Expected fault", f);
}
}

@Test
public void test112ProcessReportUserListInvalidReportOid() throws Exception {
final String TEST_NAME = "test112ProcessReportUserListInvalidReportOid";
displayTestTitle(TEST_NAME);

String query = createAllQueryString(UserType.class);
RemoteReportParametersType parameters = createReportParameters();

try {

// WHEN
displayWhen(TEST_NAME);
reportWebService.processReport("l00n3y", query, parameters, null);

assertNotReached();

} catch (Fault f) {
// THEN
displayThen(TEST_NAME);
display("Expected fault", f);
}
}

// TODO: test that violates authorization to run report
// TODO: test that violates authorization to read report
// TODO: test that violates safe profile

private String createAllQueryString(Class<?> type) {
return "<filter><type><type>"+type.getSimpleName()+"</type></type></filter>";
}

private RemoteReportParametersType createReportParameters() {
RemoteReportParametersType parameters = new RemoteReportParametersType();
return parameters;
}

private void assertUserList(ObjectListType userList) throws SchemaException, ObjectNotFoundException, SecurityViolationException, CommunicationException, ConfigurationException, ExpressionEvaluationException {
Task task = createTask("assertUserList");
OperationResult result = task.getResult();
SearchResultList<PrismObject<UserType>> currentUsers = modelService.searchObjects(UserType.class, null, null, task, result);
display("Current users in midPoint ("+currentUsers.size()+" users)", currentUsers.toString());

assertEquals("Unexpected number of returned objects", currentUsers.size(), userList.getObject().size());
}


}
@@ -0,0 +1,69 @@
/**
* Copyright (c) 2010-2019 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.report;

import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertNotNull;
import static org.testng.AssertJUnit.assertNull;
import static org.testng.AssertJUnit.assertTrue;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

import org.apache.cxf.interceptor.Fault;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.Test;

import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.report.impl.ReportWebService;
import com.evolveum.midpoint.schema.SearchResultList;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.test.util.MidPointTestConstants;
import com.evolveum.midpoint.test.util.TestUtil;
import com.evolveum.midpoint.util.exception.CommunicationException;
import com.evolveum.midpoint.util.exception.ConfigurationException;
import com.evolveum.midpoint.util.exception.ExpressionEvaluationException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SecurityViolationException;
import com.evolveum.midpoint.xml.ns._public.common.api_types_3.ObjectListType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ReportType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
import com.evolveum.midpoint.xml.ns._public.report.report_3.RemoteReportParametersType;

/**
* Basic report tests.
*/
@ContextConfiguration(locations = { "classpath:ctx-report-test-main.xml" })
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
public class TestReportWebServiceSafe extends TestReportWebService {

@Override
protected File getSystemConfigurationFile() {
return SYSTEM_CONFIGURATION_SAFE_FILE;
}

}
2 changes: 2 additions & 0 deletions model/report-impl/testng-integration.xml
Expand Up @@ -21,6 +21,8 @@
<classes>
<class name="com.evolveum.midpoint.report.TestReport"/>
<class name="com.evolveum.midpoint.report.TestReportSafe"/>
<class name="com.evolveum.midpoint.report.TestReportWebService"/>
<class name="com.evolveum.midpoint.report.TestReportWebServiceSafe"/>
</classes>
</test>
</suite>

0 comments on commit 042f12a

Please sign in to comment.