From 78add470bab419ca125e0147c75463bd10c3edee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sat, 17 Nov 2018 14:02:40 +0100 Subject: [PATCH] Provide back-compatibility in school discover --- .../io/github/wulkanowy/api/login/StartPage.kt | 11 ++++++----- .../github/wulkanowy/api/login/StartPageTest.kt | 6 +++--- .../io/github/wulkanowy/api/Start-std.html | 15 +++++++++++++-- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/api/src/main/java/io/github/wulkanowy/api/login/StartPage.kt b/api/src/main/java/io/github/wulkanowy/api/login/StartPage.kt index 2b8a3700..57463a08 100644 --- a/api/src/main/java/io/github/wulkanowy/api/login/StartPage.kt +++ b/api/src/main/java/io/github/wulkanowy/api/login/StartPage.kt @@ -5,6 +5,7 @@ import io.github.wulkanowy.api.VulcanException import io.github.wulkanowy.api.generic.School import org.jsoup.nodes.Document import org.slf4j.LoggerFactory +import java.net.URL class StartPage(val client: Client) { @@ -13,7 +14,7 @@ class StartPage(val client: Client) { fun getSchools(startPage: Document): MutableList { val schoolList = mutableListOf() - val snpLinks = startPage.select(".panel.linkownia.pracownik.klient .appLink a") + val snpLinks = startPage.select(".panel.linkownia.pracownik.klient a[href*=\"uonetplus-opiekun\"]") logger.debug("SnP links: {}", snpLinks.size) @@ -24,7 +25,7 @@ class StartPage(val client: Client) { snpLinks.map { schoolList.add(School( it.text(), - getExtractedIdFromUrl(it.attr("href")), + getExtractedSchoolSymbolFromUrl(it.attr("href")), it == snpLinks.first() )) } @@ -32,10 +33,10 @@ class StartPage(val client: Client) { return schoolList } - internal fun getExtractedIdFromUrl(snpPageUrl: String): String { - val path = snpPageUrl.split(client.host).getOrNull(1)?.split("/") + internal fun getExtractedSchoolSymbolFromUrl(snpPageUrl: String): String { + val path = URL(snpPageUrl).path.split("/") - if (6 != path?.size) { + if (6 != path.size) { logger.error("Expected snp url, got {}", snpPageUrl) throw VulcanException("Na pewno używasz konta z dostępem do Witryny ucznia i rodzica?") } diff --git a/api/src/test/java/io/github/wulkanowy/api/login/StartPageTest.kt b/api/src/test/java/io/github/wulkanowy/api/login/StartPageTest.kt index 271b3bb0..fb2bf00c 100644 --- a/api/src/test/java/io/github/wulkanowy/api/login/StartPageTest.kt +++ b/api/src/test/java/io/github/wulkanowy/api/login/StartPageTest.kt @@ -51,18 +51,18 @@ class StartPageTest { @Test fun getExtractedIDStandardTest() { assertEquals("123456", StartPage(client) - .getExtractedIdFromUrl("https://uonetplus-opiekun.fakelog.cf/powiat/123456/Start/Index/")) + .getExtractedSchoolSymbolFromUrl("https://uonetplus-opiekun.fakelog.cf/powiat/123456/Start/Index/")) } @Test fun getExtractedIDDemoTest() { assertEquals("demo12345", StartPage(client) - .getExtractedIdFromUrl("https://uonetplus-opiekun.fakelog.cf/demoupowiat/demo12345/Start/Index/")) + .getExtractedSchoolSymbolFromUrl("https://uonetplus-opiekun.fakelog.cf/demoupowiat/demo12345/Start/Index/")) } @Test(expected = VulcanException::class) fun getExtractedIDNotLoggedTest() { assertEquals("123", StartPage(client) - .getExtractedIdFromUrl("https://uonetplus.NOTfakelog.cf/powiat/")) + .getExtractedSchoolSymbolFromUrl("https://uonetplus.NOTfakelog.cf/powiat/")) } } diff --git a/api/src/test/resources/io/github/wulkanowy/api/Start-std.html b/api/src/test/resources/io/github/wulkanowy/api/Start-std.html index b65625cf..2cf03d63 100644 --- a/api/src/test/resources/io/github/wulkanowy/api/Start-std.html +++ b/api/src/test/resources/io/github/wulkanowy/api/Start-std.html @@ -10,14 +10,25 @@