Skip to content
Snippets Groups Projects
Commit e8dc0c96 authored by nathaniacalista01's avatar nathaniacalista01
Browse files

feat : test connection to BE service

parent f841558c
2 merge requests!10Develop,!5feat : test connection to BE service
......@@ -76,6 +76,8 @@ dependencies {
val retrofit_version = "2.9.0"
implementation("com.squareup.retrofit2:retrofit:$retrofit_version")
implementation("com.squareup.retrofit2:converter-gson:$retrofit_version")
implementation("com.squareup.okhttp3:mockwebserver:4.12.0")
// implementation("com.squareup.retrofit2:convertermoshi:$retrofit_version")
//
// val moshi_version = "1.15.1"
......@@ -84,6 +86,10 @@ dependencies {
// ksp("com.squareup.moshi:moshi-kotlincodegen:$moshi_version")
testImplementation("junit:junit:4.13.2")
testImplementation("com.squareup.okhttp3:mockwebserver:4.12.0")
testImplementation("com.squareup.retrofit2:retrofit:$retrofit_version")
testImplementation("com.squareup.retrofit2:converter-gson:$retrofit_version")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
\ No newline at end of file
package com.example.bondoman
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
class LoginTest {
@Test
fun testLoginSuccess() {
val jsonMediaType = "application/json; charset=utf-8".toMediaType()
val requestBody = """
{
"email": "13521139@std.stei.itb.ac.id",
"password": "password_13521139"
}
""".trimIndent().toRequestBody(jsonMediaType)
val request = Request.Builder()
.url("https://pbd-backend-2024.vercel.app/api/auth/login")
.post(requestBody)
.build()
val client = OkHttpClient()
client.newCall(request).execute().use { response ->
// Assert the response code to verify successful login
// You might want to adjust the expected response code based on your API's specification
assertEquals(200, response.code)
// Optionally, you can assert the response body or specific parts of it
// For example, you might want to check if a token is present in the response
val responseBody = response.body?.string() ?: ""
assert(responseBody.contains("token")) // Adjust this based on your actual response structure
}
}
@Test
fun testLoginFail(){
val jsonMediaType = "application/json; charset=utf-8".toMediaType()
val requestBody = """
{
"email": "wrong email",
"password": "wrong password"
}
""".trimIndent().toRequestBody(jsonMediaType)
val request = Request.Builder()
.url("https://pbd-backend-2024.vercel.app/api/auth/login")
.post(requestBody)
.build()
val client = OkHttpClient()
client.newCall(request).execute().use { response ->
// Assert the response code to verify successful login
// You might want to adjust the expected response code based on your API's specification
assertEquals(400, response.code)
// Optionally, you can assert the response body or specific parts of it
// For example, you might want to check if a token is present in the response
val responseBody = response.body?.string() ?: ""
assertTrue("Expected 'Invalid email' in the response body", responseBody.contains("Invalid email"))
}
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment