diff --git a/.github/workflows/tag.yml b/.github/workflows/tag.yml
index fcab9d8e..fae3074b 100644
--- a/.github/workflows/tag.yml
+++ b/.github/workflows/tag.yml
@@ -16,7 +16,3 @@ jobs:
👋 @{{ author }}
Thank you for raising your pull request.
Please make sure you have followed our contributing guidelines. We will review it as soon as possible
- - name: 'Auto-assign issue'
- uses: pozil/auto-assign-issue@v1
- with:
- allowSelfAssign: true
diff --git a/src/main/java/Controllers/LoginController.java b/src/main/java/Controllers/LoginController.java
index 392f5e42..354b977c 100644
--- a/src/main/java/Controllers/LoginController.java
+++ b/src/main/java/Controllers/LoginController.java
@@ -19,35 +19,10 @@
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
+import Validation.ValidationUtils;
public class LoginController extends HttpServlet {
- /**
- * Processes requests for both HTTP GET
and POST
- * methods.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- try ( PrintWriter out = response.getWriter()) {
- /* TODO output your page here. You may use following sample code. */
- out.println("");
- out.println("");
- out.println("
GET
method.
@@ -75,25 +50,32 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String contextPath = request.getContextPath();
+ ValidationUtils valid = new ValidationUtils();
+ HttpSession session = request.getSession();
if (request.getParameter("btnSubmit") != null
&& ((String) request.getParameter("btnSubmit")).equals("Submit")) {
String email = request.getParameter("txtEmail");
String password = (String) request.getAttribute("txtPassword");
-
- Account account = new Account(email, password);
+
+ if (!valid.loginValidation(email,password)){
+ session.setAttribute("isSuccessful", false);
+ response.sendRedirect("/home#failure_login_info");
+ return;
+ }
+
+ Account loginAccount = new Account(email, password);
AccountDAO dao = new AccountDAO();
boolean success;
try {
- success = dao.login(account);
+ success = dao.login(loginAccount);
} catch (SQLException ex) {
Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
success = false;
}
- // Truy xuất URL hiện tại từ session attribute
- HttpSession session = request.getSession();
+
if (success) {
-
- account = dao.getAccount(email);
+ session.setAttribute("isSuccessful", success);
+ Account account = dao.getAccount(email);
String accountType = account.getAccountType();
boolean isRemembered = (request.getParameter("chkRememberMe") != null
&& request.getParameter("chkRememberMe").equals("remember"));
@@ -110,7 +92,9 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
response.addCookie(cUser);
Cookie cUserID = new Cookie("userID", String.valueOf(userID));
cUser.setMaxAge(cAge);
+ cUserID.setMaxAge(cAge);
cUser.setPath("/");
+ cUserID.setPath("/");
response.addCookie(cUserID);
response.sendRedirect("/");
} else if (accountType.equals("admin")) {
@@ -119,7 +103,6 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
String username = account.getUsername();
username = URLEncoder.encode(username, "UTF-8");
byte adminID = account.getAdminID();
- System.out.println("adminID " + adminID);
session.setAttribute("adminID", adminID);
Cookie adminCookie = new Cookie("admin", username);
Cookie adminIDCookie = new Cookie("adminID", Byte.toString(adminID));
@@ -139,8 +122,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
session.setAttribute("staffID", staffID);
username = URLEncoder.encode(username, "UTF-8");
Cookie staffCookie = new Cookie("staff", username);
- Cookie staffIDCookie = new Cookie("staffID", Byte.toString(staffID));
-
+ Cookie staffIDCookie = new Cookie("staffID", Byte.toString(staffID));
staffCookie.setMaxAge(cAge);
staffCookie.setPath("/");
staffIDCookie.setMaxAge(cAge);
@@ -173,7 +155,6 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
String username = account.getUsername();
session = request.getSession();
byte adminID = account.getAdminID();
- System.out.println("adminID " + adminID);
session.setAttribute("adminID", adminID);
session.setAttribute("admin", username);
response.sendRedirect("/admin");
@@ -181,7 +162,8 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
account = dao.getAccount(email);
String username = account.getUsername();
session = request.getSession();
- session.setAttribute("staffID", account.getStaffID());
+ byte staffID = account.getStaffID();
+ session.setAttribute("staffID", staffID);
session.setAttribute("staff", username);
response.sendRedirect("/staff");
} else if (accountType.equals("promotionManager")) {
@@ -195,9 +177,9 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
}
}
} else {
+ session.setAttribute("isSuccessful", success);
response.sendRedirect("/home#failure_login_info");
- }
-
+ }
}
}
}
diff --git a/src/main/java/Controllers/SignUpController.java b/src/main/java/Controllers/SignUpController.java
index f81c32e2..860d0f65 100644
--- a/src/main/java/Controllers/SignUpController.java
+++ b/src/main/java/Controllers/SignUpController.java
@@ -28,6 +28,7 @@
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
+import Validation.ValidationUtils;
public class SignUpController extends HttpServlet {
@@ -78,6 +79,13 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
HttpSession session = request.getSession();
String previousUrl = (String) session.getAttribute("previousUrl");
+ ValidationUtils valid = new ValidationUtils();
+
+ if (!valid.signUpValidation(username,email,pass)){
+ response.sendRedirect("/home#failure_register");
+ return;
+ }
+
AccountDAO accountDAO = new AccountDAO();
Account account = new Account(username, email, pass, "user");
if (accountDAO.getAccount(email) != null) {
diff --git a/src/main/java/DAOs/AccountDAO.java b/src/main/java/DAOs/AccountDAO.java
index 75452bf7..c12de6af 100644
--- a/src/main/java/DAOs/AccountDAO.java
+++ b/src/main/java/DAOs/AccountDAO.java
@@ -331,40 +331,40 @@ public Account getAccount(String email) {
if (rs.getString("account_type").equals("user")) {
// Account is of User type (no adminID)
account = new Account(
- rs.getInt("account_id"),
- rs.getInt("customer_id"),
rs.getString("account_username"),
rs.getString("account_email"),
rs.getString("account_password"),
- rs.getString("account_type"));
+ rs.getString("account_type")
+ );
+ account.setAccountID( rs.getInt("account_id"));
+ account.setCustomerID(rs.getInt("customer_id"));
} else if (rs.getString("account_type").equals("admin")) {
// Account is of Admin type (no customerID)
account = new Account(
- rs.getInt("account_id"),
- rs.getByte("admin_id"),
rs.getString("account_username"),
rs.getString("account_email"),
rs.getString("account_password"),
rs.getString("account_type"));
+ account.setAccountID(rs.getInt("account_id"));
+ account.setAdminID(rs.getByte("admin_id"));
} else if (rs.getString("account_type").equals("staff")) {
// Account is of Admin type (no customerID)
- account = new Account(
- rs.getInt("account_id"),
- rs.getByte("staff_id"),
+ account = new Account(
rs.getString("account_username"),
rs.getString("account_email"),
rs.getString("account_password"),
rs.getString("account_type"));
-
+ account.setAccountID(rs.getInt("account_id"));
+ account.setStaffID(rs.getByte("staff_id"));
} else {
// Account is of Admin type (no customerID)
account = new Account(
- rs.getInt("account_id"),
- rs.getByte("pro_id"),
rs.getString("account_username"),
rs.getString("account_email"),
rs.getString("account_password"),
rs.getString("account_type"));
+ account.setAccountID(rs.getInt("account_id"));
+ account.setProID(rs.getByte("pro_id"));
}
}
return account;
@@ -389,14 +389,33 @@ public Account getAccount(int accountID) {
rs.getString("account_email"),
rs.getString("account_password"),
rs.getString("account_type"));
+ } else if (rs.getString("account_type").equals("admin")) {
+ // Account is of Admin type (no customerID)
+ account = new Account(
+ rs.getString("account_username"),
+ rs.getString("account_email"),
+ rs.getString("account_password"),
+ rs.getString("account_type"));
+ account.setAccountID(rs.getInt("account_id"));
+ account.setAdminID(rs.getByte("admin_id"));
+ } else if (rs.getString("account_type").equals("staff")) {
+ // Account is of Admin type (no customerID)
+ account = new Account(
+ rs.getString("account_username"),
+ rs.getString("account_email"),
+ rs.getString("account_password"),
+ rs.getString("account_type"));
+ account.setAccountID(rs.getInt("account_id"));
+ account.setStaffID(rs.getByte("staff_id"));
} else {
// Account is of Admin type (no customerID)
- account = new Account(rs.getInt("account_id"),
- rs.getByte("admin_id"),
+ account = new Account(
rs.getString("account_username"),
rs.getString("account_email"),
rs.getString("account_password"),
rs.getString("account_type"));
+ account.setAccountID(rs.getInt("account_id"));
+ account.setProID(rs.getByte("pro_id"));
}
}
return account;
diff --git a/src/main/java/Validation/ValidationUtils.java b/src/main/java/Validation/ValidationUtils.java
new file mode 100644
index 00000000..2977b8a1
--- /dev/null
+++ b/src/main/java/Validation/ValidationUtils.java
@@ -0,0 +1,61 @@
+/*
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
+ */
+package Validation;
+
+public class ValidationUtils {
+
+ public static boolean loginValidation(String email, String password) {
+ if (isValidEmail(email) && isValidPassword(password)) {
+ return true;
+ }
+ return false;
+ }
+
+ public static boolean signUpValidation(String username, String email, String password) {
+ if (isValidUsername(username) && isValidEmail(email) && isValidPassword(password)) {
+ return true;
+ }
+ return false;
+ }
+
+ public static boolean isValidUsername(String username) {
+ // Check if username is not empty
+ if (username == null || username.trim().isEmpty()) {
+ return false;
+ }
+
+ // Check minimum and maximum length
+ if (username.length() < 8 || username.length() > 50) {
+ return false;
+ }
+
+ // Check if username matches the specified pattern
+ if (!username.matches("^[a-zA-Z0-9-'_]+$")) {
+ return false;
+ }
+
+ // Username is valid
+ return true;
+ }
+
+ private static boolean isValidEmail(String email) {
+ if (email == null || email.trim().isEmpty()) {
+ return false;
+ } else if (email.length() > 255) {
+ return false;
+ }
+ // Kiểm tra định dạng email sử dụng regular expression
+ // Trả về true nếu email hợp lệ, ngược lại trả về false
+ return email.matches("^[\\w.-]+@[\\w.-]+\\.[a-zA-Z]{2,}$");
+ }
+
+ private static boolean isValidPassword(String password) {
+ if (password == null || password.trim().isEmpty()) {
+ return false;
+ }
+ return true;
+ }
+}
+
diff --git a/src/main/webapp/assets/js/validateForm.js b/src/main/webapp/assets/js/validateForm.js
index 5d9c527d..45de8ee0 100644
--- a/src/main/webapp/assets/js/validateForm.js
+++ b/src/main/webapp/assets/js/validateForm.js
@@ -492,7 +492,8 @@ function validateForm() {
txtAccountUsername: {
required: true,
minlength: 8,
- maxlength: 50
+ maxlength: 50,
+ pattern: /^[a-zA-Z0-9-'_]+$/
},
txtAccountEmail: {
required: true,
@@ -514,7 +515,8 @@ function validateForm() {
txtAccountUsername: {
required: "Vui lòng nhập Tên Tài khoản Người dùng",
minlength: "Tên tài khoản mới phải có ít nhất 8 ký tự",
- maxlength: "Tên Tài khoản Người dùng không được vượt quá 50 ký tự"
+ maxlength: "Tên Tài khoản Người dùng không được vượt quá 50 ký tự",
+ pattern: "Tên Tài khoản chỉ chấp nhận chữ, số, dấu gạch ngang, gạch dưới, nháy đơn và không chứa khoảng trắng"
},
txtAccountEmail: {
required: "Vui lòng nhập Email",
diff --git a/src/test/java/Controllers/LoginControllerTest.java b/src/test/java/Controllers/LoginControllerTest.java
new file mode 100644
index 00000000..229f86e8
--- /dev/null
+++ b/src/test/java/Controllers/LoginControllerTest.java
@@ -0,0 +1,328 @@
+package Controllers;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import jakarta.servlet.RequestDispatcher;
+import jakarta.servlet.http.Cookie;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import jakarta.servlet.http.HttpSession;
+import org.junit.Assert;
+
+import static org.mockito.Mockito.*;
+
+public class LoginControllerTest {
+
+ @Mock
+ private HttpServletRequest request;
+
+ @Mock
+ private HttpServletResponse response;
+
+ @Mock
+ private RequestDispatcher requestDispatcher;
+
+ @Mock
+ private HttpSession session;
+
+ @InjectMocks
+ private LoginController loginController;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ }
+
+
+ @Test
+ public void testLoginCase1() throws Exception {
+ // Mocking request parameters
+ when(request.getParameter("btnSubmit")).thenReturn("Submit");
+ when(request.getParameter("txtEmail")).thenReturn("anhnq1130@gmail.com");
+ when(request.getAttribute("txtPassword")).thenReturn("e10adc3949ba59abbe56e057f20f883e");
+ when(request.getParameter("chkRememberMe")).thenReturn("remember");
+
+ // Mocking getSession() method
+ when(request.getSession()).thenReturn(session);
+
+ // Calling the doPost method
+ loginController.doPost(request, response);
+ verify(session, times(1)).setAttribute(eq("isSuccessful"), eq(true));
+ verify(response, times(1)).sendRedirect(eq("/"));
+
+ // Verifying that cookies are set correctly
+ verify(response, times(1)).addCookie(argThat(cookie -> {
+ if (cookie.getName().equals("user")) {
+ Assert.assertEquals("user", "quocanh123", cookie.getValue());
+ Assert.assertEquals("Expected path", "/", cookie.getPath());
+ return true;
+ }
+ return false;
+ }));
+
+ verify(response, times(1)).addCookie(argThat(cookie -> {
+ if (cookie.getName().equals("userID")) {
+ Assert.assertEquals("Expected user ID string", String.valueOf(201), cookie.getValue());
+ Assert.assertEquals("Expected path", "/", cookie.getPath());
+ return true;
+ }
+ return false;
+ }));
+ }
+
+ @Test
+ public void testLoginCase2() throws Exception {
+ when(request.getParameter("btnSubmit")).thenReturn("Submit");
+ when(request.getParameter("txtEmail")).thenReturn("anhnq1130@gmail.com");
+ when(request.getAttribute("txtPassword")).thenReturn("e10adc3949ba59abbe56e057f20f883e");
+ when(request.getParameter("chkRememberMe")).thenReturn("");
+ when(request.getSession()).thenReturn(session);
+ when(request.getRequestDispatcher(anyString())).thenReturn(requestDispatcher);
+
+ loginController.doPost(request, response);
+ verify(session, times(1)).setAttribute(eq("isSuccessful"), eq(true));
+ // Verify appropriate methods are called based on your business logic
+ verify(session, times(1)).setAttribute(eq("user"), eq("quocanh123"));
+ verify(session, times(1)).setAttribute(eq("userID"), eq(201));
+ verify(response, times(1)).sendRedirect(eq("/"));
+ }
+
+ @Test
+ public void testLoginCase3() throws Exception {
+ when(request.getParameter("btnSubmit")).thenReturn("Submit");
+ when(request.getParameter("txtEmail")).thenReturn("anhnq1130@gmail.com");
+ when(request.getAttribute("txtPassword")).thenReturn("e10adc3949ba59abbe56e057f20f883x");
+ when(request.getParameter("chkRememberMe")).thenReturn("");
+ when(request.getSession()).thenReturn(session);
+ when(request.getRequestDispatcher(anyString())).thenReturn(requestDispatcher);
+
+ loginController.doPost(request, response);
+ verify(session, times(1)).setAttribute(eq("isSuccessful"), eq(false));
+ verify(response, times(1)).sendRedirect(eq("/home#failure_login_info"));
+ }
+
+ @Test
+ public void testLoginCase4() throws Exception {
+ when(request.getParameter("btnSubmit")).thenReturn("Submit");
+ when(request.getParameter("txtEmail")).thenReturn("");
+ when(request.getAttribute("txtPassword")).thenReturn("");
+ when(request.getParameter("chkRememberMe")).thenReturn("");
+ when(request.getSession()).thenReturn(session);
+ when(request.getRequestDispatcher(anyString())).thenReturn(requestDispatcher);
+
+ loginController.doPost(request, response);
+ verify(session, times(1)).setAttribute(eq("isSuccessful"), eq(false));
+ verify(response, times(1)).sendRedirect(eq("/home#failure_login_info"));
+ }
+
+ @Test
+ public void testLoginCase5() throws Exception {
+ when(request.getParameter("btnSubmit")).thenReturn("Submit");
+ when(request.getParameter("txtEmail")).thenReturn("anhnq1130@gmail.com");
+ when(request.getAttribute("txtPassword")).thenReturn("");
+ when(request.getParameter("chkRememberMe")).thenReturn("");
+ when(request.getSession()).thenReturn(session);
+ when(request.getRequestDispatcher(anyString())).thenReturn(requestDispatcher);
+
+ loginController.doPost(request, response);
+ verify(session, times(1)).setAttribute(eq("isSuccessful"), eq(false));
+ verify(response, times(1)).sendRedirect(eq("/home#failure_login_info"));
+ }
+
+ @Test
+ public void testLoginCase6() throws Exception {
+ when(request.getParameter("btnSubmit")).thenReturn("Submit");
+ when(request.getParameter("txtEmail")).thenReturn("");
+ when(request.getAttribute("txtPassword")).thenReturn("e10adc3949ba59abbe56e057f20f883e");
+ when(request.getParameter("chkRememberMe")).thenReturn("");
+ when(request.getSession()).thenReturn(session);
+ when(request.getRequestDispatcher(anyString())).thenReturn(requestDispatcher);
+
+ loginController.doPost(request, response);
+ verify(session, times(1)).setAttribute(eq("isSuccessful"), eq(false));
+ verify(response, times(1)).sendRedirect(eq("/home#failure_login_info"));
+ }
+
+ @Test
+ public void testLoginCase7() throws Exception {
+ // Mocking request parameters
+ when(request.getParameter("btnSubmit")).thenReturn("Submit");
+ when(request.getParameter("txtEmail")).thenReturn("thanhhtce171454@fpt.edu.vn");
+ when(request.getAttribute("txtPassword")).thenReturn("e10adc3949ba59abbe56e057f20f883e");
+ when(request.getParameter("chkRememberMe")).thenReturn("remember");
+
+ // Mocking getSession() method
+ when(request.getSession()).thenReturn(session);
+
+ // Calling the doPost method
+ loginController.doPost(request, response);
+ verify(session, times(1)).setAttribute(eq("isSuccessful"), eq(true));
+ verify(response, times(1)).sendRedirect(eq("/admin"));
+
+ // Verifying that cookies are set correctly
+ verify(response, times(1)).addCookie(argThat(cookie -> {
+ if (cookie.getName().equals("admin")) {
+ Assert.assertEquals("admin", "tienthanh123", cookie.getValue());
+ Assert.assertEquals("Expected path", "/", cookie.getPath());
+ return true;
+ }
+ return false;
+ }));
+
+ verify(response, times(1)).addCookie(argThat(cookie -> {
+ if (cookie.getName().equals("adminID")) {
+ Assert.assertEquals("Expected admin ID string", String.valueOf(4), cookie.getValue());
+ Assert.assertEquals("Expected path", "/", cookie.getPath());
+ return true;
+ }
+ return false;
+ }));
+ }
+
+ @Test
+ public void testLoginCase8() throws Exception {
+ when(request.getParameter("btnSubmit")).thenReturn("Submit");
+ when(request.getParameter("txtEmail")).thenReturn("thanhhtce171454@fpt.edu.vn");
+ when(request.getAttribute("txtPassword")).thenReturn("e10adc3949ba59abbe56e057f20f883e");
+ when(request.getParameter("chkRememberMe")).thenReturn("");
+ when(request.getSession()).thenReturn(session);
+ when(request.getRequestDispatcher(anyString())).thenReturn(requestDispatcher);
+
+ loginController.doPost(request, response);
+ verify(session, times(1)).setAttribute(eq("isSuccessful"), eq(true));
+ // Verify appropriate methods are called based on your business logic
+ verify(session, times(1)).setAttribute(eq("admin"), eq("tienthanh123"));
+ verify(session, times(1)).setAttribute(eq("adminID"), eq(Byte.parseByte(String.valueOf(4))));
+ verify(response, times(1)).sendRedirect(eq("/admin"));
+ }
+
+ @Test
+ public void testLoginCase9() throws Exception {
+ // Mocking request parameters
+ when(request.getParameter("btnSubmit")).thenReturn("Submit");
+ when(request.getParameter("txtEmail")).thenReturn("teststaff1@fpt.edu.vn");
+ when(request.getAttribute("txtPassword")).thenReturn("e10adc3949ba59abbe56e057f20f883e");
+ when(request.getParameter("chkRememberMe")).thenReturn("remember");
+
+ // Mocking getSession() method
+ when(request.getSession()).thenReturn(session);
+
+ // Calling the doPost method
+ loginController.doPost(request, response);
+ verify(session, times(1)).setAttribute(eq("isSuccessful"), eq(true));
+ verify(response, times(1)).sendRedirect(eq("/staff"));
+
+ // Verifying that cookies are set correctly
+ verify(response, times(1)).addCookie(argThat(cookie -> {
+ if (cookie.getName().equals("staff")) {
+ Assert.assertEquals("staff", "testStaff1", cookie.getValue());
+ Assert.assertEquals("Expected path", "/", cookie.getPath());
+ return true;
+ }
+ return false;
+ }));
+ byte sID = 1;
+ verify(response, times(1)).addCookie(argThat(cookie -> {
+ if (cookie.getName().equals("staffID")) {
+ Assert.assertEquals("Expected staff ID string", Byte.toString(sID), cookie.getValue());
+ Assert.assertEquals("Expected path", "/", cookie.getPath());
+ return true;
+ }
+ return false;
+ }));
+ }
+
+ @Test
+ public void testLoginCase10() throws Exception {
+ when(request.getParameter("btnSubmit")).thenReturn("Submit");
+ when(request.getParameter("txtEmail")).thenReturn("teststaff1@fpt.edu.vn");
+ when(request.getAttribute("txtPassword")).thenReturn("e10adc3949ba59abbe56e057f20f883e");
+ when(request.getParameter("chkRememberMe")).thenReturn("");
+ when(request.getSession()).thenReturn(session);
+ when(request.getRequestDispatcher(anyString())).thenReturn(requestDispatcher);
+
+ loginController.doPost(request, response);
+
+ verify(session, times(1)).setAttribute(eq("isSuccessful"), eq(true));
+ // Verify appropriate methods are called based on your business logic
+ verify(session, times(1)).setAttribute(eq("staff"), eq("testStaff1"));
+ verify(session, times(1)).setAttribute(eq("staffID"), eq(Byte.parseByte(String.valueOf(1))));
+ verify(response, times(1)).sendRedirect(eq("/staff"));
+ }
+
+ @Test
+ public void testLoginCase11() throws Exception {
+ // Mocking request parameters
+ when(request.getParameter("btnSubmit")).thenReturn("Submit");
+ when(request.getParameter("txtEmail")).thenReturn("testPromotion1@fpt.edu.vn");
+ when(request.getAttribute("txtPassword")).thenReturn("e10adc3949ba59abbe56e057f20f883e");
+ when(request.getParameter("chkRememberMe")).thenReturn("remember");
+
+ // Mocking getSession() method
+ when(request.getSession()).thenReturn(session);
+
+ // Calling the doPost method
+ loginController.doPost(request, response);
+ verify(session, times(1)).setAttribute(eq("isSuccessful"), eq(true));
+ verify(response, times(1)).sendRedirect(eq("/promotionManager"));
+
+ // Verifying that cookies are set correctly
+ verify(response, times(1)).addCookie(argThat(cookie -> {
+ if (cookie.getName().equals("promotionManager")) {
+ Assert.assertEquals("promotionManager", "testPromotion1", cookie.getValue());
+ Assert.assertEquals("Expected path", "/", cookie.getPath());
+ return true;
+ }
+ return false;
+ }));
+ }
+
+ @Test
+ public void testLoginCase12() throws Exception {
+ when(request.getParameter("btnSubmit")).thenReturn("Submit");
+ when(request.getParameter("txtEmail")).thenReturn("testPromotion1@fpt.edu.vn");
+ when(request.getAttribute("txtPassword")).thenReturn("e10adc3949ba59abbe56e057f20f883e");
+ when(request.getParameter("chkRememberMe")).thenReturn("");
+ when(request.getSession()).thenReturn(session);
+ when(request.getRequestDispatcher(anyString())).thenReturn(requestDispatcher);
+
+ loginController.doPost(request, response);
+ verify(session, times(1)).setAttribute(eq("isSuccessful"), eq(true));
+ // Verify appropriate methods are called based on your business logic
+ verify(session, times(1)).setAttribute(eq("promotionManager"), eq("testPromotion1"));
+ verify(response, times(1)).sendRedirect(eq("/promotionManager"));
+ }
+
+ @Test
+ public void testLoginCase13() throws Exception {
+ when(request.getParameter("btnSubmit")).thenReturn("Submit");
+ when(request.getParameter("txtEmail")).thenReturn("abcxyz@fpt.edu.vn");
+ when(request.getAttribute("txtPassword")).thenReturn("e10adc3949ba59abbe56e057f20f883x");
+ when(request.getParameter("chkRememberMe")).thenReturn("");
+ when(request.getSession()).thenReturn(session);
+ when(request.getRequestDispatcher(anyString())).thenReturn(requestDispatcher);
+
+ loginController.doPost(request, response);
+ verify(session, times(1)).setAttribute(eq("isSuccessful"), eq(false));
+ verify(response, times(1)).sendRedirect(eq("/home#failure_login_info"));
+ }
+
+ @Test
+ public void testLoginCase14() throws Exception {
+ when(request.getParameter("btnSubmit")).thenReturn("Submit");
+ when(request.getParameter("txtEmail")).thenReturn("test1fpt.edu.vn");
+ when(request.getAttribute("txtPassword")).thenReturn("e10adc3949ba59abbe56e057f20f883x");
+ when(request.getParameter("chkRememberMe")).thenReturn("");
+ when(request.getSession()).thenReturn(session);
+ when(request.getRequestDispatcher(anyString())).thenReturn(requestDispatcher);
+
+ loginController.doPost(request, response);
+ verify(session, times(1)).setAttribute(eq("isSuccessful"), eq(false));
+ verify(response, times(1)).sendRedirect(eq("/home#failure_login_info"));
+ }
+
+}