Skip to content

Commit 6f6be38

Browse files
authored
Test more bad credentials messages (#2502)
1 parent 1f4013d commit 6f6be38

2 files changed

Lines changed: 55 additions & 14 deletions

File tree

src/org/labkey/test/tests/core/login/PasswordTest.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.labkey.test.pages.core.login.DatabaseAuthConfigureDialog;
3434
import org.labkey.test.pages.core.login.LoginConfigurePage;
3535
import org.labkey.test.params.login.DatabaseAuthenticationProvider;
36+
import org.labkey.test.util.APIUserHelper;
3637
import org.labkey.test.util.LogMethod;
3738
import org.labkey.test.util.LoggedParam;
3839
import org.labkey.test.util.core.login.DbLoginUtils;
@@ -256,7 +257,11 @@ public void testPasswordParameter()
256257
@Test
257258
public void testChooseNewPasswordMessages() throws IOException
258259
{
259-
// Hold an admin connection open, allowing us to reset the config regardless of what happens during the test
260+
// Test bad API key
261+
signOut();
262+
signInShouldFailUiAndApi("apikey", "abc123", "The API key you provided is invalid.");
263+
264+
// Hold an admin API connection open, allowing us to reset the config without the browser session interfering
260265
Connection adminConnection = WebTestHelper.getRemoteApiConnection();
261266
DbLoginProperties savedProperties = DbLoginUtils.getDbLoginConfig(adminConnection);
262267

@@ -268,25 +273,28 @@ public void testChooseNewPasswordMessages() throws IOException
268273
);
269274

270275
// Set a weak password
271-
signOut();
272276
String resetUrl = userInitiatePasswordReset(USER);
273277
beginAt(resetUrl);
274278
new SetPasswordForm(getDriver())
275279
.setNewPassword(WEAK_PASSWORD)
276280
.clickSubmit();
277281

278-
// Test bogus password first
279-
signOut();
280-
signInShouldFail("bogus", "The email address and password you entered did not match any accounts on file.");
281-
signIn(USER, WEAK_PASSWORD);
282+
// Test bogus password
282283
signOut();
284+
signInShouldFailUiAndApi(USER, "bogus", "The email address and password you entered did not match any accounts on file.");
285+
286+
// Test deactivated user
287+
APIUserHelper helper = new APIUserHelper(() -> adminConnection);
288+
helper.deactivateUsers(_userId);
289+
signInShouldFailUiAndApi(USER, WEAK_PASSWORD, "Your account has been deactivated.");
290+
helper.activateUsers(_userId);
283291

284292
// Change the configuration and test password that no longer meets complexity requirements
285293
DbLoginUtils.setDbLoginConfig(adminConnection,
286294
PasswordStrength.Strong,
287295
PasswordExpiration.Never
288296
);
289-
signInShouldFail(WEAK_PASSWORD, "Your password does not meet the complexity requirements; please choose a new password.");
297+
signInShouldFailUiAndApi(USER, WEAK_PASSWORD, "Your password does not meet the complexity requirements; please choose a new password.");
290298
String strongPassword = VERY_STRONG_PASSWORD + "!";
291299
changeInvalidPassword(WEAK_PASSWORD, strongPassword);
292300

@@ -297,7 +305,7 @@ public void testChooseNewPasswordMessages() throws IOException
297305
);
298306
// Wait six seconds for expiration
299307
sleep(6000);
300-
signInShouldFail(strongPassword, "Your password has expired; please choose a new password.");
308+
signInShouldFailUiAndApi(USER, strongPassword, "Your password has expired; please choose a new password.");
301309
changeInvalidPassword(strongPassword, VERY_STRONG_PASSWORD + "@");
302310
}
303311
finally
@@ -307,10 +315,10 @@ public void testChooseNewPasswordMessages() throws IOException
307315
}
308316

