001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.pluto.testsuite.test;
018:
019: import javax.portlet.PortletRequest;
020:
021: import org.apache.pluto.testsuite.TestResult;
022:
023: /**
024: */
025: public class SecurityMappingTest extends AbstractReflectivePortletTest {
026:
027: // Test Methods ------------------------------------------------------------
028:
029: protected TestResult checkIsUserInMappedRole(PortletRequest request) {
030: TestResult result = isUserLoggedIn(request);
031: result.setDescription("Test if user is in mapped role.");
032: if (result.getReturnCode() == TestResult.WARNING) {
033: return result;
034: }
035:
036: ExpectedResults expectedResults = ExpectedResults.getInstance();
037: String role = expectedResults.getMappedSecurityRole();
038: if (request.isUserInRole(role)) {
039: result.setReturnCode(TestResult.PASSED);
040: } else {
041: result.setReturnCode(TestResult.WARNING);
042: result
043: .setResultMessage("User is not in the expected role: "
044: + role
045: + ". This may be due to misconfiuration.");
046: }
047: return result;
048: }
049:
050: protected TestResult checkIsUserInUnmappedRole(
051: PortletRequest request) {
052: TestResult result = isUserLoggedIn(request);
053: result.setDescription("Test if user is in unmapped role");
054: if (result.getReturnCode() == TestResult.WARNING) {
055: return result;
056: }
057:
058: ExpectedResults expectedResults = ExpectedResults.getInstance();
059: String role = expectedResults.getUnmappedSecurityRole();
060: if (request.isUserInRole(role)) {
061: result.setReturnCode(TestResult.PASSED);
062: } else {
063: result.setReturnCode(TestResult.WARNING);
064: result
065: .setResultMessage("User is not in the expected role: "
066: + role
067: + ". This may be due to misconfiuration.");
068: }
069: return result;
070: }
071:
072: protected TestResult checkIsUserIndUndeclaredRole(
073: PortletRequest request) {
074: TestResult result = isUserLoggedIn(request);
075: result.setDescription("Test if user is in undeclared role");
076: if (result.getReturnCode() == TestResult.WARNING) {
077: return result;
078: }
079:
080: String fakeRole = "fakeTestRoleFooBar";
081: if (!request.isUserInRole(fakeRole)) {
082: result.setReturnCode(TestResult.PASSED);
083: } else {
084: result.setReturnCode(TestResult.FAILED);
085: result.setResultMessage("User is in the fake role named "
086: + fakeRole);
087: }
088: return result;
089: }
090:
091: // Private Methods ---------------------------------------------------------
092:
093: private TestResult isUserLoggedIn(PortletRequest request) {
094: TestResult result = new TestResult();
095: if (request.getRemoteUser() == null) {
096: result.setReturnCode(TestResult.WARNING);
097: result.setResultMessage("User is not logged in.");
098: }
099: return result;
100: }
101: }
|