Add tests for semesters scrapper
This commit is contained in:
parent
3e2f55b7cf
commit
54abf3b164
3 changed files with 214 additions and 1 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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<List<Semester>> {
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -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<List<Semester>>()
|
||||
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<List<Semester>>()
|
||||
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<List<Semester>>()
|
||||
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<List<Semester>>()
|
||||
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<List<Semester>>()
|
||||
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<List<Semester>>()
|
||||
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<List<Semester>>()
|
||||
semesters.subscribe(semestersObserver)
|
||||
semestersObserver.assertComplete()
|
||||
|
||||
val items = semestersObserver.values()[0]
|
||||
|
||||
assertEquals(6, items.size)
|
||||
|
||||
assertEquals(1234567, items[0].semesterId)
|
||||
assertEquals(1234568, items[1].semesterId)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue