Add login type to NotLoggedInInterceptor
This commit is contained in:
parent
559b11901b
commit
54ae27045f
7 changed files with 34 additions and 19 deletions
|
@ -2,7 +2,6 @@ package io.github.wulkanowy.api.interceptor
|
|||
|
||||
import io.github.wulkanowy.api.login.AccountPermissionException
|
||||
import io.github.wulkanowy.api.login.BadCredentialsException
|
||||
import io.github.wulkanowy.api.login.NotLoggedInException
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Response
|
||||
import org.jsoup.Jsoup
|
||||
|
|
|
@ -1,22 +1,31 @@
|
|||
package io.github.wulkanowy.api.interceptor
|
||||
|
||||
import io.github.wulkanowy.api.Api
|
||||
import io.github.wulkanowy.api.login.NotLoggedInException
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Response
|
||||
import org.jsoup.Jsoup
|
||||
import org.jsoup.select.Elements
|
||||
|
||||
class NotLoggedInErrorInterceptor : Interceptor {
|
||||
class NotLoggedInErrorInterceptor(private val loginType: Api.LoginType) : Interceptor {
|
||||
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
val response = chain.proceed(chain.request())
|
||||
val doc = Jsoup.parse(response.peekBody(Long.MAX_VALUE).string())
|
||||
|
||||
doc.select(".loginButton, .LogOnBoard input[type=submit], #PassiveSignInButton, form #SubmitButton, form[name=form1] #SubmitButton").let {
|
||||
if (it.isNotEmpty()) throw NotLoggedInException("User not logged in")
|
||||
if (when (loginType) {
|
||||
Api.LoginType.STANDARD -> doc.select(".loginButton, .LogOnBoard input[type=submit]")
|
||||
Api.LoginType.ADFS -> doc.select("form[name=form1] #SubmitButton")
|
||||
Api.LoginType.ADFSLight -> doc.select("form #SubmitButton")
|
||||
Api.LoginType.ADFSCards -> doc.select("#PassiveSignInButton")
|
||||
else -> Elements()
|
||||
}.isNotEmpty()) {
|
||||
throw NotLoggedInException("User not logged in")
|
||||
}
|
||||
|
||||
doc.body().text().let { // /messages
|
||||
if (it.contains("The custom error module")) throw NotLoggedInException("User not logged in")
|
||||
doc.body().text().let {
|
||||
// /messages
|
||||
if (it.contains("The custom error module")) throw NotLoggedInException("Zaloguj się")
|
||||
}
|
||||
|
||||
return response
|
||||
|
|
|
@ -51,7 +51,7 @@ class ServiceManager(
|
|||
private val interceptors: MutableList<Pair<Interceptor, Boolean>> = mutableListOf(
|
||||
Pair(HttpLoggingInterceptor().setLevel(logLevel), true),
|
||||
Pair(ErrorInterceptor(), false),
|
||||
Pair(NotLoggedInErrorInterceptor(), false)
|
||||
Pair(NotLoggedInErrorInterceptor(loginType), false)
|
||||
)
|
||||
|
||||
fun setInterceptor(interceptor: Interceptor, network: Boolean = false, index: Int = -1) {
|
||||
|
@ -65,7 +65,7 @@ class ServiceManager(
|
|||
|
||||
fun getLoginService(): LoginService {
|
||||
if (email.isBlank() || password.isBlank()) throw ApiException("Email or/and password are not set")
|
||||
return getRetrofit(getClientBuilder(), url.generate(UrlGenerator.Site.LOGIN), false).create()
|
||||
return getRetrofit(getClientBuilder(true, false), url.generate(UrlGenerator.Site.LOGIN), false).create()
|
||||
}
|
||||
|
||||
fun getRegisterService(): RegisterService {
|
||||
|
|
|
@ -24,17 +24,23 @@ abstract class BaseLocalTest : BaseTest() {
|
|||
server.shutdown()
|
||||
}
|
||||
|
||||
fun getSnpRepo(testClass: Class<*>, fixture: String): StudentAndParentRepository {
|
||||
fun getSnpRepo(testClass: Class<*>, fixture: String, loginType: Api.LoginType = Api.LoginType.STANDARD): StudentAndParentRepository {
|
||||
server.enqueue(MockResponse().setBody(testClass.getResource(fixture).readText()))
|
||||
return StudentAndParentRepository(getService(StudentAndParentService::class.java))
|
||||
return StudentAndParentRepository(getService(StudentAndParentService::class.java, this.server.url("/").toString(), true, true, true, loginType))
|
||||
}
|
||||
|
||||
fun <T> getService(service: Class<T>, url: String = this.server.url("/").toString(), html: Boolean = true, errorInterceptor: Boolean = true, noLoggedInInterceptor: Boolean = true): T {
|
||||
|
||||
fun <T> getService(service: Class<T>,
|
||||
url: String = this.server.url("/").toString(),
|
||||
html: Boolean = true, errorInterceptor: Boolean = true,
|
||||
noLoggedInInterceptor: Boolean = true,
|
||||
loginType: Api.LoginType = Api.LoginType.STANDARD
|
||||
): T {
|
||||
return Retrofit.Builder()
|
||||
.client(OkHttpClient.Builder()
|
||||
.apply {
|
||||
if (errorInterceptor) addInterceptor(ErrorInterceptor())
|
||||
if (noLoggedInInterceptor) addInterceptor(NotLoggedInErrorInterceptor())
|
||||
if (noLoggedInInterceptor) addInterceptor(NotLoggedInErrorInterceptor(loginType))
|
||||
}
|
||||
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC))
|
||||
.build()
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package io.github.wulkanowy.api.interceptor
|
||||
|
||||
import io.github.wulkanowy.api.Api
|
||||
import io.github.wulkanowy.api.BaseLocalTest
|
||||
import io.github.wulkanowy.api.login.LoginTest
|
||||
import io.github.wulkanowy.api.login.NotLoggedInException
|
||||
|
@ -11,7 +12,7 @@ class ErrorInterceptorTest : BaseLocalTest() {
|
|||
|
||||
@Test
|
||||
fun notLoggedIn_standard() {
|
||||
val notes = getSnpRepo(LoginTest::class.java, "Logowanie-standard.html").getNotes()
|
||||
val notes = getSnpRepo(LoginTest::class.java, "Logowanie-standard.html", Api.LoginType.STANDARD).getNotes()
|
||||
val observer = TestObserver<List<Note>>()
|
||||
notes.subscribe(observer)
|
||||
observer.assertError(NotLoggedInException::class.java)
|
||||
|
@ -19,7 +20,7 @@ class ErrorInterceptorTest : BaseLocalTest() {
|
|||
|
||||
@Test
|
||||
fun notLoggedIn_standard2() {
|
||||
val notes = getSnpRepo(LoginTest::class.java, "LoginPage-standard.html").getNotes()
|
||||
val notes = getSnpRepo(LoginTest::class.java, "LoginPage-standard.html", Api.LoginType.STANDARD).getNotes()
|
||||
val observer = TestObserver<List<Note>>()
|
||||
notes.subscribe(observer)
|
||||
observer.assertError(NotLoggedInException::class.java)
|
||||
|
@ -27,7 +28,7 @@ class ErrorInterceptorTest : BaseLocalTest() {
|
|||
|
||||
@Test
|
||||
fun notLoggedIn_adfs() {
|
||||
val notes = getSnpRepo(LoginTest::class.java, "ADFS-form-2.html").getNotes()
|
||||
val notes = getSnpRepo(LoginTest::class.java, "ADFS-form-2.html", Api.LoginType.ADFS).getNotes()
|
||||
val observer = TestObserver<List<Note>>()
|
||||
notes.subscribe(observer)
|
||||
observer.assertError(NotLoggedInException::class.java)
|
||||
|
@ -35,7 +36,7 @@ class ErrorInterceptorTest : BaseLocalTest() {
|
|||
|
||||
@Test
|
||||
fun notLoggedIn_adfsCards() {
|
||||
val notes = getSnpRepo(LoginTest::class.java, "ADFS-form-1.html").getNotes()
|
||||
val notes = getSnpRepo(LoginTest::class.java, "ADFS-form-1.html", Api.LoginType.ADFSCards).getNotes()
|
||||
val observer = TestObserver<List<Note>>()
|
||||
notes.subscribe(observer)
|
||||
observer.assertError(NotLoggedInException::class.java)
|
||||
|
@ -43,7 +44,7 @@ class ErrorInterceptorTest : BaseLocalTest() {
|
|||
|
||||
@Test
|
||||
fun notLoggedInt_adfsLight() {
|
||||
val notes = getSnpRepo(LoginTest::class.java, "ADFSLight-form-1.html").getNotes()
|
||||
val notes = getSnpRepo(LoginTest::class.java, "ADFSLight-form-1.html", Api.LoginType.ADFSLight).getNotes()
|
||||
val observer = TestObserver<List<Note>>()
|
||||
notes.subscribe(observer)
|
||||
observer.assertError(NotLoggedInException::class.java)
|
||||
|
|
|
@ -21,7 +21,7 @@ class LoginTest : BaseLocalTest() {
|
|||
|
||||
private val adfs by lazy {
|
||||
LoginRepository(Api.LoginType.ADFSCards, "http", "fakelog.localhost:3000", "default", CookieManager(),
|
||||
getService(LoginService::class.java, "http://fakelog.localhost:3000/", true, true, false))
|
||||
getService(LoginService::class.java, "http://fakelog.localhost:3000/", true, true, false, Api.LoginType.ADFSCards))
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -19,7 +19,7 @@ class RegisterTest : BaseLocalTest() {
|
|||
|
||||
private val login by lazy {
|
||||
LoginRepository(Api.LoginType.STANDARD, "http", "fakelog.localhost:3000", "default", CookieManager(),
|
||||
getService(LoginService::class.java, "http://fakelog.localhost:3000/", true, true, false))
|
||||
getService(LoginService::class.java, "http://fakelog.localhost:3000/", true, true, false, Api.LoginType.STANDARD))
|
||||
}
|
||||
|
||||
private val register by lazy {
|
||||
|
|
Loading…
Reference in a new issue