diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 85a2ee80819ea7ab7a4599fb6951a97fb45d62df..3607c486d393e4d6b849907d9a67739b32885bac 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -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 diff --git a/app/src/test/java/com/example/bondoman/LoginTest.kt b/app/src/test/java/com/example/bondoman/LoginTest.kt new file mode 100644 index 0000000000000000000000000000000000000000..b1935835ee4e9b2b06d9b9f8201fb8cc741d8cda --- /dev/null +++ b/app/src/test/java/com/example/bondoman/LoginTest.kt @@ -0,0 +1,72 @@ +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