Plan/Plan/plugin/src/test/java/com/djrapitops/plan/system/webserver/HTTPSWebServerAuthTest.java

103 lines
3.9 KiB
Java
Raw Normal View History

package com.djrapitops.plan.system.webserver;
import com.djrapitops.plan.api.exceptions.connection.*;
2018-10-13 12:40:18 +02:00
import com.djrapitops.plan.data.WebUser;
import com.djrapitops.plan.system.PlanSystem;
2018-10-13 12:40:18 +02:00
import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plan.system.settings.paths.WebserverSettings;
import com.djrapitops.plan.utilities.Base64Util;
2018-10-13 12:40:18 +02:00
import com.djrapitops.plan.utilities.PassEncryptUtil;
[Merge] Module split (#786) * Moved project files to 'common'-module This is done so that refactoring into multiple smaller modules is easier as the IDE will not attempt to move tests incorrectly when moving things between different modules * Created 'bukkit' module Following classes were removed during the operation: - BukkitServerInfo (Renamed to ServerServerInfo) - SpongeServerInfo (could use ServerServerInfo) - Hastebin (not viable on every platform, unused) - HastebinTest - MockPlayers (unused, bukkit specific) Changes to classes: - Renamed Importer to BukkitImporter (contained bukkit related impl.) - Extracted Importer interface from BukkitImporter - Turned BukkitPlanModule and BukkitClassBindingModule to interfaces by using @Binds annotation - Added Status class since PlayersOnlineListener had a boolean, that stated if kicks were counted. This commit completes split partially and was not pushed on commit. * Created 'sponge' module Changes to classes: - Turned ServerSuperClassBindingModule, SuperClassBindingModule, SpongePlanModule and SpongeClassBindingModule to interfaces by using @Binds annotation - Renamed SystemObjectBindingModule to SystemObjectProvidingModule - Removed sponge related calls in ServerProperties - Made EmptyImportSystem injectable This commit completes split partially and was not pushed on commit. * Changed shade configuration Further tweaks required. * Created 'bungeecord' module Changes to classes: - Removed use of BungeePingTimerTask in VelocityTaskSystem - Removed use of RedisBungee in VelocityServerProperties - Fixed bukkit command.commands.RegisterCommandFilter package Changes to project structure: - Dependency versions now in main pom via dependencyManagement - Repositories now defined in main pom * Created 'velocity' module Changes to classes: - Made RawDataResponse use Gson via reflection since it is no longer available in dependencies of 'common' module * Created 'plugin' module This module is for creating a single deployment artifact and testing of system interactions. Fixes to tests: - Reflection no longer fails to initialize if Bukkit.getServer() is null - PingCountTimerBukkit no longer fails to be created if Reflection fails - Removed unnecessary @AfterClass from H2Test - Jar resource path fixes to Mocker * Shading configuration org.slf4j classes are relocated in 'common', 'bukkit' & 'bungeecord'. In 'sponge' & 'velocity' they are not relocated, allowing injection as plugin logger, while using slf4j-nop for HikariCP logging. This allows single release artifact. * Removed 'Icon' from .gitignore * Attempt to fix test resources Because all jar resources are located in 'common', an attempt to fetch them is made to common/target/Plan-common.jar, which for some reason is a bad path. This attempts to remedy that by creating a temporary file from an InputStream, read with Class#getResourceAsStream Ignored HTTPSWebServerAuthTest as the certificate path was invalid for some reason.
2018-11-11 11:55:09 +01:00
import org.junit.*;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import rules.BukkitComponentMocker;
import rules.ComponentMocker;
import utilities.HTTPConnector;
import utilities.RandomData;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
@RunWith(MockitoJUnitRunner.Silent.class)
public class HTTPSWebServerAuthTest {
private static final int TEST_PORT_NUMBER = RandomData.randomInt(9005, 9500);
@ClassRule
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
@ClassRule
public static ComponentMocker component = new BukkitComponentMocker(temporaryFolder);
private static PlanSystem bukkitSystem;
private HTTPConnector connector = new HTTPConnector();
@BeforeClass
public static void setUpClass() throws Exception {
URL resource = HTTPSWebServerAuthTest.class.getResource("/Cert.keystore");
String keyStore = resource.getPath();
String absolutePath = new File(keyStore).getAbsolutePath();
bukkitSystem = component.getPlanSystem();
2018-10-13 12:40:18 +02:00
PlanConfig config = bukkitSystem.getConfigSystem().getConfig();
config.set(WebserverSettings.CERTIFICATE_PATH, absolutePath);
config.set(WebserverSettings.CERTIFICATE_KEYPASS, "MnD3bU5HpmPXag0e");
config.set(WebserverSettings.CERTIFICATE_STOREPASS, "wDwwf663NLTm73gL");
config.set(WebserverSettings.CERTIFICATE_ALIAS, "DefaultPlanCert");
2018-10-13 12:40:18 +02:00
config.set(WebserverSettings.PORT, TEST_PORT_NUMBER);
2018-10-13 12:40:18 +02:00
bukkitSystem.enable();
bukkitSystem.getDatabaseSystem().getDatabase().save()
.webUser(new WebUser("test", PassEncryptUtil.createHash("testPass"), 0));
}
@AfterClass
public static void tearDownClass() {
if (bukkitSystem != null) {
bukkitSystem.disable();
}
}
/**
* Test case against "Perm level 0 required, got 0".
*/
@Test
[Merge] Module split (#786) * Moved project files to 'common'-module This is done so that refactoring into multiple smaller modules is easier as the IDE will not attempt to move tests incorrectly when moving things between different modules * Created 'bukkit' module Following classes were removed during the operation: - BukkitServerInfo (Renamed to ServerServerInfo) - SpongeServerInfo (could use ServerServerInfo) - Hastebin (not viable on every platform, unused) - HastebinTest - MockPlayers (unused, bukkit specific) Changes to classes: - Renamed Importer to BukkitImporter (contained bukkit related impl.) - Extracted Importer interface from BukkitImporter - Turned BukkitPlanModule and BukkitClassBindingModule to interfaces by using @Binds annotation - Added Status class since PlayersOnlineListener had a boolean, that stated if kicks were counted. This commit completes split partially and was not pushed on commit. * Created 'sponge' module Changes to classes: - Turned ServerSuperClassBindingModule, SuperClassBindingModule, SpongePlanModule and SpongeClassBindingModule to interfaces by using @Binds annotation - Renamed SystemObjectBindingModule to SystemObjectProvidingModule - Removed sponge related calls in ServerProperties - Made EmptyImportSystem injectable This commit completes split partially and was not pushed on commit. * Changed shade configuration Further tweaks required. * Created 'bungeecord' module Changes to classes: - Removed use of BungeePingTimerTask in VelocityTaskSystem - Removed use of RedisBungee in VelocityServerProperties - Fixed bukkit command.commands.RegisterCommandFilter package Changes to project structure: - Dependency versions now in main pom via dependencyManagement - Repositories now defined in main pom * Created 'velocity' module Changes to classes: - Made RawDataResponse use Gson via reflection since it is no longer available in dependencies of 'common' module * Created 'plugin' module This module is for creating a single deployment artifact and testing of system interactions. Fixes to tests: - Reflection no longer fails to initialize if Bukkit.getServer() is null - PingCountTimerBukkit no longer fails to be created if Reflection fails - Removed unnecessary @AfterClass from H2Test - Jar resource path fixes to Mocker * Shading configuration org.slf4j classes are relocated in 'common', 'bukkit' & 'bungeecord'. In 'sponge' & 'velocity' they are not relocated, allowing injection as plugin logger, while using slf4j-nop for HikariCP logging. This allows single release artifact. * Removed 'Icon' from .gitignore * Attempt to fix test resources Because all jar resources are located in 'common', an attempt to fetch them is made to common/target/Plan-common.jar, which for some reason is a bad path. This attempts to remedy that by creating a temporary file from an InputStream, read with Class#getResourceAsStream Ignored HTTPSWebServerAuthTest as the certificate path was invalid for some reason.
2018-11-11 11:55:09 +01:00
@Ignore("HTTPS Start fails due to paths being bad for some reason")
public void testHTTPSAuthForPages() throws IOException, WebException, KeyManagementException, NoSuchAlgorithmException {
String address = "https://localhost:" + TEST_PORT_NUMBER;
URL url = new URL(address);
HttpURLConnection connection = connector.getConnection("HET", address);
String user = Base64Util.encode("test:testPass");
connection.setRequestProperty("Authorization", "Basic " + user);
int responseCode = connection.getResponseCode();
switch (responseCode) {
case 200:
2018-10-13 12:40:18 +02:00
case 302:
return;
case 400:
throw new BadRequestException("Bad Request: " + url.toString());
case 403:
throw new ForbiddenException(url.toString() + " returned 403");
case 404:
throw new NotFoundException(url.toString() + " returned a 404, ensure that your server is connected to an up to date Plan server.");
case 412:
throw new UnauthorizedServerException(url.toString() + " reported that it does not recognize this server. Make sure '/plan m setup' was successful.");
case 500:
throw new InternalErrorException();
default:
throw new WebException(url.toString() + "| Wrong response code " + responseCode);
}
}
}