Skip to content

Commit

Permalink
fixes #512, login modifier reads from env
Browse files Browse the repository at this point in the history
  • Loading branch information
malaskowski committed Oct 19, 2020
1 parent d11cd37 commit f73bed6
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes to AET will be documented in this file.
## Unreleased

**List of changes that are finished but not yet released in any final version.**
- [PR-583](https://github.com/Cognifide/aet/pull/583) Enable login modifier to read login and password from environment variables (fixed [#512](https://github.com/Cognifide/aet/issues/512)).
- [PR-525](https://github.com/Cognifide/aet/pull/525) Added tests ordering. ([#509](https://github.com/Cognifide/aet/issues/509))

- [PR-524](https://github.com/Cognifide/aet/pull/524) Remove handling unescaped URLs in suites
Expand Down
4 changes: 4 additions & 0 deletions core/jobs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@
import com.cognifide.aet.job.api.ParametersValidator;
import com.cognifide.aet.job.api.exceptions.ParametersException;
import java.util.Map;
import java.util.Optional;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

class LoginModifierConfig {

private static final String DEFAULT_LOGIN = "admin";
static final String DEFAULT_LOGIN = "admin";

private static final String DEFAULT_PASSWORD = "admin";
static final String DEFAULT_PASSWORD = "admin";

private static final String DEFAULT_LOGIN_INPUT_SELECTOR = "//input[@name='j_username']";

Expand All @@ -36,11 +37,11 @@ class LoginModifierConfig {

private static final String DEFAULT_LOGIN_TOKEN = "login-token";

private static final String LOGIN_PARAM = "login";
static final String LOGIN_PARAM = "login";

private static final String PASSWORD_PARAM = "password";
static final String PASSWORD_PARAM = "password";

private static final String LOGIN_PAGE_PARAM = "login-page";
static final String LOGIN_PAGE_PARAM = "login-page";

private static final String LOGIN_INPUT_SELECTOR_PARAM = "login-input-selector";

Expand Down Expand Up @@ -82,8 +83,8 @@ class LoginModifierConfig {

public LoginModifierConfig(Map<String, String> params) throws ParametersException {
this.loginPage = params.get(LOGIN_PAGE_PARAM);
this.login = getParameter(params, LOGIN_PARAM, DEFAULT_LOGIN);
this.password = getParameter(params, PASSWORD_PARAM, DEFAULT_PASSWORD);
this.login = getEnvParameter(params, LOGIN_PARAM, DEFAULT_LOGIN);
this.password = getEnvParameter(params, PASSWORD_PARAM, DEFAULT_PASSWORD);
this.loginInputSelector = getParameter(params, LOGIN_INPUT_SELECTOR_PARAM,
DEFAULT_LOGIN_INPUT_SELECTOR);
this.passwordInputSelector = getParameter(params, PASSWORD_INPUT_SELECTOR_PARAM,
Expand All @@ -102,6 +103,22 @@ public LoginModifierConfig(Map<String, String> params) throws ParametersExceptio
.checkRange(retrialNumber, 1, Integer.MAX_VALUE, "Retrial number has to be at least 1");
}

private String getEnvParameter(Map<String, String> params, String key, String defaultValue) {
return Optional.ofNullable(params.get(key))
.filter(StringUtils::isNotEmpty)
.map(value -> isEnv(value) ? getEnv(value, defaultValue) : value)
.orElse(defaultValue);
}

private boolean isEnv(String value) {
return value.startsWith("${") && value.endsWith("}") ;
}

private String getEnv(String value, String defaultValue) {
return Optional.ofNullable(System.getenv(value.substring(2, value.length() - 1)))
.orElse(defaultValue);
}

private String getParameter(Map<String, String> params, String key, String defaultValue) {
return StringUtils.defaultString(params.get(key), defaultValue);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* AET
*
* Copyright (C) 2013 Cognifide Limited
*
* 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.cognifide.aet.job.common.modifiers.login;

import static com.cognifide.aet.job.common.modifiers.login.LoginModifierConfig.DEFAULT_LOGIN;
import static com.cognifide.aet.job.common.modifiers.login.LoginModifierConfig.DEFAULT_PASSWORD;
import static com.cognifide.aet.job.common.modifiers.login.LoginModifierConfig.LOGIN_PAGE_PARAM;
import static com.cognifide.aet.job.common.modifiers.login.LoginModifierConfig.LOGIN_PARAM;
import static com.cognifide.aet.job.common.modifiers.login.LoginModifierConfig.PASSWORD_PARAM;
import static org.junit.Assert.assertEquals;

import com.cognifide.aet.job.api.exceptions.ParametersException;
import com.googlecode.zohhak.api.TestWith;
import com.googlecode.zohhak.api.runners.ZohhakRunner;
import java.util.HashMap;
import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import org.junit.runner.RunWith;

@RunWith(ZohhakRunner.class)
public class LoginModifierConfigTest {

@Rule
public final EnvironmentVariables environmentVariables = new EnvironmentVariables();

@Test
public void whenNoPasswordProvided_expectDefaultPasswordInTheConfig() throws ParametersException {
Map<String, String> params = new HashMap<>();
params.put(LOGIN_PAGE_PARAM, "whatever");

LoginModifierConfig tested = new LoginModifierConfig(params);

assertEquals(DEFAULT_PASSWORD, tested.getPassword());
}

@TestWith({
"s3cret",
"$ecret",
"${ecret",
"$ecret}",
"s3cret}"
})
public void whenPasswordPassedAsPlainText_expectPasswordInTheConfig(String password) throws ParametersException {
Map<String, String> params = new HashMap<>();
params.put(LOGIN_PAGE_PARAM, "whatever");
params.put(PASSWORD_PARAM, password);

LoginModifierConfig tested = new LoginModifierConfig(params);

assertEquals(password, tested.getPassword());
}

@Test
public void whenPasswordPassedAsEnv_expectPasswordInTheConfig() throws ParametersException {
Map<String, String> params = new HashMap<>();
params.put(LOGIN_PAGE_PARAM, "whatever");
params.put(PASSWORD_PARAM, "${MY_SECRET_PASSWORD}");
environmentVariables.set("MY_SECRET_PASSWORD", "pass-from-env");

LoginModifierConfig tested = new LoginModifierConfig(params);

assertEquals("pass-from-env", tested.getPassword());
}

@Test
public void whenPasswordPassedAsEnvAndEnvNotSet_expectDefaultPasswordInTheConfig() throws ParametersException {
Map<String, String> params = new HashMap<>();
params.put(LOGIN_PAGE_PARAM, "whatever");
params.put(PASSWORD_PARAM, "${MY_SECRET_PASSWORD}");

LoginModifierConfig tested = new LoginModifierConfig(params);

assertEquals(DEFAULT_PASSWORD, tested.getPassword());
}

//-----

@Test
public void whenNoLoginProvided_expectDefaultLoginInTheConfig() throws ParametersException {
Map<String, String> params = new HashMap<>();
params.put(LOGIN_PAGE_PARAM, "whatever");

LoginModifierConfig tested = new LoginModifierConfig(params);

assertEquals(DEFAULT_LOGIN, tested.getLogin());
}

@TestWith({
"s3cret",
"$ecret",
"${ecret",
"$ecret}",
"s3cret}"
})
public void whenLoginPassedAsPlainText_expectLoginInTheConfig(String login) throws ParametersException {
Map<String, String> params = new HashMap<>();
params.put(LOGIN_PAGE_PARAM, "whatever");
params.put(LOGIN_PARAM, login);

LoginModifierConfig tested = new LoginModifierConfig(params);

assertEquals(login, tested.getLogin());
}

@Test
public void whenLoginPassedAsEnv_expectLoginInTheConfig() throws ParametersException {
Map<String, String> params = new HashMap<>();
params.put(LOGIN_PAGE_PARAM, "whatever");
params.put(LOGIN_PARAM, "${MY_SECRET_LOGIN}");
environmentVariables.set("MY_SECRET_LOGIN", "user-from-env");

LoginModifierConfig tested = new LoginModifierConfig(params);

assertEquals("user-from-env", tested.getLogin());
}

@Test
public void whenLoginPassedAsEnvAndEnvNotSet_expectDefaultLoginInTheConfig() throws ParametersException {
Map<String, String> params = new HashMap<>();
params.put(LOGIN_PAGE_PARAM, "whatever");
params.put(DEFAULT_LOGIN, "${MY_SECRET_PASSWORD}");

LoginModifierConfig tested = new LoginModifierConfig(params);

assertEquals(DEFAULT_LOGIN, tested.getLogin());
}

}
35 changes: 33 additions & 2 deletions documentation/src/main/wiki/LoginModifier.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Module name: **login**

| Parameter | Value | Mandatory | Default value |
| --------- | ----- | --------- | ------------- |
| `login` | User's login | no | admin |
| `password` | Password | no | admin |
| `login` | User's login, can be passed plaintext or read from environment variable using `"${ENV_VARIABLE_NAME}"`. See the example below. | no | admin |
| `password` | Password, can be passed plaintext or read from environment variable using `"${ENV_VARIABLE_NAME}"`. See the example below. | no | admin |
| `login-page` | Full url to login page (starting with `http` or `https`) or login page path that is in the `domain` specified for the suite | yes | |
| `login-input-selector` | Xpath expression for login input | no | //input[@name='j_username'] |
| `password-input-selector` | Xpath expression for password input | no | //input[@name='j_password'] |
Expand All @@ -27,6 +27,7 @@ Module name: **login**

##### Example Usage

> Pass `login` and `password` as plain text and `login-page` as full URL:
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<suite name="test-suite" company="cognifide" project="project">
Expand Down Expand Up @@ -55,6 +56,7 @@ Module name: **login**
</suite>
```

> Pass `login` and `password` as plain text and `login-page` as URL inside the `domain` specified for the suite:
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<suite name="test-suite" company="cognifide" project="project" domain="http://example.com">
Expand All @@ -81,4 +83,33 @@ Module name: **login**
...
</reports>
</suite>
```

> Pass `login` and `password` as environment variables:
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<suite name="test-suite" company="cognifide" project="project" domain="http://example.com">
<test name="login-test">
<collect>
<login login="${LOGIN_EXAMPLE_ADMIN_PANEL}"
password="${PASS_EXAMPLE_ADMIN_PANEL}"
login-page="/login.html"
login-input-selector="//input[@name='j_username']"
password-input-selector="//input[@name='j_password']"
submit-button-selector="//*[@type='submit']" />
<open />
...
</collect>
<compare>
...
</compare>
<urls>
...
</urls>
</test>
...
<reports>
...
</reports>
</suite>
```

0 comments on commit f73bed6

Please sign in to comment.