diff --git a/extensions/wikidata/src/org/openrefine/wikidata/commands/ConnectionManager.java b/extensions/wikidata/src/org/openrefine/wikidata/commands/ConnectionManager.java index c66ad6778..7f5827471 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/commands/ConnectionManager.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/commands/ConnectionManager.java @@ -190,8 +190,15 @@ public class ConnectionManager { try { connection.logout(); connection = null; - } catch (IOException | MediaWikiApiErrorException e) { + } catch (IOException e) { logger.error(e.getMessage()); + } catch (MediaWikiApiErrorException e) { + if ("assertuserfailed".equals(e.getErrorCode())) { + // it turns out we were already logged out + connection = null; + } else { + logger.error(e.getMessage()); + } } } } diff --git a/extensions/wikidata/tests/src/org/openrefine/wikidata/commands/LoginCommandTest.java b/extensions/wikidata/tests/src/org/openrefine/wikidata/commands/LoginCommandTest.java index 070772864..9957fbdcb 100644 --- a/extensions/wikidata/tests/src/org/openrefine/wikidata/commands/LoginCommandTest.java +++ b/extensions/wikidata/tests/src/org/openrefine/wikidata/commands/LoginCommandTest.java @@ -435,6 +435,33 @@ public class LoginCommandTest extends CommandTest { assertTrue(ConnectionManager.getInstance().isLoggedIn()); } + @Test + public void testLogoutFailedBecauseCredentialsExpired() throws Exception { + // if our credentials expire and we try to log out, + // we should consider that the logout succeeded. + // Workaround for https://github.com/Wikidata/Wikidata-Toolkit/issues/511 + BasicApiConnection connection = mock(BasicApiConnection.class); + whenNew(BasicApiConnection.class).withAnyArguments().thenReturn(connection); + when(connection.getCurrentUser()).thenReturn(username); + + when(request.getParameter("csrf_token")).thenReturn(Command.csrfFactory.getFreshToken()); + when(request.getParameter(USERNAME)).thenReturn(username); + when(request.getParameter(PASSWORD)).thenReturn(password); + + // login first + command.doPost(request, response); + + assertTrue(ConnectionManager.getInstance().isLoggedIn()); + + // logout + when(request.getParameter("logout")).thenReturn("true"); + doThrow(new MediaWikiApiErrorException("assertuserfailed", "No longer logged in")).when(connection).logout(); + command.doPost(request, response); + + // not logged in anymore + assertFalse(ConnectionManager.getInstance().isLoggedIn()); + } + private static Cookie[] makeRequestCookies() { List cookies = new ArrayList<>(); cookieMap.forEach((key, value) -> cookies.add(new Cookie(WIKIDATA_COOKIE_PREFIX + key, value)));