Fix wikidata logout when credentials have expired. Fixes #2873 (#2878)

This commit is contained in:
Antonin Delpeuch 2020-07-04 08:38:17 +02:00 committed by GitHub
parent f4692de9e1
commit 952447461f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 1 deletions

View File

@ -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());
}
}
}
}

View File

@ -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<Cookie> cookies = new ArrayList<>();
cookieMap.forEach((key, value) -> cookies.add(new Cookie(WIKIDATA_COOKIE_PREFIX + key, value)));