From 54abf3b164f8e4d2291e0e7d7f4f29f6ced82b95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Wed, 23 Jan 2019 18:42:59 +0100 Subject: [PATCH] Add tests for semesters scrapper --- .../api/register/StudentAndParentResponse.kt | 3 + .../StudentAndParentStartRepository.kt | 8 +- .../StudentAndParentStartRepositoryTest.kt | 204 ++++++++++++++++++ 3 files changed, 214 insertions(+), 1 deletion(-) create mode 100644 src/test/kotlin/io/github/wulkanowy/api/repository/StudentAndParentStartRepositoryTest.kt diff --git a/src/main/kotlin/io/github/wulkanowy/api/register/StudentAndParentResponse.kt b/src/main/kotlin/io/github/wulkanowy/api/register/StudentAndParentResponse.kt index e9bb741c..bfaff280 100644 --- a/src/main/kotlin/io/github/wulkanowy/api/register/StudentAndParentResponse.kt +++ b/src/main/kotlin/io/github/wulkanowy/api/register/StudentAndParentResponse.kt @@ -4,6 +4,9 @@ import pl.droidsonroids.jspoon.annotation.Selector class StudentAndParentResponse { + @Selector("title") + lateinit var title: String + @Selector("header[data-organization-name]", attr = "data-organization-name") lateinit var schoolName: String diff --git a/src/main/kotlin/io/github/wulkanowy/api/repository/StudentAndParentStartRepository.kt b/src/main/kotlin/io/github/wulkanowy/api/repository/StudentAndParentStartRepository.kt index 3697fce1..4dd09c9e 100644 --- a/src/main/kotlin/io/github/wulkanowy/api/repository/StudentAndParentStartRepository.kt +++ b/src/main/kotlin/io/github/wulkanowy/api/repository/StudentAndParentStartRepository.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.api.repository +import io.github.wulkanowy.api.interceptor.VulcanException import io.github.wulkanowy.api.register.Semester import io.github.wulkanowy.api.service.StudentAndParentService import io.reactivex.Observable @@ -13,8 +14,13 @@ class StudentAndParentStartRepository( ) { fun getSemesters(): Single> { - return api.getUserInfo(studentId).flatMapObservable { Observable.fromIterable(it.diaries.reversed()) }.flatMapSingle { diary -> + return api.getUserInfo(studentId).map { + it.apply { + if (!it.title.startsWith("Witryna ucznia i rodzica")) throw VulcanException("Unknow page with title: ${it.title}") + } + }.flatMapObservable { Observable.fromIterable(it.diaries.reversed()) }.flatMapSingle { diary -> api.getDiaryInfo(diary.id, "/$symbol/$schoolSymbol/Oceny.mvc/Wszystkie").map { res -> + if (!res.title.endsWith("Oceny")) throw VulcanException("Unknow page with title: ${res.title}") res.semesters.map { Semester(diary.id, diary.name, it.semesterId, it.semesterNumber, "selected" == it.current && "selected" == diary.current) } diff --git a/src/test/kotlin/io/github/wulkanowy/api/repository/StudentAndParentStartRepositoryTest.kt b/src/test/kotlin/io/github/wulkanowy/api/repository/StudentAndParentStartRepositoryTest.kt new file mode 100644 index 00000000..e7ab729e --- /dev/null +++ b/src/test/kotlin/io/github/wulkanowy/api/repository/StudentAndParentStartRepositoryTest.kt @@ -0,0 +1,204 @@ +package io.github.wulkanowy.api.repository + +import io.github.wulkanowy.api.Api +import io.github.wulkanowy.api.BaseLocalTest +import io.github.wulkanowy.api.grades.GradesTest +import io.github.wulkanowy.api.login.LoginTest +import io.github.wulkanowy.api.register.RegisterTest +import io.github.wulkanowy.api.register.Semester +import io.reactivex.observers.TestObserver +import okhttp3.mockwebserver.MockResponse +import org.junit.Assert.assertEquals +import org.junit.Test + +class StudentAndParentStartRepositoryTest : BaseLocalTest() { + + private val api by lazy { + Api().apply { + ssl = false + host = "fakelog.localhost:3000" // + symbol = "Default" + email = "jan@fakelog.cf" + password = "jan123" + schoolSymbol = "123456" + studentId = 1 + diaryId = 101 + } + } + + @Test + fun getSemesters_invalidStartPage() { + server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Login-success.html").readText())) + server.start(3000) // + + api.loginType = Api.LoginType.STANDARD + + val semesters = api.getSemesters() + val semestersObserver = TestObserver>() + semesters.subscribe(semestersObserver) + semestersObserver.assertTerminated() + semestersObserver.assertErrorMessage("Unknow page with title: Uonet+") + } + + @Test + fun getSemesters_invalidGradesPage() { + server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-standard.html").readText())) + + server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-uonet.html").readText())) + server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Login-success.html").readText())) + + server.enqueue(MockResponse().setBody(RegisterTest::class.java.getResource("WitrynaUczniaIRodzica.html").readText())) + server.enqueue(MockResponse().setBody(RegisterTest::class.java.getResource("WitrynaUczniaIRodzica.html").readText())) + server.start(3000) // + + api.loginType = Api.LoginType.STANDARD + + val semesters = api.getSemesters() + val semestersObserver = TestObserver>() + semesters.subscribe(semestersObserver) + semestersObserver.assertTerminated() + semestersObserver.assertErrorMessage("Unknow page with title: Witryna ucznia i rodzica – Strona główna") + } + + @Test + fun getSemesters() { + server.enqueue(MockResponse().setBody(RegisterTest::class.java.getResource("WitrynaUczniaIRodzica.html").readText())) + server.enqueue(MockResponse().setBody(GradesTest::class.java.getResource("OcenyWszystkie-details.html").readText())) + server.enqueue(MockResponse().setBody(GradesTest::class.java.getResource("OcenyWszystkie-details.html").readText())) + server.enqueue(MockResponse().setBody(GradesTest::class.java.getResource("OcenyWszystkie-details.html").readText())) + server.start(3000) // + + api.loginType = Api.LoginType.STANDARD + + val semesters = api.getSemesters() + val semestersObserver = TestObserver>() + semesters.subscribe(semestersObserver) + semestersObserver.assertComplete() + + val items = semestersObserver.values()[0] + + assertEquals(6, items.size) + + assertEquals(1234567, items[0].semesterId) + assertEquals(1234568, items[1].semesterId) + } + + @Test + fun getSemesters_normal() { + server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-standard.html").readText())) + + + server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-uonet.html").readText())) + server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Login-success.html").readText())) + + server.enqueue(MockResponse().setBody(RegisterTest::class.java.getResource("WitrynaUczniaIRodzica.html").readText())) + server.enqueue(MockResponse().setBody(GradesTest::class.java.getResource("OcenyWszystkie-details.html").readText())) + server.enqueue(MockResponse().setBody(GradesTest::class.java.getResource("OcenyWszystkie-details.html").readText())) + server.enqueue(MockResponse().setBody(GradesTest::class.java.getResource("OcenyWszystkie-details.html").readText())) + server.start(3000) // + + api.loginType = Api.LoginType.STANDARD + + val semesters = api.getSemesters() + val semestersObserver = TestObserver>() + semesters.subscribe(semestersObserver) + semestersObserver.assertComplete() + + val items = semestersObserver.values()[0] + + assertEquals(6, items.size) + + assertEquals(1234567, items[0].semesterId) + assertEquals(1234568, items[1].semesterId) + } + + @Test + fun getSemesters_ADFS() { + server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("ADFS-form-2.html").readText())) // + + server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("ADFS-form-2.html").readText())) + server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-cufs.html").readText())) + server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-uonet.html").readText())) + server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Login-success.html").readText())) + + server.enqueue(MockResponse().setBody(RegisterTest::class.java.getResource("WitrynaUczniaIRodzica.html").readText())) + server.enqueue(MockResponse().setBody(GradesTest::class.java.getResource("OcenyWszystkie-details.html").readText())) + server.enqueue(MockResponse().setBody(GradesTest::class.java.getResource("OcenyWszystkie-details.html").readText())) + server.enqueue(MockResponse().setBody(GradesTest::class.java.getResource("OcenyWszystkie-details.html").readText())) + server.start(3000) // + + api.loginType = Api.LoginType.ADFS + + val semesters = api.getSemesters() + val semestersObserver = TestObserver>() + semesters.subscribe(semestersObserver) + semestersObserver.assertComplete() + + val items = semestersObserver.values()[0] + + assertEquals(6, items.size) + + assertEquals(1234567, items[0].semesterId) + assertEquals(1234568, items[1].semesterId) + } + + @Test + fun getSemesters_ADFSLight() { + server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("ADFSLight-form-1.html").readText())) + + server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-cufs.html").readText())) + server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-uonet.html").readText())) + server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Login-success.html").readText())) + + server.enqueue(MockResponse().setBody(RegisterTest::class.java.getResource("WitrynaUczniaIRodzica.html").readText())) + server.enqueue(MockResponse().setBody(GradesTest::class.java.getResource("OcenyWszystkie-details.html").readText())) + server.enqueue(MockResponse().setBody(GradesTest::class.java.getResource("OcenyWszystkie-details.html").readText())) + server.enqueue(MockResponse().setBody(GradesTest::class.java.getResource("OcenyWszystkie-details.html").readText())) + server.start(3000) // + + api.loginType = Api.LoginType.ADFSLight + + val semesters = api.getSemesters() + val semestersObserver = TestObserver>() + semesters.subscribe(semestersObserver) + semestersObserver.assertComplete() + + val items = semestersObserver.values()[0] + + assertEquals(6, items.size) + + assertEquals(1234567, items[0].semesterId) + assertEquals(1234568, items[1].semesterId) + } + + @Test + fun getSemesters_ADFSCards() { + server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("ADFS-form-1.html").readText())) + + server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("ADFS-form-1.html").readText())) + server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("ADFS-form-2.html").readText())) + server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-cufs.html").readText())) + server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-uonet.html").readText())) + server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Login-success.html").readText())) + + server.enqueue(MockResponse().setBody(RegisterTest::class.java.getResource("WitrynaUczniaIRodzica.html").readText())) + server.enqueue(MockResponse().setBody(GradesTest::class.java.getResource("OcenyWszystkie-details.html").readText())) + server.enqueue(MockResponse().setBody(GradesTest::class.java.getResource("OcenyWszystkie-details.html").readText())) + server.enqueue(MockResponse().setBody(GradesTest::class.java.getResource("OcenyWszystkie-details.html").readText())) + server.start(3000) // + + api.loginType = Api.LoginType.ADFSCards + + val semesters = api.getSemesters() + val semestersObserver = TestObserver>() + semesters.subscribe(semestersObserver) + semestersObserver.assertComplete() + + val items = semestersObserver.values()[0] + + assertEquals(6, items.size) + + assertEquals(1234567, items[0].semesterId) + assertEquals(1234568, items[1].semesterId) + } +}