Skip to content

Commit

Permalink
[SHIBUI-1058]
Browse files Browse the repository at this point in the history
Updates based on PR feedback.
  • Loading branch information
Bill Smith committed Jan 23, 2019
1 parent 6785119 commit 63df45e
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ public class JsonSchemaComponentsConfiguration {
@Setter
private String nameIdFormatFilterUiSchemaLocation = "classpath:nameid-filter.schema.json";

@Autowired
UserRepository userRepository;

@Bean
public JsonSchemaResourceLocationRegistry jsonSchemaResourceLocationRegistry(ResourceLoader resourceLoader, ObjectMapper jacksonMapper) {
return JsonSchemaResourceLocationRegistry.inMemory()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public ResponseEntity<?> update(@RequestBody EntityDescriptorRepresentation edRe
if (existingEd == null) {
return ResponseEntity.notFound().build();
} else {
if (currentUser.getRole().equals("ROLE_ADMIN") || currentUser.getUsername().equals(existingEd.getCreatedBy())) {
if (currentUser != null && (currentUser.getRole().equals("ROLE_ADMIN") || currentUser.getUsername().equals(existingEd.getCreatedBy()))) {
// Verify we're the only one attempting to update the EntityDescriptor
if (edRepresentation.getVersion() != existingEd.hashCode()) {
return new ResponseEntity<Void>(HttpStatus.CONFLICT);
Expand Down Expand Up @@ -154,11 +154,11 @@ public ResponseEntity<?> getAll() {
User currentUser = userService.getCurrentUser();
if (currentUser != null) {
if (currentUser.getRole().equals("ROLE_ADMIN")) {
return ResponseEntity.ok(entityDescriptorRepository.findAllByCustomQueryAndStream()
return ResponseEntity.ok(entityDescriptorRepository.findAllStreamByCustomQuery()
.map(ed -> entityDescriptorService.createRepresentationFromDescriptor(ed))
.collect(Collectors.toList()));
} else {
return ResponseEntity.ok(entityDescriptorRepository.findAllByCreatedBy(currentUser.getUsername())
return ResponseEntity.ok(entityDescriptorRepository.findAllStreamByCreatedBy(currentUser.getUsername())
.map(ed -> entityDescriptorService.createRepresentationFromDescriptor(ed))
.collect(Collectors.toList()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public interface EntityDescriptorRepository extends CrudRepository<EntityDescrip

EntityDescriptor findByResourceId(String resourceId);

Stream<EntityDescriptor> findAllByServiceEnabled(boolean serviceEnabled);
Stream<EntityDescriptor> findAllStreamByServiceEnabled(boolean serviceEnabled);

@Query("select e from EntityDescriptor e")
Stream<EntityDescriptor> findAllByCustomQueryAndStream();
Stream<EntityDescriptor> findAllStreamByCustomQuery();

Stream<EntityDescriptor> findAllByCreatedBy(String createdBy);
Stream<EntityDescriptor> findAllStreamByCreatedBy(String createdBy);
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public EntityDescriptorFilesScheduledTasks(String metadataDirName,
@Scheduled(fixedRateString = "${shibui.taskRunRate:30000}")
@Transactional(readOnly = true)
public void generateEntityDescriptorFiles() throws MarshallingException {
this.entityDescriptorRepository.findAllByServiceEnabled(true)
this.entityDescriptorRepository.findAllStreamByServiceEnabled(true)
.forEach(ed -> {
Path targetFilePath = targetFilePathFor(toSha1HexString(ed.getEntityID()));
if (Files.exists(targetFilePath)) {
Expand Down Expand Up @@ -91,7 +91,7 @@ public void removeDanglingEntityDescriptorFiles() {
.map(it -> it.substring(0, it.indexOf(".")))
.collect(toSet());

Set<String> enabledEidsSha1Hashes = this.entityDescriptorRepository.findAllByServiceEnabled(true)
Set<String> enabledEidsSha1Hashes = this.entityDescriptorRepository.findAllStreamByServiceEnabled(true)
.map(EntityDescriptor::getEntityID)
.map(EntityDescriptorFilesScheduledTasks::toSha1HexString)
.collect(toSet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public void updateUserRole(User user) {
}

public User getCurrentUser() {
//TODO: Consider returning an Optional here
User user = null;
if (SecurityContextHolder.getContext() != null && SecurityContextHolder.getContext().getAuthentication() != null) {
String principal = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,12 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories
import org.springframework.security.core.Authentication
import org.springframework.security.core.context.SecurityContext
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.security.web.context.HttpSessionSecurityContextRepository
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.web.servlet.result.MockMvcResultHandlers
import org.springframework.test.web.servlet.setup.MockMvcBuilders
import org.springframework.web.client.RestTemplate
import spock.lang.Specification
import spock.lang.Subject

import javax.servlet.http.HttpSession
import java.security.Principal
import java.time.LocalDateTime

import static org.hamcrest.CoreMatchers.containsString
Expand Down Expand Up @@ -105,7 +101,7 @@ class EntityDescriptorControllerTests extends Specification {

then:
//One call to the repo expected
1 * entityDescriptorRepository.findAllByCustomQueryAndStream() >> emptyRecordsFromRepository
1 * entityDescriptorRepository.findAllStreamByCustomQuery() >> emptyRecordsFromRepository
result.andExpect(expectedHttpResponseStatus)
.andExpect(content().contentType(expectedResponseContentType))
.andExpect(content().json(expectedEmptyListResponseBody))
Expand Down Expand Up @@ -152,7 +148,7 @@ class EntityDescriptorControllerTests extends Specification {

then:
//One call to the repo expected
1 * entityDescriptorRepository.findAllByCustomQueryAndStream() >> oneRecordFromRepository
1 * entityDescriptorRepository.findAllStreamByCustomQuery() >> oneRecordFromRepository
result.andExpect(expectedHttpResponseStatus)
.andExpect(content().contentType(expectedResponseContentType))
.andExpect(content().json(expectedOneRecordListResponseBody, true))
Expand Down Expand Up @@ -223,7 +219,7 @@ class EntityDescriptorControllerTests extends Specification {

then:
//One call to the repo expected
1 * entityDescriptorRepository.findAllByCustomQueryAndStream() >> twoRecordsFromRepository
1 * entityDescriptorRepository.findAllStreamByCustomQuery() >> twoRecordsFromRepository
result.andExpect(expectedHttpResponseStatus)
.andExpect(content().contentType(expectedResponseContentType))
.andExpect(content().json(expectedTwoRecordsListResponseBody, true))
Expand Down Expand Up @@ -272,14 +268,15 @@ class EntityDescriptorControllerTests extends Specification {

then:
//One call to the repo expected
1 * entityDescriptorRepository.findAllByCreatedBy('someUser') >> oneRecordFromRepository
1 * entityDescriptorRepository.findAllStreamByCreatedBy('someUser') >> oneRecordFromRepository
result.andExpect(expectedHttpResponseStatus)
.andExpect(content().contentType(expectedResponseContentType))
.andExpect(content().json(expectedOneRecordListResponseBody, true))
}

def 'POST /EntityDescriptor and successfully create new record'() {
given:
prepareUser('admin', 'ROLE_ADMIN')
def expectedCreationDate = '2017-10-23T11:11:11'
def expectedEntityId = 'https://shib'
def expectedSpName = 'sp1'
Expand Down Expand Up @@ -394,6 +391,7 @@ class EntityDescriptorControllerTests extends Specification {

def 'GET /EntityDescriptor/{resourceId} non-existent'() {
given:
prepareUser('admin', 'ROLE_ADMIN')
def providedResourceId = 'uuid-1'

when:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class EntityDescriptorFilesScheduledTasksTests extends Specification {
}
it
})
1 * entityDescriptorRepository.findAllByServiceEnabled(true) >> [entityDescriptor].stream()
1 * entityDescriptorRepository.findAllStreamByServiceEnabled(true) >> [entityDescriptor].stream()

when:
if (directory.exists()) {
Expand Down Expand Up @@ -137,7 +137,7 @@ class EntityDescriptorFilesScheduledTasksTests extends Specification {
def file = new File(directory, randomGenerator.randomId() + ".xml")
file.text = "Delete me!"

1 * entityDescriptorRepository.findAllByServiceEnabled(true) >> [entityDescriptor].stream()
1 * entityDescriptorRepository.findAllStreamByServiceEnabled(true) >> [entityDescriptor].stream()

when:
entityDescriptorFilesScheduledTasks.removeDanglingEntityDescriptorFiles()
Expand Down

0 comments on commit 63df45e

Please sign in to comment.