[Feature] Lucky number (#6)

This commit is contained in:
Kacper Ziubryniewicz 2019-01-14 22:54:35 +01:00 committed by Mikołaj Pich
parent 8730f850d1
commit 0b675978a9
15 changed files with 151 additions and 20 deletions

View file

@ -152,6 +152,10 @@ class Api {
MessagesRepository(serviceManager.getMessagesService())
}
private val homepage by resettableLazy(changeManager) {
HomepageRepository(serviceManager.getHomepageService())
}
fun getPupils() = register.getPupils()
fun getSemesters() = if (useNewStudent) studentStart.getSemesters() else snpStart.getSemesters()
@ -209,4 +213,6 @@ class Api {
fun getTimetable(startDate: LocalDate, endDate: LocalDate? = null) = if (useNewStudent) student.getTimetable(startDate, endDate) else snp.getTimetable(startDate, endDate)
fun getRealized(startDate: LocalDate? = null) = snp.getRealized(startDate)
fun getLuckyNumber() = homepage.getLuckyNumber()
}

View file

@ -0,0 +1,10 @@
package io.github.wulkanowy.api.luckynumber
import pl.droidsonroids.jspoon.annotation.Selector
class LuckyNumberResponse {
@Selector(".panel.szczesliweNumery .subDiv:not(.pCont)", regex = "(\\d+)", defValue = "0")
var luckyNumer: Int = 0
}

View file

@ -2,11 +2,12 @@ package io.github.wulkanowy.api.register
import pl.droidsonroids.jspoon.annotation.Selector
class HomepageResponse {
class SendCertificateResponse {
@Selector(".panel.linkownia.pracownik.klient a[href*=\"uonetplus-opiekun\"]", attr = "href")
var oldStudentSchools: List<String> = emptyList()
@Selector(".panel.linkownia.pracownik.klient a[href*=\"uonetplus-uczen\"]", attr = "href")
var studentSchools: List<String> = emptyList()
}

View file

@ -0,0 +1,12 @@
package io.github.wulkanowy.api.repository
import io.github.wulkanowy.api.service.HomepageService
import io.reactivex.Single
class HomepageRepository(private val api: HomepageService) {
fun getLuckyNumber(): Single<Int> {
return api.getLuckyNumber().map { it.luckyNumer }
}
}

View file

@ -4,7 +4,7 @@ import io.github.wulkanowy.api.Api
import io.github.wulkanowy.api.ApiException
import io.github.wulkanowy.api.interceptor.VulcanException
import io.github.wulkanowy.api.login.CertificateResponse
import io.github.wulkanowy.api.register.HomepageResponse
import io.github.wulkanowy.api.register.SendCertificateResponse
import io.github.wulkanowy.api.service.LoginService
import io.reactivex.Single
import org.threeten.bp.LocalDateTime.now
@ -34,10 +34,10 @@ class LoginRepository(
}
@Synchronized
fun login(email: String, password: String): Single<HomepageResponse> {
fun login(email: String, password: String): Single<SendCertificateResponse> {
return sendCredentials(email, password).flatMap {
when {
it.title.startsWith("Witryna ucznia i rodzica") -> return@flatMap Single.just(HomepageResponse())
it.title.startsWith("Witryna ucznia i rodzica") -> return@flatMap Single.just(SendCertificateResponse())
it.action.isBlank() -> throw VulcanException("Invalid certificate page: '${it.title}'. Try again")
}
@ -55,7 +55,7 @@ class LoginRepository(
}
}
fun sendCertificate(certificate: CertificateResponse, url: String = certificate.action): Single<HomepageResponse> {
fun sendCertificate(certificate: CertificateResponse, url: String = certificate.action): Single<SendCertificateResponse> {
cookies.cookieStore.removeAll()
return api.sendCertificate(url, mapOf(
"wa" to certificate.wa,

View file

@ -4,7 +4,7 @@ import io.github.wulkanowy.api.Api
import io.github.wulkanowy.api.ApiException
import io.github.wulkanowy.api.login.AccountPermissionException
import io.github.wulkanowy.api.login.CertificateResponse
import io.github.wulkanowy.api.register.HomepageResponse
import io.github.wulkanowy.api.register.SendCertificateResponse
import io.github.wulkanowy.api.register.Pupil
import io.github.wulkanowy.api.register.StudentAndParentResponse
import io.github.wulkanowy.api.service.RegisterService
@ -33,7 +33,7 @@ class RegisterRepository(
return getSymbols().flatMapObservable { Observable.fromIterable(it) }.flatMap { symbol ->
loginRepo.sendCertificate(symbol.second, symbol.second.action.replace(startSymbol, symbol.first))
.onErrorResumeNext { t ->
if (t is AccountPermissionException) Single.just(HomepageResponse())
if (t is AccountPermissionException) Single.just(SendCertificateResponse())
else Single.error(t)
}
.flatMapObservable { Observable.fromIterable(if (useNewStudent) it.studentSchools else it.oldStudentSchools) }

View file

@ -0,0 +1,12 @@
package io.github.wulkanowy.api.service
import io.github.wulkanowy.api.luckynumber.LuckyNumberResponse
import io.reactivex.Single
import retrofit2.http.GET
interface HomepageService {
@GET("Start.mvc/Index")
fun getLuckyNumber(): Single<LuckyNumberResponse>
}

View file

@ -1,7 +1,7 @@
package io.github.wulkanowy.api.service
import io.github.wulkanowy.api.login.ADFSFormResponse
import io.github.wulkanowy.api.register.HomepageResponse
import io.github.wulkanowy.api.register.SendCertificateResponse
import io.reactivex.Single
import retrofit2.http.*
@ -13,7 +13,7 @@ interface LoginService {
@POST
@FormUrlEncoded
fun sendCertificate(@Url url: String, @FieldMap certificate: Map<String, String>): Single<HomepageResponse>
fun sendCertificate(@Url url: String, @FieldMap certificate: Map<String, String>): Single<SendCertificateResponse>
// ADFS

View file

@ -11,7 +11,7 @@ import io.github.wulkanowy.api.interceptor.NotLoggedInErrorInterceptor
import io.github.wulkanowy.api.interceptor.StudentAndParentInterceptor
import io.github.wulkanowy.api.interceptor.UserAgentInterceptor
import io.github.wulkanowy.api.login.NotLoggedInException
import io.github.wulkanowy.api.register.HomepageResponse
import io.github.wulkanowy.api.register.SendCertificateResponse
import io.github.wulkanowy.api.repository.LoginRepository
import io.reactivex.Flowable
import okhttp3.Interceptor
@ -106,6 +106,10 @@ class ServiceManager(
return getRetrofit(getClientBuilder(), urlGenerator.generate(UrlGenerator.Site.MESSAGES), true, true).create()
}
fun getHomepageService(): HomepageService {
return getRetrofit(getClientBuilder(), urlGenerator.generate(UrlGenerator.Site.HOME), true, false).create()
}
private fun getRetrofit(client: OkHttpClient.Builder, baseUrl: String, login: Boolean = true, gson: Boolean = false): Retrofit {
return Retrofit.Builder()
.baseUrl(baseUrl)
@ -140,7 +144,7 @@ class ServiceManager(
}
}
private fun getLoginHelper(): Flowable<HomepageResponse> {
private fun getLoginHelper(): Flowable<SendCertificateResponse> {
return LoginRepository(loginType, schema, host, symbol, cookies, getLoginService())
.login(email, password)
.toFlowable()
@ -150,7 +154,7 @@ class ServiceManager(
class UrlGenerator(private val schema: String, private val host: String, var symbol: String, var schoolId: String) {
enum class Site {
LOGIN, SNP, STUDENT, MESSAGES
LOGIN, HOME, SNP, STUDENT, MESSAGES
}
fun generate(type: Site): String {
@ -160,6 +164,7 @@ class ServiceManager(
private fun getSubDomain(type: Site): String {
return when (type) {
Site.LOGIN -> "cufs"
Site.HOME -> "uonetplus"
Site.SNP -> "uonetplus-opiekun"
Site.STUDENT -> "uonetplus-uczen"
Site.MESSAGES -> "uonetplus-uzytkownik"

View file

@ -6,12 +6,10 @@ import io.github.wulkanowy.api.exams.ExamRequest
import io.github.wulkanowy.api.exams.ExamResponse
import io.github.wulkanowy.api.grades.GradeRequest
import io.github.wulkanowy.api.grades.GradesResponse
import io.github.wulkanowy.api.homework.Homework
import io.github.wulkanowy.api.homework.HomeworkResponse
import io.github.wulkanowy.api.mobile.Device
import io.github.wulkanowy.api.notes.NotesResponse
import io.github.wulkanowy.api.register.Diary
import io.github.wulkanowy.api.register.HomepageResponse
import io.github.wulkanowy.api.timetable.CacheResponse
import io.github.wulkanowy.api.timetable.TimetableRequest
import io.github.wulkanowy.api.timetable.TimetableResponse

View file

@ -485,4 +485,14 @@ class ApiRemoteTest : BaseTest() {
assertEquals("Nieobecność nieusprawiedliwiona", absence)
}
}
@Test
fun luckyNumberTest() {
val luckyNumber = api.getLuckyNumber()
val luckyNumberObserver = TestObserver<Int>()
luckyNumber.subscribe(luckyNumberObserver)
luckyNumberObserver.assertComplete()
assertEquals(0, luckyNumberObserver.values()[0])
}
}

View file

@ -5,7 +5,7 @@ import io.github.wulkanowy.api.BaseLocalTest
import io.github.wulkanowy.api.homework.HomeworkTest
import io.github.wulkanowy.api.interceptor.ErrorInterceptorTest
import io.github.wulkanowy.api.interceptor.VulcanException
import io.github.wulkanowy.api.register.HomepageResponse
import io.github.wulkanowy.api.register.SendCertificateResponse
import io.github.wulkanowy.api.repository.LoginRepository
import io.github.wulkanowy.api.service.LoginService
import io.reactivex.observers.TestObserver
@ -83,7 +83,7 @@ class LoginTest : BaseLocalTest() {
server.start(3000)
val res = adfs.login("jan@fakelog.cf", "jan1234")
val observer = TestObserver<HomepageResponse>()
val observer = TestObserver<SendCertificateResponse>()
res.subscribe(observer)
observer.assertTerminated()
observer.assertError(BadCredentialsException::class.java)
@ -96,7 +96,7 @@ class LoginTest : BaseLocalTest() {
server.start(3000)
val res = normal.login("jan@fakelog.cf", "jan1234")
val observer = TestObserver<HomepageResponse>()
val observer = TestObserver<SendCertificateResponse>()
res.subscribe(observer)
observer.assertTerminated()
observer.assertError(BadCredentialsException::class.java)
@ -109,7 +109,7 @@ class LoginTest : BaseLocalTest() {
server.start(3000)
val res = normal.login("jan@fakelog.cf", "jan123")
val observer = TestObserver<HomepageResponse>()
val observer = TestObserver<SendCertificateResponse>()
res.subscribe(observer)
observer.assertTerminated()
observer.assertError(AccountPermissionException::class.java)
@ -124,7 +124,7 @@ class LoginTest : BaseLocalTest() {
server.start(3000)
val res = normal.login("jan@fakelog.cf", "jan123")
val observer = TestObserver<HomepageResponse>()
val observer = TestObserver<SendCertificateResponse>()
res.subscribe(observer)
observer.assertComplete()
}
@ -135,7 +135,7 @@ class LoginTest : BaseLocalTest() {
server.start(3000)
val res = normal.login("jan@fakelog.cf", "jan123")
val observer = TestObserver<HomepageResponse>()
val observer = TestObserver<SendCertificateResponse>()
res.subscribe(observer)
observer.assertTerminated()
observer.assertError(VulcanException::class.java)

View file

@ -0,0 +1,32 @@
package io.github.wulkanowy.api.luckynumber
import io.github.wulkanowy.api.BaseLocalTest
import io.github.wulkanowy.api.repository.HomepageRepository
import io.github.wulkanowy.api.service.HomepageService
import okhttp3.mockwebserver.MockResponse
import org.junit.Assert.assertEquals
import org.junit.Test
class LuckyNumberTest : BaseLocalTest() {
private val api by lazy {
HomepageRepository(getService(HomepageService::class.java, "http://fakelog.localhost:3000/", true))
}
@Test
fun getLuckyNumber() {
server.enqueue(MockResponse().setBody(LuckyNumberTest::class.java.getResource("Index.html").readText()))
server.start(3000)
assertEquals(18, api.getLuckyNumber().blockingGet())
}
@Test
fun getLuckyNumber_empty() {
server.enqueue(MockResponse().setBody(LuckyNumberTest::class.java.getResource("Index-withoutLuckyNumber.html").readText()))
server.start(3000)
assertEquals(0, api.getLuckyNumber().blockingGet())
}
}

View file

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Uonet+</title>
</head>
<body>
<div class="startScreen">
<div class="holder">
<div class="content" id="content"></div>
</div>
</div>
</body>
</html>

View file

@ -0,0 +1,31 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Uonet+</title>
</head>
<body>
<div class="startScreen">
<div class="holder">
<div class="content" id="content">
<div class="panel szczesliweNumery klient">
<div class="imagedHeader">
<div class="szczesliweNumeryImg appImg"></div>
<div class="name">Szczęśliwy numer w dzienniku</div>
</div>
<div class="separator"></div>
<div class="subDiv pCont">
<div class="subDiv pCont">
<span class="header">SP</span>
<div class="subDiv">
Szczęśliwy numer w dzienniku: 18<br/>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>