TouchPoint extends OkHttpBaseApi implements OpacApi { protected LoginResponse login(Account acc) throws OpacErrorException, IOException { String html; FormBody.Builder body = new FormBody.Builder(); try { httpGet(opac_url + "/login.do", ENCODING); } catch (IOException e1) { e1.printStackTrace(); } body.add("username", acc.getName()); body.add("password", acc.getPassword()); body.add("CSId", CSId); body.add("methodToCall", "submit"); body.add("login_action", "Login"); html = httpPost(opac_url + "/login.do", body.build(), ENCODING); Document doc = Jsoup.parse(html); if (doc.getElementsByClass("alert").size() > 0) { String message = doc.select(".alert").get(0).text(); if ((message.contains("Nutzungseinschr") || message.contains("Datenbankauswahl")) && doc.select("a[href*=methodToCall=done]").size() > 0) { httpGet(opac_url + "/login.do?methodToCall=done", ENCODING); logged_in = System.currentTimeMillis(); logged_in_as = acc; boolean showMessage = message.contains("Nutzungseinschr"); return new LoginResponse(true, showMessage ? message : null); } else { throw new OpacErrorException(doc.getElementsByClass("alert").get(0).text()); } } Element changePasswordForm = doc.select("form[action$=methodToCall=changePassword]").first(); if (changePasswordForm != null) { throw new OpacErrorException(stringProvider.getString("please_change_password")); } logged_in = System.currentTimeMillis(); logged_in_as = acc; return new LoginResponse(true); }  List<SearchField> parseSearchFields(); @Override void start(); @Override void init(Library lib, HttpClientFactory httpClientFactory); @Override SearchRequestResult search(List<SearchQuery> query); SearchRequestResult volumeSearch(Map<String, String> query); @Override SearchRequestResult searchGetPage(int page); @Override DetailedItem getResultById(String id, String homebranch); String getUrlForId(String id); @Override DetailedItem getResult(int nr); @Override ReservationResult reservation(DetailedItem item, Account acc,
            int useraction, String selection); @Override ProlongResult prolong(String a, Account account, int useraction,
            String Selection); @Override CancelResult cancel(String media, Account account, int useraction,
            String selection); @Override AccountData account(Account acc); @Override String getShareUrl(String id, String title); @Override int getSupportFlags(); @Override ProlongAllResult prolongAll(Account account, int useraction,
            String selection); @Override SearchRequestResult filterResults(Filter filter, Option option); @Override void checkAccountData(Account account); @Override void setLanguage(String language); @Override Set<String> getSupportedLanguages();  }
@Test public void testLoginError() throws IOException { doReturn(readResource("/touchpoint/login/loginPage.html")) .when(touchPoint).httpGet(eq(touchPoint.opac_url + "/login.do"), anyString()); doReturn(readResource("/touchpoint/login/errorMessage.html")) .when(touchPoint) .httpPost(eq(touchPoint.opac_url + "/login.do"), any(RequestBody.class), anyString()); try { touchPoint.login(account); } catch (OpacApi.OpacErrorException e) { assertEquals( "Anmeldung fehlgeschlagen, ein Fehler ist aufgetreten. 0000000test: " + "Benutzernummer nicht korrekt oder keine Benutzerdaten vorhanden.", e.getMessage()); return; } fail("no OpacErrorException thrown"); }
@Test public void testLoginSuccess() throws OpacApi.OpacErrorException, IOException { doReturn(readResource("/touchpoint/login/loginPage.html")) .when(touchPoint).httpGet(eq(touchPoint.opac_url + "/login.do"), anyString()); doReturn(readResource("/touchpoint/login/loggedIn.html")) .when(touchPoint) .httpPost(eq(touchPoint.opac_url + "/login.do"), any(RequestBody.class), anyString()); TouchPoint.LoginResponse login = touchPoint.login(account); assertTrue(login.success); assertNull(login.warning); }
@Test public void testLoginMessage() throws OpacApi.OpacErrorException, IOException { doReturn(readResource("/touchpoint/login/loginPage.html")) .when(touchPoint).httpGet(eq(touchPoint.opac_url + "/login.do"), anyString()); doReturn(readResource("/touchpoint/login/alertMessage.html")) .when(touchPoint) .httpPost(eq(touchPoint.opac_url + "/login.do"), any(RequestBody.class), anyString()); doReturn(readResource("/touchpoint/login/loggedIn.html")) .when(touchPoint) .httpGet(eq(touchPoint.opac_url + "/login.do?methodToCall=done"), anyString()); TouchPoint.LoginResponse login = touchPoint.login(account); assertTrue(login.success); assertNull(login.warning); }
@Test public void testLoginWarning() throws OpacApi.OpacErrorException, IOException { doReturn(readResource("/touchpoint/login/loginPage.html")) .when(touchPoint).httpGet(eq(touchPoint.opac_url + "/login.do"), anyString()); doReturn(readResource("/touchpoint/login/warningMessage.html")) .when(touchPoint) .httpPost(eq(touchPoint.opac_url + "/login.do"), any(RequestBody.class), anyString()); doReturn(readResource("/touchpoint/login/loggedIn.html")) .when(touchPoint) .httpGet(eq(touchPoint.opac_url + "/login.do?methodToCall=done"), anyString()); TouchPoint.LoginResponse login = touchPoint.login(account); assertTrue(login.success); assertEquals("Nutzungseinschränkung", login.warning); }