Skip to content

Commit f73bed6

Browse files
committed
fixes #512, login modifier reads from env
1 parent d11cd37 commit f73bed6

File tree

5 files changed

+205
-9
lines changed

5 files changed

+205
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ All notable changes to AET will be documented in this file.
1010
## Unreleased
1111

1212
**List of changes that are finished but not yet released in any final version.**
13+
- [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)).
1314
- [PR-525](https://github.com/Cognifide/aet/pull/525) Added tests ordering. ([#509](https://github.com/Cognifide/aet/issues/509))
1415

1516
- [PR-524](https://github.com/Cognifide/aet/pull/524) Remove handling unescaped URLs in suites

core/jobs/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@
149149
<groupId>org.assertj</groupId>
150150
<artifactId>assertj-core</artifactId>
151151
</dependency>
152+
<dependency>
153+
<groupId>com.github.stefanbirkner</groupId>
154+
<artifactId>system-rules</artifactId>
155+
</dependency>
152156

153157
</dependencies>
154158

core/jobs/src/main/java/com/cognifide/aet/job/common/modifiers/login/LoginModifierConfig.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@
1818
import com.cognifide.aet.job.api.ParametersValidator;
1919
import com.cognifide.aet.job.api.exceptions.ParametersException;
2020
import java.util.Map;
21+
import java.util.Optional;
2122
import org.apache.commons.lang3.BooleanUtils;
2223
import org.apache.commons.lang3.StringUtils;
2324
import org.apache.commons.lang3.math.NumberUtils;
2425

2526
class LoginModifierConfig {
2627

27-
private static final String DEFAULT_LOGIN = "admin";
28+
static final String DEFAULT_LOGIN = "admin";
2829

29-
private static final String DEFAULT_PASSWORD = "admin";
30+
static final String DEFAULT_PASSWORD = "admin";
3031

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

@@ -36,11 +37,11 @@ class LoginModifierConfig {
3637

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

39-
private static final String LOGIN_PARAM = "login";
40+
static final String LOGIN_PARAM = "login";
4041

41-
private static final String PASSWORD_PARAM = "password";
42+
static final String PASSWORD_PARAM = "password";
4243

43-
private static final String LOGIN_PAGE_PARAM = "login-page";
44+
static final String LOGIN_PAGE_PARAM = "login-page";
4445

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

@@ -82,8 +83,8 @@ class LoginModifierConfig {
8283

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

106+
private String getEnvParameter(Map<String, String> params, String key, String defaultValue) {
107+
return Optional.ofNullable(params.get(key))
108+
.filter(StringUtils::isNotEmpty)
109+
.map(value -> isEnv(value) ? getEnv(value, defaultValue) : value)
110+
.orElse(defaultValue);
111+
}
112+
113+
private boolean isEnv(String value) {
114+
return value.startsWith("${") && value.endsWith("}") ;
115+
}
116+
117+
private String getEnv(String value, String defaultValue) {
118+
return Optional.ofNullable(System.getenv(value.substring(2, value.length() - 1)))
119+
.orElse(defaultValue);
120+
}
121+
105122
private String getParameter(Map<String, String> params, String key, String defaultValue) {
106123
return StringUtils.defaultString(params.get(key), defaultValue);
107124
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* AET
3+
*
4+
* Copyright (C) 2013 Cognifide Limited
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7+
* in compliance with the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software distributed under the License
12+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13+
* or implied. See the License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.cognifide.aet.job.common.modifiers.login;
17+
18+
import static com.cognifide.aet.job.common.modifiers.login.LoginModifierConfig.DEFAULT_LOGIN;
19+
import static com.cognifide.aet.job.common.modifiers.login.LoginModifierConfig.DEFAULT_PASSWORD;
20+
import static com.cognifide.aet.job.common.modifiers.login.LoginModifierConfig.LOGIN_PAGE_PARAM;
21+
import static com.cognifide.aet.job.common.modifiers.login.LoginModifierConfig.LOGIN_PARAM;
22+
import static com.cognifide.aet.job.common.modifiers.login.LoginModifierConfig.PASSWORD_PARAM;
23+
import static org.junit.Assert.assertEquals;
24+
25+
import com.cognifide.aet.job.api.exceptions.ParametersException;
26+
import com.googlecode.zohhak.api.TestWith;
27+
import com.googlecode.zohhak.api.runners.ZohhakRunner;
28+
import java.util.HashMap;
29+
import java.util.Map;
30+
import org.junit.Rule;
31+
import org.junit.Test;
32+
import org.junit.contrib.java.lang.system.EnvironmentVariables;
33+
import org.junit.runner.RunWith;
34+
35+
@RunWith(ZohhakRunner.class)
36+
public class LoginModifierConfigTest {
37+
38+
@Rule
39+
public final EnvironmentVariables environmentVariables = new EnvironmentVariables();
40+
41+
@Test
42+
public void whenNoPasswordProvided_expectDefaultPasswordInTheConfig() throws ParametersException {
43+
Map<String, String> params = new HashMap<>();
44+
params.put(LOGIN_PAGE_PARAM, "whatever");
45+
46+
LoginModifierConfig tested = new LoginModifierConfig(params);
47+
48+
assertEquals(DEFAULT_PASSWORD, tested.getPassword());
49+
}
50+
51+
@TestWith({
52+
"s3cret",
53+
"$ecret",
54+
"${ecret",
55+
"$ecret}",
56+
"s3cret}"
57+
})
58+
public void whenPasswordPassedAsPlainText_expectPasswordInTheConfig(String password) throws ParametersException {
59+
Map<String, String> params = new HashMap<>();
60+
params.put(LOGIN_PAGE_PARAM, "whatever");
61+
params.put(PASSWORD_PARAM, password);
62+
63+
LoginModifierConfig tested = new LoginModifierConfig(params);
64+
65+
assertEquals(password, tested.getPassword());
66+
}
67+
68+
@Test
69+
public void whenPasswordPassedAsEnv_expectPasswordInTheConfig() throws ParametersException {
70+
Map<String, String> params = new HashMap<>();
71+
params.put(LOGIN_PAGE_PARAM, "whatever");
72+
params.put(PASSWORD_PARAM, "${MY_SECRET_PASSWORD}");
73+
environmentVariables.set("MY_SECRET_PASSWORD", "pass-from-env");
74+
75+
LoginModifierConfig tested = new LoginModifierConfig(params);
76+
77+
assertEquals("pass-from-env", tested.getPassword());
78+
}
79+
80+
@Test
81+
public void whenPasswordPassedAsEnvAndEnvNotSet_expectDefaultPasswordInTheConfig() throws ParametersException {
82+
Map<String, String> params = new HashMap<>();
83+
params.put(LOGIN_PAGE_PARAM, "whatever");
84+
params.put(PASSWORD_PARAM, "${MY_SECRET_PASSWORD}");
85+
86+
LoginModifierConfig tested = new LoginModifierConfig(params);
87+
88+
assertEquals(DEFAULT_PASSWORD, tested.getPassword());
89+
}
90+
91+
//-----
92+
93+
@Test
94+
public void whenNoLoginProvided_expectDefaultLoginInTheConfig() throws ParametersException {
95+
Map<String, String> params = new HashMap<>();
96+
params.put(LOGIN_PAGE_PARAM, "whatever");
97+
98+
LoginModifierConfig tested = new LoginModifierConfig(params);
99+
100+
assertEquals(DEFAULT_LOGIN, tested.getLogin());
101+
}
102+
103+
@TestWith({
104+
"s3cret",
105+
"$ecret",
106+
"${ecret",
107+
"$ecret}",
108+
"s3cret}"
109+
})
110+
public void whenLoginPassedAsPlainText_expectLoginInTheConfig(String login) throws ParametersException {
111+
Map<String, String> params = new HashMap<>();
112+
params.put(LOGIN_PAGE_PARAM, "whatever");
113+
params.put(LOGIN_PARAM, login);
114+
115+
LoginModifierConfig tested = new LoginModifierConfig(params);
116+
117+
assertEquals(login, tested.getLogin());
118+
}
119+
120+
@Test
121+
public void whenLoginPassedAsEnv_expectLoginInTheConfig() throws ParametersException {
122+
Map<String, String> params = new HashMap<>();
123+
params.put(LOGIN_PAGE_PARAM, "whatever");
124+
params.put(LOGIN_PARAM, "${MY_SECRET_LOGIN}");
125+
environmentVariables.set("MY_SECRET_LOGIN", "user-from-env");
126+
127+
LoginModifierConfig tested = new LoginModifierConfig(params);
128+
129+
assertEquals("user-from-env", tested.getLogin());
130+
}
131+
132+
@Test
133+
public void whenLoginPassedAsEnvAndEnvNotSet_expectDefaultLoginInTheConfig() throws ParametersException {
134+
Map<String, String> params = new HashMap<>();
135+
params.put(LOGIN_PAGE_PARAM, "whatever");
136+
params.put(DEFAULT_LOGIN, "${MY_SECRET_PASSWORD}");
137+
138+
LoginModifierConfig tested = new LoginModifierConfig(params);
139+
140+
assertEquals(DEFAULT_LOGIN, tested.getLogin());
141+
}
142+
143+
}

documentation/src/main/wiki/LoginModifier.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ Module name: **login**
1414

1515
| Parameter | Value | Mandatory | Default value |
1616
| --------- | ----- | --------- | ------------- |
17-
| `login` | User's login | no | admin |
18-
| `password` | Password | no | admin |
17+
| `login` | User's login, can be passed plaintext or read from environment variable using `"${ENV_VARIABLE_NAME}"`. See the example below. | no | admin |
18+
| `password` | Password, can be passed plaintext or read from environment variable using `"${ENV_VARIABLE_NAME}"`. See the example below. | no | admin |
1919
| `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 | |
2020
| `login-input-selector` | Xpath expression for login input | no | //input[@name='j_username'] |
2121
| `password-input-selector` | Xpath expression for password input | no | //input[@name='j_password'] |
@@ -27,6 +27,7 @@ Module name: **login**
2727

2828
##### Example Usage
2929

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

59+
> Pass `login` and `password` as plain text and `login-page` as URL inside the `domain` specified for the suite:
5860
```xml
5961
<?xml version="1.0" encoding="UTF-8" ?>
6062
<suite name="test-suite" company="cognifide" project="project" domain="http://example.com">
@@ -81,4 +83,33 @@ Module name: **login**
8183
...
8284
</reports>
8385
</suite>
86+
```
87+
88+
> Pass `login` and `password` as environment variables:
89+
```xml
90+
<?xml version="1.0" encoding="UTF-8" ?>
91+
<suite name="test-suite" company="cognifide" project="project" domain="http://example.com">
92+
<test name="login-test">
93+
<collect>
94+
<login login="${LOGIN_EXAMPLE_ADMIN_PANEL}"
95+
password="${PASS_EXAMPLE_ADMIN_PANEL}"
96+
login-page="/login.html"
97+
login-input-selector="//input[@name='j_username']"
98+
password-input-selector="//input[@name='j_password']"
99+
submit-button-selector="//*[@type='submit']" />
100+
<open />
101+
...
102+
</collect>
103+
<compare>
104+
...
105+
</compare>
106+
<urls>
107+
...
108+
</urls>
109+
</test>
110+
...
111+
<reports>
112+
...
113+
</reports>
114+
</suite>
84115
```

0 commit comments

Comments
 (0)