testiki
This commit is contained in:
parent
ba5e517178
commit
c0cfb196f2
@ -2,23 +2,81 @@ package com.example.shoesapptest
|
|||||||
|
|
||||||
import androidx.test.platform.app.InstrumentationRegistry
|
import androidx.test.platform.app.InstrumentationRegistry
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||||
|
import com.example.shoesapptest.domain.usecase.validator.EmailValidator
|
||||||
|
import com.example.shoesapptest.domain.usecase.validator.PasswordValidator
|
||||||
|
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.junit.runner.RunWith
|
import org.junit.runner.RunWith
|
||||||
|
|
||||||
import org.junit.Assert.*
|
import org.junit.Assert.*
|
||||||
|
import org.junit.runners.Parameterized
|
||||||
|
import org.junit.runners.Parameterized.Parameter
|
||||||
|
import org.junit.runners.Parameterized.Parameters
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instrumented test, which will execute on an Android device.
|
* Instrumented test, which will execute on an Android device.
|
||||||
*
|
*
|
||||||
* See [testing documentation](http://d.android.com/tools/testing).
|
* See [testing documentation](http://d.android.com/tools/testing).
|
||||||
*/
|
*/
|
||||||
@RunWith(AndroidJUnit4::class)
|
@RunWith(Parameterized::class)
|
||||||
class ExampleInstrumentedTest {
|
class EmailValidationTest(private val email: String, private val expectedResult: Boolean) {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun useAppContext() {
|
fun testEmailValidation() {
|
||||||
// Context of the app under test.
|
val result = EmailValidator().validate(email)
|
||||||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
|
assertEquals(expectedResult, result)
|
||||||
assertEquals("com.example.shoesapptest", appContext.packageName)
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
@JvmStatic
|
||||||
|
@Parameters
|
||||||
|
fun getData() = listOf(
|
||||||
|
|
||||||
|
arrayOf("test@mail.ru", true),
|
||||||
|
arrayOf("user123@domain.ab", true),
|
||||||
|
arrayOf("a@b.cd", true),
|
||||||
|
|
||||||
|
|
||||||
|
arrayOf("Test@mail.ru", false),
|
||||||
|
arrayOf("test@Mail.ru", false),
|
||||||
|
arrayOf("test@mail.russia", false),
|
||||||
|
arrayOf("test@mail.r", false),
|
||||||
|
arrayOf("test@mail.1", false),
|
||||||
|
arrayOf("test@.ru", false),
|
||||||
|
arrayOf("@mail.ru", false),
|
||||||
|
arrayOf("testmail.ru", false),
|
||||||
|
arrayOf("test@mail..ru", false),
|
||||||
|
arrayOf("test@mail.ru ", false),
|
||||||
|
arrayOf(" test@mail.ru", false),
|
||||||
|
arrayOf("", false)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@RunWith(Parameterized::class)
|
||||||
|
class PasswordValidationTest(private val password: String, private val expectedResult: Boolean) {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testPasswordValidation() {
|
||||||
|
val result = PasswordValidator().validate(password)
|
||||||
|
assertEquals(expectedResult, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
@JvmStatic
|
||||||
|
@Parameters
|
||||||
|
fun getData() = listOf(
|
||||||
|
|
||||||
|
arrayOf("ValidPass1!", true),
|
||||||
|
arrayOf("NewPass2@", true),
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
arrayOf("short", false),
|
||||||
|
arrayOf("noDigits!", false),
|
||||||
|
arrayOf("alllower1!", false),
|
||||||
|
arrayOf("", false)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -7,6 +7,8 @@ import com.example.shoesapptest.data.remote.network.dto.AuthorizationResponse
|
|||||||
import com.example.shoesapptest.data.remote.network.dto.RegistrationRequest
|
import com.example.shoesapptest.data.remote.network.dto.RegistrationRequest
|
||||||
import com.example.shoesapptest.data.remote.network.dto.RegistrationResponse
|
import com.example.shoesapptest.data.remote.network.dto.RegistrationResponse
|
||||||
import com.example.shoesapptest.data.repository.AuthRepository
|
import com.example.shoesapptest.data.repository.AuthRepository
|
||||||
|
import com.example.shoesapptest.domain.usecase.validator.EmailValidator
|
||||||
|
import com.example.shoesapptest.domain.usecase.validator.PasswordValidator
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.flow
|
import kotlinx.coroutines.flow.flow
|
||||||
|
|
||||||
@ -16,15 +18,22 @@ class AuthUseCase(private val dataStore: DataStore, private val authRepository:
|
|||||||
suspend fun registration(registrationRequest: RegistrationRequest): Flow<NetworkResponse<RegistrationResponse>> = flow {
|
suspend fun registration(registrationRequest: RegistrationRequest): Flow<NetworkResponse<RegistrationResponse>> = flow {
|
||||||
try {
|
try {
|
||||||
emit(NetworkResponse.Loading)
|
emit(NetworkResponse.Loading)
|
||||||
|
|
||||||
|
|
||||||
|
if (!EmailValidator().validate(registrationRequest.email)) {
|
||||||
|
emit(NetworkResponse.Error("Invalid email format"))
|
||||||
|
return@flow
|
||||||
|
}
|
||||||
|
if (!PasswordValidator().validate(registrationRequest.password)) {
|
||||||
|
emit(NetworkResponse.Error("Password must contain: 8+ chars, 1 uppercase, 1 digit, 1 special char"))
|
||||||
|
return@flow
|
||||||
|
}
|
||||||
|
|
||||||
val result = authRepository.registration(registrationRequest)
|
val result = authRepository.registration(registrationRequest)
|
||||||
dataStore.setToken(result.second)
|
dataStore.setToken(result.second)
|
||||||
emit(NetworkResponse.Success(result))
|
emit(NetworkResponse.Success(result))
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
e.message?.let {
|
emit(NetworkResponse.Error(e.message ?: "Unknown Error"))
|
||||||
emit(NetworkResponse.Error(it))
|
|
||||||
return@flow
|
|
||||||
}
|
|
||||||
emit(NetworkResponse.Error("Unknown Error"))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,11 +44,7 @@ class AuthUseCase(private val dataStore: DataStore, private val authRepository:
|
|||||||
dataStore.setToken(result.token)
|
dataStore.setToken(result.token)
|
||||||
emit(NetworkResponse.Success(result))
|
emit(NetworkResponse.Success(result))
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
e.message?.let {
|
emit(NetworkResponse.Error(e.message ?: "Unknown Error"))
|
||||||
emit(NetworkResponse.Error(it))
|
|
||||||
return@flow
|
|
||||||
}
|
|
||||||
emit(NetworkResponse.Error("Unknown Error"))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.example.shoesapptest.domain.usecase.validator
|
||||||
|
|
||||||
|
class EmailValidator: Validator {
|
||||||
|
override fun <T> validate(value: T): Boolean {
|
||||||
|
val email = value as? String ?: return false
|
||||||
|
|
||||||
|
val emailChars = "^[a-z0-9]+@[a-z0-9]+\\.[a-z]{2}\$".toRegex()
|
||||||
|
|
||||||
|
return email.matches(emailChars)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.example.shoesapptest.domain.usecase.validator
|
||||||
|
|
||||||
|
class PasswordValidator : Validator {
|
||||||
|
override fun <T> validate(value: T): Boolean {
|
||||||
|
val password = value as? String ?: return false
|
||||||
|
|
||||||
|
|
||||||
|
if (password.length < 8) return false
|
||||||
|
|
||||||
|
if (!password.any { it.isUpperCase() }) return false
|
||||||
|
|
||||||
|
if (!password.any { it.isDigit() }) return false
|
||||||
|
|
||||||
|
val specialChars = setOf('!', '@', '#', '$', '%', '^', '&', '*')
|
||||||
|
if (!password.any { it in specialChars }) return false
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.example.shoesapptest.domain.usecase.validator
|
||||||
|
|
||||||
|
interface Validator {
|
||||||
|
fun <T> validate(value: T ): Boolean
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user