Add sendPasswordResetRequest() method

This commit is contained in:
Mikołaj Pich 2020-01-04 00:58:04 +01:00
parent 008887c359
commit 84d9a6779d
No known key found for this signature in database
GPG key ID: F62B26E36D4C4BAA
7 changed files with 69 additions and 6 deletions

View file

@ -191,10 +191,14 @@ class Scrapper {
HomepageRepository(serviceManager.getHomepageService())
}
fun getPasswordResetCaptcha(registerBaseUrl: String, symbol: String = "Default"): Single<String> {
fun getPasswordResetCaptcha(registerBaseUrl: String, symbol: String): Single<String> {
return register.getPasswordResetCaptcha(registerBaseUrl, symbol)
}
fun sendPasswordResetRequest(registerBaseUrl: String, symbol: String, email: String, captchaCode: String): Single<Pair<Boolean, String>> {
return register.sendPasswordResetRequest(registerBaseUrl, symbol, email, captchaCode)
}
fun getStudents() = register.getStudents()
fun getSemesters() = if (useNewStudent) studentStart.getSemesters() else snpStart.getSemesters()

View file

@ -0,0 +1,16 @@
package io.github.wulkanowy.sdk.scrapper.register
import org.jsoup.nodes.Element
import pl.droidsonroids.jspoon.annotation.Selector
class SentUnlockAccountResponse {
@Selector("title")
lateinit var title: String
@Selector(".ErrorMessage, #ErrorTextLabel, .UnlockAccountSummary p, #box .box-p")
lateinit var message: String
@Selector("html")
lateinit var doc: Element
}

View file

@ -2,7 +2,7 @@ package io.github.wulkanowy.sdk.scrapper.register
import pl.droidsonroids.jspoon.annotation.Selector
class UnlockAccount {
class UnlockAccountResponse {
@Selector(".g-recaptcha", attr = "data-sitekey")
lateinit var recaptcha: String

View file

@ -41,6 +41,20 @@ class RegisterRepository(
}
fun getPasswordResetCaptcha(registerBaseUrl: String, symbol: String): Single<String> {
return get(registerBaseUrl, symbol)
.flatMap { register.getPasswordResetPageWithCaptcha(it) }
.map { it.recaptcha }
}
fun sendPasswordResetRequest(registerBaseUrl: String, symbol: String, email: String, captchaCode: String): Single<Pair<Boolean, String>> {
return get(registerBaseUrl, symbol)
.flatMap { register.sendPasswordResetRequest(it, email, captchaCode) }
.map {
(it.title == "Podsumowanie operacji") to it.message
}
}
fun get(registerBaseUrl: String, symbol: String): Single<String> {
val url = URL(registerBaseUrl)
return when (url.host) {
"fakelog.cf" -> Single.just("https://cufs.fakelog.cf/Default/AccountManage/UnlockAccount")
@ -56,7 +70,7 @@ class RegisterRepository(
}
}
else -> throw ScrapperException("Nieznany dziennik")
}.flatMap { register.getPasswordResetPageWithCaptcha(it) }.map { it.recaptcha }
}
}
fun getStudents(): Single<List<Student>> {

View file

@ -1,9 +1,13 @@
package io.github.wulkanowy.sdk.scrapper.service
import io.github.wulkanowy.sdk.scrapper.register.LoginForm
import io.github.wulkanowy.sdk.scrapper.register.UnlockAccount
import io.github.wulkanowy.sdk.scrapper.register.SentUnlockAccountResponse
import io.github.wulkanowy.sdk.scrapper.register.UnlockAccountResponse
import io.reactivex.Single
import retrofit2.http.Field
import retrofit2.http.FormUrlEncoded
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Url
interface RegisterService {
@ -12,5 +16,13 @@ interface RegisterService {
fun getFormType(@Url url: String): Single<LoginForm>
@GET
fun getPasswordResetPageWithCaptcha(@Url url: String): Single<UnlockAccount>
fun getPasswordResetPageWithCaptcha(@Url url: String): Single<UnlockAccountResponse>
@POST
@FormUrlEncoded
fun sendPasswordResetRequest(
@Url registerBaseUrl: String,
@Field("Email") email: String,
@Field("g-recaptcha-response") captchaCode: String
): Single<SentUnlockAccountResponse>
}

View file

@ -73,6 +73,19 @@ class ScrapperRemoteTest : BaseTest() {
assertEquals("6LeAGMYUAAAAAMszd5VWZTEb5WQHqsNT1F4GCqUd", codeObserver.values()[0])
}
@Test
fun sendPasswordResetRequest() {
val res = api.sendPasswordResetRequest("https://fakelog.cf", "Default", "jan@fakelog.cf", "03AOLTBLQRPyr0pWvWLRAgD4hRLfxktoqD2IVweeMuXwbkpR_8S9YQtcS3cAXqUOyEw3NxfvwzV0lTjgFWyl8j3UXGQpsc2nvQcqIofj1N8DYfxvtZO-h24W_S0Z9-fDnfXErd7vERS-Ny4d5IU1FupBAEKvT8rrf3OA3GYYbMM7TwB8b_o9Tt192TqYnSxkyIYE4UdaZnLBA0KIXxlBAoqM6QGlPEsSPK9gmCGx-0hn68w-UBQkv_ghRruf4kpv2Shw5emcP-qHBlv3YjAagsb_358K0v8uGJeyLrx4dXN9Ky02TXFMKYWNHz29fjhfunxT73u_PrsLj56f-MjOXrqO894NkUlJ7RkTTclwIsqXtJ794LEBH--mtsqZBND0miR5-odmZszqiNB3V5UsS5ObsqF_fWMl2TCWyNTTvF4elOGwOEeKiumVpjB6e740COxvxN3vbkNWxP9eeghpd5nPN5l2wUV3VL2R5s44TbqHqkrkNpUOd3h7efs3cQtCfGc-tCXoqLC26LxT7aztvKpjXMuqGEf-7wbQ")
val resObserver = TestObserver<Pair<Boolean, String>>()
res.subscribe(resObserver)
resObserver.assertComplete()
with(resObserver.values()[0]) {
assertEquals(true, first)
assertTrue(second.startsWith("Wysłano wiadomość na zapisany w systemie adres e-mail"))
}
}
@Test
fun studentsTest() {
val students = api.getStudents()
@ -124,7 +137,7 @@ class ScrapperRemoteTest : BaseTest() {
}
values[6].run {
// assertEquals(12, semesterId)
// assertEquals(12, semesterId)
// assertEquals(2, semesterNumber)
}
}

View file

@ -171,6 +171,10 @@ class Sdk {
return scrapper.getPasswordResetCaptcha(registerBaseUrl, symbol)
}
fun sendPasswordResetRequest(registerBaseUrl: String, symbol: String, email: String, captchaCode: String): Single<Pair<Boolean, String>> {
return scrapper.sendPasswordResetRequest(registerBaseUrl, symbol, email, captchaCode)
}
fun getStudentsFromMobileApi(token: String, pin: String, symbol: String, apiKey: String = ""): Single<List<Student>> {
return mobile.getCertificate(token, pin, symbol, buildTag, androidVersion)
.flatMap { mobile.getStudents(it, apiKey) }