This repository has been archived by the owner on Jan 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from fictadvisor/api_tests
Api tests
- Loading branch information
Showing
2 changed files
with
266 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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 | ||
|
@@ -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) | ||
|
||
|
@@ -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 | ||
|
@@ -88,3 +340,7 @@ class AuthApiTest { | |
private const val TIMEOUT_CONNECT = 30 // In seconds | ||
} | ||
} | ||
|
||
|
||
|
||
|