ProjectController { @Secured({Roles.ROLE_USER}) @RequestMapping(value = "/projects", method = RequestMethod.GET) @JsonView(Project.ProjectSummaryView.class) public List<Project> getProjects(Authentication auth) { UserEntity userEntity = userFromAuthentication(auth); final List<Project> projects = projectService.getProjects(userEntity); return projects.stream().filter(project -> mayViewProjectFilter(project, auth)).collect(Collectors.toList()); }  @Secured({Roles.ROLE_USER}) @RequestMapping(value = "/projects", method = RequestMethod.GET) @JsonView(Project.ProjectSummaryView.class) List<Project> getProjects(Authentication auth); @Secured({Roles.ROLE_USER}) @RequestMapping(value = "/project/{projectId}", method = RequestMethod.GET) Project getProject(@PathVariable String projectId, Authentication auth); @Secured(Roles.ROLE_USER) @ResponseStatus(HttpStatus.CREATED) @RequestMapping(value = "/project", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) Project addProject(@RequestBody @Valid Project project, Principal principal); @Secured(Roles.ROLE_USER) @ResponseStatus(HttpStatus.CREATED) @RequestMapping(value = "/project/{projectId}/pledges", method = RequestMethod.POST) void pledgeProject(@PathVariable String projectId, @RequestBody @Valid Pledge pledge, Principal principal); @Secured(Roles.ROLE_ADMIN) @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/project/{projectId}/status", method = RequestMethod.PATCH) Project modifyProjectStatus(@PathVariable("projectId") String projectId, @RequestBody @Valid @NotNull ProjectStatusUpdate newStatus, Principal principal); @Secured(Roles.ROLE_USER) @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/project/{projectId}", method = RequestMethod.PUT) Project modifyProjectMasterdata(@PathVariable("projectId") String projectId, @RequestBody @Valid @NotNull Project modifiedProject, Principal principal); @Secured(Roles.ROLE_USER) @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/projects/{projectId}/likes", method = RequestMethod.POST) void likeProject(@PathVariable("projectId") String projectId, Principal principal); @Secured(Roles.ROLE_USER) @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/projects/{projectId}/likes", method = RequestMethod.DELETE) void unlikeProject(@PathVariable("projectId") String projectId, Principal principal); @Secured(Roles.ROLE_USER) @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/projects/{projectId}/attachments", method = RequestMethod.POST) Attachment addProjectAttachment(@PathVariable("projectId") String projectId, @RequestParam("file") MultipartFile file, Principal principal); @Secured({Roles.ROLE_TRUSTED_ANONYMOUS, Roles.ROLE_USER}) @RequestMapping(value = "/projects/{projectId}/attachments/{fileReference}", method = RequestMethod.GET) ResponseEntity<InputStreamResource> serveProjectAttachment(@PathVariable("projectId") String projectId, @PathVariable("fileReference") String fileReference); @Secured(Roles.ROLE_USER) @ResponseStatus(HttpStatus.NO_CONTENT) @RequestMapping(value = "/projects/{projectId}/attachments/{fileReference}", method = RequestMethod.DELETE) void deleteProjectAttachment(@PathVariable("projectId") String projectId, @PathVariable("fileReference") String fileReference, Principal principal);  }
@Test public void getProjects_shouldReturnPublishedProjectsOnly() throws Exception { final UserEntity user = userEntity(USER_EMAIL, USER_FIRST_NAME, USER_LAST_NAME, Roles.ROLE_USER); final UserEntity creator = userEntity(ADMIN1_EMAIL, ADMIN1_FIRST_NAME, ADMIN1_LAST_NAME, Roles.ROLE_USER); final String projectId = "existingProjectId"; final Project expProjcet_0 = toCreatedProject(project("title0", "descr", "shortDescr", 44, ProjectStatus.PUBLISHED), creator); final Project unexpProjcet = toCreatedProject(project("title1", "descr", "shortDescr", 44, ProjectStatus.PROPOSED), creator); final Project expProjcet_1 = toCreatedProject(project("title2", "descr", "shortDescr", 44, ProjectStatus.PUBLISHED_DEFERRED), creator); when(projectService.getProjects(user)).thenReturn(Arrays.asList(expProjcet_0, unexpProjcet, expProjcet_1)); MvcResult mvcResult = mockMvc.perform(get("/projects", projectId) .principal(authentication(user))) .andExpect(status().isOk()) .andReturn(); toProjectSummaryViewRepresentation(expProjcet_0); toProjectSummaryViewRepresentation(expProjcet_1); assertThat(mapper.readValue(mvcResult.getResponse().getContentAsString(), Project[].class)[0], is(equalTo(expProjcet_0))); assertThat(mapper.readValue(mvcResult.getResponse().getContentAsString(), Project[].class)[1], is(equalTo(expProjcet_1))); }
@Test public void getProjects_shouldReturnPublishedProjectsForAnonymousUsersToo() throws Exception { final UserEntity user = userEntity(USER_EMAIL, USER_FIRST_NAME, USER_LAST_NAME, Roles.ROLE_USER, Roles.ROLE_USER); final UserEntity creator = userEntity(ADMIN1_EMAIL, ADMIN1_FIRST_NAME, ADMIN1_LAST_NAME, Roles.ROLE_USER); final String projectId = "existingProjectId"; final Project expProjcet = toCreatedProject(project("title", "descr", "shortDescr", 44, ProjectStatus.PUBLISHED), creator); final Project unexpProjcet = toCreatedProject(project("title", "descr", "shortDescr", 44, ProjectStatus.PROPOSED), creator); when(projectService.getProjects(null)).thenReturn(Arrays.asList(expProjcet, unexpProjcet)); MvcResult mvcResult = mockMvc.perform(get("/projects", projectId) .principal(anonymousAuthentication())) .andExpect(status().isOk()) .andReturn(); toProjectSummaryViewRepresentation(expProjcet); assertThat(mapper.readValue(mvcResult.getResponse().getContentAsString(), Project[].class)[0], is(equalTo(expProjcet))); }
@Test public void getProjects_shouldReturnNothingWhenProjectNotPublishedAndProjectCreatorNotRequestor() throws Exception { final UserEntity user = userEntity(USER_EMAIL, USER_FIRST_NAME, USER_LAST_NAME, Roles.ROLE_USER, Roles.ROLE_USER); final UserEntity creator = userEntity(ADMIN1_EMAIL, ADMIN1_FIRST_NAME, ADMIN1_LAST_NAME, Roles.ROLE_USER); final String projectId = "existingProjectId"; final Project expProjcet = toCreatedProject(project("title", "descr", "shortDescr", 44, ProjectStatus.PROPOSED), creator); when(projectService.getProjects(user)).thenReturn(Collections.singletonList(expProjcet)); MvcResult mvcResult = mockMvc.perform(get("/projects", projectId) .principal(authentication(user))) .andExpect(status().isOk()) .andReturn(); toProjectSummaryViewRepresentation(expProjcet); assertThat(mvcResult.getResponse().getContentAsString(), is("[]")); }
@Test public void getProjects_shouldReturnUnpublishedProjectsWhenRequestorIsAdmin() throws Exception { final UserEntity user = userEntity(USER_EMAIL, USER_FIRST_NAME, USER_LAST_NAME, Roles.ROLE_USER, Roles.ROLE_ADMIN); final UserEntity creator = userEntity(ADMIN1_EMAIL, ADMIN1_FIRST_NAME, ADMIN1_LAST_NAME, Roles.ROLE_USER); final String projectId = "existingProjectId"; final Project expProjcet = toCreatedProject(project("title", "descr", "shortDescr", 44, ProjectStatus.PROPOSED), creator); when(projectService.getProjects(user)).thenReturn(Collections.singletonList(expProjcet)); MvcResult mvcResult = mockMvc.perform(get("/projects", projectId) .principal(authentication(user))) .andExpect(status().isOk()) .andReturn(); toProjectSummaryViewRepresentation(expProjcet); assertThat(mapper.readValue(mvcResult.getResponse().getContentAsString(), Project[].class)[0], is(equalTo(expProjcet))); }
@Test public void getProjects_shouldReturnUnpublishedProjectWhenRequestorIsCreator() throws Exception { final UserEntity creator = userEntity(ADMIN1_EMAIL, ADMIN1_FIRST_NAME, ADMIN1_LAST_NAME, Roles.ROLE_USER); final UserEntity anotherCreator = userEntity(ADMIN2_EMAIL, ADMIN2_FIRST_NAME, ADMIN2_LAST_NAME, Roles.ROLE_USER); final String projectId = "existingProjectId"; final Project expProjcet = toCreatedProject(project("title", "descr", "shortDescr", 44, ProjectStatus.PROPOSED), creator); final Project nonExpProjcet = toCreatedProject(project("title2", "descr2", "shortDescr2", 45, ProjectStatus.PROPOSED), anotherCreator); when(projectService.getProjects(creator)).thenReturn(Arrays.asList(expProjcet, nonExpProjcet)); MvcResult mvcResult = mockMvc.perform(get("/projects", projectId) .principal(authentication(creator))) .andExpect(status().isOk()) .andReturn(); toProjectSummaryViewRepresentation(expProjcet); List<Project> projects = Arrays.asList(mapper.readValue(mvcResult.getResponse().getContentAsString(), Project[].class)); assertThat(projects.size(), is(1)); assertThat("Result should contain creator's proposed project", projects.contains(expProjcet)); }