Repository: Wikidata/Wikidata-Toolkit
Pull request #713 (2022-07-27 13:58:39)

712: add FAILED login handling

Lhaaits: @wetneb I added a test :)

Patch:
diff --git a/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/BasicApiConnection.java b/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/BasicApiConnection.java
index c455b399..4fd15adb 100644
--- a/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/BasicApiConnection.java
+++ b/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/BasicApiConnection.java
@@ -221,7 +221,13 @@ public class BasicApiConnection extends ApiConnection {
 			this.username = username;
 			this.password = password;
 		} else {
-			String message = LoginValue.of(result).getMessage(result);
+			String message = null;
+			if (FAILED.getLoginText().equals(result)) {
+				message = root.path("login").path("reason").textValue();
+			} 
+			if (message == null) { // Not 'FAILED' or no 'reason' node
+				message = LoginValue.of(result).getMessage(result);
+			}
 			logger.warn(message);
 			if (LOGIN_WRONG_TOKEN.getLoginText().equals(result)) {
 				throw new NeedLoginTokenException(message);
diff --git a/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/LoginValue.java b/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/LoginValue.java
index 580eb23b..c715149e 100644
--- a/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/LoginValue.java
+++ b/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/LoginValue.java
@@ -47,6 +47,11 @@ public enum LoginValue {
      * wrong.
      */
     LOGIN_WRONG_PASS("WrongPass", "wrongpassword", "Wrong Password."),
+    /**
+     * String value in the result field of the JSON response if the password was
+     * wrong.
+     */
+    FAILED("Failed", "failed", "Something went wrong"),
     /**
      * String value in the result field of the JSON response if the password was
      * rejected by an authentication plugin.


Test patch:
diff --git a/wdtk-wikibaseapi/src/test/java/org/wikidata/wdtk/wikibaseapi/BasicApiConnectionTest.java b/wdtk-wikibaseapi/src/test/java/org/wikidata/wdtk/wikibaseapi/BasicApiConnectionTest.java
index f49acb11..ad5fad10 100644
--- a/wdtk-wikibaseapi/src/test/java/org/wikidata/wdtk/wikibaseapi/BasicApiConnectionTest.java
+++ b/wdtk-wikibaseapi/src/test/java/org/wikidata/wdtk/wikibaseapi/BasicApiConnectionTest.java
@@ -114,7 +114,9 @@ public class BasicApiConnectionTest {
 						case "lgtoken=b5780b6e2f27e20b450921d9461010b4&lgpassword=password&format=json&action=login&lgname=username":
 							return makeJsonResponseFrom("/loginSuccess.json");
 						case "lgtoken=b5780b6e2f27e20b450921d9461010b4&lgpassword=password1&format=json&action=login&lgname=username1":
-							return makeJsonResponseFrom("/loginError.json");
+							return makeJsonResponseFrom("/loginError.json"); 
+						case "lgtoken=b5780b6e2f27e20b450921d9461010b4&lgpassword=password2&format=json&action=login&lgname=username":
+							return makeJsonResponseFrom("/loginFailed.json");
 						case "meta=tokens&assert=user&format=json&action=query&type=csrf":
 							return makeJsonResponseFrom("/query-csrf-token-loggedin-response.json");
 						case "assert=user&format=json&action=logout&token=42307b93c79b0cb558d2dfb4c3c92e0955e06041%2B%5C":
@@ -241,10 +243,20 @@ public class BasicApiConnectionTest {
 		assertFalse(connection.loggedIn);
 	}
 
-	@Test(expected = LoginFailedException.class)
-	public void loginUserErrors() throws LoginFailedException {
+	@Test
+	public void loginUserError() {
+		// This will fail because the user is not known
+		LoginFailedException loginFailedException = assertThrows(LoginFailedException.class, 
+				() -> connection.login("username1", "password1"));	
+		assertEquals("NotExists: Username does not exist.", loginFailedException.getMessage());
+	}
+
+	@Test
+	public void loginFailedUsesReason() {
 		// This will fail because the user is not known
-		connection.login("username1", "password1");
+		LoginFailedException loginFailedException = assertThrows(LoginFailedException.class,
+				() -> connection.login("username", "password2"));
+		assertEquals("Incorrect username or password entered. Please try again.", loginFailedException.getMessage());
 	}
 
 	@Test
diff --git a/wdtk-wikibaseapi/src/test/resources/loginError.json b/wdtk-wikibaseapi/src/test/resources/loginError.json
index dc9f307c..86fa4e99 100644
--- a/wdtk-wikibaseapi/src/test/resources/loginError.json
+++ b/wdtk-wikibaseapi/src/test/resources/loginError.json
@@ -1,1 +1,5 @@
-{"login":{"result":"NotExists"}}
+{
+  "login": {
+    "result": "NotExists"
+  }
+}
diff --git a/wdtk-wikibaseapi/src/test/resources/loginFailed.json b/wdtk-wikibaseapi/src/test/resources/loginFailed.json
new file mode 100644
index 00000000..ed5d7f98
--- /dev/null
+++ b/wdtk-wikibaseapi/src/test/resources/loginFailed.json
@@ -0,0 +1,6 @@
+{
+  "login": {
+    "result": "Failed",
+    "reason": "Incorrect username or password entered. Please try again."
+  }
+}