This commit is contained in:
irreaaa 2025-04-15 13:06:12 +03:00
parent ba5e517178
commit c0cfb196f2
5 changed files with 114 additions and 16 deletions

View File

@ -2,23 +2,81 @@ package com.example.shoesapptest
import androidx.test.platform.app.InstrumentationRegistry
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.runner.RunWith
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.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@RunWith(Parameterized::class)
class EmailValidationTest(private val email: String, private val expectedResult: Boolean) {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.example.shoesapptest", appContext.packageName)
fun testEmailValidation() {
val result = EmailValidator().validate(email)
assertEquals(expectedResult, result)
}
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)
)
}
}

View File

@ -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.RegistrationResponse
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
@ -16,15 +18,22 @@ class AuthUseCase(private val dataStore: DataStore, private val authRepository:
suspend fun registration(registrationRequest: RegistrationRequest): Flow<NetworkResponse<RegistrationResponse>> = flow {
try {
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)
dataStore.setToken(result.second)
emit(NetworkResponse.Success(result))
} catch (e: Exception) {
e.message?.let {
emit(NetworkResponse.Error(it))
return@flow
}
emit(NetworkResponse.Error("Unknown Error"))
emit(NetworkResponse.Error(e.message ?: "Unknown Error"))
}
}
@ -35,11 +44,7 @@ class AuthUseCase(private val dataStore: DataStore, private val authRepository:
dataStore.setToken(result.token)
emit(NetworkResponse.Success(result))
} catch (e: Exception) {
e.message?.let {
emit(NetworkResponse.Error(it))
return@flow
}
emit(NetworkResponse.Error("Unknown Error"))
emit(NetworkResponse.Error(e.message ?: "Unknown Error"))
}
}
}

View File

@ -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)
}
}

View File

@ -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
}
}

View File

@ -0,0 +1,5 @@
package com.example.shoesapptest.domain.usecase.validator
interface Validator {
fun <T> validate(value: T ): Boolean
}