Skip to content
Snippets Groups Projects
Commit a3560fe7 authored by Birk Øvstetun Narvhus's avatar Birk Øvstetun Narvhus
Browse files

changed test to use authentication

parent 047697bb
No related branches found
No related tags found
No related merge requests found
......@@ -94,7 +94,8 @@ public class FridgeController {
}
try {
return fridgeService.addProductToFridge(request).map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
return fridgeService.addProductToFridge(request).map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.notFound().build());
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().build();
}
......@@ -148,8 +149,7 @@ public class FridgeController {
return ResponseEntity.badRequest().body("Amount must be greater than or equal to 0.");
}
return fridgeService.deleteAmountFromFridge(fridgeProductId, amount)
.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
return ResponseEntity.ok(fridgeService.deleteAmountFromFridge(fridgeProductId, amount));
} catch (NumberFormatException e) {
return ResponseEntity.badRequest().body("Invalid amount format. Please provide a valid number.");
}
......@@ -173,11 +173,10 @@ public class FridgeController {
}
try {
boolean success = fridgeService.removeProductFromFridge(fridgeProductId);
if (success){
return ResponseEntity.ok("Success");
}
return ResponseEntity.badRequest().body("Product not found in the fridge");
return (fridgeService.removeProductFromFridge(fridgeProductId)) ?
ResponseEntity.ok("Product removed from fridge")
: ResponseEntity.status(HttpStatus.NOT_FOUND).body("Product not found in the fridge");
} catch (Exception e) {
return ResponseEntity.status(500).body("Internal server error");
}
......@@ -199,7 +198,8 @@ public class FridgeController {
return ResponseEntity.status(403).body("You are not a member of this group");
}
return fridgeService.wasteProductFromFridge(fridgeProductId).map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
return fridgeService.wasteProductFromFridge(fridgeProductId)
.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}
......
package ntnu.idatt2016.v233.SmartMat.controller.group;
import com.fasterxml.jackson.databind.ObjectMapper;
import ntnu.idatt2016.v233.SmartMat.dto.enums.Authority;
import ntnu.idatt2016.v233.SmartMat.dto.request.FridgeProductRequest;
import ntnu.idatt2016.v233.SmartMat.entity.fridgeProduct.FridgeProductAsso;
import ntnu.idatt2016.v233.SmartMat.entity.group.Fridge;
......@@ -9,40 +10,122 @@ import ntnu.idatt2016.v233.SmartMat.service.group.FridgeService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@ExtendWith(SpringExtension.class)
@WebMvcTest(FridgeController.class)
@AutoConfigureMockMvc(addFilters = false)
@ExtendWith(MockitoExtension.class)
public class FridgeControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@MockBean
@InjectMocks
private FridgeController fridgeController;
@Mock
private FridgeService fridgeService;
private Fridge fridge;
private Product product;
private FridgeProductAsso fridgeProductAsso;
private FridgeProductRequest fridgeProductRequest;
private Authentication regularUser = new Authentication() {
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return List.of(new SimpleGrantedAuthority(Authority.USER.name()));
}
@Override
public Object getCredentials() {
return null;
}
@Override
public Object getDetails() {
return null;
}
@Override
public Object getPrincipal() {
return null;
}
@Override
public boolean isAuthenticated() {
return true;
}
@Override
public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
}
@Override
public String getName() {
return "test";
}
};
private Authentication adminUser = new Authentication() {
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return List.of(new SimpleGrantedAuthority(Authority.ADMIN.name()));
}
@Override
public Object getCredentials() {
return null;
}
@Override
public Object getDetails() {
return null;
}
@Override
public Object getPrincipal() {
return null;
}
@Override
public boolean isAuthenticated() {
return true;
}
@Override
public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
}
@Override
public String getName() {
return "test";
}
};
@BeforeEach
public void setUp() {
// Create and set up your test data here
......@@ -53,92 +136,171 @@ public class FridgeControllerTest {
}
@Test
public void getFridgeByGroupId() throws Exception {
public void getFridgeByGroupIdAsAdmin() throws Exception {
when(fridgeService.getFridgeByGroupId(1L)).thenReturn(Optional.of(fridge));
mockMvc.perform(get("/api/fridges/group/1"))
.andExpect(status().isOk());
ResponseEntity<Fridge> responseEntity = fridgeController.getFridgeByGroupId(1L, adminUser);
verify(fridgeService).getFridgeByGroupId(1L);
assertEquals(responseEntity.getStatusCode(), HttpStatus.OK);
}
@Test
public void getFridgeByGroupIdAsUser() throws Exception {
when(fridgeService.getFridgeByGroupId(1L)).thenReturn(Optional.of(fridge));
ResponseEntity<Fridge> responseEntity = fridgeController.getFridgeByGroupId(1L, regularUser);
verify(fridgeService).getFridgeByGroupId(1L);
assertEquals(responseEntity.getStatusCode(), HttpStatus.FORBIDDEN);
}
@Test
public void getFridgeByGroupId_notFound() throws Exception {
when(fridgeService.getFridgeByGroupId(1L)).thenReturn(Optional.empty());
mockMvc.perform(get("/api/fridges/group/1"))
.andExpect(status().isNotFound());
ResponseEntity<Fridge> responseEntity = fridgeController.getFridgeByGroupId(1L, adminUser);
verify(fridgeService).getFridgeByGroupId(1L);
assertEquals(responseEntity.getStatusCode(), HttpStatus.NOT_FOUND);
}
@Test
public void getFridgeByFridgeId() throws Exception {
when(fridgeService.getFridgeByFridgeId(1L)).thenReturn(Optional.of(fridge));
mockMvc.perform(get("/api/fridges/fridge/1"))
.andExpect(status().isOk());
ResponseEntity<Fridge> responseEntity = fridgeController.getFridgeByFridgeId(1L, adminUser);
verify(fridgeService).getFridgeByFridgeId(1L);
assertEquals(responseEntity.getStatusCode(), HttpStatus.OK);
}
@Test
public void getFridgeByFridgeIdAsUser() throws Exception {
ResponseEntity<Fridge> responseEntity = fridgeController.getFridgeByFridgeId(1L, regularUser);
verify(fridgeService).isUserInFridge("test", 1L);
assertEquals(responseEntity.getStatusCode(), HttpStatus.FORBIDDEN);
}
@Test
public void getFridgeByFridgeId_notFound() throws Exception {
when(fridgeService.getFridgeByFridgeId(1L)).thenReturn(Optional.empty());
mockMvc.perform(get("/api/fridges/fridge/1"))
.andExpect(status().isNotFound());
ResponseEntity<Fridge> responseEntity = fridgeController.getFridgeByFridgeId(1L, adminUser);
verify(fridgeService).getFridgeByFridgeId(1L);
assertEquals(responseEntity.getStatusCode(), HttpStatus.NOT_FOUND);
}
@Test
public void addProductToFridge() throws Exception {
when(fridgeService.addProductToFridge(any(FridgeProductRequest.class))).thenReturn(Optional.of(product));
mockMvc.perform(post("/api/fridges/group/product")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(fridgeProductRequest)))
.andExpect(status().isOk());
when(fridgeService.getFridgeByGroupId(1L)).thenReturn(Optional.of(fridge));
ResponseEntity<Product> responseEntity = fridgeController.addProductToFridge(fridgeProductRequest, adminUser);
verify(fridgeService).addProductToFridge(any(FridgeProductRequest.class));
assertEquals(responseEntity.getStatusCode(), HttpStatus.OK);
}
@Test
public void addProductToFridgeAsUser() throws Exception {
when(fridgeService.getFridgeByGroupId(1L)).thenReturn(Optional.of(fridge));
ResponseEntity<Product> responseEntity = fridgeController.addProductToFridge(fridgeProductRequest, regularUser);
verify(fridgeService).isUserInFridge("test", 0L);
assertEquals(responseEntity.getStatusCode(), HttpStatus.FORBIDDEN);
}
@Test
public void addProductToFridge_notFound() throws Exception {
when(fridgeService.addProductToFridge(any(FridgeProductRequest.class))).thenReturn(Optional.empty());
when(fridgeService.addProductToFridge(any(FridgeProductRequest.class))).thenReturn(Optional.empty( ));
when(fridgeService.getFridgeByGroupId(1L)).thenReturn(Optional.of(fridge));
ResponseEntity<Product> responseEntity = fridgeController.addProductToFridge(fridgeProductRequest, adminUser);
verify(fridgeService).isUserInFridge("test", 0L);
mockMvc.perform(post("/api/fridges/group/product")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(fridgeProductRequest)))
.andExpect(status().isNotFound());
assertEquals(responseEntity.getStatusCode(), HttpStatus.NOT_FOUND);
}
@Test
public void updateProductInFridge() throws Exception {
when(fridgeService.updateProductInFridge(any(FridgeProductRequest.class))).thenReturn(Optional.of(fridgeProductAsso));
mockMvc.perform(put("/api/fridges/group/product")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(fridgeProductRequest)))
.andExpect(status().isOk());
when(fridgeService.getFridgeByGroupId(1L)).thenReturn(Optional.of(fridge));
ResponseEntity<FridgeProductAsso> responseEntity =
fridgeController.updateProductInFridge(fridgeProductRequest, adminUser);
verify(fridgeService).updateProductInFridge(any(FridgeProductRequest.class));
assertEquals(responseEntity.getStatusCode(), HttpStatus.OK);
}
@Test
public void updateProductInFridge_notFound() throws Exception {
when(fridgeService.updateProductInFridge(any(FridgeProductRequest.class))).thenReturn(Optional.empty());
mockMvc.perform(put("/api/fridges/group/product")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(fridgeProductRequest)))
.andExpect(status().isNotFound());
when(fridgeService.getFridgeByGroupId(1L)).thenReturn(Optional.of(fridge));
ResponseEntity<FridgeProductAsso> responseEntity =
fridgeController.updateProductInFridge(fridgeProductRequest, adminUser);
verify(fridgeService).updateProductInFridge(any(FridgeProductRequest.class));
assertEquals(responseEntity.getStatusCode(), HttpStatus.NOT_FOUND);
}
@Test
public void removeProductFromFridge_success() throws Exception {
when(fridgeService.removeProductFromFridge(1L)).thenReturn(true);
mockMvc.perform(delete("/api/fridges/delete/product/1"))
.andExpect(status().isOk())
.andExpect(content().string("Success"));
ResponseEntity<String> responseEntity =
fridgeController.removeProductFromFridge(1L, adminUser);
verify(fridgeService).removeProductFromFridge(1L);
assertEquals(responseEntity.getStatusCode(), HttpStatus.OK);
}
@Test
public void removeProductFromFridge_badRequest() throws Exception {
public void removeProductFromFridge_notFound() throws Exception {
when(fridgeService.removeProductFromFridge(1L)).thenReturn(false);
mockMvc.perform(delete("/api/fridges/delete/product/1"))
.andExpect(status().isBadRequest())
.andExpect(content().string("Product not found in the fridge"));
ResponseEntity<String> responseEntity =
fridgeController.removeProductFromFridge(1L, adminUser);
verify(fridgeService).removeProductFromFridge(1L);
assertEquals(responseEntity.getStatusCode(), HttpStatus.NOT_FOUND);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment