diff --git a/.circleci/config.yml b/.circleci/config.yml index 16ad564b7..f55c7ec8f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -31,9 +31,18 @@ jobs: - run: name: Setup environment command: ./gradlew dependencies --no-daemon --stacktrace --console=plain -PdisablePreDex || true + - run: + name: Decrypt keys + command: | + openssl aes-256-cbc -d -in ./app/key-encrypted.p12 -k $ENCRYPT_KEY >> ./app/key.p12 + openssl aes-256-cbc -d -in ./app/upload-key-encrypted.jks -k $ENCRYPT_KEY >> ./app/upload-key.jks - run: name: Initial build command: ./gradlew build assembleDebug -x test -x lint -x fabricGenerateResourcesRelease --no-daemon --stacktrace --console=plain -PdisablePreDex + - run: + name: Clear keys + command: | + rm ./app/key.p12 ./app/upload-key.jks - store_artifacts: path: ./app/build/outputs/apk/ destination: apks/ @@ -172,6 +181,21 @@ jobs: name: Run sonarqube runner command: ./gradlew -x test -x lint sonarqube -Dsonar.host.url=$SONAR_HOST -Dsonar.organization=$SONAR_ORG -Dsonar.login=$SONAR_KEY -Dsonar.branch.name=$CIRCLE_BRANCH --no-daemon --stacktrace --console=plain -PdisablePreDex + deploy: + <<: *container_config + steps: + - *attach_workspace + - restore_cache: + <<: *general_cache_key + - run: + name: Decrypt keys + command: | + openssl aes-256-cbc -d -in ./app/key-encrypted.p12 -k $ENCRYPT_KEY >> ./app/key.p12 + openssl aes-256-cbc -d -in ./app/upload-key-encrypted.jks -k $ENCRYPT_KEY >> ./app/upload-key.jks + - run: + name: Publish release + command: ./gradlew publishRelease --no-daemon --stacktrace --console=plain -PdisablePreDex + workflows: version: 2 @@ -199,3 +223,9 @@ workflows: - app-test - api-test - instrumented + - deploy: + requires: + - instrumented + filters: + tags: + only: /.*/ diff --git a/.gitignore b/.gitignore index eec8adbe8..8ad04ebf0 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,7 @@ local.properties .Trashes ehthumbs.db Thumbs.db +.idea/codeStyles/ +.idea/caches/ +./app/key.p12 +./app/upload-key.jks 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 c3abb2ee6..c3693c407 100644 --- a/api/src/main/java/io/github/wulkanowy/api/Client.java +++ b/api/src/main/java/io/github/wulkanowy/api/Client.java @@ -21,11 +21,9 @@ public class Client { private String password; - private String symbol = "Default"; + private String symbol; - private Login login; - - private Date lastSuccessRequest = new Date(); + private Date lastSuccessRequest = null; private Cookies cookies = new Cookies(); @@ -46,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]; } } @@ -56,25 +58,16 @@ public class Client { return; } - this.symbol = getLogin().login(email, password, symbol); + this.cookies = new Cookies(); + this.symbol = new Login(this).login(email, password, symbol); } private boolean isLoggedIn() { - return getCookies().size() > 0 && + return getCookies().size() > 0 && lastSuccessRequest != null && 29 > TimeUnit.MILLISECONDS.toMinutes(new Date().getTime() - lastSuccessRequest.getTime()); } - Login getLogin() { - if (null != login) { - return login; - } - - login = new Login(this); - - return login; - } - public String getSymbol() { return symbol; } @@ -83,6 +76,10 @@ public class Client { this.symbol = symbol; } + public void addCookies(Map items) { + cookies.addItems(items); + } + private Map getCookies() { return cookies.getItems(); } @@ -98,8 +95,22 @@ public class Client { .replace("{symbol}", symbol); } - Document getPageByUrl(String url) throws IOException, VulcanException { - login(); + public Document getPageByUrl(String url) throws IOException, VulcanException { + return getPageByUrl(url, true, null); + } + + public Document getPageByUrl(String url, boolean loginBefore) throws IOException, VulcanException { + return getPageByUrl(url, loginBefore, null); + } + + public Document getPageByUrl(String url, boolean loginBefore, Map cookies) throws IOException, VulcanException { + if (loginBefore) { + login(); + } + + if (null != cookies) { + this.cookies.addItems(cookies); + } Connection.Response response = Jsoup.connect(getFilledUrl(url)) .followRedirects(true) @@ -110,7 +121,9 @@ public class Client { Document doc = checkForErrors(response.parse()); - lastSuccessRequest = new Date(); + if (loginBefore) { + lastSuccessRequest = new Date(); + } return doc; } @@ -169,6 +182,8 @@ public class Client { } Document checkForErrors(Document doc) throws VulcanException { + lastSuccessRequest = null; + String title = doc.select("title").text(); if ("Przerwa techniczna".equals(title)) { throw new VulcanOfflineException(title); @@ -179,6 +194,10 @@ public class Client { throw new NotLoggedInErrorException(singIn); } + if ("Błąd strony".equals(title)) { + throw new VulcanException("Nieznany błąd"); + } + return doc; } } diff --git a/api/src/main/java/io/github/wulkanowy/api/Diary.java b/api/src/main/java/io/github/wulkanowy/api/Diary.java new file mode 100644 index 000000000..d7d09f9ce --- /dev/null +++ b/api/src/main/java/io/github/wulkanowy/api/Diary.java @@ -0,0 +1,38 @@ +package io.github.wulkanowy.api; + +public class Diary implements ParamItem { + + private String id = ""; + + private String name = ""; + + private boolean current = false; + + public String getId() { + return id; + } + + public Diary setId(String id) { + this.id = id; + return this; + } + + public String getName() { + return name; + } + + public Diary setName(String name) { + this.name = name; + return this; + } + + @Override + public boolean isCurrent() { + return current; + } + + public Diary setCurrent(boolean current) { + this.current = current; + return this; + } +} diff --git a/api/src/main/java/io/github/wulkanowy/api/ParamItem.java b/api/src/main/java/io/github/wulkanowy/api/ParamItem.java new file mode 100644 index 000000000..c830467ad --- /dev/null +++ b/api/src/main/java/io/github/wulkanowy/api/ParamItem.java @@ -0,0 +1,12 @@ +package io.github.wulkanowy.api; + +interface ParamItem { + + ParamItem setId(String id); + + ParamItem setName(String name); + + ParamItem setCurrent(boolean isCurrent); + + boolean isCurrent(); +} diff --git a/api/src/main/java/io/github/wulkanowy/api/Semester.java b/api/src/main/java/io/github/wulkanowy/api/Semester.java index 64a07c976..31230f012 100644 --- a/api/src/main/java/io/github/wulkanowy/api/Semester.java +++ b/api/src/main/java/io/github/wulkanowy/api/Semester.java @@ -1,21 +1,12 @@ package io.github.wulkanowy.api; -public class Semester { - - private String number = ""; +public class Semester implements ParamItem { private String id = ""; - private boolean isCurrent = false; + private String name = ""; - public String getNumber() { - return number; - } - - public Semester setNumber(String number) { - this.number = number; - return this; - } + private boolean current = false; public String getId() { return id; @@ -26,12 +17,21 @@ public class Semester { return this; } + public String getName() { + return name; + } + + public Semester setName(String number) { + this.name = number; + return this; + } + public boolean isCurrent() { - return isCurrent; + return current; } public Semester setCurrent(boolean current) { - isCurrent = current; + this.current = current; return this; } } diff --git a/api/src/main/java/io/github/wulkanowy/api/SnP.java b/api/src/main/java/io/github/wulkanowy/api/SnP.java index adbffd4b0..7f074071c 100644 --- a/api/src/main/java/io/github/wulkanowy/api/SnP.java +++ b/api/src/main/java/io/github/wulkanowy/api/SnP.java @@ -8,17 +8,25 @@ import java.util.List; public interface SnP { - String getId(); + String getSchoolID(); - StudentAndParent storeContextCookies() throws IOException, VulcanException; + void setDiaryID(String id); + + String getStudentID(); + + List getStudents() throws IOException, VulcanException; + + StudentAndParent setUp() throws IOException, VulcanException; String getRowDataChildValue(Element e, int index); Document getSnPPageDocument(String url) throws IOException, VulcanException; + List getDiaries() throws IOException, VulcanException; + List getSemesters() throws IOException, VulcanException; List getSemesters(Document gradesPage); - Semester getCurrentSemester(List semesterList); + T getCurrent(List list); } diff --git a/api/src/main/java/io/github/wulkanowy/api/Student.java b/api/src/main/java/io/github/wulkanowy/api/Student.java new file mode 100644 index 000000000..8b22f1d83 --- /dev/null +++ b/api/src/main/java/io/github/wulkanowy/api/Student.java @@ -0,0 +1,37 @@ +package io.github.wulkanowy.api; + +public class Student implements ParamItem { + + private String id = ""; + + private String name = ""; + + private boolean current = false; + + public String getId() { + return id; + } + + public Student setId(String id) { + this.id = id; + return this; + } + + public String getName() { + return name; + } + + public Student setName(String name) { + this.name = name; + return this; + } + + public boolean isCurrent() { + return current; + } + + public Student setCurrent(boolean current) { + this.current = current; + return this; + } +} 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 8f67da39c..fbc144868 100644 --- a/api/src/main/java/io/github/wulkanowy/api/StudentAndParent.java +++ b/api/src/main/java/io/github/wulkanowy/api/StudentAndParent.java @@ -5,8 +5,11 @@ import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; +import java.net.URL; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class StudentAndParent implements SnP { @@ -18,28 +21,47 @@ public class StudentAndParent implements SnP { private Client client; - private String id; + private String schoolID; - StudentAndParent(Client client, String id) { + private String studentID; + + private String diaryID; + + StudentAndParent(Client client, String schoolID, String studentID, String diaryID) { this.client = client; - this.id = id; + this.schoolID = schoolID; + this.studentID = studentID; + this.diaryID = diaryID; } - private String getBaseUrl() { - return BASE_URL.replace("{ID}", getId()); - } + public StudentAndParent setUp() throws IOException, VulcanException { + if (null == getStudentID() || "".equals(getStudentID())) { + Document doc = client.getPageByUrl(getSnpHomePageUrl()); - public String getId() { - return id; - } + Student student = getCurrent(getStudents(doc)); + studentID = student.getId(); + + Diary diary = getCurrent(getDiaries(doc)); + diaryID = diary.getId(); + } - public StudentAndParent storeContextCookies() throws IOException, VulcanException { - client.getPageByUrl(getSnpHomePageUrl()); return this; } + public String getSchoolID() { + return schoolID; + } + + public String getStudentID() { + return studentID; + } + + private String getBaseUrl() { + return BASE_URL.replace("{ID}", getSchoolID()); + } + String getSnpHomePageUrl() throws IOException, VulcanException { - if (null != getId()) { + if (null != getSchoolID()) { return getBaseUrl(); } @@ -53,7 +75,7 @@ public class StudentAndParent implements SnP { String snpPageUrl = studentTileLink.attr("href"); - this.id = getExtractedIdFromUrl(snpPageUrl); + this.schoolID = getExtractedIdFromUrl(snpPageUrl); return snpPageUrl; } @@ -72,8 +94,39 @@ public class StudentAndParent implements SnP { return e.select(".daneWiersz .wartosc").get(index - 1).text(); } + public void setDiaryID(String id) { + this.diaryID = id; + } + public Document getSnPPageDocument(String url) throws IOException, VulcanException { - return client.getPageByUrl(getBaseUrl() + url); + Map cookies = new HashMap<>(); + cookies.put("idBiezacyDziennik", diaryID); + cookies.put("idBiezacyUczen", studentID); + client.addCookies(cookies); + + Document doc = client.getPageByUrl(getBaseUrl() + url, true, cookies); + + if ("Witryna ucznia i rodzica – Strona główna".equals(doc.select("title").first().text())) { + throw new VulcanException("Sesja została nieprawidłowo zainicjowana"); + } + + return doc; + } + + public List getDiaries() throws IOException, VulcanException { + return getDiaries(client.getPageByUrl(getBaseUrl())); + } + + private List getDiaries(Document doc) throws IOException, VulcanException { + return getList(doc.select("#dziennikDropDownList option"), Diary.class); + } + + public List getStudents() throws IOException, VulcanException { + return getStudents(client.getPageByUrl(getBaseUrl())); + } + + private List getStudents(Document doc) throws IOException, VulcanException { + return getList(doc.select("#uczenDropDownList option"), Student.class); } public List getSemesters() throws IOException, VulcanException { @@ -87,10 +140,10 @@ public class StudentAndParent implements SnP { for (Element e : semesterOptions) { Semester semester = new Semester() - .setId(e.text()) - .setNumber(e.attr("value")); + .setId(e.attr("value")) + .setName(e.text()); - if ("selected".equals(e.attr("selected"))) { + if (isCurrent(e)) { semester.setCurrent(true); } @@ -100,15 +153,44 @@ public class StudentAndParent implements SnP { return semesters; } - public Semester getCurrentSemester(List semesterList) { - Semester current = null; - for (Semester s : semesterList) { + @SuppressWarnings("unchecked") + private List getList(Elements options, Class type) throws IOException, VulcanException { + List list = new ArrayList<>(); + + for (Element e : options) { + URL url = new URL(e.val()); + try { + ParamItem item = type.newInstance() + .setId(url.getQuery().split("=")[1]) + .setName(e.text()); + + if (isCurrent(e)) { + item.setCurrent(true); + } + + list.add((T) item); + } catch (Exception ex) { + throw new VulcanException("Error while trying to parse params list", ex); + } + } + + return list; + } + + @SuppressWarnings("unchecked") + public T getCurrent(List list) { + ParamItem current = null; + for (ParamItem s : list) { if (s.isCurrent()) { current = s; break; } } - return current; + return (T) current; + } + + private boolean isCurrent(Element e) { + return "selected".equals(e.attr("selected")); } } diff --git a/api/src/main/java/io/github/wulkanowy/api/Vulcan.java b/api/src/main/java/io/github/wulkanowy/api/Vulcan.java index 75b939ddf..d90d38741 100644 --- a/api/src/main/java/io/github/wulkanowy/api/Vulcan.java +++ b/api/src/main/java/io/github/wulkanowy/api/Vulcan.java @@ -18,16 +18,22 @@ import io.github.wulkanowy.api.user.FamilyInformation; public class Vulcan { - private String id; - private SnP snp; private Client client; - public void setCredentials(String email, String password, String symbol, String id) { - client = new Client(email, password, symbol); + private String schoolId; - this.id = id; + private String studentId; + + private String diaryId; + + public void setCredentials(String email, String password, String symbol, String schoolId, String studentId, String diaryId) { + this.schoolId = schoolId; + this.studentId = studentId; + this.diaryId = diaryId; + + client = new Client(email, password, symbol); } public Client getClient() throws NotLoggedInErrorException { @@ -43,20 +49,17 @@ public class Vulcan { } - public SnP getStudentAndParent() throws IOException, VulcanException { + public SnP getStudentAndParent() throws VulcanException, IOException { if (null != this.snp) { return this.snp; } - this.snp = new StudentAndParent(getClient(), id).storeContextCookies(); + this.snp = new StudentAndParent(getClient(), schoolId, studentId, diaryId) + .setUp(); return this.snp; } - public String getId() throws IOException, VulcanException { - return getStudentAndParent().getId(); - } - public AttendanceTable getAttendanceTable() throws IOException, VulcanException { return new AttendanceTable(getStudentAndParent()); } 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 723cab91c..0bc0c51fc 100644 --- a/api/src/main/java/io/github/wulkanowy/api/VulcanException.java +++ b/api/src/main/java/io/github/wulkanowy/api/VulcanException.java @@ -1,8 +1,12 @@ package io.github.wulkanowy.api; -public abstract class VulcanException extends Exception { +public class VulcanException extends Exception { - protected VulcanException(String message) { + public VulcanException(String message) { super(message); } + + protected VulcanException(String message, Exception e) { + super(message, e); + } } diff --git a/api/src/main/java/io/github/wulkanowy/api/exams/ExamsWeek.java b/api/src/main/java/io/github/wulkanowy/api/exams/ExamsWeek.java index 3dd056d9a..c111ce848 100644 --- a/api/src/main/java/io/github/wulkanowy/api/exams/ExamsWeek.java +++ b/api/src/main/java/io/github/wulkanowy/api/exams/ExamsWeek.java @@ -1,12 +1,17 @@ package io.github.wulkanowy.api.exams; +import org.apache.commons.lang3.StringUtils; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.List; +import java.util.Locale; import io.github.wulkanowy.api.SnP; import io.github.wulkanowy.api.VulcanException; @@ -22,11 +27,11 @@ public class ExamsWeek { this.snp = snp; } - public Week getCurrent() throws IOException, VulcanException { + public Week getCurrent() throws IOException, VulcanException, ParseException { return getWeek("", true); } - public Week getWeek(String tick, final boolean onlyNotEmpty) throws IOException, VulcanException { + public Week getWeek(String tick, final boolean onlyNotEmpty) throws IOException, VulcanException, ParseException { Document examsPage = snp.getSnPPageDocument(EXAMS_PAGE_URL + tick); Elements examsDays = examsPage.select(".mainContainer > div:not(.navigation)"); @@ -41,7 +46,9 @@ public class ExamsWeek { } if (null != dayHeading) { - day.setDate(dayHeading.text().split(", ")[1]); + String[] dateHeader = dayHeading.text().split(", "); + day.setDayName(StringUtils.capitalize(dateHeader[0])); + day.setDate(getFormattedDate(dateHeader[1])); } Elements exams = item.select("article"); @@ -51,15 +58,24 @@ public class ExamsWeek { .setType(snp.getRowDataChildValue(e, 2)) .setDescription(snp.getRowDataChildValue(e, 3)) .setTeacher(snp.getRowDataChildValue(e, 4).split(", ")[0]) - .setEntryDate(snp.getRowDataChildValue(e, 4).split(", ")[1]) + .setEntryDate(getFormattedDate(snp.getRowDataChildValue(e, 4).split(", ")[1])) ); } days.add(day); } + return new Week() - .setStartDayDate(examsDays.select("h2").first().text().split(" ")[1]) + .setStartDayDate(getFormattedDate(examsPage.select(".mainContainer > h2") + .first().text().split(" ")[1])) .setDays(days); } + + private String getFormattedDate(String date) throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy", Locale.ROOT); + Date d = sdf.parse(date); + sdf.applyPattern("yyyy-MM-dd"); + return sdf.format(d); + } } diff --git a/api/src/main/java/io/github/wulkanowy/api/generic/Day.java b/api/src/main/java/io/github/wulkanowy/api/generic/Day.java index 6bf26c040..5b9086a37 100644 --- a/api/src/main/java/io/github/wulkanowy/api/generic/Day.java +++ b/api/src/main/java/io/github/wulkanowy/api/generic/Day.java @@ -11,10 +11,6 @@ public class Day { private String dayName = ""; - private boolean isFreeDay = false; - - private String freeDayName = ""; - public Lesson getLesson(int index) { return lessons.get(index); } @@ -44,20 +40,4 @@ public class Day { public void setDayName(String dayName) { this.dayName = dayName; } - - public boolean isFreeDay() { - return isFreeDay; - } - - public void setFreeDay(boolean freeDay) { - isFreeDay = freeDay; - } - - public String getFreeDayName() { - return freeDayName; - } - - public void setFreeDayName(String freeDayName) { - this.freeDayName = freeDayName; - } } diff --git a/api/src/main/java/io/github/wulkanowy/api/grades/Grade.java b/api/src/main/java/io/github/wulkanowy/api/grades/Grade.java index 564b3e47c..31e1bbbd9 100644 --- a/api/src/main/java/io/github/wulkanowy/api/grades/Grade.java +++ b/api/src/main/java/io/github/wulkanowy/api/grades/Grade.java @@ -18,8 +18,6 @@ public class Grade { private String teacher = ""; - private String semester = ""; - public String getSubject() { return subject; } @@ -99,14 +97,4 @@ public class Grade { return this; } - - public String getSemester() { - return semester; - } - - public Grade setSemester(String semester) { - this.semester = semester; - - return this; - } } diff --git a/api/src/main/java/io/github/wulkanowy/api/grades/GradesList.java b/api/src/main/java/io/github/wulkanowy/api/grades/GradesList.java index ce8c0f770..81e8f3ae3 100644 --- a/api/src/main/java/io/github/wulkanowy/api/grades/GradesList.java +++ b/api/src/main/java/io/github/wulkanowy/api/grades/GradesList.java @@ -14,7 +14,6 @@ import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; -import io.github.wulkanowy.api.Semester; import io.github.wulkanowy.api.SnP; import io.github.wulkanowy.api.VulcanException; @@ -22,7 +21,7 @@ public class GradesList { private static final String GRADES_PAGE_URL = "Oceny/Wszystkie?details=2&okres="; - private SnP snp = null; + private SnP snp; private List grades = new ArrayList<>(); @@ -41,43 +40,54 @@ public class GradesList { public List getAll(String semester) throws IOException, ParseException, VulcanException { Document gradesPage = snp.getSnPPageDocument(getGradesPageUrl() + semester); Elements gradesRows = gradesPage.select(".ocenySzczegoly-table > tbody > tr"); - Semester currentSemester = snp.getCurrentSemester(snp.getSemesters(gradesPage)); for (Element row : gradesRows) { if ("Brak ocen".equals(row.select("td:nth-child(2)").text())) { continue; } - String descriptions = row.select("td:nth-child(3)").text(); - String symbol = descriptions.split(", ")[0]; - String description = descriptions.replaceFirst(symbol, "").replaceFirst(", ", ""); - - Pattern pattern = Pattern.compile("#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})"); - Matcher matcher = pattern.matcher(row.select("td:nth-child(2) span.ocenaCzastkowa") - .attr("style")); - - String color = ""; - while (matcher.find()) { - color = matcher.group(1); - } - - SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy", Locale.ROOT); - Date d = sdf.parse(row.select("td:nth-child(5)").text()); - sdf.applyPattern("yyyy-MM-dd"); - - grades.add(new Grade() - .setSubject(row.select("td:nth-child(1)").text()) - .setValue(row.select("td:nth-child(2)").text()) - .setColor(color) - .setSymbol(symbol) - .setDescription(description) - .setWeight(row.select("td:nth-child(4)").text()) - .setDate(sdf.format(d)) - .setTeacher(row.select("td:nth-child(6)").text()) - .setSemester(currentSemester.getNumber()) - ); + grades.add(getGrade(row)); } return grades; } + + private Grade getGrade(Element row) throws ParseException { + String descriptions = row.select("td:nth-child(3)").text(); + + String symbol = descriptions.split(", ")[0]; + String description = descriptions.replaceFirst(symbol, "").replaceFirst(", ", ""); + String color = getColor(row.select("td:nth-child(2) span.ocenaCzastkowa").attr("style")); + String date = formatDate(row.select("td:nth-child(5)").text()); + + return new Grade() + .setSubject(row.select("td:nth-child(1)").text()) + .setValue(row.select("td:nth-child(2)").text()) + .setColor(color) + .setSymbol(symbol) + .setDescription(description) + .setWeight(row.select("td:nth-child(4)").text()) + .setDate(date) + .setTeacher(row.select("td:nth-child(6)").text()); + } + + private String getColor(String styleAttr) { + Pattern pattern = Pattern.compile("#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})"); + Matcher matcher = pattern.matcher(styleAttr); + + String color = ""; + while (matcher.find()) { + color = matcher.group(1); + } + + return color; + } + + private String formatDate(String date) throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy", Locale.ROOT); + Date d = sdf.parse(date); + sdf.applyPattern("yyyy-MM-dd"); + + return sdf.format(d); + } } diff --git a/api/src/main/java/io/github/wulkanowy/api/grades/SubjectsList.java b/api/src/main/java/io/github/wulkanowy/api/grades/SubjectsList.java index 993594137..37902d0d8 100644 --- a/api/src/main/java/io/github/wulkanowy/api/grades/SubjectsList.java +++ b/api/src/main/java/io/github/wulkanowy/api/grades/SubjectsList.java @@ -13,16 +13,21 @@ import io.github.wulkanowy.api.VulcanException; public class SubjectsList { - private static final String SUBJECTS_PAGE_URL = "Oceny/Wszystkie?details=1"; + private static final String SUBJECTS_PAGE_URL = "Oceny/Wszystkie?details=1&okres="; - private SnP snp = null; + private SnP snp; public SubjectsList(SnP snp) { this.snp = snp; } + public List getAll() throws IOException, VulcanException { - Document subjectPage = snp.getSnPPageDocument(SUBJECTS_PAGE_URL); + return getAll(""); + } + + public List getAll(String semester) throws IOException, VulcanException { + Document subjectPage = snp.getSnPPageDocument(SUBJECTS_PAGE_URL + semester); Elements rows = subjectPage.select(".ocenyZwykle-table > tbody > tr"); 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 e7aecec09..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 @@ -18,57 +18,94 @@ public class Login { "{schema}%253a%252f%252fuonetplus.{host}%252f{symbol}%252fLoginEndpoint.aspx%26wctx%3D" + "{schema}%253a%252f%252fuonetplus.{host}%252f{symbol}%252fLoginEndpoint.aspx"; - private static final String LOGIN_ENDPOINT_PAGE_URL = - "{schema}://uonetplus.{host}/{symbol}/LoginEndpoint.aspx"; - private Client client; - private String symbol; - public Login(Client client) { this.client = client; } public String login(String email, String password, String symbol) throws VulcanException, IOException { - String certificate = sendCredentials(email, password, symbol); + Document certDoc = sendCredentials(email, password); - return sendCertificate(certificate, symbol); + return sendCertificate(certDoc, symbol); } - String sendCredentials(String email, String password, String symbol) throws IOException, VulcanException { - this.symbol = symbol; - - Document html = client.postPageByUrl(LOGIN_PAGE_URL, new String[][]{ + Document sendCredentials(String email, String password) throws IOException, VulcanException { + 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()); } - return html.select("input[name=wresult]").attr("value"); + return html; } - String sendCertificate(String certificate, String defaultSymbol) throws IOException, VulcanException { - this.symbol = findSymbol(defaultSymbol, certificate); - client.setSymbol(this.symbol); + 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 title = client.postPageByUrl(LOGIN_ENDPOINT_PAGE_URL, new String[][]{ - {"wa", "wsignin1.0"}, - {"wresult", certificate} - }).select("title").text(); + String sendCertificate(Document doc, String defaultSymbol) throws IOException, VulcanException { + String certificate = doc.select("input[name=wresult]").val(); + + String symbol = findSymbol(defaultSymbol, certificate); + client.setSymbol(symbol); + + 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"); } if (!"Uonet+".equals(title)) { - throw new LoginErrorException("Could not log in, unknown error"); + throw new LoginErrorException("Expected page title `UONET+`, got " + title); } - return this.symbol; + return symbol; + } + + private Document sendCertData(Document doc) throws IOException, VulcanException { + String url = doc.select("form[name=hiddenform]").attr("action"); + + return client.postPageByUrl(url.replaceFirst("Default", "{symbol}"), new String[][]{ + {"wa", "wsignin1.0"}, + {"wresult", doc.select("input[name=wresult]").val()}, + {"wctx", doc.select("input[name=wctx]").val()} + }); } private String findSymbol(String symbol, String certificate) { @@ -80,14 +117,14 @@ public class Login { } String findSymbolInCertificate(String certificate) { - Elements els = Jsoup + Elements instances = Jsoup .parse(certificate.replaceAll(":", ""), "", Parser.xmlParser()) .select("[AttributeName=\"UserInstance\"] samlAttributeValue"); - if (els.isEmpty()) { + if (instances.isEmpty()) { return ""; } - return els.get(1).text(); + return instances.get(1).text(); } } 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..b5c7efc2c 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 @@ -14,7 +14,6 @@ import java.util.Locale; import io.github.wulkanowy.api.SnP; import io.github.wulkanowy.api.VulcanException; -import io.github.wulkanowy.api.generic.Day; import io.github.wulkanowy.api.generic.Lesson; import io.github.wulkanowy.api.generic.Week; @@ -28,25 +27,25 @@ public class Timetable { this.snp = snp; } - public Week getWeekTable() throws IOException, ParseException, VulcanException { + public Week getWeekTable() throws IOException, ParseException, VulcanException { return getWeekTable(""); } - public Week getWeekTable(final String tick) throws IOException, ParseException, VulcanException { + public Week getWeekTable(final String tick) throws IOException, ParseException, VulcanException { Element table = snp.getSnPPageDocument(TIMETABLE_PAGE_URL + tick) .select(".mainContainer .presentData").first(); - List days = getDays(table.select("thead th")); + List days = getDays(table.select("thead th")); setLessonToDays(table, days); - return new Week() + return new Week() .setStartDayDate(days.get(0).getDate()) .setDays(days); } - private List getDays(Elements tableHeaderCells) throws ParseException { - List days = new ArrayList<>(); + private List getDays(Elements tableHeaderCells) throws ParseException { + List days = new ArrayList<>(); for (int i = 2; i < 7; i++) { String[] dayHeaderCell = tableHeaderCells.get(i).html().split("
"); @@ -55,7 +54,7 @@ public class Timetable { Date d = sdf.parse(dayHeaderCell[1].trim()); sdf.applyPattern("yyyy-MM-dd"); - Day day = new Day(); + TimetableDay day = new TimetableDay(); day.setDayName(dayHeaderCell[0]); day.setDate(sdf.format(d)); @@ -70,7 +69,7 @@ public class Timetable { return days; } - private void setLessonToDays(Element table, List days) { + private void setLessonToDays(Element table, List days) { for (Element row : table.select("tbody tr")) { Elements hours = row.select("td"); @@ -95,12 +94,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/main/java/io/github/wulkanowy/api/timetable/TimetableDay.java b/api/src/main/java/io/github/wulkanowy/api/timetable/TimetableDay.java new file mode 100644 index 000000000..1aa29de28 --- /dev/null +++ b/api/src/main/java/io/github/wulkanowy/api/timetable/TimetableDay.java @@ -0,0 +1,26 @@ +package io.github.wulkanowy.api.timetable; + +import io.github.wulkanowy.api.generic.Day; + +public class TimetableDay extends Day { + + private boolean isFreeDay = false; + + private String freeDayName = ""; + + public boolean isFreeDay() { + return isFreeDay; + } + + public void setFreeDay(boolean freeDay) { + isFreeDay = freeDay; + } + + public String getFreeDayName() { + return freeDayName; + } + + public void setFreeDayName(String freeDayName) { + this.freeDayName = freeDayName; + } +} 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 4aa1be915..38f856172 100644 --- a/api/src/test/java/io/github/wulkanowy/api/ClientTest.java +++ b/api/src/test/java/io/github/wulkanowy/api/ClientTest.java @@ -1,13 +1,10 @@ package io.github.wulkanowy.api; -import org.hamcrest.CoreMatchers; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.junit.Assert; import org.junit.Test; -import io.github.wulkanowy.api.login.Login; - public class ClientTest { private String getFixtureAsString(String fixtureFileName) { @@ -49,20 +46,6 @@ public class ClientTest { client.checkForErrors(doc); } - @Test - public void getClientTest() throws Exception { - Client client = new Client("", "", ""); - - Assert.assertThat(client.getLogin(), CoreMatchers.instanceOf(Login.class)); - } - - @Test - public void getClientTwiceTest() throws Exception { - Client client = new Client("", "", ""); - - Assert.assertEquals(client.getLogin(), client.getLogin()); - } - @Test public void getFilledUrlTest() throws Exception { Client client = new Client("http://fakelog.cf\\\\admin", "", "symbol123"); 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 5cde9b644..2b3d72500 100644 --- a/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTest.java +++ b/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTest.java @@ -7,6 +7,7 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -22,18 +23,21 @@ public class StudentAndParentTest { client = Mockito.mock(Client.class); Mockito.when(client.getPageByUrl(Mockito.anyString())).thenReturn(gradesPageDocument); + Mockito.when(client.getPageByUrl( + Mockito.anyString(), + Mockito.anyBoolean(), Mockito.anyMap())).thenReturn(gradesPageDocument); } @Test - public void snpTest() throws Exception { - StudentAndParent snp = new StudentAndParent(client, "id123"); - Assert.assertEquals("id123", snp.getId()); + public void snpTest() { + StudentAndParent snp = new StudentAndParent(client, "id123", null, null); + Assert.assertEquals("id123", snp.getSchoolID()); } @Test public void getSnpPageUrlWithIdTest() throws Exception { Assert.assertEquals("{schema}://uonetplus-opiekun.{host}/{symbol}/123456/", - (new StudentAndParent(client, "123456")).getSnpHomePageUrl()); + (new StudentAndParent(client, "123456", null, null)).getSnpHomePageUrl()); } @Test @@ -43,7 +47,7 @@ public class StudentAndParentTest { Mockito.when(client.getHost()).thenReturn("vulcan.net.pl"); Mockito.when(client.getPageByUrl(Mockito.anyString())).thenReturn(startPageDocument); - StudentAndParent snp = new StudentAndParent(client, null); + StudentAndParent snp = new StudentAndParent(client, null, null, null); Assert.assertEquals("https://uonetplus-opiekun.vulcan.net.pl/symbol/534213/Start/Index/", snp.getSnpHomePageUrl()); @@ -56,7 +60,7 @@ public class StudentAndParentTest { ); Mockito.when(client.getPageByUrl(Mockito.anyString())).thenReturn(wrongPageDocument); - StudentAndParent snp = new StudentAndParent(client, null); + StudentAndParent snp = new StudentAndParent(client, null, null, null); snp.getSnpHomePageUrl(); } @@ -64,7 +68,7 @@ public class StudentAndParentTest { @Test public void getExtractedIDStandardTest() throws Exception { Mockito.when(client.getHost()).thenReturn("vulcan.net.pl"); - StudentAndParent snp = new StudentAndParent(client, "symbol"); + StudentAndParent snp = new StudentAndParent(client, "symbol", null, null); Assert.assertEquals("123456", snp.getExtractedIdFromUrl("https://uonetplus-opiekun" + ".vulcan.net.pl/powiat/123456/Start/Index/")); } @@ -72,7 +76,7 @@ public class StudentAndParentTest { @Test public void getExtractedIDDemoTest() throws Exception { Mockito.when(client.getHost()).thenReturn("vulcan.net.pl"); - StudentAndParent snp = new StudentAndParent(client, "symbol"); + StudentAndParent snp = new StudentAndParent(client, "symbol", null, null); Assert.assertEquals("demo12345", snp.getExtractedIdFromUrl("https://uonetplus-opiekun.vulcan.net.pl/demoupowiat/demo12345/Start/Index/")); } @@ -80,44 +84,73 @@ public class StudentAndParentTest { @Test(expected = NotLoggedInErrorException.class) public void getExtractedIDNotLoggedTest() throws Exception { Mockito.when(client.getHost()).thenReturn("vulcan.net.pl"); - StudentAndParent snp = new StudentAndParent(client, "symbol"); + StudentAndParent snp = new StudentAndParent(client, "symbol", null, null); Assert.assertEquals("123", snp.getExtractedIdFromUrl("https://uonetplus.vulcan.net.pl/powiat/")); } @Test public void getSemestersTest() throws Exception { - SnP snp = new StudentAndParent(client, "123456"); + SnP snp = new StudentAndParent(client, "123456", null, null); List semesters = snp.getSemesters(); Assert.assertEquals(2, semesters.size()); - Assert.assertEquals("1", semesters.get(0).getId()); - Assert.assertEquals("1234", semesters.get(0).getNumber()); + Assert.assertEquals("1", semesters.get(0).getName()); + Assert.assertEquals("1234", semesters.get(0).getId()); Assert.assertFalse(semesters.get(0).isCurrent()); - Assert.assertEquals("2", semesters.get(1).getId()); - Assert.assertEquals("1235", semesters.get(1).getNumber()); + Assert.assertEquals("2", semesters.get(1).getName()); + Assert.assertEquals("1235", semesters.get(1).getId()); Assert.assertTrue(semesters.get(1).isCurrent()); } @Test - public void getCurrentSemesterTest() throws Exception { + public void getCurrentSemesterTest() { List semesters = new ArrayList<>(); - semesters.add(new Semester().setNumber("1500100900").setId("1").setCurrent(false)); - semesters.add(new Semester().setNumber("1500100901").setId("2").setCurrent(true)); + semesters.add(new Semester().setName("1500100900").setId("1").setCurrent(false)); + semesters.add(new Semester().setName("1500100901").setId("2").setCurrent(true)); - SnP snp = new StudentAndParent(client, ""); - Assert.assertTrue(snp.getCurrentSemester(semesters).isCurrent()); - Assert.assertEquals("2", snp.getCurrentSemester(semesters).getId()); - Assert.assertEquals("1500100901", snp.getCurrentSemester(semesters).getNumber()); + SnP snp = new StudentAndParent(client, "", null, null); + Semester semester = snp.getCurrent(semesters); + + Assert.assertTrue(semester.isCurrent()); + Assert.assertEquals("2", semester.getId()); + Assert.assertEquals("1500100901", semester.getName()); } @Test - public void getCurrentSemesterFromEmptyTest() throws Exception { - SnP snp = new StudentAndParent(client, ""); + public void getCurrentSemesterFromEmptyTest() { + SnP snp = new StudentAndParent(client, "", null, null); List semesters = new ArrayList<>(); - Assert.assertNull(snp.getCurrentSemester(semesters)); + Assert.assertNull(snp.getCurrent(semesters)); + } + + @Test + public void getDiariesAndStudentTest() throws IOException, VulcanException { + Document snpHome = Jsoup.parse(FixtureHelper.getAsString( + getClass().getResourceAsStream("StudentAndParent.html"))); + + client = Mockito.mock(Client.class); + Mockito.when(client.getPageByUrl(Mockito.anyString())).thenReturn(snpHome); + SnP snp = new StudentAndParent(client, "", null, null); + + snp.setUp(); + + Assert.assertEquals("3Ti 2017", snp.getDiaries().get(0).getName()); + Assert.assertEquals("2Ti 2016", snp.getDiaries().get(1).getName()); + Assert.assertEquals("1Ti 2015", snp.getDiaries().get(2).getName()); + + Assert.assertEquals("1300", snp.getDiaries().get(0).getId()); + Assert.assertEquals("1200", snp.getDiaries().get(1).getId()); + Assert.assertEquals("1100", snp.getDiaries().get(2).getId()); + + Assert.assertTrue(snp.getDiaries().get(0).isCurrent()); + Assert.assertFalse(snp.getDiaries().get(1).isCurrent()); + Assert.assertFalse(snp.getDiaries().get(2).isCurrent()); + + Assert.assertEquals("Jan Kowal", snp.getStudents().get(0).getName()); + Assert.assertEquals("100", snp.getStudents().get(0).getId()); } } diff --git a/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTestCase.java b/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTestCase.java index b18076cfa..8d32291fe 100644 --- a/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTestCase.java +++ b/api/src/test/java/io/github/wulkanowy/api/StudentAndParentTestCase.java @@ -16,7 +16,7 @@ public abstract class StudentAndParentTestCase { Mockito.when(snp.getSnPPageDocument(Mockito.anyString())) .thenReturn(tablePageDocument); Mockito.when(snp.getSemesters(Mockito.any(Document.class))).thenCallRealMethod(); - Mockito.when(snp.getCurrentSemester(Mockito.anyList())) + Mockito.when(snp.getCurrent(Mockito.anyList())) .thenCallRealMethod(); Mockito.when(snp.getRowDataChildValue(Mockito.any(Element.class), Mockito.anyInt())).thenCallRealMethod(); diff --git a/api/src/test/java/io/github/wulkanowy/api/VulcanTest.java b/api/src/test/java/io/github/wulkanowy/api/VulcanTest.java index bda66e8d1..287935c00 100644 --- a/api/src/test/java/io/github/wulkanowy/api/VulcanTest.java +++ b/api/src/test/java/io/github/wulkanowy/api/VulcanTest.java @@ -16,7 +16,7 @@ public class VulcanTest { @Test public void getClientTest() throws Exception { Vulcan vulcan = new Vulcan(); - vulcan.setCredentials("email", "password", "symbol", null); + vulcan.setCredentials("email", "password", "symbol", null, null, null); Assert.assertThat(vulcan.getClient(), CoreMatchers.instanceOf(Client.class)); } @@ -24,7 +24,7 @@ public class VulcanTest { @Test public void getClientTwiceTest() throws Exception { Vulcan vulcan = new Vulcan(); - vulcan.setCredentials("email", "password", "symbol", null); + vulcan.setCredentials("email", "password", "symbol", null, null, null); Assert.assertEquals(vulcan.getClient(), vulcan.getClient()); } diff --git a/api/src/test/java/io/github/wulkanowy/api/exams/ExamsWeekTest.java b/api/src/test/java/io/github/wulkanowy/api/exams/ExamsWeekTest.java index 9b57ddd68..0a99c4657 100644 --- a/api/src/test/java/io/github/wulkanowy/api/exams/ExamsWeekTest.java +++ b/api/src/test/java/io/github/wulkanowy/api/exams/ExamsWeekTest.java @@ -12,20 +12,25 @@ public class ExamsWeekTest extends StudentAndParentTestCase { private ExamsWeek onePerDay; + private ExamsWeek empty; + @Before public void getCurrent() throws Exception { onePerDay = new ExamsWeek(getSnp("Sprawdziany-one-per-day.html")); + empty = new ExamsWeek(getSnp("Sprawdziany-empty.html")); } @Test public void getWeekTest() throws Exception { - Assert.assertEquals("23.10.2017", onePerDay.getCurrent().getStartDayDate()); + Assert.assertEquals("2017-10-23", onePerDay.getCurrent().getStartDayDate()); + Assert.assertEquals("2018-04-30", empty.getCurrent().getStartDayDate()); } @Test public void getDaysListTest() throws Exception { Assert.assertEquals(3, onePerDay.getCurrent().getDays().size()); Assert.assertEquals(7, onePerDay.getWeek("", false).getDays().size()); + Assert.assertEquals(0, empty.getCurrent().getDays().size()); } @Test @@ -45,9 +50,18 @@ public class ExamsWeekTest extends StudentAndParentTestCase { public void getDayDateTest() throws Exception { List dayList = onePerDay.getCurrent().getDays(); - Assert.assertEquals("23.10.2017", dayList.get(0).getDate()); - Assert.assertEquals("24.10.2017", dayList.get(1).getDate()); - Assert.assertEquals("27.10.2017", dayList.get(2).getDate()); + Assert.assertEquals("2017-10-23", dayList.get(0).getDate()); + Assert.assertEquals("2017-10-24", dayList.get(1).getDate()); + Assert.assertEquals("2017-10-27", dayList.get(2).getDate()); + } + + @Test + public void getDayNameTest() throws Exception { + List dayList = onePerDay.getCurrent().getDays(); + + Assert.assertEquals("Poniedziałek", dayList.get(0).getDayName()); + Assert.assertEquals("Wtorek", dayList.get(1).getDayName()); + Assert.assertEquals("Piątek", dayList.get(2).getDayName()); } @Test @@ -90,8 +104,8 @@ public class ExamsWeekTest extends StudentAndParentTestCase { public void getExamEntryDateTest() throws Exception { List dayList = onePerDay.getCurrent().getDays(); - Assert.assertEquals("16.10.2017", dayList.get(0).getExamList().get(0).getEntryDate()); - Assert.assertEquals("17.10.2017", dayList.get(1).getExamList().get(0).getEntryDate()); - Assert.assertEquals("16.10.2017", dayList.get(2).getExamList().get(0).getEntryDate()); + Assert.assertEquals("2017-10-16", dayList.get(0).getExamList().get(0).getEntryDate()); + Assert.assertEquals("2017-10-17", dayList.get(1).getExamList().get(0).getEntryDate()); + Assert.assertEquals("2017-10-16", dayList.get(2).getExamList().get(0).getEntryDate()); } } diff --git a/api/src/test/java/io/github/wulkanowy/api/grades/GradesListTest.java b/api/src/test/java/io/github/wulkanowy/api/grades/GradesListTest.java index bee5b4fee..c52fbb30c 100644 --- a/api/src/test/java/io/github/wulkanowy/api/grades/GradesListTest.java +++ b/api/src/test/java/io/github/wulkanowy/api/grades/GradesListTest.java @@ -101,14 +101,4 @@ public class GradesListTest extends StudentAndParentTestCase { Assert.assertEquals("Klaudia Dziedzic", list.get(4).getTeacher()); Assert.assertEquals("Amelia Stępień", list.get(5).getTeacher()); } - - @Test - public void getSemesterTest() throws Exception { - List list = filled.getAll(); - - Assert.assertEquals("7654321", list.get(0).getSemester()); - Assert.assertEquals("7654321", list.get(3).getSemester()); - Assert.assertEquals("7654321", list.get(4).getSemester()); - Assert.assertEquals("7654321", list.get(5).getSemester()); - } } 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 69118018b..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 @@ -11,40 +11,54 @@ import io.github.wulkanowy.api.FixtureHelper; public class LoginTest { + private Document getFixtureAsDocument(String fixtureFileName) { + return Jsoup.parse(getFixtureAsString(fixtureFileName)); + } + private String getFixtureAsString(String fixtureFileName) { return FixtureHelper.getAsString(getClass().getResourceAsStream(fixtureFileName)); } private Client getClient(String fixtureFileName) throws Exception { - Document doc = Jsoup.parse(getFixtureAsString(fixtureFileName)); + Document doc = getFixtureAsDocument(fixtureFileName); 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; } @Test public void loginTest() throws Exception { - Login login = new Login(getClient("Logowanie-success.html")); + Client client = getClient("Logowanie-success.html"); + Mockito.when(client.getPageByUrl(Mockito.anyString(), Mockito.anyBoolean())) + .thenReturn(getFixtureAsDocument("Logowanie-error.html")); + Login login = new Login(client); Assert.assertEquals("d123", login.login("a@a", "pswd", "d123")); } @Test(expected = BadCredentialsException.class) public void sendWrongCredentialsTest() throws Exception { - Login login = new Login(getClient("Logowanie-error.html")); + Client client = getClient("Logowanie-error.html"); + Mockito.when(client.getPageByUrl(Mockito.anyString(), Mockito.anyBoolean())) + .thenReturn(getFixtureAsDocument("Logowanie-error.html")); // -error.html because it html with form used by + Login login = new Login(client); - login.sendCredentials("a@a", "pswd", "d123"); + login.sendCredentials("a@a", "pswd"); } @Test public void sendCredentialsCertificateTest() throws Exception { - Login login = new Login(getClient("Logowanie-certyfikat.html")); + Client client = getClient("Logowanie-certyfikat.html"); + Mockito.when(client.getPageByUrl(Mockito.anyString(), Mockito.anyBoolean())) + .thenReturn(getFixtureAsDocument("Logowanie-error.html")); // -error.html because it html with form used by + Login login = new Login(client); Assert.assertEquals( - getFixtureAsString("cert.xml").replaceAll("\\s+",""), - login.sendCredentials("a@a", "passwd", "d123").replaceAll("\\s+","") + getFixtureAsString("cert-stock.xml").replaceAll("\\s+",""), + login.sendCredentials("a@a", "passwd").select("input[name=wresult]").attr("value").replaceAll("\\s+","") ); } @@ -53,7 +67,7 @@ public class LoginTest { Login login = new Login(getClient("Logowanie-success.html")); Assert.assertEquals("wulkanowyschool321", - login.sendCertificate("", "wulkanowyschool321")); + login.sendCertificate(new Document(""), "wulkanowyschool321")); } @Test @@ -61,28 +75,28 @@ public class LoginTest { Login login = new Login(getClient("Logowanie-success.html")); Assert.assertEquals("demo12345", - login.sendCertificate(getFixtureAsString("cert.xml"), "Default")); + login.sendCertificate(getFixtureAsDocument("Logowanie-certyfikat.html"), "Default")); } @Test(expected = AccountPermissionException.class) public void sendCertificateAccountPermissionTest() throws Exception { Login login = new Login(getClient("Logowanie-brak-dostepu.html")); - login.sendCertificate(getFixtureAsString("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(getFixtureAsString("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/OcenyWszystkie-semester.html b/api/src/test/resources/io/github/wulkanowy/api/OcenyWszystkie-semester.html index f4b712c17..dc7c6c3b6 100644 --- a/api/src/test/resources/io/github/wulkanowy/api/OcenyWszystkie-semester.html +++ b/api/src/test/resources/io/github/wulkanowy/api/OcenyWszystkie-semester.html @@ -15,7 +15,8 @@ - + +
wersja: 17.05.0000.24042
diff --git a/api/src/test/resources/io/github/wulkanowy/api/StudentAndParent.html b/api/src/test/resources/io/github/wulkanowy/api/StudentAndParent.html new file mode 100644 index 000000000..66e2942fd --- /dev/null +++ b/api/src/test/resources/io/github/wulkanowy/api/StudentAndParent.html @@ -0,0 +1,37 @@ + + + + + Witryna ucznia i rodzica – Strona główna + + +
    +
  • + + +
  • +
  • + + +
  • +
+ +
wersja: 17.09.0008.26553
+ + diff --git a/api/src/test/resources/io/github/wulkanowy/api/exams/Sprawdziany-empty.html b/api/src/test/resources/io/github/wulkanowy/api/exams/Sprawdziany-empty.html new file mode 100644 index 000000000..bf1032b4d --- /dev/null +++ b/api/src/test/resources/io/github/wulkanowy/api/exams/Sprawdziany-empty.html @@ -0,0 +1,19 @@ + + + + + Witryna ucznia i rodzica – Terminarz sprawdzianów + + +
+

Sprawdziany

+

Tydzień 30.04.2018 - 06.05.2018

+

Nie zaplanowano żadnych sprawdzianów na wybrany tydzień

+ +
+
wersja: 17.09.0009.26859
+ + 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... -
+ - - + +