From 47d67a8b3f5248184232244360e8b1c7b5a3522d Mon Sep 17 00:00:00 2001 From: Mirna Gama Date: Fri, 12 Jan 2024 14:40:07 -0300 Subject: [PATCH] [R9] Unit test for auth service login and register methods --- .../service/auth/AuthServiceTest.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/test/java/com/mirna/hospitalmanagementapi/unit/application/service/auth/AuthServiceTest.java diff --git a/src/test/java/com/mirna/hospitalmanagementapi/unit/application/service/auth/AuthServiceTest.java b/src/test/java/com/mirna/hospitalmanagementapi/unit/application/service/auth/AuthServiceTest.java new file mode 100644 index 0000000..26da1e8 --- /dev/null +++ b/src/test/java/com/mirna/hospitalmanagementapi/unit/application/service/auth/AuthServiceTest.java @@ -0,0 +1,63 @@ +package com.mirna.hospitalmanagementapi.unit.application.service.auth; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestMethodOrder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.core.Authentication; +import org.springframework.test.context.ActiveProfiles; + +import com.mirna.hospitalmanagementapi.application.services.auth.AuthServiceImpl; +import com.mirna.hospitalmanagementapi.domain.dtos.auth.UserDTO; +import com.mirna.hospitalmanagementapi.domain.entities.auth.User; + +/** + * + * @author Mirna Gama + * @version 1.0 + */ +@SpringBootTest +@AutoConfigureMockMvc(addFilters = false) +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@TestMethodOrder(OrderAnnotation.class) +@ActiveProfiles("test") +public class AuthServiceTest { + + @Autowired + private AuthServiceImpl authService; + + /** + * Performs user registration + */ + @Test + @Order(1) + @DisplayName("Should perform user registration") + public void testRegister() throws Exception { + UserDTO userDTO = new UserDTO("testUser2", "password"); + + User user = authService.register(userDTO); + + assertNotNull(user.getId()); + } + + /** + * Performs user login + */ + @Test + @Order(2) + @DisplayName("Should perform user login") + public void testLogin() throws Exception { + UserDTO userDTO = new UserDTO("testUser2", "password"); + + Authentication auth = authService.login(userDTO); + + assertNotNull(auth.getPrincipal()); + } +}