Skip to content
This repository has been archived by the owner on Jan 7, 2025. It is now read-only.

Commit

Permalink
Merge pull request #13 from fictadvisor/api_tests
Browse files Browse the repository at this point in the history
Api tests
  • Loading branch information
NikitaSutulov authored Mar 14, 2024
2 parents f6d5645 + a6ffd02 commit d976d2b
Show file tree
Hide file tree
Showing 2 changed files with 266 additions and 5 deletions.
7 changes: 6 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ dependencies {
def coroutines_version = "1.7.3"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
def lifecycle_version = "2.6.2"
// - - Coroutine test
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version"

// - - ViewModel
def lifecycle_version = "2.6.2"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
// - - LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
Expand All @@ -99,4 +102,6 @@ dependencies {
// - - MockWebServer
def mock_web_server_version = "4.3.1"
testImplementation "com.squareup.okhttp3:mockwebserver:$mock_web_server_version"

testImplementation "org.mockito:mockito-core:5.10.0"
}
264 changes: 260 additions & 4 deletions app/src/test/java/com/fictadvisor/android/AuthApiTest.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package com.fictadvisor.android

import com.fictadvisor.android.data.dto.ForgotPasswordDTO
import com.fictadvisor.android.data.dto.LoginRequest
import com.fictadvisor.android.data.dto.OrdinaryStudentResponse
import com.fictadvisor.android.data.dto.RegisterTelegramDTO
import com.fictadvisor.android.data.dto.RegistrationDTO
import com.fictadvisor.android.data.dto.ResetPasswordDTO
import com.fictadvisor.android.data.dto.TelegramDTO
import com.fictadvisor.android.data.dto.UpdatePasswordDTO
import com.fictadvisor.android.data.dto.VerificationEmailDTO
import com.fictadvisor.android.data.remote.api.AuthApi
import kotlinx.coroutines.runBlocking
import com.google.gson.Gson
import kotlinx.coroutines.test.runTest
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
Expand All @@ -14,6 +23,7 @@ import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.mockito.Mockito.mock
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -64,12 +74,11 @@ class AuthApiTest {
}

@Test
fun testLogin() = runBlocking {
fun `login should return access and refresh tokens`() = runTest {
val responseJson =
"""{"accessToken": "some_access_token", "refreshToken": "some_refresh_token"}"""
server.enqueue(MockResponse().setBody(responseJson))

val loginRequest = LoginRequest(username = "some_username", password = "some_password")
val loginRequest = mock(LoginRequest::class.java)

val response = authApi.login(loginRequest)

Expand All @@ -79,6 +88,249 @@ class AuthApiTest {
assertEquals("some_access_token", authResponse!!.accessToken)
assertEquals("some_refresh_token", authResponse.refreshToken)
}
@Test
fun `loginTelegram should return access and refresh tokens`() = runTest {
val responseJson = """{"accessToken": "some_access_token", "refreshToken": "some_refresh_token"}"""
server.enqueue(MockResponse().setBody(responseJson))

val telegramDTO = mock(TelegramDTO::class.java)

val response = authApi.loginTelegram(telegramDTO)

assert(response.isSuccessful)
val authLoginResponse = response.body()
assertNotNull(authLoginResponse)
assertEquals("some_access_token", authLoginResponse!!.accessToken)
assertEquals("some_refresh_token", authLoginResponse.refreshToken)
}

@Test
fun `register should create a new user`() = runTest {
val responseJson = """User registered successfully"""
server.enqueue(MockResponse().setBody(responseJson))

val registrationRequest = mock(RegistrationDTO::class.java)
val response = authApi.register(registrationRequest)


assertTrue(response.isSuccessful)
val responseBody = response.body()
assertNotNull(responseBody)
assertEquals("User registered successfully", responseBody!!.string())
}

@Test
fun `registerTelegram should register with Telegram` () = runTest {
val responseJson = """Telegram registration successful"""
server.enqueue(MockResponse().setBody(responseJson))

val registrationTelegramRequest = mock(RegisterTelegramDTO::class.java)
val response = authApi.registerTelegram(registrationTelegramRequest)

assertTrue(response.isSuccessful)
val responseBody = response.body()
assertNotNull(responseBody)
assertEquals("Telegram registration successful", responseBody!!.string())
}

@Test
fun `refresh should return new tokens` () = runTest {
val responseJson =
"""{"accessToken": "new_access_token"}"""
server.enqueue(MockResponse().setBody(responseJson))

val response = authApi.refresh()

assertTrue(response.isSuccessful)
val authRefreshResponse = response.body()
assertNotNull(authRefreshResponse)
assertEquals("new_access_token", authRefreshResponse!!.accessToken)
}

@Test
fun `verifyEmail should verify user's email` () = runTest {
val responseJson = """Email verification successful"""
server.enqueue(MockResponse().setBody(responseJson))

val verifyEmailRequest = mock(VerificationEmailDTO::class.java)
val response = authApi.verifyEmail(verifyEmailRequest)

assertTrue(response.isSuccessful)
val responseBody = response.body()
assertNotNull(responseBody)
assertEquals("Email verification successful", responseBody!!.string())
}


@Test
fun `forgotPassword should return success response`() = runTest {
val responseJson = """Password reset email sent successfully"""
server.enqueue(MockResponse().setBody(responseJson))

val forgotPasswordRequest = mock(ForgotPasswordDTO::class.java)
val response = authApi.forgotPassword(forgotPasswordRequest)

assertTrue(response.isSuccessful)
val responseBody = response.body()
assertNotNull(responseBody)
assertEquals("Password reset email sent successfully", responseBody!!.string())
}

@Test
fun `verifyEmailToken should return access and refresh tokens`() = runTest {
val responseJson =
"""{"accessToken": "some_access_token", "refreshToken": "some_refresh_token"}"""
server.enqueue(MockResponse().setBody(responseJson))

val token = "some_token"
val response = authApi.verifyEmailToken(token)

assertTrue(response.isSuccessful)
val authResponse = response.body()
assertNotNull(authResponse)
assertEquals("some_access_token", authResponse!!.accessToken)
assertEquals("some_refresh_token", authResponse.refreshToken)
}

@Test
fun `updatePassword should return access and refresh tokens`() = runTest {
val responseJson =
"""{"accessToken": "some_access_token", "refreshToken": "some_refresh_token"}"""
server.enqueue(MockResponse().setBody(responseJson))

val updatePasswordRequest = mock(UpdatePasswordDTO::class.java)
val response = authApi.updatePassword(updatePasswordRequest)

assertTrue(response.isSuccessful)
val authResponse = response.body()
assertNotNull(authResponse)
assertEquals("some_access_token", authResponse!!.accessToken)
assertEquals("some_refresh_token", authResponse.refreshToken)
}

@Test
fun `resetPassword should return access and refresh tokens`() = runTest {
val responseJson =
"""{"accessToken": "some_access_token", "refreshToken": "some_refresh_token"}"""
server.enqueue(MockResponse().setBody(responseJson))

val token = "some_token"
val resetPasswordRequest = mock(ResetPasswordDTO::class.java)
val response = authApi.resetPassword(token, resetPasswordRequest)

assertTrue(response.isSuccessful)
val authResponse = response.body()
assertNotNull(authResponse)
assertEquals("some_access_token", authResponse!!.accessToken)
assertEquals("some_refresh_token", authResponse.refreshToken)
}

//auth
@Test
fun `getStudent should return successful response` () = runTest {
}

@Test
fun `verifyIsRegistered should return true when user is registered`() = runTest {
val responseJson = "true"
server.enqueue(MockResponse().setBody(responseJson))

val response = authApi.verifyIsRegistered("username", "email")

assertTrue(response.isSuccessful)
val isRegistered = response.body()
assertNotNull(isRegistered)
assertTrue(isRegistered!!)
}

@Test
fun `checkCaptain should return true when user is a captain`() = runTest {
val responseJson = "true"
server.enqueue(MockResponse().setBody(responseJson))

val groupId = "some_group_id"
val response = authApi.checkCaptain(groupId)

assertTrue(response.isSuccessful)
val isCaptain = response.body()
assertNotNull(isCaptain)
assertTrue(isCaptain!!)
}

@Test
fun `checkResetToken should return token status`() = runTest {
val responseJson = """{"isAvailable": true}"""
server.enqueue(MockResponse().setBody(responseJson))

val token = "some_token"
val response = authApi.checkResetToken(token)

assertTrue(response.isSuccessful)
val tokenStatus = response.body()
assertNotNull(tokenStatus)
assertTrue(tokenStatus!!.isAvailable)
}

@Test
fun `checkRegisterTelegram should return registration status`() = runTest {
val responseJson = """{"isRegistered": true}"""
server.enqueue(MockResponse().setBody(responseJson))

val token = "some_token"
val response = authApi.checkRegisterTelegram(token)

assertTrue(response.isSuccessful)
val registrationStatus = response.body()
assertNotNull(registrationStatus)
assertTrue(registrationStatus!!.isRegistered)
}

@Test
fun `getStudent should return student information`() = runTest {
// Prepare a sample response JSON representing a student
val responseJson = """
{
"id": "123456",
"firstName": "John",
"middleName": "",
"lastName": "Doe",
"state": "APPROVED",
"username": "johndoe",
"email": "[email protected]",
"avatar": "http://example.com/avatar.jpg",
"telegramId": 123456,
"group": {
"id": "789",
"code": "group_code",
"state": "APPROVED",
"role": "USER"
}
}
""".trimIndent()

server.enqueue(MockResponse().setBody(responseJson))

val response = authApi.getStudent()

assertTrue(response.isSuccessful)

val studentResponse = response.body()

assertNotNull(studentResponse)

assertEquals("123456", studentResponse!!.id)
assertEquals("John", studentResponse.firstName)
assertEquals("", studentResponse.middleName)
assertEquals("Doe", studentResponse.lastName)
assertEquals("johndoe", studentResponse.username)
assertEquals("[email protected]", studentResponse.email)
assertEquals("http://example.com/avatar.jpg", studentResponse.avatar)
assertEquals(123456, studentResponse.telegramId)
assertEquals("789", studentResponse.group.id)
assertEquals("group_code", studentResponse.group.code)
assertEquals("APPROVED", studentResponse.group.state)
assertEquals("USER", studentResponse.group.role)
}

companion object {
const val MOCK_WEBSERVER_PORT = 8080
Expand All @@ -88,3 +340,7 @@ class AuthApiTest {
private const val TIMEOUT_CONNECT = 30 // In seconds
}
}




0 comments on commit d976d2b

Please sign in to comment.