diff options
author | Gunther6070 <haydenhartman10@yahoo.com> | 2025-03-17 14:16:42 -0400 |
---|---|---|
committer | Gunther6070 <haydenhartman10@yahoo.com> | 2025-03-17 14:16:42 -0400 |
commit | 1b617f8c7a1b596c8725a0e1e2791902190f415b (patch) | |
tree | 212e9df145c34f97903b318a83e79134b667a84b | |
parent | 7d5df1efe71963f100c445f3cd0da1546e7ffb6e (diff) | |
download | JellySolutions-1b617f8c7a1b596c8725a0e1e2791902190f415b.tar.gz JellySolutions-1b617f8c7a1b596c8725a0e1e2791902190f415b.tar.bz2 JellySolutions-1b617f8c7a1b596c8725a0e1e2791902190f415b.zip |
Created tests for authService
-rw-r--r-- | ufund-api/src/test/java/com/ufund/api/ufundapi/service/AuthServiceTest.java | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/service/AuthServiceTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/AuthServiceTest.java new file mode 100644 index 0000000..68217bf --- /dev/null +++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/AuthServiceTest.java @@ -0,0 +1,130 @@ +package com.ufund.api.ufundapi.service; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.IOException; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.ufund.api.ufundapi.DuplicateKeyException; +import com.ufund.api.ufundapi.model.User; +import com.ufund.api.ufundapi.model.UserAuth; +import com.ufund.api.ufundapi.persistence.UserAuthDAO; + +public class AuthServiceTest { + + private UserAuthDAO mockAuthDAO; + private UserService mockUserService; + private AuthService authService; + + @BeforeEach + public void setupAuthService() { + mockAuthDAO = mock(UserAuthDAO.class); + mockUserService = mock(UserService.class); + authService = new AuthService(mockAuthDAO, mockUserService); + + } + + @Test + public void testAuthenticate() throws IOException { + // Setup + String username = "Fish"; + String key = UserAuth.generate(username).getKey(); + + // Mock + when(mockAuthDAO.getUserAuth(key)).thenReturn(new UserAuth(key, username, null)); + + // Analyze + assertDoesNotThrow(() -> authService.authenticate(username, key)); + + } + + @Test + public void testAuthenticateMismatchName() throws IOException { + // Setup + String username = "Fish"; + String key = UserAuth.generate(username).getKey(); + + // Mock + when(mockAuthDAO.getUserAuth(key)).thenReturn(new UserAuth(key, "EvilFish", null)); + + // Analyze + assertThrows(IllegalAccessException.class, () -> authService.authenticate(username, key)); + + } + + @Test + public void testAuthenticateMissingUserAuth() throws IOException { + // Setup + String username = "Fish"; + String key = UserAuth.generate(username).getKey(); + + // Mock + when(mockAuthDAO.getUserAuth(key)).thenReturn(null); + + // Analyze + assertThrows(IllegalAccessException.class, () -> authService.authenticate(username, key)); + + } + + @Test + public void testLogin() throws IOException, DuplicateKeyException, IllegalAccessException { + // Setup + String username = "Fish"; + String password = "Chips"; + User user = User.create(username, password); + + // Mock + when(mockUserService.getUser(username)).thenReturn(user); + + + // Analyze + assertDoesNotThrow(() -> authService.login(username, password)); + } + + @Test + public void testLoginNullUser() throws IOException, DuplicateKeyException, IllegalAccessException { + // Setup + String username = "Fish"; + String password = "Chips"; + User user = User.create(username, password); + + // Mock + when(mockUserService.getUser(username)).thenReturn(null); + + // Analyze + assertThrows(IllegalAccessException.class, () -> authService.login(username, password)); + } + + @Test + public void testLoginMismatchPasswords() throws IOException, DuplicateKeyException, IllegalAccessException { + // Setup + String username = "Fish"; + String password = "Chips"; + User user = User.create(username, password); + + // Mock + when(mockUserService.getUser(username)).thenReturn(User.create(username, "fries")); + + // Analyze + assertThrows(IllegalAccessException.class, () -> authService.login(username, password)); + } + + @Test + public void testLogout() throws IOException, DuplicateKeyException, IllegalAccessException { + // Setup + String username = "Fish"; + String password = "Chips"; + String key = UserAuth.generate(username).getKey(); + User user = User.create(username, password); + + // Analyze + assertDoesNotThrow(() -> authService.logout(key)); + + } + +} |