Skip to content

Package: AuthenticationServiceSteps

AuthenticationServiceSteps

nameinstructionbranchcomplexitylinemethod
AuthenticationServiceSteps(StepData, DBHelper)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
afterScenario()
M: 26 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
beforeScenario(Scenario)
M: 73 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
checkConfigValue(String, String)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
checkNullConfigValue(String)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
createPasswordCredential(String)
M: 31 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
getConfigurationValue()
M: 32 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
setConfigurationValue(List)
M: 69 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 20 C: 0
0%
M: 1 C: 0
0%
setCredentialServiceConfig(int, List)
M: 36 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2020, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.service.authentication.steps;
14:
15: import java.util.HashMap;
16: import java.util.List;
17: import java.util.Map;
18:
19: import javax.inject.Inject;
20:
21: import org.eclipse.kapua.KapuaException;
22: import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
23: import org.eclipse.kapua.commons.security.KapuaSession;
24: import org.eclipse.kapua.commons.util.xml.XmlUtil;
25: import org.eclipse.kapua.locator.KapuaLocator;
26: import org.eclipse.kapua.model.id.KapuaId;
27: import org.eclipse.kapua.qa.common.DBHelper;
28: import org.eclipse.kapua.qa.common.StepData;
29: import org.eclipse.kapua.qa.common.TestBase;
30: import org.eclipse.kapua.qa.common.TestJAXBContextProvider;
31: import org.eclipse.kapua.qa.common.cucumber.CucConfig;
32: import org.eclipse.kapua.service.account.Account;
33: import org.eclipse.kapua.service.authentication.credential.CredentialCreator;
34: import org.eclipse.kapua.service.authentication.credential.CredentialFactory;
35: import org.eclipse.kapua.service.authentication.credential.CredentialService;
36: import org.eclipse.kapua.service.authentication.credential.CredentialStatus;
37: import org.eclipse.kapua.service.authentication.credential.CredentialType;
38: import org.eclipse.kapua.service.user.User;
39: import org.eclipse.kapua.service.user.UserCreator;
40: import org.eclipse.kapua.service.user.UserFactory;
41: import org.eclipse.kapua.service.user.UserService;
42:
43: import cucumber.api.Scenario;
44: import cucumber.api.java.After;
45: import cucumber.api.java.Before;
46: import cucumber.api.java.en.Given;
47: import cucumber.api.java.en.When;
48: import cucumber.runtime.java.guice.ScenarioScoped;
49: import org.apache.shiro.SecurityUtils;
50: import org.junit.Assert;
51: import org.slf4j.Logger;
52: import org.slf4j.LoggerFactory;
53:
54: // Implementation of Gherkin steps used to test miscellaneous Shiro
55: // authorization functionality.
56:
57: @ScenarioScoped
58: public class AuthenticationServiceSteps extends TestBase {
59:
60: private static final String LAST_ACCOUNT = "LastAccount";
61:
62: private static final Logger logger = LoggerFactory.getLogger(AuthenticationServiceSteps.class);
63:
64: private CredentialService credentialService;
65: private CredentialFactory credentialFactory;
66: private UserService userService;
67: private UserFactory userFactory;
68:
69: @Inject
70: public AuthenticationServiceSteps(StepData stepData, DBHelper dbHelper) {
71: this.stepData = stepData;
72: this.database = dbHelper;
73: }
74:
75: // Database setup and tear-down steps
76: @Before
77: public void beforeScenario(Scenario scenario) throws KapuaException {
78:
79: this.scenario = scenario;
80: database.setup();
81: stepData.clear();
82:
83: locator = KapuaLocator.getInstance();
84: credentialService = locator.getService(CredentialService.class);
85: credentialFactory = locator.getFactory(CredentialFactory.class);
86: userService = locator.getService(UserService.class);
87: userFactory = locator.getFactory(UserFactory.class);
88:
89: UserCreator userCreator = userFactory.newCreator(KapuaId.ONE, "test-user");
90: User user = userService.create(userCreator);
91:
92: stepData.put("user", user);
93:
94:• if (isUnitTest()) {
95: // Create KapuaSession using KapuaSecurtiyUtils and kapua-sys user as logged in user.
96: // All operations on database are performed using system user.
97: // Only for unit tests. Integration tests assume that a real logon is performed.
98: KapuaSession kapuaSession = new KapuaSession(null, SYS_SCOPE_ID, SYS_USER_ID);
99: KapuaSecurityUtils.setSession(kapuaSession);
100: }
101:
102: XmlUtil.setContextProvider(new TestJAXBContextProvider());
103: }
104:
105: @After
106: public void afterScenario() {
107:
108: // Clean up the database
109: try {
110: logger.info("Logging out in cleanup");
111:• if (isIntegrationTest()) {
112: database.deleteAll();
113: SecurityUtils.getSubject().logout();
114: } else {
115: database.dropAll();
116: database.close();
117: }
118: KapuaSecurityUtils.clearSession();
119: } catch (Exception e) {
120: logger.error("Failed to log out in @After", e);
121: }
122: }
123:
124: // Cucumber test steps
125:
126: @When("^I configure the credential service$")
127: public void setConfigurationValue(List<CucConfig> cucConfigs)
128: throws Exception {
129:
130: Map<String, Object> valueMap = new HashMap<>();
131: KapuaId scopeId;
132: KapuaId parentScopeId;
133: Account tmpAccount = (Account) stepData.get(LAST_ACCOUNT);
134:
135:• if (tmpAccount != null) {
136: scopeId = tmpAccount.getId();
137: parentScopeId = tmpAccount.getScopeId();
138: } else {
139: scopeId = SYS_SCOPE_ID;
140: parentScopeId = SYS_SCOPE_ID;
141: }
142:
143:• for (CucConfig config : cucConfigs) {
144: config.addConfigToMap(valueMap);
145:• if (config.getScopeId() != null) {
146: scopeId = getKapuaId(config.getScopeId());
147: }
148:• if (config.getParentId() != null) {
149: parentScopeId = getKapuaId(config.getParentId());
150: }
151: }
152: try {
153: primeException();
154: credentialService.setConfigValues(scopeId, parentScopeId, valueMap);
155: } catch (KapuaException ex) {
156: verifyException(ex);
157: }
158: }
159:
160: @When("^I read credential service configuration$")
161: public void getConfigurationValue()
162: throws Exception {
163:
164: KapuaId scopeId;
165: Account tmpAccount = (Account) stepData.get(LAST_ACCOUNT);
166:
167:• if (tmpAccount != null) {
168: scopeId = tmpAccount.getId();
169: } else {
170: scopeId = SYS_SCOPE_ID;
171: }
172:
173: try {
174: primeException();
175: Map<String, Object> configValues = credentialService.getConfigValues(scopeId);
176: stepData.put("configValues", configValues);
177: } catch (KapuaException ex) {
178: verifyException(ex);
179: }
180: }
181:
182: @When("I check that service configuration \"(.*)\" matches value \"(.*)\"")
183: public void checkConfigValue(String key, String value) {
184: Map<String, Object> configValues = (Map<String, Object>)stepData.get("configValues");
185: Object configValue = configValues.get(key);
186: Assert.assertEquals(value, configValue);
187: }
188:
189: @When("I check that service configuration \"(.*)\" has no value")
190: public void checkNullConfigValue(String key) {
191: Map<String, Object> configValues = (Map<String, Object>)stepData.get("configValues");
192: Object configValue = configValues.get(key);
193: Assert.assertEquals(configValue, null);
194: }
195:
196: @When("^I configure the credential service for the account with the id (\\d+)$")
197: public void setCredentialServiceConfig(int accountId, List<CucConfig> cucConfigs)
198: throws Exception {
199:
200: Map<String, Object> valueMap = new HashMap<>();
201: KapuaId accId = getKapuaId(accountId);
202:
203:• for (CucConfig config : cucConfigs) {
204: config.addConfigToMap(valueMap);
205: }
206:
207: primeException();
208: try {
209: credentialService.setConfigValues(accId, SYS_SCOPE_ID, valueMap);
210: } catch (KapuaException ex) {
211: verifyException(ex);
212: }
213: }
214:
215: @Given("^I create a new PASSWORD credential for the default user with password \"(.+)\"$")
216: public void createPasswordCredential(String password)
217: throws Exception {
218: primeException();
219:
220: User user = (User) stepData.get("user");
221: CredentialCreator credentialCreator = credentialFactory.newCreator(user.getScopeId(), user.getId(), CredentialType.PASSWORD, password, CredentialStatus.ENABLED, null);
222: try {
223: credentialService.create(credentialCreator);
224: } catch (Exception ex) {
225: verifyException(ex);
226: }
227: }
228:
229: }