309317
// Attempt to sign in via UI and API, expecting both to fail with the specified message
310-
private void signInShouldFail(String password, String expectedMessage) throws IOException
318+
private void signInShouldFailUiAndApi(String email, String password, String expectedMessage) throws IOException
311319
{
312-
signInShouldFail(USER, password, expectedMessage);
313-
Connection userConnection = new Connection(WebTestHelper.getBaseURL(), USER, password);
320+
signInShouldFail(email, password, expectedMessage);
321+
Connection userConnection = new Connection(WebTestHelper.getBaseURL(), email, password);
314322
EnsureLoginCommand ensureLoginCommand = new EnsureLoginCommand();
315323
try
316324
{

src/org/labkey/test/util/APIUserHelper.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.labkey.test.util;
1717

18+
import org.apache.commons.lang3.ArrayUtils;
1819
import org.apache.hc.client5.http.classic.methods.HttpPost;
1920
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
2021
import org.apache.hc.core5.http.message.BasicNameValuePair;
@@ -27,6 +28,7 @@
2728
import org.labkey.remoteapi.Connection;
2829
import org.labkey.remoteapi.PostCommand;
2930
import org.labkey.remoteapi.SimpleGetCommand;
31+
import org.labkey.remoteapi.SimplePostCommand;
3032
import org.labkey.remoteapi.security.CreateUserCommand;
3133
import org.labkey.remoteapi.security.CreateUserResponse;
3234
import org.labkey.remoteapi.security.DeleteUserCommand;
@@ -52,7 +54,6 @@
5254

5355
import static org.junit.Assert.assertEquals;
5456
import static org.junit.Assert.assertNotNull;
55-
import static org.junit.Assert.assertTrue;
5657

5758
public class APIUserHelper extends AbstractUserHelper
5859
{
@@ -239,14 +240,17 @@ private void deleteUser(@NotNull Integer userId)
239240
protected void _deleteUsers(boolean failIfNotFound, String... userEmails)
240241
{
241242
Map<String, Integer> userIds = getUserIds(Arrays.asList(userEmails), true);
243+
List<Integer> validUserIds = new ArrayList<>();
242244
for (String userEmail : new HashSet<>(Arrays.asList(userEmails)))
243245
{
244246
Integer userId = userIds.get(userEmail);
245247
if (failIfNotFound)
246-
assertTrue(userEmail + " was not present", userId != null);
248+
assertNotNull(userEmail + " was not present", userId);
247249
if (userId != null)
248-
deleteUser(userId);
250+
validUserIds.add(userId);
249251
}
252+
if (!validUserIds.isEmpty())
253+
deleteUsers(ArrayUtils.toPrimitive(validUserIds.toArray(new Integer[0])));
250254
}
251255

252256
private static final Pattern regEmailVerification = Pattern.compile("verification=([A-Za-z0-9]+)");
@@ -289,6 +293,35 @@ public String setInitialPassword(int userId)
289293
throw new RuntimeException(e);
290294
}
291295
}
296+
297+
public void deleteUsers(int... userIds)
298+
{
299+
updateUsersState(false, true, userIds);
300+
}
301+
302+
public void deactivateUsers(int... userIds)
303+
{
304+
updateUsersState(false, false, userIds);
305+
}
306+
307+
public void activateUsers(int... userIds)
308+
{
309+
updateUsersState(true, false, userIds);
310+
}
311+
312+
private void updateUsersState(boolean activate, boolean delete, int... userIds)
313+
{
314+
SimplePostCommand updateUsers = new SimplePostCommand("user", "updateUsersStateApi");
315+
updateUsers.setJsonObject(new JSONObject(Map.of("userId", userIds, "activate", activate, "delete", delete)));
316+
try
317+
{
318+
updateUsers.execute(connectionSupplier.get(), null);
319+
}
320+
catch (IOException | CommandException e)
321+
{
322+
throw new RuntimeException(e);
323+
}
324+
}
292325
}
293326

294327
class SetPasswordCommand extends PostCommand<CommandResponse>

0 commit comments

Comments
 (0)