diff --git a/src/main/java/org/jboss/tools/intellij/openshift/telemetry/TelemetrySender.java b/src/main/java/org/jboss/tools/intellij/openshift/telemetry/TelemetrySender.java index 2a639308a..d91c3ac4e 100644 --- a/src/main/java/org/jboss/tools/intellij/openshift/telemetry/TelemetrySender.java +++ b/src/main/java/org/jboss/tools/intellij/openshift/telemetry/TelemetrySender.java @@ -26,7 +26,7 @@ public class TelemetrySender implements TelemetryHandler { private static final Pattern CLUSTER_URL_PATTERN = Pattern.compile("http(.*)/apis", Pattern.CASE_INSENSITIVE); public static final String ANONYMOUS_CLUSTER_URL = ""; - private static final Pattern TOKEN_PATTERN = Pattern.compile("token-(.*):([\\d]+)", Pattern.CASE_INSENSITIVE); + private static final Pattern TOKEN_PATTERN = Pattern.compile("token-(.*):(\\d+)", Pattern.CASE_INSENSITIVE); public static final String ANONYMOUS_TOKEN = ""; private final TelemetryMessageBuilder.ActionMessage telemetry; diff --git a/src/main/java/org/jboss/tools/intellij/openshift/ui/cluster/TokenExtractor.java b/src/main/java/org/jboss/tools/intellij/openshift/ui/cluster/TokenExtractor.java index 6f92668dc..4ffbd384c 100644 --- a/src/main/java/org/jboss/tools/intellij/openshift/ui/cluster/TokenExtractor.java +++ b/src/main/java/org/jboss/tools/intellij/openshift/ui/cluster/TokenExtractor.java @@ -10,31 +10,25 @@ ******************************************************************************/ package org.jboss.tools.intellij.openshift.ui.cluster; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; /** * Helper class to handle Openshift token authentication response - * */ public class TokenExtractor { - /** - * Regular expression used to check if browser page is page that displays the OAuth token - */ - public static final Pattern TOKEN_PAGE_PATTERN = Pattern - .compile(".*

Your API token is<\\/h2>.*(.*)<\\/code>.*", Pattern.DOTALL); - private final Matcher matcher; + private final Document doc; - public TokenExtractor(String content) { - matcher = TOKEN_PAGE_PATTERN.matcher(content); - } + public TokenExtractor(String content) { + doc = Jsoup.parse(content); + } - public boolean isTokenPage() { - return matcher.matches(); - } + public boolean isTokenPage() { + return doc.select("h2").text().contains("Your API token is"); + } - public String getToken() { - return matcher.group(1); - } + public String getToken() { + return doc.select("h2").next().select("code").text(); + } } diff --git a/src/main/java/org/jboss/tools/intellij/openshift/ui/sandbox/CountryCodeValidator.java b/src/main/java/org/jboss/tools/intellij/openshift/ui/sandbox/CountryCodeValidator.java index 0ff300b3b..d3ebe46ef 100644 --- a/src/main/java/org/jboss/tools/intellij/openshift/ui/sandbox/CountryCodeValidator.java +++ b/src/main/java/org/jboss/tools/intellij/openshift/ui/sandbox/CountryCodeValidator.java @@ -17,27 +17,24 @@ import java.util.regex.Pattern; public class CountryCodeValidator implements Supplier { - private final JTextComponent component; + private final JTextComponent component; - /* - * see https://github.com/codeready-toolchain/registration-service/blob/master/pkg/assets/landingpage.js - */ - private static final Pattern pattern = Pattern - .compile("^[+]?\\d+$"); + private static final Pattern pattern = Pattern + .compile("^[+]?\\d{1,3}$"); - public CountryCodeValidator(JTextComponent component) { - this.component = component; - } + public CountryCodeValidator(JTextComponent component) { + this.component = component; + } - @Override - public ValidationInfo get() { - String text = component.getText(); - if (text.isEmpty()) { - return new ValidationInfo("Please provide a country code", component); - } - if (pattern.matcher(text).matches()) { - return null; - } - return new ValidationInfo("Country code must be a number", component); + @Override + public ValidationInfo get() { + String text = component.getText(); + if (text.isEmpty()) { + return new ValidationInfo("Please provide a country code", component); + } + if (pattern.matcher(text).matches()) { + return null; } + return new ValidationInfo("Country code must be a number", component); + } } diff --git a/src/main/java/org/jboss/tools/intellij/openshift/ui/sandbox/PhoneNumberValidator.java b/src/main/java/org/jboss/tools/intellij/openshift/ui/sandbox/PhoneNumberValidator.java index 1dc7c9d7f..99aa51532 100644 --- a/src/main/java/org/jboss/tools/intellij/openshift/ui/sandbox/PhoneNumberValidator.java +++ b/src/main/java/org/jboss/tools/intellij/openshift/ui/sandbox/PhoneNumberValidator.java @@ -17,27 +17,27 @@ import java.util.regex.Pattern; public class PhoneNumberValidator implements Supplier { - private final JTextComponent component; + private final JTextComponent component; - /* - * see https://github.com/codeready-toolchain/registration-service/blob/master/pkg/assets/landingpage.js - */ - private static final Pattern pattern = Pattern - .compile("^[(]?[0-9]+[)]?[-\\s\\.]?[0-9]+[-\\s\\.\\/0-9]*$", Pattern.CASE_INSENSITIVE); + private static final Pattern pattern = Pattern + .compile("^[(]?\\d{2,3}[)]?[ \\-./]?\\d{2,3}[ \\-./]?\\d{2,4}[ \\-./]?\\d{0,4}[ \\-./]?\\d{0,4}$", Pattern.CASE_INSENSITIVE); - public PhoneNumberValidator(JTextComponent component) { - this.component = component; - } + public PhoneNumberValidator(JTextComponent component) { + this.component = component; + } - @Override - public ValidationInfo get() { - String text = component.getText(); - if (text.isEmpty()) { - return new ValidationInfo("Please provide a phone number", component); - } - if (pattern.matcher(text).matches()) { - return null; - } - return new ValidationInfo("Phone number should be digits optionally separated by - or ' '", component); + @Override + public ValidationInfo get() { + String text = component.getText(); + if (text.isEmpty()) { + return new ValidationInfo("Please provide a phone number", component); + } + if (pattern.matcher(text).matches()) { + if (text.chars().allMatch(Character::isDigit) && text.length() < 7) { + return new ValidationInfo("Phone number should be at least 7 digits", component); + } + return null; } + return new ValidationInfo("Phone number should be digits optionally separated by space or '-' or '.' or '/'", component); + } } diff --git a/src/main/java/org/jboss/tools/intellij/openshift/utils/OCCommandUtils.java b/src/main/java/org/jboss/tools/intellij/openshift/utils/OCCommandUtils.java index bf3e9c371..2610751a6 100644 --- a/src/main/java/org/jboss/tools/intellij/openshift/utils/OCCommandUtils.java +++ b/src/main/java/org/jboss/tools/intellij/openshift/utils/OCCommandUtils.java @@ -20,107 +20,106 @@ */ public final class OCCommandUtils { - private OCCommandUtils() { - //hide constructor - } + private OCCommandUtils() { + //hide constructor + } - /** - * Validates is oc command is in correct format with required fields. - * - * @param ocCommand the oc command - * @return false command has not correct format - */ - public static boolean isValidCommand(String ocCommand) { - ocCommand = ocCommand.trim(); - return (ocCommand.startsWith("oc login ") && isValidAuthMethod(ocCommand) - && getServer(ocCommand) != null); - } + /** + * Validates is oc command is in correct format with required fields. + * + * @param ocCommand the oc command + * @return false command has not correct format + */ + public static boolean isValidCommand(String ocCommand) { + ocCommand = ocCommand.trim(); + return (ocCommand.startsWith("oc login ") && isValidAuthMethod(ocCommand) + && getServer(ocCommand) != null); + } - /** - * Returns true if authorization schema of oc command is valid (basic/OAuth). - * - * @param ocCommand the oc command - * @return false command has not correct authorization schema - */ - public static boolean isValidAuthMethod(String ocCommand) { - ocCommand = ocCommand.trim(); - return (ocCommand.contains(" -u") || ocCommand.contains(" --username") || ocCommand.contains("--token=")); - } + /** + * Returns true if authorization schema of oc command is valid (basic/OAuth). + * + * @param ocCommand the oc command + * @return false command has not correct authorization schema + */ + private static boolean isValidAuthMethod(String ocCommand) { + return (ocCommand.contains(" -u") || ocCommand.contains(" --username") || ocCommand.contains("--token=")); + } - /** - * Parses server address from oc command. - * - * @param ocCommand the oc command - * @return server address - */ - public static String getServer(String ocCommand) { - String server = applyPattern(ocCommand, "(?<=\\s)https[a-zA-Z0-9:/.-]+", 0); - if (server != null) { - return server; - } else { - return applyPattern(ocCommand, "(?<=[\\s=])https[a-zA-Z0-9:/.-]+", 0); - } + /** + * Parses server address from oc command. + * + * @param ocCommand the oc command + * @return server address + */ + public static String getServer(String ocCommand) { + String server = applyPattern(ocCommand, "(?<=\\s)https[a-zA-Z0-9:/.-]+", 0); + if (server != null) { + return server; + } else { + return applyPattern(ocCommand, "(?<=[\\s=])https[a-zA-Z0-9:/.-]+", 0); } + } - /** - * Parses token from oc command. - * - * @param ocCommand the oc command - * @return token - */ - public static String getToken(String ocCommand) { - return applyPattern(ocCommand, "(?<=--token=)[~a-zA-Z0-9:._-]+", 0); - } + /** + * Parses token from oc command. + * + * @param ocCommand the oc command + * @return token + */ + public static String getToken(String ocCommand) { + return applyPattern(ocCommand, "(?<=--token=)[~a-zA-Z0-9:._-]+", 0); + } - /** - * Parses username from oc command. - * - * @param ocCommand the oc command - * @return username - */ - public static String getUsername(String ocCommand) { - String username = applyPattern(ocCommand, "(?<=-u[\\s=])[a-zA-Z0-9:]+", 0); - if (username != null) { - return username; - } else { - return applyPattern(ocCommand, "(?<=--username[\\s=])[a-zA-Z0-9:]+", 0); - } + /** + * Parses username from oc command. + * + * @param ocCommand the oc command + * @return username + */ + public static String getUsername(String ocCommand) { + String username = applyPattern(ocCommand, "(?<=-u[\\s=])[a-zA-Z0-9:]+", 0); + if (username != null) { + return username; + } else { + return applyPattern(ocCommand, "(?<=--username[\\s=])[a-zA-Z0-9:]+", 0); } + } - /** - * Parses password from oc command. - * - * @param ocCommand the oc command - * @return password - */ - public static String getPassword(String ocCommand) { - String password = searchInStringForPattern(ocCommand, "(?<=-p[\\s=])(.*)(?=\\b)"); - if (password != null) { - return password; - } else { - return searchInStringForPattern(ocCommand, "(?<=--password[\\s=])(.*)(?=\\b)"); - } + /** + * Parses password from oc command. + * + * @param ocCommand the oc command + * @return password + */ + public static String getPassword(String ocCommand) { + String password = searchInStringForPattern(ocCommand, "(?<=-p[\\s=])([^\\s ]*)(?=\\b)"); + if (password != null) { + return password; + } else { + return searchInStringForPattern(ocCommand, "(?<=--password[\\s=])([^\\s ]*)(?=\\b)"); } + } - private static String searchInStringForPattern(String stringToVerify, String pattern) { - if (stringToVerify.contains("-p")) { - return applyPattern(stringToVerify, pattern); - } - return null; + private static String searchInStringForPattern(String stringToVerify, String pattern) { + if (stringToVerify.contains("-p")) { + return applyPattern(stringToVerify, pattern); } + return null; + } - private static String applyPattern(String stringToVerify, String pattern) { - return applyPattern(stringToVerify, pattern, 1); - } + private static String applyPattern(String stringToVerify, String pattern) { + return applyPattern(stringToVerify, pattern, 1); + } - private static String applyPattern(String stringToVerify, String pattern, int group) { - stringToVerify = stringToVerify.trim(); - Pattern patternToken = Pattern.compile(pattern); - java.util.regex.Matcher matcherToken = patternToken.matcher(stringToVerify); - if (matcherToken.find()) { - return matcherToken.group(group); - } - return null; + private static String applyPattern(String stringToVerify, String pattern, int group) { + stringToVerify = stringToVerify.trim(); + Pattern patternToken = Pattern.compile(pattern); + java.util.regex.Matcher matcherToken = patternToken.matcher(stringToVerify); + if (matcherToken.find()) { + return matcherToken.group(group); } + return null; + } } \ No newline at end of file diff --git a/src/test/java/org/jboss/tools/intellij/openshift/ui/cluster/TokenExtractorTest.java b/src/test/java/org/jboss/tools/intellij/openshift/ui/cluster/TokenExtractorTest.java new file mode 100644 index 000000000..15f488fa5 --- /dev/null +++ b/src/test/java/org/jboss/tools/intellij/openshift/ui/cluster/TokenExtractorTest.java @@ -0,0 +1,52 @@ +/******************************************************************************* + * Copyright (c) 2024 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package org.jboss.tools.intellij.openshift.ui.cluster; + + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class TokenExtractorTest { + + @Test + public void checkTokenExtractionOk() { + String contents = "

Your API token is

\n sha256~abcd-1234567890ABCDEF\n"; + TokenExtractor extractor = new TokenExtractor(contents); + assertTrue(extractor.isTokenPage()); + assertEquals("sha256~abcd-1234567890ABCDEF", extractor.getToken()); + } + + @Test + public void checkTokenExtractionEmpty() { + String contents = "

Your API token is

\n \n"; + TokenExtractor extractor = new TokenExtractor(contents); + assertTrue(extractor.isTokenPage()); + assertEquals("", extractor.getToken()); + } + + @Test + public void checkTokenExtractionFails() { + String contents = ""; + TokenExtractor extractor = new TokenExtractor(contents); + assertFalse(extractor.isTokenPage()); + } + + @Test + public void checkTokenExtractionWithMultipleH2() { + String contents = "

Your API token is

\n sha256~abcd-1234567890ABCDEF\n

Log in with this token

\n
oc login --token=sha256~abcd-1234567890ABCDEF --server=https://url.com:1234
"; + TokenExtractor extractor = new TokenExtractor(contents); + assertTrue(extractor.isTokenPage()); + assertEquals("sha256~abcd-1234567890ABCDEF", extractor.getToken()); + } +} diff --git a/src/test/java/org/jboss/tools/intellij/openshift/ui/sandbox/CountryCodeValidatorTest.java b/src/test/java/org/jboss/tools/intellij/openshift/ui/sandbox/CountryCodeValidatorTest.java new file mode 100644 index 000000000..1794c1800 --- /dev/null +++ b/src/test/java/org/jboss/tools/intellij/openshift/ui/sandbox/CountryCodeValidatorTest.java @@ -0,0 +1,64 @@ +/******************************************************************************* + * Copyright (c) 2024 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package org.jboss.tools.intellij.openshift.ui.sandbox; + +import com.intellij.openapi.ui.ValidationInfo; +import org.junit.Test; + +import javax.swing.JTextField; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + + +public class CountryCodeValidatorTest { + + JTextField textField = new JTextField(); + CountryCodeValidator validator = new CountryCodeValidator(textField); + + @Test + public void testEmptyCountryCode() { + textField.setText(""); + ValidationInfo result = validator.get(); + assertNotNull(result); + assertEquals("Please provide a country code", result.message); + } + + @Test + public void testValidCountry() { + List data = Arrays.asList("123", + "+001", + "+33"); + for (String code : data) { + textField.setText(code); + ValidationInfo result = validator.get(); + assertNull(result); + } + } + + @Test + public void testInValidCountry() { + List data = Arrays.asList("1234", + "abc", + "+1a"); + for (String code : data) { + textField.setText(code); + ValidationInfo result = validator.get(); + assertNotNull(result); + assertTrue(result.message, result.message.startsWith("Country code must be a number")); + } + } + +} diff --git a/src/test/java/org/jboss/tools/intellij/openshift/ui/sandbox/PhoneNumberValidatorTest.java b/src/test/java/org/jboss/tools/intellij/openshift/ui/sandbox/PhoneNumberValidatorTest.java new file mode 100644 index 000000000..1873fae8a --- /dev/null +++ b/src/test/java/org/jboss/tools/intellij/openshift/ui/sandbox/PhoneNumberValidatorTest.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * Copyright (c) 2024 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package org.jboss.tools.intellij.openshift.ui.sandbox; + +import com.intellij.openapi.ui.ValidationInfo; +import org.junit.Test; + +import javax.swing.JTextField; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + + +public class PhoneNumberValidatorTest { + + JTextField textField = new JTextField(); + PhoneNumberValidator validator = new PhoneNumberValidator(textField); + + @Test + public void testEmptyPhoneNumber() { + textField.setText(""); + ValidationInfo result = validator.get(); + assertNotNull(result); + assertEquals("Please provide a phone number", result.message); + } + + @Test + public void testValidPhoneNumber() { + List data = Arrays.asList("123-456-7890", + "(123) 456-7890", + "(123)4567890", + "123.456.7890", + "123/456/7890", + "1234567890", + "12 34 56 78 90", + "12.34.56.78.90"); + for (String phoneNumber : data) { + textField.setText(phoneNumber); + ValidationInfo result = validator.get(); + assertNull(result); + } + } + + @Test + public void testInValidPhoneNumber() { + List data = Arrays.asList("123+456+7890", + "123..456..7890", + "-123 456 7890", + "[123] 456 7890", + "abcdefeghi", + "123456789a", + "..-- //", + "()1234567890", + "123456", + "1234567890123456789"); + for (String phoneNumber : data) { + textField.setText(phoneNumber); + ValidationInfo result = validator.get(); + assertNotNull(result); + assertTrue(result.message, result.message.startsWith("Phone number should be")); + } + } + +} diff --git a/src/test/java/org/jboss/tools/intellij/openshift/ui/sandbox/SandboxProcessorTest.java b/src/test/java/org/jboss/tools/intellij/openshift/ui/sandbox/SandboxProcessorTest.java index 3aaa6ffae..15da27474 100644 --- a/src/test/java/org/jboss/tools/intellij/openshift/ui/sandbox/SandboxProcessorTest.java +++ b/src/test/java/org/jboss/tools/intellij/openshift/ui/sandbox/SandboxProcessorTest.java @@ -24,119 +24,120 @@ import static org.jboss.tools.intellij.openshift.ui.sandbox.SandboxProcessor.State; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; /** * @author Jeff MAURY - * */ public class SandboxProcessorTest { - - private MockServerClient mockServer; - private SandboxModel mockModel; - - @Before - public void init() { - mockServer = ClientAndServer.startClientAndServer(0); - mockModel = mock(SandboxModel.class); - } - - @After - public void shutdown() { - mockServer.stop(); - } - - @Test - public void checkProcessorIsInitialized() { - SandboxProcessor processor = new SandboxProcessor("token", "http://localhost:1080"); - assertEquals(State.NONE, processor.getState()); - } - @Test - public void checkNeedsSignupIf404() throws IOException { - mockServer.when(HttpRequest.request() - .withPath("/api/v1/signup") - .withMethod("GET")) - .respond(HttpResponse.notFoundResponse()); - SandboxProcessor processor = new SandboxProcessor("token", "http://localhost:" + mockServer.getPort()); - assertEquals(State.NONE, processor.getState()); - State state = processor.advance(mockModel); - assertEquals(State.NEEDS_SIGNUP, state); - } - - @Test - public void checkNeedsVerification() throws IOException { - mockServer.when(HttpRequest.request() - .withPath("/api/v1/signup") - .withMethod("GET")) - .respond(HttpResponse.response("{ \"status\": { \"ready\": false, \"verificationRequired\": true}}")); - SandboxProcessor processor = new SandboxProcessor("token", "http://localhost:" + mockServer.getPort()); - assertEquals(State.NONE, processor.getState()); - State state = processor.advance(mockModel); - assertEquals(State.NEEDS_VERIFICATION, state); - } - - @Test - public void checkNeedsApproval() throws IOException { - mockServer.when(HttpRequest.request() - .withPath("/api/v1/signup") - .withMethod("GET")) - .respond(HttpResponse.response("{ \"status\": { \"ready\": false, \"verificationRequired\": false}}")); - SandboxProcessor processor = new SandboxProcessor("token", "http://localhost:" + mockServer.getPort()); - assertEquals(State.NONE, processor.getState()); - State state = processor.advance(mockModel); - assertEquals(State.NEEDS_APPROVAL, state); - } + private MockServerClient mockServer; + private SandboxModel mockModel; + + @Before + public void init() { + mockServer = ClientAndServer.startClientAndServer(0); + mockModel = mock(SandboxModel.class); + } + + @After + public void shutdown() { + mockServer.stop(); + } + + @Test + public void checkProcessorIsInitialized() { + SandboxProcessor processor = new SandboxProcessor("token", "http://localhost:1080"); + assertEquals(State.NONE, processor.getState()); + } + + @Test + public void checkNeedsSignupIf404() throws IOException { + mockServer.when(HttpRequest.request() + .withPath("/api/v1/signup") + .withMethod("GET")) + .respond(HttpResponse.notFoundResponse()); + SandboxProcessor processor = new SandboxProcessor("token", "http://localhost:" + mockServer.getPort()); + assertEquals(State.NONE, processor.getState()); + State state = processor.advance(mockModel); + assertEquals(State.NEEDS_SIGNUP, state); + } + + @Test + public void checkNeedsVerification() throws IOException { + mockServer.when(HttpRequest.request() + .withPath("/api/v1/signup") + .withMethod("GET")) + .respond(HttpResponse.response("{ \"status\": { \"ready\": false, \"verificationRequired\": true}}")); + SandboxProcessor processor = new SandboxProcessor("token", "http://localhost:" + mockServer.getPort()); + assertEquals(State.NONE, processor.getState()); + State state = processor.advance(mockModel); + assertEquals(State.NEEDS_VERIFICATION, state); + } + + @Test + public void checkNeedsApproval() throws IOException { + mockServer.when(HttpRequest.request() + .withPath("/api/v1/signup") + .withMethod("GET")) + .respond(HttpResponse.response("{ \"status\": { \"ready\": false, \"verificationRequired\": false}}")); + SandboxProcessor processor = new SandboxProcessor("token", "http://localhost:" + mockServer.getPort()); + assertEquals(State.NONE, processor.getState()); + State state = processor.advance(mockModel); + assertEquals(State.NEEDS_APPROVAL, state); + } + + @Test + public void checkReady() throws IOException { + mockServer.when(HttpRequest.request() + .withPath("/api/v1/signup") + .withMethod("GET")) + .respond(HttpResponse.response("{ \"status\": { \"ready\": true}}")); + SandboxProcessor processor = new SandboxProcessor("token", "http://localhost:" + mockServer.getPort()); + assertEquals(State.NONE, processor.getState()); + State state = processor.advance(mockModel); + assertEquals(State.READY, state); + } - @Test - public void checkReady() throws IOException { - mockServer.when(HttpRequest.request() - .withPath("/api/v1/signup") - .withMethod("GET")) - .respond(HttpResponse.response("{ \"status\": { \"ready\": true}}")); - SandboxProcessor processor = new SandboxProcessor("token", "http://localhost:" + mockServer.getPort()); - assertEquals(State.NONE, processor.getState()); - State state = processor.advance(mockModel); - assertEquals(State.READY, state); - } + @Test + public void checkConfirmVerification() throws IOException { + mockServer.when(HttpRequest.request() + .withPath("/api/v1/signup") + .withMethod("GET")) + .respond(HttpResponse.response("{ \"status\": { \"ready\": false, \"verificationRequired\": true}}")); + mockServer.when(HttpRequest.request() + .withPath("/api/v1/signup/verification") + .withMethod("PUT")) + .respond(HttpResponse.response().withStatusCode(204)); + SandboxProcessor processor = new SandboxProcessor("token", "http://localhost:" + mockServer.getPort()); + assertEquals(State.NONE, processor.getState()); + State state = processor.advance(mockModel); + assertEquals(State.NEEDS_VERIFICATION, state); + state = processor.advance(mockModel); + assertEquals(State.CONFIRM_VERIFICATION, state); + } - @Test - public void checkConfirmVerification() throws IOException { - mockServer.when(HttpRequest.request() - .withPath("/api/v1/signup") - .withMethod("GET")) - .respond(HttpResponse.response("{ \"status\": { \"ready\": false, \"verificationRequired\": true}}")); - mockServer.when(HttpRequest.request() - .withPath("/api/v1/signup/verification") - .withMethod("PUT")) - .respond(HttpResponse.response().withStatusCode(204)); - SandboxProcessor processor = new SandboxProcessor("token", "http://localhost:" + mockServer.getPort()); - assertEquals(State.NONE, processor.getState()); - State state = processor.advance(mockModel); - assertEquals(State.NEEDS_VERIFICATION, state); - state = processor.advance(mockModel); - assertEquals(State.CONFIRM_VERIFICATION, state); - } - - @Test - public void checkConfirmVerificationTimeout() throws IOException { - mockServer.when(HttpRequest.request() - .withPath("/api/v1/signup") - .withMethod("GET")) - .respond(HttpResponse.response("{ \"status\": { \"ready\": false, \"verificationRequired\": true}}")); - mockServer.when(HttpRequest.request() - .withPath("/api/v1/signup/verification") - .withMethod("PUT")) - .respond(HttpResponse.response().withStatusCode(204).withDelay(TimeUnit.SECONDS, 45)); - SandboxProcessor processor = new SandboxProcessor("token", "http://localhost:" + mockServer.getPort()); - assertEquals(State.NONE, processor.getState()); - State state = processor.advance(mockModel); - assertEquals(State.NEEDS_VERIFICATION, state); - try { - state = processor.advance(mockModel); - fail("should timeout"); - } catch (SocketTimeoutException e) { - } - } + @Test + public void checkConfirmVerificationTimeout() throws IOException { + mockServer.when(HttpRequest.request() + .withPath("/api/v1/signup") + .withMethod("GET")) + .respond(HttpResponse.response("{ \"status\": { \"ready\": false, \"verificationRequired\": true}}")); + mockServer.when(HttpRequest.request() + .withPath("/api/v1/signup/verification") + .withMethod("PUT")) + .respond(HttpResponse.response().withStatusCode(204).withDelay(TimeUnit.SECONDS, 45)); + SandboxProcessor processor = new SandboxProcessor("token", "http://localhost:" + mockServer.getPort()); + assertEquals(State.NONE, processor.getState()); + State state = processor.advance(mockModel); + assertEquals(State.NEEDS_VERIFICATION, state); + try { + processor.advance(mockModel); + fail("should timeout"); + } catch (SocketTimeoutException e) { + assertNotNull(e.getMessage()); + } + } } diff --git a/src/test/java/org/jboss/tools/intellij/openshift/utils/OCCommandUtilsTest.java b/src/test/java/org/jboss/tools/intellij/openshift/utils/OCCommandUtilsTest.java index 22c5f0e20..2c195b568 100644 --- a/src/test/java/org/jboss/tools/intellij/openshift/utils/OCCommandUtilsTest.java +++ b/src/test/java/org/jboss/tools/intellij/openshift/utils/OCCommandUtilsTest.java @@ -19,97 +19,107 @@ /** * Unit tests for OCCommandUtils for testing correct parsing of oc login command + * * @author Josef Kopriva */ public class OCCommandUtilsTest { - @Test - public void testCommandWithToken() { - assertEquals("123456789123456789", - OCCommandUtils.getToken("oc login https://api.engint.openshift.com --token=123456789123456789")); - assertEquals("123456789123456789", - OCCommandUtils.getToken("oc login https://api.-uengint.openshift.com --token=123456789123456789 ")); - assertEquals("1234567891234567..89", - OCCommandUtils.getToken("oc login https://api.-uengint.openshift.com --token=1234567891234567..89 ")); - assertEquals("12345678912345678_9", - OCCommandUtils.getToken("oc login https://api.-uengint.openshift.com --token=12345678912345678_9 ")); - assertEquals("1234567891234567.89", - OCCommandUtils.getToken("oc login https://api.-uengint.openshift.com --token=1234567891234567.89 ")); - assertEquals("1234567891234567-89", - OCCommandUtils.getToken("oc login https://api.-uengint.openshift.com --token=1234567891234567-89 ")); - } + @Test + public void testCommandWithToken() { + assertEquals("123456789123456789", + OCCommandUtils.getToken("oc login https://api.engint.openshift.com --token=123456789123456789")); + assertEquals("123456789123456789", + OCCommandUtils.getToken("oc login https://api.-uengint.openshift.com --token=123456789123456789 ")); + assertEquals("1234567891234567..89", + OCCommandUtils.getToken("oc login https://api.-uengint.openshift.com --token=1234567891234567..89 ")); + assertEquals("12345678912345678_9", + OCCommandUtils.getToken("oc login https://api.-uengint.openshift.com --token=12345678912345678_9 ")); + assertEquals("1234567891234567.89", + OCCommandUtils.getToken("oc login https://api.-uengint.openshift.com --token=1234567891234567.89 ")); + assertEquals("1234567891234567-89", + OCCommandUtils.getToken("oc login https://api.-uengint.openshift.com --token=1234567891234567-89 ")); + } + + @Test + public void testCommandWithTokenOCP4() { + assertEquals("sha256~123456789123456789", + OCCommandUtils.getToken("oc login https://api.engint.openshift.com --token=sha256~123456789123456789")); + assertEquals("sha256~123456789123456789", + OCCommandUtils.getToken("oc login https://api.-uengint.openshift.com --token=sha256~123456789123456789 ")); + assertEquals("sha256~1234567891234567..89", + OCCommandUtils.getToken("oc login https://api.-uengint.openshift.com --token=sha256~1234567891234567..89 ")); + assertEquals("sha256~12345678912345678_9", + OCCommandUtils.getToken("oc login https://api.-uengint.openshift.com --token=sha256~12345678912345678_9 ")); + assertEquals("sha256~1234567891234567.89", + OCCommandUtils.getToken("oc login https://api.-uengint.openshift.com --token=sha256~1234567891234567.89 ")); + assertEquals("sha256~1234567891234567-89", + OCCommandUtils.getToken("oc login https://api.-uengint.openshift.com --token=sha256~1234567891234567-89 ")); + } - @Test - public void testCommandWithTokenOCP4() { - assertEquals("sha256~123456789123456789", - OCCommandUtils.getToken("oc login https://api.engint.openshift.com --token=sha256~123456789123456789")); - assertEquals("sha256~123456789123456789", - OCCommandUtils.getToken("oc login https://api.-uengint.openshift.com --token=sha256~123456789123456789 ")); - assertEquals("sha256~1234567891234567..89", - OCCommandUtils.getToken("oc login https://api.-uengint.openshift.com --token=sha256~1234567891234567..89 ")); - assertEquals("sha256~12345678912345678_9", - OCCommandUtils.getToken("oc login https://api.-uengint.openshift.com --token=sha256~12345678912345678_9 ")); - assertEquals("sha256~1234567891234567.89", - OCCommandUtils.getToken("oc login https://api.-uengint.openshift.com --token=sha256~1234567891234567.89 ")); - assertEquals("sha256~1234567891234567-89", - OCCommandUtils.getToken("oc login https://api.-uengint.openshift.com --token=sha256~1234567891234567-89 ")); - } + @Test + public void testCommandWithPassword() { + assertEquals("developerPassword", OCCommandUtils.getPassword("oc login -u developer -p developerPassword")); + assertEquals("developerPassword", OCCommandUtils.getPassword("oc login -u developer --password developerPassword")); + assertEquals("developerPassword", OCCommandUtils.getPassword("oc login -u developer --password=developerPassword")); + assertNull(OCCommandUtils.getPassword("oc login -u developer ")); + assertNull(OCCommandUtils.getPassword("oc login -u developer --passwod bad")); + assertNull(OCCommandUtils.getPassword("oc login -u developer --passwod=bad")); + } - @Test - public void testCommandWithPassword() { - assertEquals("developerPassword", OCCommandUtils.getPassword("oc login -u developer -p developerPassword")); - assertEquals("developerPassword", OCCommandUtils.getPassword("oc login -u developer --password developerPassword")); - assertEquals("developerPassword", OCCommandUtils.getPassword("oc login -u developer --password=developerPassword")); - assertNull(OCCommandUtils.getPassword("oc login -u developer ")); - assertNull(OCCommandUtils.getPassword("oc login -u developer --passwod bad")); - assertNull(OCCommandUtils.getPassword("oc login -u developer --passwod=bad")); - } + @Test + public void testValidCommand() { + assertFalse( + OCCommandUtils.isValidCommand("oc loginhttps://api.engint.openshift.com --token=123456789123456789")); + assertTrue( + OCCommandUtils.isValidCommand("oc login https://api.engint.openshift.com --token=123456789123456789")); + assertTrue(OCCommandUtils.isValidCommand("oc login https://12.34.5.6:8443 -u developer -p deve")); + } - @Test - public void testValidCommand() { - assertFalse( - OCCommandUtils.isValidCommand("oc loginhttps://api.engint.openshift.com --token=123456789123456789")); - assertTrue( - OCCommandUtils.isValidCommand("oc login https://api.engint.openshift.com --token=123456789123456789")); - assertTrue(OCCommandUtils.isValidCommand("oc login https://12.34.5.6:8443 -u developer -p deve")); - } + @Test + public void testServerAddress() { + assertEquals("https://api.engint.openshift.com", + OCCommandUtils.getServer("oc login https://api.engint.openshift.com --token=123456789123456789")); + assertEquals("https://api.engint.openshift.com", + OCCommandUtils.getServer("oc login -s=https://api.engint.openshift.com --token=123456789123456789")); + assertEquals("https://api.engint.openshift.com", + OCCommandUtils.getServer("oc login -s https://api.engint.openshift.com --token=123456789123456789")); + assertEquals("https://api.engint.openshift.com", + OCCommandUtils.getServer("oc login --server=https://api.engint.openshift.com --token=123456789123456789")); + assertEquals("https://api.engint.openshift.com", + OCCommandUtils.getServer("oc login --server https://api.engint.openshift.com --token=123456789123456789")); + } - @Test - public void testServerAddress() { - assertEquals("https://api.engint.openshift.com", - OCCommandUtils.getServer("oc login https://api.engint.openshift.com --token=123456789123456789")); - assertEquals("https://api.engint.openshift.com", - OCCommandUtils.getServer("oc login -s=https://api.engint.openshift.com --token=123456789123456789")); - assertEquals("https://api.engint.openshift.com", - OCCommandUtils.getServer("oc login -s https://api.engint.openshift.com --token=123456789123456789")); - assertEquals("https://api.engint.openshift.com", - OCCommandUtils.getServer("oc login --server=https://api.engint.openshift.com --token=123456789123456789")); - assertEquals("https://api.engint.openshift.com", - OCCommandUtils.getServer("oc login --server https://api.engint.openshift.com --token=123456789123456789")); - } + @Test + public void testUsername() { + assertEquals("developer", OCCommandUtils.getUsername("oc login -u developer -p developerPassword")); + assertEquals("developer", OCCommandUtils.getUsername("oc login -u=developer -p developerPassword")); + assertEquals("developer", OCCommandUtils.getUsername("oc login --username developer -p developerPassword")); + assertEquals("developer", OCCommandUtils.getUsername("oc login --username=developer -p developerPassword")); + } - @Test - public void testUsername() { - assertEquals("developer", OCCommandUtils.getUsername("oc login -u developer -p developerPassword")); - assertEquals("developer", OCCommandUtils.getUsername("oc login -u=developer -p developerPassword")); - assertEquals("developer", OCCommandUtils.getUsername("oc login --username developer -p developerPassword")); - assertEquals("developer", OCCommandUtils.getUsername("oc login --username=developer -p developerPassword")); - } + @Test + public void testCommandValidOnlyUsername() { + String command = "oc login -u system:admin -n default https://api.starter-us-west-1.openshift.com"; + assertTrue(OCCommandUtils.isValidCommand(command)); + assertEquals("system:admin", OCCommandUtils.getUsername(command)); + assertEquals("https://api.starter-us-west-1.openshift.com", OCCommandUtils.getServer(command)); + } - @Test - public void testCommandValidOnlyUsername() { - String command = "oc login -u system:admin -n default https://api.starter-us-west-1.openshift.com"; - assertTrue(OCCommandUtils.isValidCommand(command)); - assertEquals("system:admin", OCCommandUtils.getUsername(command)); - assertEquals("https://api.starter-us-west-1.openshift.com",OCCommandUtils.getServer(command)); - } + @Test + public void testCommandValidDifferentOrder() { + String command = "oc login -u system:admin -n default https://api.starter-us-west-1.openshift.com -p 123456"; + assertTrue(OCCommandUtils.isValidCommand(command)); + assertEquals("system:admin", OCCommandUtils.getUsername(command)); + assertEquals("123456", OCCommandUtils.getPassword(command)); + assertEquals("https://api.starter-us-west-1.openshift.com", OCCommandUtils.getServer(command)); + } - @Test - public void testCommandValidDifferentOrder() { - String command = "oc login -u system:admin -n default https://api.starter-us-west-1.openshift.com -p 123456"; - assertTrue(OCCommandUtils.isValidCommand(command)); - assertEquals("system:admin", OCCommandUtils.getUsername(command)); - assertEquals("123456", OCCommandUtils.getPassword(command)); - assertEquals("https://api.starter-us-west-1.openshift.com",OCCommandUtils.getServer(command)); - } + @Test + public void testCommandValidIssue871() { + String command = "oc login -u testUser -p testPassword https://api.sample-url.com"; + assertTrue(OCCommandUtils.isValidCommand(command)); + assertEquals("testUser", OCCommandUtils.getUsername(command)); + assertEquals("testPassword", OCCommandUtils.getPassword(command)); + assertEquals("https://api.sample-url.com", OCCommandUtils.getServer(command)); + } } \ No newline at end of file