diff --git a/.gitignore b/.gitignore index eec8adbe8..e70681531 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,5 @@ local.properties .Trashes ehthumbs.db Thumbs.db +.idea/codeStyles/ +.idea/caches/ 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 dde27c68c..c3693c407 100644 --- a/api/src/main/java/io/github/wulkanowy/api/Client.java +++ b/api/src/main/java/io/github/wulkanowy/api/Client.java @@ -44,7 +44,11 @@ public class Client { String[] url = creds[0].split("://"); protocol = url[0]; - host = url[1]; + String[] path = url[1].split("/"); + host = path[0]; + if (path.length > 1) { + symbol = path[1]; + } email = creds[2]; } } diff --git a/api/src/main/java/io/github/wulkanowy/api/VulcanException.java b/api/src/main/java/io/github/wulkanowy/api/VulcanException.java index aba020573..0bc0c51fc 100644 --- a/api/src/main/java/io/github/wulkanowy/api/VulcanException.java +++ b/api/src/main/java/io/github/wulkanowy/api/VulcanException.java @@ -2,7 +2,7 @@ package io.github.wulkanowy.api; public class VulcanException extends Exception { - protected VulcanException(String message) { + public VulcanException(String message) { super(message); } 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 2814950f8..32e372c1a 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 @@ -31,12 +31,27 @@ public class Login { } Document sendCredentials(String email, String password) throws IOException, VulcanException { - Document html = client.postPageByUrl(LOGIN_PAGE_URL, new String[][]{ + String[][] credentials = new String[][]{ {"LoginName", email}, {"Password", password} - }); + }; - Element errorMessage = html.select(".ErrorMessage").first(); + String nextUrl = LOGIN_PAGE_URL; + Document loginPage = client.getPageByUrl(nextUrl, false); + + Element formFirst = loginPage.select("#form1").first(); + if (null != formFirst) { // on adfs login + Document formSecond = client.postPageByUrl( + formFirst.attr("abs:action"), + getFormStateParams(formFirst, "", "") + ); + credentials = getFormStateParams(formSecond, email, password); + nextUrl = formSecond.select("#form1").first().attr("abs:action"); + } + + Document html = client.postPageByUrl(nextUrl, credentials); + + Element errorMessage = html.select(".ErrorMessage, #ErrorTextLabel").first(); if (null != errorMessage) { throw new BadCredentialsException(errorMessage.text()); } @@ -44,6 +59,21 @@ public class Login { return html; } + private String[][] getFormStateParams(Element form, String email, String password) { + return new String[][]{ + {"__VIEWSTATE", form.select("#__VIEWSTATE").val()}, + {"__VIEWSTATEGENERATOR", form.select("#__VIEWSTATEGENERATOR").val()}, + {"__EVENTVALIDATION", form.select("#__EVENTVALIDATION").val()}, + {"__db", form.select("input[name=__db]").val()}, + {"PassiveSignInButton.x", "0"}, + {"PassiveSignInButton.y", "0"}, + {"SubmitButton.x", "0"}, + {"SubmitButton.y", "0"}, + {"UsernameTextBox", email}, + {"PasswordTextBox", password}, + }; + } + String sendCertificate(Document doc, String defaultSymbol) throws IOException, VulcanException { String certificate = doc.select("input[name=wresult]").val(); @@ -53,6 +83,10 @@ public class Login { Document targetDoc = sendCertData(doc); String title = targetDoc.select("title").text(); + if ("Working...".equals(title)) { // on adfs login + title = sendCertData(targetDoc).select("title").text(); + } + if ("Logowanie".equals(title)) { throw new AccountPermissionException("No account access. Try another symbol"); } diff --git a/api/src/main/java/io/github/wulkanowy/api/timetable/Timetable.java b/api/src/main/java/io/github/wulkanowy/api/timetable/Timetable.java index f575edb38..5cb623b4e 100644 --- a/api/src/main/java/io/github/wulkanowy/api/timetable/Timetable.java +++ b/api/src/main/java/io/github/wulkanowy/api/timetable/Timetable.java @@ -95,12 +95,15 @@ public class Timetable { moveWarningToLessonNode(e); switch (e.size()) { + case 2: + Element span = e.last().select("span").first(); + if (span.hasClass(LessonTypes.CLASS_MOVED_OR_CANCELED)) { + lesson.setNewMovedInOrChanged(true); + lesson.setDescription("poprzednio: " + getLessonAndGroupInfoFromSpan(span)[0]); + } case 1: addLessonInfoFromElement(lesson, e.first()); break; - case 2: - addLessonInfoFromElement(lesson, e.last()); - break; case 3: addLessonInfoFromElement(lesson, e.get(1)); break; diff --git a/api/src/test/java/io/github/wulkanowy/api/login/LoginTest.java b/api/src/test/java/io/github/wulkanowy/api/login/LoginTest.java index aac3b795b..01602ebe4 100644 --- a/api/src/test/java/io/github/wulkanowy/api/login/LoginTest.java +++ b/api/src/test/java/io/github/wulkanowy/api/login/LoginTest.java @@ -24,6 +24,7 @@ public class LoginTest { Client client = Mockito.mock(Client.class); Mockito.when(client.postPageByUrl(Mockito.anyString(), Mockito.any(String[][].class))).thenReturn(doc); + Mockito.when(client.getPageByUrl(Mockito.anyString(), Mockito.anyBoolean())).thenReturn(doc); return client; } @@ -56,10 +57,8 @@ public class LoginTest { Login login = new Login(client); Assert.assertEquals( - getFixtureAsString("cert.xml").replaceAll("\\s+",""), - login.sendCredentials("a@a", "passwd") - .select("input[name=wresult]").attr("value") - .replaceAll("\\s+","") + getFixtureAsString("cert-stock.xml").replaceAll("\\s+",""), + login.sendCredentials("a@a", "passwd").select("input[name=wresult]").attr("value").replaceAll("\\s+","") ); } @@ -83,21 +82,21 @@ public class LoginTest { public void sendCertificateAccountPermissionTest() throws Exception { Login login = new Login(getClient("Logowanie-brak-dostepu.html")); - login.sendCertificate(getFixtureAsDocument("cert.xml"), "demo123"); + login.sendCertificate(getFixtureAsDocument("cert-stock.xml"), "demo123"); } @Test(expected = LoginErrorException.class) public void sendCertificateLoginErrorTest() throws Exception { Login login = new Login(getClient("Logowanie-certyfikat.html")); // change to other document - login.sendCertificate(getFixtureAsDocument("cert.xml"), "demo123"); + login.sendCertificate(getFixtureAsDocument("cert-stock.xml"), "demo123"); } @Test public void findSymbolInCertificateTest() throws Exception { Login login = new Login(getClient("Logowanie-certyfikat.html")); - String certificate = getFixtureAsString("cert.xml"); + String certificate = getFixtureAsString("cert-stock.xml"); Assert.assertEquals("demo12345", login.findSymbolInCertificate(certificate)); } diff --git a/api/src/test/java/io/github/wulkanowy/api/timetable/TimetableTest.java b/api/src/test/java/io/github/wulkanowy/api/timetable/TimetableTest.java index 652676348..d7df9411b 100644 --- a/api/src/test/java/io/github/wulkanowy/api/timetable/TimetableTest.java +++ b/api/src/test/java/io/github/wulkanowy/api/timetable/TimetableTest.java @@ -110,6 +110,7 @@ public class TimetableTest extends StudentAndParentTestCase { Assert.assertEquals("Uroczyste zakończenie roku szkolnego", full.getWeekTable().getDay(4).getLesson(0).getSubject()); Assert.assertEquals("Fizyka", full.getWeekTable().getDay(0).getLesson(0).getSubject()); Assert.assertEquals("Metodologia programowania", full.getWeekTable().getDay(1).getLesson(0).getSubject()); + Assert.assertEquals("Język niemiecki", full.getWeekTable().getDay(4).getLesson(2).getSubject()); Assert.assertEquals("", holidays.getWeekTable().getDay(3).getLesson(3).getSubject()); } @@ -122,6 +123,7 @@ public class TimetableTest extends StudentAndParentTestCase { Assert.assertEquals("Nowak Jadwiga", full.getWeekTable().getDay(2).getLesson(0).getTeacher()); Assert.assertEquals("Nowicka Irena", full.getWeekTable().getDay(3).getLesson(1).getTeacher()); Assert.assertEquals("Baran Małgorzata", full.getWeekTable().getDay(4).getLesson(0).getTeacher()); + Assert.assertEquals("", full.getWeekTable().getDay(4).getLesson(1).getTeacher()); Assert.assertEquals("", holidays.getWeekTable().getDay(3).getLesson(3).getTeacher()); } @@ -148,7 +150,10 @@ public class TimetableTest extends StudentAndParentTestCase { Assert.assertEquals("zastępstwo (poprzednio: Religia)", full.getWeekTable().getDay(2).getLesson(0).getDescription()); Assert.assertEquals("zastępstwo (poprzednio: Wychowanie fizyczne)", full.getWeekTable().getDay(3).getLesson(1).getDescription()); Assert.assertEquals("", full.getWeekTable().getDay(4).getLesson(0).getDescription()); + Assert.assertEquals("", full.getWeekTable().getDay(4).getLesson(1).getDescription()); + Assert.assertEquals("poprzednio: Wychowanie fizyczne", full.getWeekTable().getDay(4).getLesson(2).getDescription()); Assert.assertEquals("egzamin", full.getWeekTable().getDay(3).getLesson(0).getDescription()); + Assert.assertEquals("", full.getWeekTable().getDay(4).getLesson(1).getDescription()); Assert.assertEquals("", holidays.getWeekTable().getDay(3).getLesson(3).getDescription()); } @@ -237,6 +242,8 @@ public class TimetableTest extends StudentAndParentTestCase { Assert.assertTrue(full.getWeekTable().getDay(1).getLesson(2).isNewMovedInOrChanged()); Assert.assertTrue(full.getWeekTable().getDay(1).getLesson(3).isNewMovedInOrChanged()); Assert.assertTrue(full.getWeekTable().getDay(3).getLesson(1).isNewMovedInOrChanged()); + Assert.assertFalse(full.getWeekTable().getDay(4).getLesson(1).isNewMovedInOrChanged()); + Assert.assertTrue(full.getWeekTable().getDay(4).getLesson(2).isNewMovedInOrChanged()); Assert.assertFalse(holidays.getWeekTable().getDay(3).getLesson(3).isNewMovedInOrChanged()); } } diff --git a/api/src/test/resources/io/github/wulkanowy/api/login/Logowanie-certyfikat.html b/api/src/test/resources/io/github/wulkanowy/api/login/Logowanie-certyfikat.html index a8496cd1b..f53a34856 100644 --- a/api/src/test/resources/io/github/wulkanowy/api/login/Logowanie-certyfikat.html +++ b/api/src/test/resources/io/github/wulkanowy/api/login/Logowanie-certyfikat.html @@ -3,10 +3,10 @@ Working... -
+ - - + +