diff options
Diffstat (limited to 'ufund-api/src/test/java')
4 files changed, 28 insertions, 21 deletions
diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java index 6ef6710..89697bf 100644 --- a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java +++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java @@ -7,10 +7,11 @@ import static java.util.Map.entry;  import static org.junit.jupiter.api.Assertions.assertArrayEquals;  import static org.junit.jupiter.api.Assertions.assertEquals;  import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.Mockito.*; + +import com.ufund.api.ufundapi.service.AuthService;  import org.junit.jupiter.api.BeforeEach;  import org.junit.jupiter.api.Test; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when;  import org.springframework.http.HttpStatus;  import com.ufund.api.ufundapi.DuplicateKeyException; @@ -21,11 +22,17 @@ import com.ufund.api.ufundapi.service.CupboardService;  public class CupboardControllerTest {      private CupboardController cupboardController;      private CupboardService mockCupboardService; +    private final String key = "dummyKey";      @BeforeEach      public void setupCupboardDAO() { +        AuthService mockAuthService = mock(AuthService.class);          mockCupboardService = mock(CupboardService.class); -        cupboardController = new CupboardController(mockCupboardService); +        cupboardController = new CupboardController(mockCupboardService, mockAuthService); + +        try { +            doThrow().when(mockAuthService).keyHasAccessToCupboard(key); +        } catch (Exception ignored) {}      }      @Test @@ -43,7 +50,7 @@ public class CupboardControllerTest {                  entry("type", "MONETARY")          ); -        var res = cupboardController.createNeed(needMap); +        var res = cupboardController.createNeed(needMap, key);          assertEquals(HttpStatus.OK, res.getStatusCode());          assertEquals(need, res.getBody()); @@ -58,7 +65,7 @@ public class CupboardControllerTest {                  entry("maxGoal", -100.0),                  entry("type", "MONETARY")); -        var res = cupboardController.createNeed(needMap); +        var res = cupboardController.createNeed(needMap, key);          assertEquals(HttpStatus.BAD_REQUEST, res.getStatusCode());      } @@ -72,7 +79,7 @@ public class CupboardControllerTest {                  entry("maxGoal", 100.0),                  entry("type", "MONETARY")); -        var res = cupboardController.createNeed(needMap); +        var res = cupboardController.createNeed(needMap, key);          assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode());      } @@ -174,7 +181,7 @@ public class CupboardControllerTest {          var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);          when(mockCupboardService.updateNeed(need, 1)).thenReturn(need); -        var res = cupboardController.updateNeed(need, 1); +        var res = cupboardController.updateNeed(need, 1, key);          assertEquals(HttpStatus.OK, res.getStatusCode());          assertEquals(need, res.getBody()); @@ -185,7 +192,7 @@ public class CupboardControllerTest {          var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);          when(mockCupboardService.updateNeed(need, 1)).thenThrow(new IOException()); -        var res = cupboardController.updateNeed(need, 1); +        var res = cupboardController.updateNeed(need, 1, key);          assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode());      } @@ -196,7 +203,7 @@ public class CupboardControllerTest {          when(mockCupboardService.getNeed(1)).thenReturn(need);          when(mockCupboardService.deleteNeed(1)).thenReturn(true); -        var res = cupboardController.deleteNeed(1); +        var res = cupboardController.deleteNeed(1, key);          assertEquals(HttpStatus.OK, res.getStatusCode());      } @@ -206,7 +213,7 @@ public class CupboardControllerTest {          when(mockCupboardService.getNeed(1)).thenReturn(null);          when(mockCupboardService.deleteNeed(1)).thenReturn(false); -        var res = cupboardController.deleteNeed(1); +        var res = cupboardController.deleteNeed(1, key);          assertEquals(HttpStatus.NOT_FOUND, res.getStatusCode());      } @@ -217,7 +224,7 @@ public class CupboardControllerTest {          when(mockCupboardService.getNeed(1)).thenReturn(need);          when(mockCupboardService.deleteNeed(1)).thenThrow(new IOException()); -        var res = cupboardController.deleteNeed(1); +        var res = cupboardController.deleteNeed(1, key);          assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode());      } 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 cc7df40..06fb6cd 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 @@ -82,7 +82,7 @@ public class UserControllerTest {          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); +        doThrow(new IllegalAccessException()).when(mockAuthService).keyHasAccessToUser(username, key);          // Invoke          ResponseEntity<User> response = userController.getUser(username, key); @@ -237,7 +237,7 @@ public class UserControllerTest {          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); +        doThrow(new IllegalAccessException()).when(mockAuthService).keyHasAccessToUser(username, key);          // Invoke @@ -298,7 +298,7 @@ public class UserControllerTest {          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); +        doThrow(new IllegalAccessException()).when(mockAuthService).keyHasAccessToUser(username, key);          // Invoke          ResponseEntity<Boolean> response = userController.deleteUser(username, key); diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/UserAuthFileDAOTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/UserAuthFileDAOTest.java index 5e92deb..a4842c5 100644 --- a/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/UserAuthFileDAOTest.java +++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/UserAuthFileDAOTest.java @@ -43,7 +43,7 @@ public class UserAuthFileDAOTest {          String key = "123";          UserAuth auth = userAuthFIleDAO.getUserAuth(key); -        assertEquals(auth, userAuths[0]); +        assertEquals(userAuths[0], auth);      }      @Test 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 index d3085e5..4f58b12 100644 --- 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 @@ -40,34 +40,34 @@ public class AuthServiceTest {      }      @Test -    public void testAuthenticate() throws IOException { +    public void testKeyIsValid() throws IOException {          // Mock          when(mockAuthDAO.getUserAuth(key)).thenReturn(new UserAuth(key, username, null));          when(mockUserService.getUser(username)).thenReturn(user);          // Analyze -        assertDoesNotThrow(() -> authService.authenticate(username, key)); +        assertDoesNotThrow(() -> authService.keyHasAccessToUser(username, key));      }      @Test -    public void testAuthenticateMismatchName() throws IOException { +    public void testKeyIsValidMismatchName() throws IOException {          // Mock          when(mockAuthDAO.getUserAuth(key)).thenReturn(new UserAuth(key, "EvilFish", null));          when(mockUserService.getUser("EvilFish")).thenReturn(user);          // Analyze -        assertThrows(IllegalAccessException.class, () -> authService.authenticate(username, key)); +        assertThrows(IllegalAccessException.class, () -> authService.keyHasAccessToUser(username, key));      }      @Test -    public void testAuthenticateMissingUserAuth() throws IOException { +    public void testKeyIsValidMissingUserAuth() throws IOException {          // Mock          when(mockAuthDAO.getUserAuth(key)).thenReturn(null);          // Analyze -        assertThrows(IllegalAccessException.class, () -> authService.authenticate(username, key)); +        assertThrows(IllegalAccessException.class, () -> authService.keyHasAccessToUser(username, key));      }  | 
