chore: add test (#11)
Release package / release (push) Successful in 3m37s

Co-authored-by: TakahashiNg <83152264+TakahashiNguyen@users.noreply.github.com>
Co-authored-by: TranHuuDanh <tranhuudanh@demonkernel.io.vn>
Reviewed-on: #11
This commit was merged in pull request #11.
This commit is contained in:
2026-03-31 10:07:49 +00:00
parent 4ae4fa6f2e
commit 6938b764fc
12 changed files with 219 additions and 18 deletions
+4 -2
View File
@@ -21,6 +21,8 @@
}
},
"containerEnv": {
"TESTCONTAINERS_RYUK_DISABLED": "true"
}
"TESTCONTAINERS_RYUK_DISABLED": "true",
"QUARKUS_DEBUG_HOST": "0.0.0.0"
},
"forwardPorts": [5005]
}
-2
View File
@@ -1,6 +1,4 @@
include:
- ./docker-compose.kafka.yaml
- ./docker-compose.postgres.yaml
- ./docker-compose.redis.yaml
services:
+10 -2
View File
@@ -5,6 +5,10 @@ on:
branches:
- main
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
build-and-test:
runs-on: ubuntu-latest
@@ -14,11 +18,15 @@ jobs:
with:
fetch-depth: 0
- uses: shogo82148/actions-setup-redis@v1
with:
redis-version: "7.x"
- name: Set up JDK
uses: actions/setup-java@v5
with:
distribution: "temurin"
java-version: "25"
java-version: "21"
- name: Cache Maven packages
uses: actions/cache@v4
@@ -29,7 +37,7 @@ jobs:
${{ runner.os }}-maven-
- name: Build and Test all modules
run: ./mvnw -B clean verify -Dquarkus.container-image.build=false -Dquarkus.container-image.push=false
run: ./mvnw -B install -Dquarkus.container-image.build=false -Dquarkus.container-image.push=false
- name: Publish Test Report
uses: scacap/action-surefire-report@v1
+1 -1
View File
@@ -15,7 +15,7 @@
"@semantic-release/exec",
{
"prepareCmd": "./mvnw versions:set -DnewVersion=${nextRelease.version}",
"publishCmd": "if [ \"${branch.name}\" = \"main\" ]; then ./mvnw clean package -Dquarkus.container-image.tag=${nextRelease.version} -DskipTests; fi"
"publishCmd": "if [ \"${branch.name}\" = \"main\" ]; then ./mvnw package -Dquarkus.container-image.tag=${nextRelease.version} -DskipTests; fi"
}
],
[
@@ -77,4 +77,45 @@ public class UserModelTest {
assertEquals("InvalidPhoneNumber", exception.getMessage());
}
@Test
@Transactional
public void testLoginSuccess() {
//dang nhap thanh cong
TestUserEntity user = new TestUserEntity();
String phone =
"+8477" + String.format("%07d", (new Random()).nextInt(10000000));
String password = "02092006Danh";
user.signup("Tran Huu Danh", phone, password);
Boolean loginResult = user.login(password);
assertTrue(loginResult);
}
@Test
@Transactional
public void testLoginFailWrongPassword() {
//dang nhap that bai
TestUserEntity user = new TestUserEntity();
String phone =
"+8477" + String.format("%07d", (new Random()).nextInt(10000000));
String truePassword = "02092006Danh";
String wrongPassword = "02092006danhh";
user.signup("Tran Huu Danh", phone, truePassword);
Boolean wrongTest = user.login(wrongPassword);
assertFalse(wrongTest);
}
@Test
@Transactional
public void testLoginFailNullHashedPassword() {
TestUserEntity user = new TestUserEntity();
user.hashedPassword = null;
boolean loginResult = user.login("anhdomixi");
assertFalse(loginResult);
}
}
@@ -0,0 +1,150 @@
package com.drinkool.services;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.*;
import com.drinkool.dtos.*;
import com.drinkool.entities.CustomerEntity;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.http.ContentType;
import java.util.Random;
import org.junit.jupiter.api.Test;
@QuarkusTest
public class AuthenticationServiceTest {
private Random random = new Random();
private String generateRandomPhone() {
return "+8477" + String.format("%07d", random.nextInt(10000000));
}
@Test
void testSignup() {
String phone = generateRandomPhone();
Signup input = new Signup();
input.name = "Test User";
input.phone = phone;
input.password = "password123";
given()
.contentType(ContentType.JSON)
.body(input)
.when()
.post("/api/signup")
.then()
.statusCode(201);
// Kiểm tra database: Phải tìm thấy 1 user với số điện thoại này
assertEquals(1, CustomerEntity.find("phone", phone).count());
}
@Test
void testQuickSignup() {
String phone = generateRandomPhone();
QuickSignup input = new QuickSignup();
input.phone = phone;
given()
.contentType(ContentType.JSON)
.body(input)
.when()
.post("/api/quickSignup")
.then()
.statusCode(201);
assertEquals(1, CustomerEntity.find("phone", phone).count());
}
@Test
void testLoginSuccess() {
// BƯỚC 1: Tạo user trước bằng signup
String phone = generateRandomPhone();
Signup signupData = new Signup();
signupData.name = "Login User";
signupData.phone = phone;
signupData.password = "secure_pass";
given()
.contentType(ContentType.JSON)
.body(signupData)
.post("/api/signup")
.then()
.statusCode(201);
// BƯỚC 2: Thử đăng nhập
Login loginInput = new Login();
loginInput.phone = phone;
loginInput.password = "secure_pass";
given()
.contentType(ContentType.JSON)
.body(loginInput)
.when()
.post("/api/login")
.then()
.statusCode(202);
}
@Test
void testLoginFailWrongPassword() {
String phone = generateRandomPhone();
Signup signupData = new Signup();
signupData.name = "Wrong Pass User";
signupData.phone = phone;
signupData.password = "correct_password";
given()
.contentType(ContentType.JSON)
.body(signupData)
.post("/api/signup")
.then()
.statusCode(201);
Login loginInput = new Login();
loginInput.phone = phone;
loginInput.password = "wrong_password";
given()
.contentType(ContentType.JSON)
.body(loginInput)
.when()
.post("/api/login")
.then()
.statusCode(400); // BadRequestException
}
@Test
void testSmsOtp() {
String phone = generateRandomPhone();
SmsOtp input = new SmsOtp();
input.phone = phone;
given()
.contentType(ContentType.JSON)
.body(input)
.when()
.post("/api/sms_otp")
.then()
.statusCode(202);
// Lưu ý: Vì không dùng Mock, code sẽ chạy qua OtpService thật.
// Nếu OtpService lưu vào DB, bạn có thể bổ sung Assert tại đây để check OtpEntity.
}
@Test
void testQuickLoginFailInvalidOtp() {
String phone = generateRandomPhone();
QuickLogin input = new QuickLogin();
input.phone = phone;
input.otp = "000000"; // Giả sử mã này luôn sai vì chưa được tạo
given()
.contentType(ContentType.JSON)
.body(input)
.when()
.post("/api/quicklogin")
.then()
.statusCode(401);
}
}
@@ -1,9 +1,10 @@
# Quarkus
quarkus.http.host=0.0.0.0
# Database
## Datasource
%test.quarkus.datasource.db-kind=h2
%test.quarkus.datasource.jdbc.url=jdbc:h2:mem:test_db;DB_CLOSE_DELAY=-1
## Orm
%test.quarkus.hibernate-orm.database.generation=drop-and-create
# Docker
%test.quarkus.container-image.build=false
%test.quarkus.container-image.push=false
@@ -19,8 +19,6 @@ public class MenuItemEntity extends BaseEntity {
)
public List<MenuItemIngredientEntity> ingredients = new ArrayList<>();
private MenuItemEntity() {}
/**
* Thêm nguyên liệu vào món ăn với định lượng cụ thể
*/
@@ -29,4 +29,6 @@ public class InventoryServiceTest {
assertEquals(1, InventoryItemEntity.find("name", name).count());
}
}
@@ -2,8 +2,9 @@
quarkus.http.host=0.0.0.0
# Database
# Database
## Datasource
%test.quarkus.datasource.db-kind=h2
%test.quarkus.datasource.jdbc.url=jdbc:h2:mem:test_db;DB_CLOSE_DELAY=-1
## Orm
%test.quarkus.hibernate-orm.database.generation=drop-and-create
+2 -2
View File
@@ -15,8 +15,8 @@
<artifactId>lib</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
+1 -1
View File
@@ -10,7 +10,7 @@
<version>1.1.0-dev.1</version>
<packaging>pom</packaging>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.source>21</maven.compiler.source>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
<quarkus.platform.version>3.32.3</quarkus.platform.version>