diff options
Diffstat (limited to 'ufund-api/src/test')
3 files changed, 257 insertions, 7 deletions
diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java index e2c959a..efe639e 100644 --- a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java +++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java @@ -5,11 +5,10 @@ import java.util.Map; import static java.util.Map.entry; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -85,11 +84,6 @@ public class UserControllerTest { assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); } - /***************************************************************** - * The following tests will fail until all userController methods - * are implemented. - ****************************************************************/ - @Test public void testCreateUser() throws IOException, DuplicateKeyException { // createUser may throw IOException // Setup 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)); + + } + +} diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java new file mode 100644 index 0000000..0a0cb71 --- /dev/null +++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java @@ -0,0 +1,126 @@ +package com.ufund.api.ufundapi.service; + +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.ufund.api.ufundapi.DuplicateKeyException; +import com.ufund.api.ufundapi.model.User; +import com.ufund.api.ufundapi.persistence.UserDAO; + +public class UserServiceTest { + + private UserService userService; + private UserDAO mockUserDAO; + + + @BeforeEach + public void setupUserService() { + mockUserDAO = mock(UserDAO.class); + userService = new UserService(mockUserDAO); + } + + @Test + public void testCreateUser() throws IOException, DuplicateKeyException { + // Setup + String username = "Jelly"; + String password = "Fish"; + User user = User.create(username, password); + + // Mock + when(mockUserDAO.getUser(username)).thenReturn(null); + when(mockUserDAO.addUser(any())).thenReturn(user); + + // Invoke + + // Analyze + assertEquals(user, userService.createUser(username, password)); + } + + @Test + public void testCreateUserDuplicate() throws IOException, DuplicateKeyException { + // Setup + String username = "Jelly"; + String password = "Fish"; + User user = User.create(username, password); + + // Mock + when(mockUserDAO.getUser(username)).thenReturn(User.create("Phil", "Phil")); + when(mockUserDAO.addUser(any())).thenReturn(user); + + // Analyze + assertThrows(DuplicateKeyException.class, () -> userService.createUser(username, password)); + } + + @Test + public void testGetUser() throws IOException, DuplicateKeyException { + // Setup + String username = "Jelly"; + String password = "Fish"; + User user = User.create(username, password); + + // Mock + when(mockUserDAO.getUser(username)).thenReturn(user); + + // Analyze + assertEquals(user, userService.getUser(username)); + } + + @Test + public void testUpdateUser() throws IOException, DuplicateKeyException { + // Setup + String username = "Jelly"; + String password = "Fish"; + User oldUser = User.create(username, password); + + String newUsername = "Jelly"; + String newPassword = "Dog"; + User newUser = User.create(newUsername, newPassword); + + // Mock + when(mockUserDAO.updateUser(newUser)).thenReturn(newUser); + + // Analyze + assertEquals(newUser, userService.updateUser(newUser, oldUser.getUsername())); + } + + @Test + public void testUpdateUserDifferentUsernames() throws IOException, DuplicateKeyException { + // Setup + String username = "Jelly"; + String password = "Fish"; + User oldUser = User.create(username, password); + + String newUsername = "Cat"; + String newPassword = "Fish"; + User newUser = User.create(newUsername, newPassword); + + // Mock + when(mockUserDAO.updateUser(newUser)).thenReturn(newUser); + + // Analyze + assertThrows(IllegalArgumentException.class, () -> userService.updateUser(newUser, oldUser.getUsername())); + } + + @Test + public void testDeleteUser() throws IOException, DuplicateKeyException { + // Setup + String username = "Jelly"; + String password = "Fish"; + User user = User.create(username, password); + + // Mock + when(mockUserDAO.deleteUser(username)).thenReturn(true); + + // Analyze + assertTrue(userService.deleteUser(username)); + } + +} |