parent
e3a8c82d2c
commit
35c10d0d7c
2 changed files with 82 additions and 15 deletions
|
@ -18,6 +18,7 @@ import io.reactivex.Single
|
||||||
import org.jsoup.Jsoup
|
import org.jsoup.Jsoup
|
||||||
import org.jsoup.parser.Parser
|
import org.jsoup.parser.Parser
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
|
import java.text.Normalizer
|
||||||
|
|
||||||
class RegisterRepository(
|
class RegisterRepository(
|
||||||
private val startSymbol: String,
|
private val startSymbol: String,
|
||||||
|
@ -32,20 +33,20 @@ class RegisterRepository(
|
||||||
) {
|
) {
|
||||||
|
|
||||||
fun getStudents(): Single<List<Student>> {
|
fun getStudents(): Single<List<Student>> {
|
||||||
return getSymbols().flatMapObservable { Observable.fromIterable(it) }.flatMap { symbol ->
|
return getSymbols().flatMapObservable { Observable.fromIterable(it) }.flatMap { (symbol, certificate) ->
|
||||||
loginHelper.sendCertificate(symbol.second, symbol.second.action.replace(startSymbol, symbol.first))
|
loginHelper.sendCertificate(certificate, certificate.action.replace(startSymbol.getNormalizedSymbol(), symbol))
|
||||||
.onErrorResumeNext { t ->
|
.onErrorResumeNext { t ->
|
||||||
if (t is AccountPermissionException) Single.just(SendCertificateResponse())
|
if (t is AccountPermissionException) Single.just(SendCertificateResponse())
|
||||||
else Single.error(t)
|
else Single.error(t)
|
||||||
}
|
}
|
||||||
.flatMapObservable { Observable.fromIterable(if (useNewStudent) it.studentSchools else it.oldStudentSchools) }
|
.flatMapObservable { Observable.fromIterable(if (useNewStudent) it.studentSchools else it.oldStudentSchools) }
|
||||||
.flatMapSingle { schoolUrl ->
|
.flatMapSingle { schoolUrl ->
|
||||||
getLoginType(symbol.first).flatMap { loginType ->
|
getLoginType(symbol).flatMap { loginType ->
|
||||||
getStudents(symbol.first, schoolUrl).map {
|
getStudents(symbol, schoolUrl).map {
|
||||||
it.map { student ->
|
it.map { student ->
|
||||||
Student(
|
Student(
|
||||||
email = email,
|
email = email,
|
||||||
symbol = symbol.first,
|
symbol = symbol,
|
||||||
studentId = student.id,
|
studentId = student.id,
|
||||||
studentName = student.name,
|
studentName = student.name,
|
||||||
schoolSymbol = getExtractedSchoolSymbolFromUrl(schoolUrl),
|
schoolSymbol = getExtractedSchoolSymbolFromUrl(schoolUrl),
|
||||||
|
@ -57,7 +58,11 @@ class RegisterRepository(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.toList().map { it.flatten().distinctBy { pupil -> listOf(pupil.studentId, pupil.classId, pupil.schoolSymbol) } }
|
}.toList().map {
|
||||||
|
it.flatten().distinctBy { pupil ->
|
||||||
|
listOf(pupil.studentId, pupil.classId, pupil.schoolSymbol)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getStudents(symbol: String, schoolUrl: String): Single<List<StudentAndParentResponse.Student>> {
|
private fun getStudents(symbol: String, schoolUrl: String): Single<List<StudentAndParentResponse.Student>> {
|
||||||
|
@ -87,7 +92,7 @@ class RegisterRepository(
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getSymbols(): Single<List<Pair<String, CertificateResponse>>> {
|
private fun getSymbols(): Single<List<Pair<String, CertificateResponse>>> {
|
||||||
return getLoginType(startSymbol).map {
|
return getLoginType(startSymbol.getNormalizedSymbol()).map {
|
||||||
loginHelper.apply { loginType = it }
|
loginHelper.apply { loginType = it }
|
||||||
}.flatMap { login ->
|
}.flatMap { login ->
|
||||||
login.sendCredentials(email, password).flatMap { Single.just(it) }.flatMap { cert ->
|
login.sendCredentials(email, password).flatMap { Single.just(it) }.flatMap { cert ->
|
||||||
|
@ -123,4 +128,12 @@ class RegisterRepository(
|
||||||
|
|
||||||
return path[2]
|
return path[2]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun String.getNormalizedSymbol(): String {
|
||||||
|
return trim().toLowerCase().replace("default", "").run {
|
||||||
|
Normalizer.normalize(this, Normalizer.Form.NFD).run {
|
||||||
|
"\\p{InCombiningDiacriticalMarks}+".toRegex().replace(this, "")
|
||||||
|
}
|
||||||
|
}.replace("[^a-z]".toRegex(), "").ifBlank { "Default" }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,19 +14,22 @@ import io.github.wulkanowy.api.service.StudentAndParentService
|
||||||
import io.github.wulkanowy.api.service.StudentService
|
import io.github.wulkanowy.api.service.StudentService
|
||||||
import io.reactivex.observers.TestObserver
|
import io.reactivex.observers.TestObserver
|
||||||
import okhttp3.mockwebserver.MockResponse
|
import okhttp3.mockwebserver.MockResponse
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import java.net.CookieManager
|
import java.net.CookieManager
|
||||||
|
|
||||||
class RegisterRepositoryTest : BaseLocalTest() {
|
class RegisterRepositoryTest : BaseLocalTest() {
|
||||||
|
|
||||||
private val normal by lazy {
|
private val normal by lazy { getRegisterRepository("Default") }
|
||||||
RegisterRepository("Default", "jan@fakelog.cf", "jan123", false,
|
|
||||||
LoginHelper(Api.LoginType.STANDARD, "http", "fakelog.localhost:3000", "Default", CookieManager(),
|
private fun getRegisterRepository(symbol: String): RegisterRepository {
|
||||||
getService(LoginService::class.java, "http://fakelog.localhost:3000/")),
|
return RegisterRepository(symbol, "jan@fakelog.cf", "jan123", false,
|
||||||
getService(service = RegisterService::class.java, url = "http://fakelog.localhost:3000/", errorInterceptor = false, noLoggedInInterceptor = false),
|
LoginHelper(Api.LoginType.STANDARD, "http", "fakelog.localhost:3000", symbol, CookieManager(),
|
||||||
getService(StudentAndParentService::class.java),
|
getService(LoginService::class.java, "http://fakelog.localhost:3000/")),
|
||||||
getService(StudentService::class.java),
|
getService(service = RegisterService::class.java, url = "http://fakelog.localhost:3000/", errorInterceptor = false, noLoggedInInterceptor = false),
|
||||||
ServiceManager.UrlGenerator("http", "fakelog.localhost:3000", "Default", "")
|
getService(StudentAndParentService::class.java),
|
||||||
|
getService(StudentService::class.java),
|
||||||
|
ServiceManager.UrlGenerator("http", "fakelog.localhost:3000", symbol, "")
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,4 +66,55 @@ class RegisterRepositoryTest : BaseLocalTest() {
|
||||||
res.subscribe(observer)
|
res.subscribe(observer)
|
||||||
observer.assertComplete()
|
observer.assertComplete()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun normalizeInvalidSymbol_default() {
|
||||||
|
server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("LoginPage-standard.html").readText()))
|
||||||
|
server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-uonet.html").readText()))
|
||||||
|
server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-brak-dostepu.html").readText()))
|
||||||
|
server.enqueue(MockResponse().setBody(ErrorInterceptorTest::class.java.getResource("Offline.html").readText()))
|
||||||
|
|
||||||
|
server.start(3000)
|
||||||
|
|
||||||
|
val res = getRegisterRepository("Default").getStudents()
|
||||||
|
val observer = TestObserver<List<Student>>()
|
||||||
|
res.subscribe(observer)
|
||||||
|
observer.assertTerminated()
|
||||||
|
|
||||||
|
assertEquals("/Default/Account/LogOn", server.takeRequest().path)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun normalizeInvalidSymbol_custom() {
|
||||||
|
server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("LoginPage-standard.html").readText()))
|
||||||
|
server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-uonet.html").readText()))
|
||||||
|
server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-brak-dostepu.html").readText()))
|
||||||
|
server.enqueue(MockResponse().setBody(ErrorInterceptorTest::class.java.getResource("Offline.html").readText()))
|
||||||
|
|
||||||
|
server.start(3000)
|
||||||
|
|
||||||
|
val res = getRegisterRepository(" Rzeszów + ").getStudents()
|
||||||
|
val observer = TestObserver<List<Student>>()
|
||||||
|
res.subscribe(observer)
|
||||||
|
observer.assertTerminated()
|
||||||
|
|
||||||
|
assertEquals("/rzeszow/Account/LogOn", server.takeRequest().path)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun normalizeInvalidSymbol_emptyFallback() {
|
||||||
|
server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("LoginPage-standard.html").readText()))
|
||||||
|
server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-uonet.html").readText()))
|
||||||
|
server.enqueue(MockResponse().setBody(LoginTest::class.java.getResource("Logowanie-brak-dostepu.html").readText()))
|
||||||
|
server.enqueue(MockResponse().setBody(ErrorInterceptorTest::class.java.getResource("Offline.html").readText()))
|
||||||
|
|
||||||
|
server.start(3000)
|
||||||
|
|
||||||
|
val res = getRegisterRepository(" + ").getStudents()
|
||||||
|
val observer = TestObserver<List<Student>>()
|
||||||
|
res.subscribe(observer)
|
||||||
|
observer.assertTerminated()
|
||||||
|
|
||||||
|
assertEquals("/Default/Account/LogOn", server.takeRequest().path)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue