Skip to content

Commit

Permalink
Merge f6806d8 into 199531e
Browse files Browse the repository at this point in the history
  • Loading branch information
wwelling committed Jul 12, 2018
2 parents 199531e + f6806d8 commit 932fa14
Show file tree
Hide file tree
Showing 29 changed files with 440 additions and 44 deletions.
6 changes: 3 additions & 3 deletions src/main/java/edu/tamu/app/controller/NoteController.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ public ApiResponse getById(@PathVariable Long id) {
}

@RequestMapping("/create")
@PreAuthorize("hasRole('SERVICE_MANAGER')")
@PreAuthorize("hasRole('WEB_MANAGER')")
@WeaverValidation(business = { @WeaverValidation.Business(value = CREATE) })
public ApiResponse create(@WeaverValidatedModel Note note, @WeaverCredentials Credentials credentials) throws UserNotFoundException {
return new ApiResponse(SUCCESS, noteRepo.create(note, credentials));
}

@RequestMapping("/update")
@PreAuthorize("hasRole('SERVICE_MANAGER')")
@PreAuthorize("hasRole('WEB_MANAGER')")
public ApiResponse update(@WeaverValidatedModel Note note) {
return new ApiResponse(SUCCESS, noteRepo.update(note));
}

@Transactional
@RequestMapping("/remove")
@PreAuthorize("hasRole('SERVICE_MANAGER')")
@PreAuthorize("hasRole('WEB_MANAGER')")
public ApiResponse remove(@WeaverValidatedModel Note note) {
noteRepo.delete(note);
return new ApiResponse(SUCCESS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ public ApiResponse getById(@PathVariable Long id) {
}

@RequestMapping("/create")
@PreAuthorize("hasRole('WEB_MANAGER')")
@PreAuthorize("hasAnyRole('ADMIN','SERVICE_ADMIN','NOTICE_MANAGER')")
@WeaverValidation(business = { @WeaverValidation.Business(value = CREATE) })
public ApiResponse create(@WeaverValidatedModel Notification notification) {
return new ApiResponse(SUCCESS, notificationRepo.create(notification));
}

@RequestMapping("/update")
@PreAuthorize("hasRole('WEB_MANAGER')")
@PreAuthorize("hasAnyRole('ADMIN','SERVICE_ADMIN','NOTICE_MANAGER')")
@WeaverValidation(business = { @WeaverValidation.Business(value = UPDATE) })
public ApiResponse update(@WeaverValidatedModel Notification notification) {
return new ApiResponse(SUCCESS, notificationRepo.update(notification));
}

@RequestMapping("/remove")
@PreAuthorize("hasRole('WEB_MANAGER')")
@PreAuthorize("hasAnyRole('ADMIN','SERVICE_ADMIN','NOTICE_MANAGER')")
@WeaverValidation(business = { @WeaverValidation.Business(value = DELETE) })
public ApiResponse remove(@WeaverValidatedModel Notification notification) {
notificationRepo.delete(notification);
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/edu/tamu/app/controller/ServiceController.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import edu.tamu.app.model.Service;
import edu.tamu.app.model.repo.IdeaRepo;
import edu.tamu.app.model.repo.ServiceRepo;
import edu.tamu.app.model.request.FilteredPageRequest;
import edu.tamu.app.model.request.IssueRequest;
import edu.tamu.app.model.request.ServiceRequest;
import edu.tamu.app.service.ProjectService;
Expand Down Expand Up @@ -52,6 +53,12 @@ public ApiResponse getPublicServices() {
return new ApiResponse(SUCCESS, serviceRepo.findByIsPublicOrderByStatusDescNameAsc(true));
}

@RequestMapping("/page")
@PreAuthorize("hasRole('ANONYMOUS')")
public ApiResponse page(@RequestBody FilteredPageRequest filteredPageRequest) {
return new ApiResponse(SUCCESS, serviceRepo.findAll(filteredPageRequest.getServiceSpecification(), filteredPageRequest.getPageRequest()));
}

@RequestMapping("/{id}")
@PreAuthorize("hasRole('ANONYMOUS')")
public ApiResponse getService(@PathVariable Long id) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/edu/tamu/app/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public ApiResponse getUser(@WeaverUser User user) {
* @throws Exception
*/
@RequestMapping
@PreAuthorize("hasRole('WEB_MANAGER')")
@PreAuthorize("hasRole('ADMIN')")
public ApiResponse allUsers() throws Exception {
return new ApiResponse(SUCCESS, userRepo.findAll());
}
Expand All @@ -90,7 +90,7 @@ public ApiResponse allUsers() throws Exception {
* @throws Exception
*/
@RequestMapping("/update")
@PreAuthorize("hasRole('WEB_MANAGER')")
@PreAuthorize("hasRole('ADMIN')")
public ApiResponse updateUser(@RequestBody User user) throws Exception {
user = userRepo.save(user);
simpMessagingTemplate.convertAndSend("/channel/user", new ApiResponse(SUCCESS, userRepo.findAll()));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/tamu/app/enums/Role.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

public enum Role implements IRole {

ROLE_ADMIN, ROLE_WEB_MANAGER, ROLE_SERVICE_MANAGER, ROLE_STAFF, ROLE_USER, ROLE_ANONYMOUS
ROLE_ADMIN, ROLE_SERVICE_ADMIN, ROLE_SERVICE_MANAGER, ROLE_WEB_MANAGER, ROLE_NOTICE_MANAGER, ROLE_STAFF, ROLE_USER, ROLE_ANONYMOUS

}
5 changes: 5 additions & 0 deletions src/main/java/edu/tamu/app/model/repo/ServiceRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;

import edu.tamu.app.enums.Status;
Expand All @@ -10,6 +13,8 @@

public interface ServiceRepo extends JpaRepository<Service, Long>, ServiceRepoCustom {

public Page<Service> findAll(Specification<Service> specification, Pageable pageable);

public List<Service> findByIsPublicOrderByStatusDescNameAsc(Boolean isPublic);

public List<Service> findByIsAuto(Boolean isAuto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,18 @@ public Predicate toPredicate(Root<E> root, CriteriaQuery<?> query, CriteriaBuild
}
}

query.orderBy(cb.desc(root.get("lastModified")));
toPredicateDefaultQueryOrderBy(root, query, cb);

return builder.build(cb);
}

/**
* Allow implementing classes to control order by in case lastModified is non-existent.
*/
protected void toPredicateDefaultQueryOrderBy(Root<E> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
query.orderBy(cb.desc(root.get("lastModified")));
}

private class PredicateBuilder {

private final Map<String, List<Predicate>> predicates;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package edu.tamu.app.model.repo.specification;

import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

public class ServiceSpecification<E> extends AbstractSpecification<E> {

public ServiceSpecification(Map<String, String[]> filters) {
super(filters);
}

@Override
protected void toPredicateDefaultQueryOrderBy(Root<E> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
query.orderBy(cb.desc(root.get("name")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
import edu.tamu.app.model.FeatureProposal;
import edu.tamu.app.model.Idea;
import edu.tamu.app.model.Note;
import edu.tamu.app.model.Service;
import edu.tamu.app.model.repo.specification.FeatureProposalSpecification;
import edu.tamu.app.model.repo.specification.IdeaSpecification;
import edu.tamu.app.model.repo.specification.NoteSpecification;
import edu.tamu.app.model.repo.specification.ServiceSpecification;

public class FilteredPageRequest {

Expand All @@ -42,6 +44,11 @@ public IdeaSpecification<Idea> getIdeaSpecification() {
return new IdeaSpecification<Idea>(filters);
}

@JsonIgnore
public ServiceSpecification<Service> getServiceSpecification() {
return new ServiceSpecification<Service>(filters);
}

@JsonIgnore
public FeatureProposalSpecification<FeatureProposal> getFeatureProposalSpecification() {
return new FeatureProposalSpecification<FeatureProposal>(filters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void updateAll() {
if (serviceStatus != service.getStatus()) {
logger.debug("Updating the status of [" + service.getName() + "] to: " + serviceStatus);
service.setStatus(serviceStatus);
serviceRepo.save(service);
serviceRepo.update(service);
}
} catch (MalformedURLException e) {
logger.error("Did not check the status of [" + service.getName() + "] due to a malformed URL: " + service.getServiceUrl());
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/edu/tamu/app/auth/model/AppUserDetailsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package edu.tamu.app.auth.model;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;

import edu.tamu.app.enums.Role;
import edu.tamu.app.model.User;
import edu.tamu.weaver.auth.model.Credentials;

@RunWith(SpringRunner.class)
public class AppUserDetailsTest {

private static final Credentials TEST_CREDENTIALS = new Credentials();
static {
TEST_CREDENTIALS.setUin("123456789");
TEST_CREDENTIALS.setEmail("aggieJack@tamu.edu");
TEST_CREDENTIALS.setFirstName("Aggie");
TEST_CREDENTIALS.setLastName("Jack");
TEST_CREDENTIALS.setRole("ROLE_USER");
}

private User testUser = new User(TEST_CREDENTIALS.getUin(), TEST_CREDENTIALS.getEmail(), TEST_CREDENTIALS.getFirstName(), TEST_CREDENTIALS.getLastName(), Role.valueOf(TEST_CREDENTIALS.getRole()));

@Test
public void testConstructor() {
AppUserDetails appUser = new AppUserDetails(testUser);
assertEquals("The parent constructor was not called correctly", testUser.getId(), appUser.getId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package edu.tamu.app.auth.service;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.test.util.ReflectionTestUtils.setField;

import java.util.Optional;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.test.context.junit4.SpringRunner;

import edu.tamu.app.enums.Role;
import edu.tamu.app.model.User;
import edu.tamu.app.model.repo.UserRepo;
import edu.tamu.weaver.auth.model.Credentials;

@RunWith(SpringRunner.class)
public class AppUserCredentialsServiceTest {

private static final Credentials TEST_CREDENTIALS_1 = new Credentials();
static {
TEST_CREDENTIALS_1.setUin("123456789");
TEST_CREDENTIALS_1.setEmail("aggieJack@tamu.edu");
TEST_CREDENTIALS_1.setFirstName("Aggie");
TEST_CREDENTIALS_1.setLastName("Jack");
TEST_CREDENTIALS_1.setRole("ROLE_USER");
}

private static final Credentials TEST_CREDENTIALS_2 = new Credentials();
static {
TEST_CREDENTIALS_2.setUin("987654321");
TEST_CREDENTIALS_2.setEmail("aggieJack@tamu.edu");
TEST_CREDENTIALS_2.setFirstName("Aggie");
TEST_CREDENTIALS_2.setLastName("Jack");
TEST_CREDENTIALS_2.setRole("ROLE_USER");
}

private static final Credentials TEST_NULL_CREDENTIALS = new Credentials();
static {
TEST_NULL_CREDENTIALS.setUin("987654321");
TEST_NULL_CREDENTIALS.setEmail("aggieJack@tamu.edu");
TEST_NULL_CREDENTIALS.setFirstName("Aggie");
TEST_NULL_CREDENTIALS.setLastName("Jack");
}

private static final Credentials TEST_CHANGED_CREDENTIALS = new Credentials();
static {
TEST_CHANGED_CREDENTIALS.setUin("111111111");
TEST_CHANGED_CREDENTIALS.setEmail("jsmithk@tamu.edu");
TEST_CHANGED_CREDENTIALS.setFirstName("John");
TEST_CHANGED_CREDENTIALS.setLastName("Smith");
TEST_CHANGED_CREDENTIALS.setRole("ROLE_ADMIN");
}

private User testUser1 = new User(TEST_CREDENTIALS_1.getUin(), TEST_CREDENTIALS_1.getEmail(), TEST_CREDENTIALS_1.getFirstName(), TEST_CREDENTIALS_1.getLastName(), Role.valueOf(TEST_CREDENTIALS_1.getRole()));
private User testUser2 = new User(TEST_CREDENTIALS_2.getUin(), TEST_CREDENTIALS_2.getEmail(), TEST_CREDENTIALS_2.getFirstName(), TEST_CREDENTIALS_2.getLastName(), Role.valueOf(TEST_CREDENTIALS_2.getRole()));

private static final String[] testAdmins = { "123456789", "987654321" };

private Optional<User> optionalUser1 = Optional.of(testUser1);

@Mock
private UserRepo userRepo;

@InjectMocks
private AppUserCredentialsService credentialsService;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(userRepo.findByUsername(TEST_CREDENTIALS_1.getUin())).thenReturn(optionalUser1);
when(userRepo.findByUsername(TEST_CREDENTIALS_2.getUin())).thenReturn(Optional.empty());
when(userRepo.findByUsername(TEST_CHANGED_CREDENTIALS.getUin())).thenReturn(optionalUser1);
when(userRepo.create(any(String.class), any(String.class), any(String.class), any(String.class), any(Role.class))).thenReturn(testUser2);
when(userRepo.save(any(User.class))).thenReturn(testUser1);
}

@Test
public void testUpdateUserByCredentials() {
setField(credentialsService, "admins", testAdmins);
User foundUser = credentialsService.updateUserByCredentials(TEST_CREDENTIALS_1);
assertEquals("Unable to find user", testUser1, foundUser);
User unfoundUser = credentialsService.updateUserByCredentials(TEST_CREDENTIALS_2);
assertEquals("Unable to find user", testUser2, unfoundUser);
}

@Test
public void testGetAnonymousRole() {
String anonRole = credentialsService.getAnonymousRole();
assertEquals("Anonymous Role not set correctly", Role.ROLE_ANONYMOUS.toString(), anonRole);
}

@Test
public void testNullRole() {
setField(credentialsService, "admins", testAdmins);
User nullUser = credentialsService.updateUserByCredentials(TEST_NULL_CREDENTIALS);
assertEquals("Null Role not updated", TEST_CREDENTIALS_1.getRole(), nullUser.getRole().toString());
}

@Test
public void testChangedUser() {
User changedUser = credentialsService.updateUserByCredentials(TEST_CHANGED_CREDENTIALS);
assertEquals("is present", changedUser, optionalUser1.get());
assertEquals("Username was not updated", TEST_CHANGED_CREDENTIALS.getUin(), changedUser.getUsername());
assertEquals("Email was not updated", TEST_CHANGED_CREDENTIALS.getEmail(), changedUser.getEmail());
assertEquals("First name was not updated", TEST_CHANGED_CREDENTIALS.getFirstName(), changedUser.getFirstName());
assertEquals("Last name was not updated", TEST_CHANGED_CREDENTIALS.getLastName(), changedUser.getLastName());
assertEquals("Role was not updated", TEST_CHANGED_CREDENTIALS.getRole(), changedUser.getRole().toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package edu.tamu.app.auth.service;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.test.context.junit4.SpringRunner;

import edu.tamu.app.enums.Role;
import edu.tamu.app.model.User;
import edu.tamu.weaver.auth.model.Credentials;

@RunWith(SpringRunner.class)
public class AppUserDetailsServiceTest {

private static final Credentials TEST_CREDENTIALS_1 = new Credentials();
static {
TEST_CREDENTIALS_1.setUin("123456789");
TEST_CREDENTIALS_1.setEmail("aggieJack@tamu.edu");
TEST_CREDENTIALS_1.setFirstName("Aggie");
TEST_CREDENTIALS_1.setLastName("Jack");
TEST_CREDENTIALS_1.setRole("ROLE_USER");
}

private User testUser1 = new User(TEST_CREDENTIALS_1.getUin(), TEST_CREDENTIALS_1.getEmail(), TEST_CREDENTIALS_1.getFirstName(), TEST_CREDENTIALS_1.getLastName(), Role.valueOf(TEST_CREDENTIALS_1.getRole()));

@InjectMocks
private AppUserDetailsService appUserDetailsService;

@Test
public void testBuildUserDetails() {
UserDetails details = appUserDetailsService.buildUserDetails(testUser1);
assertEquals("User details not built correctly", TEST_CREDENTIALS_1.getUin(), details.getUsername());
}

}

0 comments on commit 932fa14

Please sign in to comment.