aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/test/java/com/ufund/api/ufundapi/controller
diff options
context:
space:
mode:
Diffstat (limited to 'ufund-api/src/test/java/com/ufund/api/ufundapi/controller')
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java29
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java6
2 files changed, 21 insertions, 14 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);