Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests fixes universal #676

Merged
merged 11 commits into from
Jul 25, 2023
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2013 predic8 GmbH, www.predic8.com
/* Copyright 2023 predic8 GmbH, www.predic8.com

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class Constants {
public static final String VERSION;

static {
String version = "5"; // fallback
String version = "5.1"; // fallback
try {
Properties p = new Properties(); // Production
p.load(Constants.class.getResourceAsStream("/META-INF/maven/org.membrane-soa/service-proxy-core/pom.properties"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,27 @@

package com.predic8.membrane.core.interceptor.authentication;

import com.google.common.collect.*;
import com.predic8.membrane.annot.*;
import com.predic8.membrane.core.*;
import com.predic8.membrane.core.exchange.*;
import com.predic8.membrane.core.http.*;
import com.predic8.membrane.core.interceptor.*;
import com.predic8.membrane.core.interceptor.authentication.session.*;
import com.predic8.membrane.core.interceptor.authentication.session.StaticUserDataProvider.*;
import com.predic8.membrane.core.util.*;

import java.util.*;

import static com.predic8.membrane.core.Constants.*;
import static com.predic8.membrane.core.http.Header.*;
import static java.nio.charset.StandardCharsets.*;
import static org.apache.commons.codec.binary.Base64.*;
import static org.apache.commons.text.StringEscapeUtils.*;
import com.google.common.collect.ImmutableMap;
import com.predic8.membrane.annot.MCChildElement;
import com.predic8.membrane.annot.MCElement;
import com.predic8.membrane.core.Router;
import com.predic8.membrane.core.exchange.Exchange;
import com.predic8.membrane.core.http.Response;
import com.predic8.membrane.core.interceptor.AbstractInterceptor;
import com.predic8.membrane.core.interceptor.Outcome;
import com.predic8.membrane.core.interceptor.authentication.session.StaticUserDataProvider;
import com.predic8.membrane.core.interceptor.authentication.session.StaticUserDataProvider.User;
import com.predic8.membrane.core.interceptor.authentication.session.UserDataProvider;
import com.predic8.membrane.core.util.HttpUtil;

import java.util.List;
import java.util.NoSuchElementException;

import static com.predic8.membrane.core.Constants.PRODUCT_NAME;
import static com.predic8.membrane.core.http.Header.AUTHORIZATION;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.commons.codec.binary.Base64.decodeBase64;
import static org.apache.commons.text.StringEscapeUtils.escapeHtml4;

/**
* @description Blocks requests which do not have the correct RFC 1945 basic authentication credentials (HTTP header "Authentication: Basic ....").
Expand Down Expand Up @@ -95,7 +99,10 @@ private String getAuthorizationHeaderDecoded(Exchange exc) {
}

public List<User> getUsers() {
return ((StaticUserDataProvider)userDataProvider).getUsers();
if (userDataProvider instanceof StaticUserDataProvider sud) {
return sud.getUsers();
}
throw new UnsupportedOperationException("getUsers not implemented for this userDataProvider.");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public void setHtpasswdPath(String path) {
this.htpasswdPath = path;
}

public String getHtpasswdPath() { return this.htpasswdPath; }

@Override
public Map<String, String> verify(Map<String, String> postData) {
String username = postData.get("username");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
/* Copyright 2023 predic8 GmbH, www.predic8.com

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.predic8.membrane.core.interceptor.json;

public class JsonProtectionException extends Exception{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public void setReportError(boolean reportError) {
this.reportError = reportError;
}

public boolean getReportError() {
public Boolean getReportError() {
return reportError;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.predic8.membrane.core.interceptor.server.WebServerInterceptor;
import com.predic8.membrane.core.interceptor.statistics.*;
import com.predic8.membrane.core.interceptor.xslt.XSLTInterceptor;
import jdk.jshell.spi.ExecutionControl;
t-burch marked this conversation as resolved.
Show resolved Hide resolved
import org.junit.jupiter.api.*;

@SuppressWarnings("unused")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class ConfigSerializationTest {
"stax-interceptor", // has external classpath dependencies
"soap", // has external classpath dependencies
"basic-xml-interceptor", // has external classpath dependencies
"database", // contains a reference to a DataSource bean (not serializable)
"template");

public static List<Object[]> getPorts() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
limitations under the License. */
package com.predic8.membrane.examples;

