From 3592946e6f2d59be10b66cd10441bd99be109852 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Wed, 23 May 2018 19:21:35 +0200 Subject: [PATCH] Fix api login(#119) --- api/src/main/java/io/github/wulkanowy/api/Client.java | 10 +++++----- .../java/io/github/wulkanowy/api/StudentAndParent.java | 6 +++--- .../main/java/io/github/wulkanowy/api/login/Login.java | 5 +++++ .../test/java/io/github/wulkanowy/api/ClientTest.java | 6 +++--- .../io/github/wulkanowy/api/StudentAndParentTest.java | 2 +- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/api/src/main/java/io/github/wulkanowy/api/Client.java b/api/src/main/java/io/github/wulkanowy/api/Client.java index 53300788a..277b80393 100644 --- a/api/src/main/java/io/github/wulkanowy/api/Client.java +++ b/api/src/main/java/io/github/wulkanowy/api/Client.java @@ -118,7 +118,7 @@ public class Client { this.cookies.addItems(response.cookies()); - Document doc = checkForErrors(response.parse()); + Document doc = checkForErrors(response.parse(), response.statusCode()); if (loginBefore) { lastSuccessRequest = new Date(); @@ -144,7 +144,7 @@ public class Client { response.bufferUp(); // fixes cert parsing issues #109 - return checkForErrors(response.parse()); + return checkForErrors(response.parse(), response.statusCode()); } public String getJsonStringByUrl(String url) throws IOException, VulcanException { @@ -182,7 +182,7 @@ public class Client { return response.body(); } - Document checkForErrors(Document doc) throws VulcanException { + Document checkForErrors(Document doc, int code) throws VulcanException { lastSuccessRequest = null; String title = doc.select("title").text(); @@ -195,8 +195,8 @@ public class Client { throw new NotLoggedInErrorException(singIn); } - if (title.startsWith("Błąd")) { - throw new NotLoggedInErrorException(title + " " + doc.selectFirst("p, body")); + if ("Błąd strony".equals(title)) { + throw new NotLoggedInErrorException(title + " " + doc.selectFirst("p, body") + ", status: " + code); } return doc; diff --git a/api/src/main/java/io/github/wulkanowy/api/StudentAndParent.java b/api/src/main/java/io/github/wulkanowy/api/StudentAndParent.java index 1acecfd3b..16ee002e0 100644 --- a/api/src/main/java/io/github/wulkanowy/api/StudentAndParent.java +++ b/api/src/main/java/io/github/wulkanowy/api/StudentAndParent.java @@ -65,12 +65,12 @@ public class StudentAndParent implements SnP { return getBaseUrl(); } - // get url to uonetplus-opiekun.vulcan.net.pl + // get url to uonetplus-opiekun.fakelog.cf Document startPage = client.getPageByUrl(START_PAGE_URL); Element studentTileLink = startPage.select(".panel.linkownia.pracownik.klient > a").first(); if (null == studentTileLink) { - throw new NotLoggedInErrorException("You are probably not logged in. Force login"); + throw new VulcanException("Na pewno używasz konta z dostępem do Witryny ucznia i rodzica?"); } String snpPageUrl = studentTileLink.attr("href"); @@ -84,7 +84,7 @@ public class StudentAndParent implements SnP { String[] path = snpPageUrl.split(client.getHost())[1].split("/"); if (5 != path.length) { - throw new NotLoggedInErrorException("You are probably not logged in"); + throw new NotLoggedInErrorException("You are probably not logged in " + snpPageUrl); } return path[2]; diff --git a/api/src/main/java/io/github/wulkanowy/api/login/Login.java b/api/src/main/java/io/github/wulkanowy/api/login/Login.java index a734501ea..04e1efc16 100644 --- a/api/src/main/java/io/github/wulkanowy/api/login/Login.java +++ b/api/src/main/java/io/github/wulkanowy/api/login/Login.java @@ -9,6 +9,7 @@ import org.jsoup.select.Elements; import java.io.IOException; import io.github.wulkanowy.api.Client; +import io.github.wulkanowy.api.NotLoggedInErrorException; import io.github.wulkanowy.api.VulcanException; public class Login { @@ -27,6 +28,10 @@ public class Login { public String login(String email, String password, String symbol) throws VulcanException, IOException { Document certDoc = sendCredentials(email, password); + if ("Błąd".equals(certDoc.title())) { + throw new NotLoggedInErrorException(certDoc.selectFirst("body").text()); + } + return sendCertificate(certDoc, symbol); } diff --git a/api/src/test/java/io/github/wulkanowy/api/ClientTest.java b/api/src/test/java/io/github/wulkanowy/api/ClientTest.java index 38f856172..50724875d 100644 --- a/api/src/test/java/io/github/wulkanowy/api/ClientTest.java +++ b/api/src/test/java/io/github/wulkanowy/api/ClientTest.java @@ -25,7 +25,7 @@ public class ClientTest { Document doc = Jsoup.parse(getFixtureAsString("login/Logowanie-success.html")); - Assert.assertEquals(doc, client.checkForErrors(doc)); + Assert.assertEquals(doc, client.checkForErrors(doc, 200)); } @Test(expected = VulcanOfflineException.class) @@ -34,7 +34,7 @@ public class ClientTest { Document doc = Jsoup.parse(getFixtureAsString("login/PrzerwaTechniczna.html")); - client.checkForErrors(doc); + client.checkForErrors(doc, 200); } @Test(expected = NotLoggedInErrorException.class) @@ -43,7 +43,7 @@ public class ClientTest { Document doc = Jsoup.parse(getFixtureAsString("login/Logowanie-notLoggedIn.html")); - client.checkForErrors(doc); + client.checkForErrors(doc, 200); } @Test diff --git a/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTest.java b/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTest.java index 2b3d72500..387d18f58 100644 --- a/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTest.java +++ b/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTest.java @@ -53,7 +53,7 @@ public class StudentAndParentTest { snp.getSnpHomePageUrl()); } - @Test(expected = NotLoggedInErrorException.class) + @Test(expected = VulcanException.class) public void getSnpPageUrlWithWrongPage() throws Exception { Document wrongPageDocument = Jsoup.parse( FixtureHelper.getAsString(getClass().getResourceAsStream("OcenyWszystkie-semester.html"))