From c02c47efcb00782feb1461534923023a711d4f15 Mon Sep 17 00:00:00 2001 From: sowgro Date: Sun, 2 Mar 2025 11:22:48 -0500 Subject: First attempt at an authentication system. --- .../java/com/ufund/api/ufundapi/controller/UserControllerTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java') 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 681f47c..496c68c 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 @@ -84,7 +84,7 @@ public class UserControllerTest { User user = new User(username); // when createUser is called, return true simulating successful // creation and save - when(mockUserDAO.createUser(user)).thenReturn(user); + when(mockUserDAO.addUser(user)).thenReturn(user); // Invoke ResponseEntity response = userController.createUser(user); @@ -101,7 +101,7 @@ public class UserControllerTest { User user = new User(username); // when createUser is called, return false simulating failed // creation and save - when(mockUserDAO.createUser(user)).thenReturn(null); + when(mockUserDAO.addUser(user)).thenReturn(null); // Invoke ResponseEntity response = userController.createUser(user); @@ -117,7 +117,7 @@ public class UserControllerTest { User user = new User(username); // When createUser is called on the Mock User DAO, throw an IOException - doThrow(new IOException()).when(mockUserDAO).createUser(user); + doThrow(new IOException()).when(mockUserDAO).addUser(user); // Invoke ResponseEntity response = userController.createUser(user); -- cgit v1.2.3 From b3487d216b576a2b2614674eae0a1be139eb1ea3 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Thu, 6 Mar 2025 08:16:42 -0500 Subject: Fixed some UserController tests --- .../ufundapi/controller/UserControllerTest.java | 24 ++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java') 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 496c68c..d189836 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 @@ -1,6 +1,7 @@ package com.ufund.api.ufundapi.controller; import java.io.IOException; +import java.util.HashMap; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.BeforeEach; @@ -13,18 +14,21 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import com.ufund.api.ufundapi.model.User; +import com.ufund.api.ufundapi.model.UserAuth; +import com.ufund.api.ufundapi.persistence.UserAuthFIleDAO; import com.ufund.api.ufundapi.persistence.UserFileDAO; @Tag("Controller-tier") public class UserControllerTest { private UserController userController; private UserFileDAO mockUserDAO; + private UserAuthFIleDAO mockAuthUserDAO; @BeforeEach public void setupUserController() { mockUserDAO = mock(UserFileDAO.class); - userController = new UserController(mockUserDAO); - + mockAuthUserDAO = mock(UserAuthFIleDAO.class); + userController = new UserController(mockUserDAO, mockAuthUserDAO); } @Test @@ -32,11 +36,13 @@ public class UserControllerTest { // Setup String username = "Test"; User user = new User(username); + String key = UserAuth.generate(username).getKey(); // When the same id is passed in, our mock User DAO will return the User object when(mockUserDAO.getUser(username)).thenReturn(user); + // Invoke - ResponseEntity response = userController.getUser(username); + ResponseEntity response = userController.getUser(username, key); // Analyze assertEquals(HttpStatus.OK, response.getStatusCode()); @@ -47,12 +53,14 @@ public class UserControllerTest { public void testGetUserNotFound() throws Exception { // createUser may throw IOException // Setup String username = "Test"; + String key = UserAuth.generate(username).getKey(); // When the same id is passed in, our mock User DAO will return null, simulating // no User found when(mockUserDAO.getUser(username)).thenReturn(null); + // Invoke - ResponseEntity response = userController.getUser(username); + ResponseEntity response = userController.getUser(username, key); // Analyze assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); @@ -62,11 +70,12 @@ public class UserControllerTest { public void testGetUserHandleException() throws Exception { // createUser may throw IOException // Setup String username = "Test"; + String key = UserAuth.generate(username).getKey(); // When getUser is called on the Mock User DAO, throw an IOException doThrow(new IOException()).when(mockUserDAO).getUser(username); // Invoke - ResponseEntity response = userController.getUser(username); + ResponseEntity response = userController.getUser(username, key); // Analyze assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); @@ -82,12 +91,15 @@ public class UserControllerTest { // Setup String username = "Test"; User user = new User(username); + String key = UserAuth.generate(username).getKey(); // when createUser is called, return true simulating successful // creation and save when(mockUserDAO.addUser(user)).thenReturn(user); + + // Invoke - ResponseEntity response = userController.createUser(user); + ResponseEntity response = userController.createUser(params); // Analyze assertEquals(HttpStatus.CREATED, response.getStatusCode()); -- cgit v1.2.3 From 7ed118ef9af842d7a376f53d1463529db3c796d8 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Thu, 13 Mar 2025 20:38:48 -0400 Subject: Fixed broken tests --- .../ufundapi/controller/UserControllerTest.java | 93 +++++++++++++--------- 1 file changed, 57 insertions(+), 36 deletions(-) (limited to 'ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java') 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 d189836..11fa6a4 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 @@ -1,7 +1,8 @@ package com.ufund.api.ufundapi.controller; import java.io.IOException; -import java.util.HashMap; +import java.util.Map; +import static java.util.Map.entry; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.BeforeEach; @@ -13,22 +14,23 @@ import static org.mockito.Mockito.when; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +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.UserAuthFIleDAO; -import com.ufund.api.ufundapi.persistence.UserFileDAO; +import com.ufund.api.ufundapi.service.AuthService; +import com.ufund.api.ufundapi.service.UserService; @Tag("Controller-tier") public class UserControllerTest { private UserController userController; - private UserFileDAO mockUserDAO; - private UserAuthFIleDAO mockAuthUserDAO; + private UserService mockUserService; + private AuthService mockAuthService; @BeforeEach public void setupUserController() { - mockUserDAO = mock(UserFileDAO.class); - mockAuthUserDAO = mock(UserAuthFIleDAO.class); - userController = new UserController(mockUserDAO, mockAuthUserDAO); + mockUserService = mock(UserService.class); + mockAuthService = mock(AuthService.class); + userController = new UserController(mockUserService, mockAuthService); } @Test @@ -38,7 +40,7 @@ public class UserControllerTest { User user = new User(username); String key = UserAuth.generate(username).getKey(); // When the same id is passed in, our mock User DAO will return the User object - when(mockUserDAO.getUser(username)).thenReturn(user); + when(mockUserService.getUser(username)).thenReturn(user); // Invoke @@ -56,7 +58,7 @@ public class UserControllerTest { String key = UserAuth.generate(username).getKey(); // When the same id is passed in, our mock User DAO will return null, simulating // no User found - when(mockUserDAO.getUser(username)).thenReturn(null); + when(mockUserService.getUser(username)).thenReturn(null); // Invoke @@ -72,7 +74,7 @@ public class UserControllerTest { String username = "Test"; String key = UserAuth.generate(username).getKey(); // When getUser is called on the Mock User DAO, throw an IOException - doThrow(new IOException()).when(mockUserDAO).getUser(username); + doThrow(new IOException()).when(mockUserService).getUser(username); // Invoke ResponseEntity response = userController.getUser(username, key); @@ -87,19 +89,22 @@ public class UserControllerTest { ****************************************************************/ @Test - public void testCreateUser() throws IOException { // createUser may throw IOException + public void testCreateUser() throws IOException, DuplicateKeyException { // createUser may throw IOException // Setup String username = "Test"; + String password = "Pass"; User user = new User(username); - String key = UserAuth.generate(username).getKey(); // when createUser is called, return true simulating successful // creation and save - when(mockUserDAO.addUser(user)).thenReturn(user); + when(mockUserService.createUser(username, password)).thenReturn(user); - + Map userMap = Map.ofEntries( + entry("username", "Test"), + entry("password", "Pass") + ); // Invoke - ResponseEntity response = userController.createUser(params); + ResponseEntity response = userController.createUser(userMap); // Analyze assertEquals(HttpStatus.CREATED, response.getStatusCode()); @@ -107,32 +112,42 @@ public class UserControllerTest { } @Test - public void testCreateUserFailed() throws IOException { // createUser may throw IOException + public void testCreateUserFailed() throws IOException, DuplicateKeyException { // createUser may throw IOException // Setup String username = "Test"; - User user = new User(username); + String password = "Pass"; // when createUser is called, return false simulating failed // creation and save - when(mockUserDAO.addUser(user)).thenReturn(null); + when(mockUserService.createUser(username, password)).thenReturn(null); + + Map userMap = Map.ofEntries( + entry("username", "Test"), + entry("password", "Pass") + ); // Invoke - ResponseEntity response = userController.createUser(user); + ResponseEntity response = userController.createUser(userMap); // Analyze assertEquals(HttpStatus.CONFLICT, response.getStatusCode()); } @Test - public void testCreateUserHandleException() throws IOException { // createUser may throw IOException + public void testCreateUserHandleException() throws IOException, DuplicateKeyException { // createUser may throw IOException // Setup String username = "Test"; - User user = new User(username); + String password = "Pass"; // When createUser is called on the Mock User DAO, throw an IOException - doThrow(new IOException()).when(mockUserDAO).addUser(user); + doThrow(new IOException()).when(mockUserService).createUser(username, password); + + Map userMap = Map.ofEntries( + entry("username", "Test"), + entry("password", "Pass") + ); // Invoke - ResponseEntity response = userController.createUser(user); + ResponseEntity response = userController.createUser(userMap); // Analyze assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); @@ -143,12 +158,13 @@ public class UserControllerTest { // Setup String username = "Test"; User user = new User("Bob"); + String key = UserAuth.generate(username).getKey(); // when updateUser is called, return true simulating successful // update and save - when(mockUserDAO.updateUser(user, username)).thenReturn(user); + when(mockUserService.updateUser(user, username)).thenReturn(user); // Invoke - ResponseEntity response = userController.updateUser(user, username); + ResponseEntity response = userController.updateUser(user, username, key); // Analyze assertEquals(HttpStatus.OK, response.getStatusCode()); @@ -160,12 +176,13 @@ public class UserControllerTest { // Setup String username = "Test"; User user = new User("Bob"); + String key = UserAuth.generate(username).getKey(); // when updateUser is called, return true simulating successful // update and save - when(mockUserDAO.updateUser(user, username)).thenReturn(null); + when(mockUserService.updateUser(user, username)).thenReturn(null); // Invoke - ResponseEntity response = userController.updateUser(user, username); + ResponseEntity response = userController.updateUser(user, username, key); // Analyze assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); @@ -176,11 +193,12 @@ public class UserControllerTest { // Setup String username = "Test"; User user = new User("Bob"); + String key = UserAuth.generate(username).getKey(); // When updateUser is called on the Mock User DAO, throw an IOException - doThrow(new IOException()).when(mockUserDAO).updateUser(user, username); + doThrow(new IOException()).when(mockUserService).updateUser(user, username); // Invoke - ResponseEntity response = userController.updateUser(user, username); + ResponseEntity response = userController.updateUser(user, username, key); // Analyze assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); @@ -190,11 +208,12 @@ public class UserControllerTest { public void testDeleteUser() throws IOException { // deleteUser may throw IOException // Setup String username = "Test"; + String key = UserAuth.generate(username).getKey(); // when deleteUser is called return true, simulating successful deletion - when(mockUserDAO.deleteUser(username)).thenReturn(true); + when(mockUserService.deleteUser(username)).thenReturn(true); // Invoke - ResponseEntity response = userController.deleteUser(username); + ResponseEntity response = userController.deleteUser(username, key); // Analyze assertEquals(HttpStatus.OK, response.getStatusCode()); @@ -204,11 +223,12 @@ public class UserControllerTest { public void testDeleteUserNotFound() throws IOException { // deleteUser may throw IOException // Setup String username = "Test"; + String key = UserAuth.generate(username).getKey(); // when deleteUser is called return false, simulating failed deletion - when(mockUserDAO.deleteUser(username)).thenReturn(false); + when(mockUserService.deleteUser(username)).thenReturn(false); // Invoke - ResponseEntity response = userController.deleteUser(username); + ResponseEntity response = userController.deleteUser(username, key); // Analyze assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); @@ -218,11 +238,12 @@ public class UserControllerTest { public void testDeleteUserHandleException() throws IOException { // deleteUser may throw IOException // Setup String username = "Test"; + String key = UserAuth.generate(username).getKey(); // When deleteUser is called on the Mock User DAO, throw an IOException - doThrow(new IOException()).when(mockUserDAO).deleteUser(username); + doThrow(new IOException()).when(mockUserService).deleteUser(username); // Invoke - ResponseEntity response = userController.deleteUser(username); + ResponseEntity response = userController.deleteUser(username, key); // Analyze assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); -- cgit v1.2.3 From 1bf30a02d52bb4f9503e3a5ad9cc52638196a8c4 Mon Sep 17 00:00:00 2001 From: sowgro Date: Thu, 13 Mar 2025 20:56:21 -0400 Subject: Fix a failing testGetUser() test --- .../com/ufund/api/ufundapi/controller/UserControllerTest.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java') 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 11fa6a4..e2c959a 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 @@ -8,6 +8,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; 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; @@ -24,12 +26,11 @@ import com.ufund.api.ufundapi.service.UserService; public class UserControllerTest { private UserController userController; private UserService mockUserService; - private AuthService mockAuthService; @BeforeEach public void setupUserController() { mockUserService = mock(UserService.class); - mockAuthService = mock(AuthService.class); + AuthService mockAuthService = mock(AuthService.class); userController = new UserController(mockUserService, mockAuthService); } @@ -48,7 +49,8 @@ public class UserControllerTest { // Analyze assertEquals(HttpStatus.OK, response.getStatusCode()); - assertEquals(user, response.getBody()); + assertNotNull(response.getBody()); + assertEquals(user.getUsername(), response.getBody().getUsername()); } @Test -- cgit v1.2.3 From 7acedfb955d800c806b8b59dc7261c53a6ec15d9 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Mon, 17 Mar 2025 14:20:43 -0400 Subject: Modified imports --- .../com/ufund/api/ufundapi/controller/UserControllerTest.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java') 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 -- cgit v1.2.3 From 251f30c402700169213ed4560a7797a785a50e78 Mon Sep 17 00:00:00 2001 From: sowgro Date: Mon, 17 Mar 2025 16:08:11 -0400 Subject: Refactoring --- .../ufund/api/ufundapi/controller/UserControllerTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java') 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 efe639e..3f110cb 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 @@ -37,8 +37,8 @@ public class UserControllerTest { public void testGetUser() throws IOException { // getUser may throw IOException // Setup String username = "Test"; - User user = new User(username); - String key = UserAuth.generate(username).getKey(); + User user = User.create(username, "pass"); + String key = UserAuth.generate(username).getKey( ); // When the same id is passed in, our mock User DAO will return the User object when(mockUserService.getUser(username)).thenReturn(user); @@ -89,7 +89,7 @@ public class UserControllerTest { // Setup String username = "Test"; String password = "Pass"; - User user = new User(username); + User user = User.create(username, "pass"); // when createUser is called, return true simulating successful // creation and save when(mockUserService.createUser(username, password)).thenReturn(user); @@ -153,7 +153,7 @@ public class UserControllerTest { public void testUpdateUser() throws IOException { // updateUser may throw IOException // Setup String username = "Test"; - User user = new User("Bob"); + User user = User.create("Bob", "pass"); String key = UserAuth.generate(username).getKey(); // when updateUser is called, return true simulating successful // update and save @@ -171,7 +171,7 @@ public class UserControllerTest { public void testUpdateUserFailed() throws IOException { // updateUser may throw IOException // Setup String username = "Test"; - User user = new User("Bob"); + User user = User.create("Bob", "pass"); String key = UserAuth.generate(username).getKey(); // when updateUser is called, return true simulating successful // update and save @@ -188,7 +188,7 @@ public class UserControllerTest { public void testUpdateUserHandleException() throws IOException { // updateUser may throw IOException // Setup String username = "Test"; - User user = new User("Bob"); + User user = User.create("Bob", "pass"); String key = UserAuth.generate(username).getKey(); // When updateUser is called on the Mock User DAO, throw an IOException doThrow(new IOException()).when(mockUserService).updateUser(user, username); -- cgit v1.2.3 From 60dd2db634de6146671be9546fb3e4bdf6d9b7d9 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Mon, 17 Mar 2025 16:11:26 -0400 Subject: Added additional tests to further coverage --- .../ufundapi/controller/UserControllerTest.java | 116 +++++++++++++++++---- 1 file changed, 97 insertions(+), 19 deletions(-) (limited to 'ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java') 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 efe639e..a25ec8a 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 @@ -1,6 +1,7 @@ package com.ufund.api.ufundapi.controller; import java.io.IOException; +import java.security.InvalidParameterException; import java.util.Map; import static java.util.Map.entry; @@ -24,13 +25,19 @@ import com.ufund.api.ufundapi.service.UserService; @Tag("Controller-tier") public class UserControllerTest { private UserController userController; + private AuthService mockAuthService; private UserService mockUserService; + private Map userMap; @BeforeEach public void setupUserController() { mockUserService = mock(UserService.class); - AuthService mockAuthService = mock(AuthService.class); + mockAuthService = mock(AuthService.class); userController = new UserController(mockUserService, mockAuthService); + userMap = Map.ofEntries( + entry("username", "Test"), + entry("password", "Pass") + ); } @Test @@ -39,7 +46,7 @@ public class UserControllerTest { String username = "Test"; User user = new User(username); String key = UserAuth.generate(username).getKey(); - // When the same id is passed in, our mock User DAO will return the User object + // When the same id is passed in, our mock User service will return the User object when(mockUserService.getUser(username)).thenReturn(user); @@ -57,7 +64,7 @@ public class UserControllerTest { // Setup String username = "Test"; String key = UserAuth.generate(username).getKey(); - // When the same id is passed in, our mock User DAO will return null, simulating + // When the same id is passed in, our mock User service will return null, simulating // no User found when(mockUserService.getUser(username)).thenReturn(null); @@ -69,12 +76,28 @@ public class UserControllerTest { assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); } + @Test + public void testGetUserUnauthorized() throws Exception { // createUser may throw IOException + // Setup + String username = "Test"; + String key = UserAuth.generate(username).getKey(); + // When getUser is called on the Mock User service, throw an IOException + // doThrow(new IllegalAccessException()).when(mockUserService).getUser(username); + doThrow(new IllegalAccessException()).when(mockAuthService).authenticate(username, key); + + // Invoke + ResponseEntity response = userController.getUser(username, key); + + // Analyze + assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); + } + @Test public void testGetUserHandleException() throws Exception { // createUser may throw IOException // Setup String username = "Test"; String key = UserAuth.generate(username).getKey(); - // When getUser is called on the Mock User DAO, throw an IOException + // When getUser is called on the Mock User service, throw an IOException doThrow(new IOException()).when(mockUserService).getUser(username); // Invoke @@ -94,10 +117,7 @@ public class UserControllerTest { // creation and save when(mockUserService.createUser(username, password)).thenReturn(user); - Map userMap = Map.ofEntries( - entry("username", "Test"), - entry("password", "Pass") - ); + // Invoke ResponseEntity response = userController.createUser(userMap); @@ -116,10 +136,23 @@ public class UserControllerTest { // creation and save when(mockUserService.createUser(username, password)).thenReturn(null); - Map userMap = Map.ofEntries( - entry("username", "Test"), - entry("password", "Pass") - ); + + + // Invoke + ResponseEntity response = userController.createUser(userMap); + + // Analyze + assertEquals(HttpStatus.CONFLICT, response.getStatusCode()); + } + + @Test + public void testCreateUserDuplicate() throws IOException, DuplicateKeyException { // createUser may throw IOException + // Setup + String username = "Test"; + String password = "Pass"; + // when createUser is called, return false simulating failed + // creation and save + when(mockUserService.createUser(username, password)).thenThrow(DuplicateKeyException.class); // Invoke ResponseEntity response = userController.createUser(userMap); @@ -134,13 +167,10 @@ public class UserControllerTest { String username = "Test"; String password = "Pass"; - // When createUser is called on the Mock User DAO, throw an IOException + // When createUser is called on the Mock User service, throw an IOException doThrow(new IOException()).when(mockUserService).createUser(username, password); - Map userMap = Map.ofEntries( - entry("username", "Test"), - entry("password", "Pass") - ); + // Invoke ResponseEntity response = userController.createUser(userMap); @@ -184,13 +214,29 @@ public class UserControllerTest { assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); } + @Test + public void testUpdateUserInvalidParameter() throws IOException { // updateUser may throw IOException + // Setup + String username = "Test"; + User user = new User("Bob"); + String key = UserAuth.generate(username).getKey(); + // When updateUser is called on the Mock User service, throw a Invalid Parameter exception + doThrow(new InvalidParameterException()).when(mockUserService).updateUser(user, username); + + // Invoke + ResponseEntity response = userController.updateUser(user, username, key); + + // Analyze + assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); + } + @Test public void testUpdateUserHandleException() throws IOException { // updateUser may throw IOException // Setup String username = "Test"; User user = new User("Bob"); String key = UserAuth.generate(username).getKey(); - // When updateUser is called on the Mock User DAO, throw an IOException + // When updateUser is called on the Mock User service, throw an IOException doThrow(new IOException()).when(mockUserService).updateUser(user, username); // Invoke @@ -200,6 +246,23 @@ public class UserControllerTest { assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); } + @Test + public void testUpdateUserUnauthorized() throws IOException, IllegalAccessException { // updateUser may throw IOException + // Setup + String username = "Test"; + User user = new User("Bob"); + String key = UserAuth.generate(username).getKey(); + // When updateUser is called on the Mock User service, throw a Invalid Parameter exception + // exception + doThrow(new IllegalAccessException()).when(mockAuthService).authenticate(username, key); + + // Invoke + ResponseEntity response = userController.updateUser(user, username, key); + + // Analyze + assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); + } + @Test public void testDeleteUser() throws IOException { // deleteUser may throw IOException // Setup @@ -235,7 +298,7 @@ public class UserControllerTest { // Setup String username = "Test"; String key = UserAuth.generate(username).getKey(); - // When deleteUser is called on the Mock User DAO, throw an IOException + // When deleteUser is called on the Mock User service, throw an IOException doThrow(new IOException()).when(mockUserService).deleteUser(username); // Invoke @@ -245,4 +308,19 @@ public class UserControllerTest { assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); } + @Test + public void testDeleteUserUnauthorized() throws IOException, IllegalAccessException { // deleteUser may throw IOException + // Setup + String username = "Test"; + String key = UserAuth.generate(username).getKey(); + // When deleteUser is called on the Mock User service, throw an IOException + doThrow(new IllegalAccessException()).when(mockAuthService).authenticate(username, key); + + // Invoke + ResponseEntity response = userController.deleteUser(username, key); + + // Analyze + assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); + } + } -- cgit v1.2.3 From 2bab07c55153f33f3321a487ffcda9f5f27b1788 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Mon, 17 Mar 2025 16:14:30 -0400 Subject: Modified user declarations in tests --- .../com/ufund/api/ufundapi/controller/UserControllerTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java') 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 7bedd3e..b6367ad 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 @@ -183,7 +183,7 @@ public class UserControllerTest { public void testUpdateUser() throws IOException { // updateUser may throw IOException // Setup String username = "Test"; - User user = User.create("Bob", "pass"); + User user = User.create(username, "pass"); String key = UserAuth.generate(username).getKey(); // when updateUser is called, return true simulating successful // update and save @@ -201,7 +201,7 @@ public class UserControllerTest { public void testUpdateUserFailed() throws IOException { // updateUser may throw IOException // Setup String username = "Test"; - User user = User.create("Bob", "pass"); + User user = User.create(username, "pass"); String key = UserAuth.generate(username).getKey(); // when updateUser is called, return true simulating successful // update and save @@ -218,7 +218,7 @@ public class UserControllerTest { public void testUpdateUserInvalidParameter() throws IOException { // updateUser may throw IOException // Setup String username = "Test"; - User user = User.create("Bob", "pass"); + User user = User.create(username, "pass"); String key = UserAuth.generate(username).getKey(); // When updateUser is called on the Mock User DAO, throw an IOException doThrow(new IOException()).when(mockUserService).updateUser(user, username); @@ -234,7 +234,7 @@ public class UserControllerTest { public void testUpdateUserUnauthorized() throws IOException, IllegalAccessException { // updateUser may throw IOException // Setup String username = "Test"; - User user = new User("Bob"); + User user = User.create(username, "pass"); String key = UserAuth.generate(username).getKey(); // When updateUser is called on the Mock User service, throw a Invalid Parameter exception // exception -- cgit v1.2.3