import com.predic8.membrane.examples.env.ConsistentVersionNumbersTest;
import com.predic8.membrane.examples.tests.integration.OAuth2Test;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectClasses({
ConfigSerializationTest.class,
ConsistentVersionNumbersTest.class,
OAuth2Test.class
})
public class ExampleUnitTests {}
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,12 @@ private static void transformRecursively(File baseDirectory, VersionTransformer
}

private static void handleConstants(File file, VersionTransformer versionTransformer) throws Exception {
// String version = "5"; // fallback
Pattern versionPattern = Pattern.compile("(\\s*String version = \")(\\d+)(\";.*)");
handleByRegex(file, versionTransformer, versionPattern, v -> v.getMajor().toString());
// String version = "5.1"; // fallback
handleByRegex(file, versionTransformer, getConstantsVersionPattern(), v -> "%d.%d".formatted(v.getMajor(), v.getMinor()));
}

static Pattern getConstantsVersionPattern() {
t-burch marked this conversation as resolved.
Show resolved Hide resolved
return Pattern.compile("(\\s*String version = \")(\\d+.\\d+)(\";.*)");
}

private static void handleRpmSpec(File file, VersionTransformer versionTransformer) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.predic8.membrane.examples.env;

import org.junit.jupiter.api.Test;

import static com.predic8.membrane.examples.env.ConsistentVersionNumbers.getConstantsVersionPattern;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ConsistentVersionNumbersTest {

@Test
public void constantsVersionRegexPatternMatchTest() {
assertTrue(getConstantsVersionPattern().matcher("String version = \"5.2\";").find());
}

@Test
public void constantsVersionRegexPatternNoMatchTest() {
assertTrue(!getConstantsVersionPattern().matcher("String version = \"5\";").find());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class JsonProtectionTest extends AbstractSampleMembraneStartStopTestcase

@Override
protected String getExampleDirName() {
return "json-protection";
return "security/json-protection";
}

private final HashMap<String, Integer> statusCodeFileMap = new HashMap<>() {{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void stopMembrane() throws IOException, InterruptedException {
void testIt() throws Exception {

BufferLogger logger = new BufferLogger();
try(Process2 ignored = new Process2.Builder().in(getExampleDir("oauth2/api")).withWatcher(logger).script("client").parameters("john password").waitAfterStartFor("200 O").start()) {
try(Process2 ignored = new Process2.Builder().in(getExampleDir("oauth2/api")).withWatcher(logger).script("client").parameters("john password").waitAfterStartFor("success").start()) {
assertTrue(logger.contains("""
{"success":"true"}
"""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void stopMembrane() throws IOException, InterruptedException {
void testIt() throws Exception {

BufferLogger logger = new BufferLogger();
try(Process2 ignored = new Process2.Builder().in(getExampleDir("oauth2/api")).withWatcher(logger).script("client").parameters("john password").waitAfterStartFor("200 O").start()) {
try(Process2 ignored = new Process2.Builder().in(getExampleDir("oauth2/api")).withWatcher(logger).script("client").parameters("john password").waitAfterStartFor("success").start()) {
assertTrue(logger.contains("""
{"success":"true"}
"""));
Expand Down
2 changes: 1 addition & 1 deletion membrane.spec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
%global logdir %{_var}/log/%{name}

Name: membrane
Version: 5.1.19
Version: 5.1.20-SNAPSHOT
Release: 1%{?dist}
URL: https://github.com/membrane/service-proxy
Summary: Membrane - Open Source API Gateway written in Java for REST APIs, WebSockets, STOMP and legacy Web Services
Expand Down