BasicApiConnection extends ApiConnection { public void login(String username, String password) throws LoginFailedException { try { String token = getOrFetchToken("login"); try { this.confirmLogin(token, username, password); } catch (NeedLoginTokenException e) { clearToken("login"); token = getOrFetchToken("login"); this.confirmLogin(token, username, password); } } catch (IOException | MediaWikiApiErrorException e1) { throw new LoginFailedException(e1.getMessage(), e1); } } BasicApiConnection(String apiBaseUrl); @JsonCreator protected  BasicApiConnection(
			@JsonProperty("baseUrl") String apiBaseUrl,
			@JsonProperty("cookies") List<HttpCookieWrapper> cookies,
			@JsonProperty("username") String username,
			@JsonProperty("loggedIn") boolean loggedIn,
			@JsonProperty("tokens") Map<String, String> tokens,
			@JsonProperty("connectTimeout") int connectTimeout,
			@JsonProperty("readTimeout") int readTimeout); static BasicApiConnection getTestWikidataApiConnection(); static BasicApiConnection getWikidataApiConnection(); static BasicApiConnection getWikimediaCommonsApiConnection(); void login(String username, String password); @JsonProperty("cookies") List<HttpCookie> getCookies(); void clearCookies(); void logout();  }
@Test public void testGetToken() throws LoginFailedException, IOException, MediaWikiApiErrorException, InterruptedException { connection.login("username", "password"); assertNotNull(connection.getOrFetchToken("csrf")); }
@Test public void testLogin() throws LoginFailedException { assertFalse(connection.loggedIn); connection.login("username", "password"); assertEquals("username", connection.getCurrentUser()); assertEquals("password", connection.password); assertTrue(connection.isLoggedIn()); }
@Test public void testSerialize() throws LoginFailedException, IOException { connection.login("username", "password"); connection.setConnectTimeout(5000); connection.setReadTimeout(6000); assertTrue(connection.isLoggedIn()); String jsonSerialization = mapper.writeValueAsString(connection); jsonSerialization = jsonSerialization.replaceAll("\"domain\":\"[^\"]*\"", "\"domain\":\"domain comparison should be skipped\""); assertEquals(LOGGED_IN_SERIALIZED_CONNECTION, jsonSerialization); }
@Test(expected = LoginFailedException.class) public void loginUserErrors() throws LoginFailedException { connection.login("username1", "password1"); }
@Test(expected = AssertUserFailedException.class) public void testCheckCredentials() throws IOException, MediaWikiApiErrorException, LoginFailedException { connection.login("username", "password"); assertTrue(connection.isLoggedIn()); connection.checkCredentials(); }