#93 merged with dev
commit
015a1d7fb8
@ -0,0 +1,21 @@
|
||||
package ru.ulstu.core.model;
|
||||
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.ulstu.user.error.UserBlockedException;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
@Component
|
||||
public class AuthFailureHandler implements AuthenticationFailureHandler {
|
||||
@Override
|
||||
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
|
||||
AuthenticationException ex) throws IOException {
|
||||
if (ex.getClass() == UserBlockedException.class) {
|
||||
response.sendRedirect("/users/block");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package ru.ulstu.user.error;
|
||||
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
|
||||
public class UserBlockedException extends AuthenticationException {
|
||||
public UserBlockedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
|
||||
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
|
||||
<changeSet author="arefyev" id="20190528_000000-3">
|
||||
<addColumn tableName="users">
|
||||
<column name="blocker_id" type="integer"/>
|
||||
</addColumn>
|
||||
<addForeignKeyConstraint baseTableName="users" baseColumnNames="blocker_id" constraintName="fk_blocker"
|
||||
referencedTableName="users" referencedColumnNames="id"/>
|
||||
</changeSet>
|
||||
</databaseChangeLog>
|
@ -0,0 +1,21 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||
layout:decorator="default">
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container" layout:fragment="content">
|
||||
<section id="services">
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col-lg-12 text-center">
|
||||
<h2 class="section-heading text-uppercase">Ваш аккаунт заблокирован</h2>
|
||||
<a href="/"><h3>Вернуться на страницу авторизации</h3></a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,231 @@
|
||||
package ru.ulstu.grant.service;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import ru.ulstu.deadline.model.Deadline;
|
||||
import ru.ulstu.deadline.service.DeadlineService;
|
||||
import ru.ulstu.grant.model.Grant;
|
||||
import ru.ulstu.grant.model.GrantDto;
|
||||
import ru.ulstu.grant.repository.GrantRepository;
|
||||
import ru.ulstu.paper.model.Paper;
|
||||
import ru.ulstu.paper.model.PaperDto;
|
||||
import ru.ulstu.paper.service.PaperService;
|
||||
import ru.ulstu.timeline.service.EventService;
|
||||
import ru.ulstu.user.model.User;
|
||||
import ru.ulstu.user.service.UserService;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class GrantServiceTest {
|
||||
|
||||
@Mock
|
||||
GrantRepository grantRepository;
|
||||
|
||||
@Mock
|
||||
DeadlineService deadlineService;
|
||||
|
||||
@Mock
|
||||
PaperService paperService;
|
||||
|
||||
@Mock
|
||||
UserService userService;
|
||||
|
||||
@Mock
|
||||
EventService eventService;
|
||||
|
||||
@Mock
|
||||
GrantNotificationService grantNotificationService;
|
||||
|
||||
@InjectMocks
|
||||
GrantService grantService;
|
||||
|
||||
private final static Integer ID = 1;
|
||||
private final static Integer INDEX = 0;
|
||||
private final static String TITLE = "Title";
|
||||
private final static String COMMENT = "Comment";
|
||||
private final static boolean TRUE = true;
|
||||
private final static Integer YEAR = 2019;
|
||||
private final static Integer MAX_DISPLAY_SIZE = 50;
|
||||
|
||||
private List<Grant> grants;
|
||||
private List<GrantDto> grantDtos;
|
||||
private List<Deadline> deadlines;
|
||||
private List<Paper> papers;
|
||||
private PaperDto paperDto;
|
||||
private List<PaperDto> paperDtos;
|
||||
private Set<User> authors;
|
||||
private GrantDto grantDto;
|
||||
private Deadline deadline;
|
||||
private User leader;
|
||||
private User author;
|
||||
|
||||
private Grant grantWithId;
|
||||
private Paper paperWithId;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
grants = new ArrayList<>();
|
||||
grantDtos = new ArrayList<>();
|
||||
paperDtos = new ArrayList<>();
|
||||
grantWithId = new Grant();
|
||||
|
||||
deadlines = new ArrayList<>();
|
||||
deadline = new Deadline(new Date(), COMMENT);
|
||||
deadline.setId(ID);
|
||||
deadlines.add(deadline);
|
||||
|
||||
leader = Mockito.mock(User.class);
|
||||
|
||||
papers = new ArrayList<>();
|
||||
paperWithId = new Paper();
|
||||
paperWithId.setId(ID);
|
||||
paperWithId.setTitle(TITLE);
|
||||
papers.add(paperWithId);
|
||||
paperDto = new PaperDto(paperWithId);
|
||||
paperDtos.add(paperDto);
|
||||
|
||||
authors = new HashSet<>();
|
||||
author = leader;
|
||||
authors.add(author);
|
||||
|
||||
grantWithId.setId(ID);
|
||||
grantWithId.setTitle(TITLE);
|
||||
grantWithId.setComment(COMMENT);
|
||||
grantWithId.setDeadlines(deadlines);
|
||||
grantWithId.setLeader(leader);
|
||||
grantWithId.setPapers(papers);
|
||||
grantWithId.setAuthors(authors);
|
||||
|
||||
grants.add(grantWithId);
|
||||
grantDto = new GrantDto(grantWithId);
|
||||
grantDtos.add(grantDto);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getExistGrantById() {
|
||||
when(grantRepository.findOne(ID)).thenReturn(grantWithId);
|
||||
|
||||
GrantDto newGrantDto = new GrantDto(grantWithId);
|
||||
GrantDto result = grantService.getExistGrantById(ID);
|
||||
|
||||
assertEquals(newGrantDto.getId(), result.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAll() {
|
||||
when(grantRepository.findAll()).thenReturn(grants);
|
||||
|
||||
assertEquals(Collections.singletonList(grantWithId), grantService.findAll());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void create() throws IOException {
|
||||
when(deadlineService.saveOrCreate(new ArrayList<>())).thenReturn(deadlines);
|
||||
when(userService.getUserByLogin("admin")).thenReturn(leader);
|
||||
when(grantRepository.save(new Grant())).thenReturn(grantWithId);
|
||||
|
||||
Grant newGrant = new Grant();
|
||||
newGrant.setId(ID);
|
||||
newGrant.setTitle(TITLE);
|
||||
newGrant.setComment(COMMENT);
|
||||
newGrant.setDeadlines(deadlines);
|
||||
newGrant.setLeader(leader);
|
||||
|
||||
assertEquals(newGrant, grantService.create(grantDto));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getGrantStatuses() {
|
||||
assertEquals(Arrays.asList(Grant.GrantStatus.values()), grantService.getGrantStatuses());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getGrantPapers() {
|
||||
when(paperService.findAllSelect(Collections.singletonList(ID))).thenReturn(paperDtos);
|
||||
|
||||
assertEquals(paperDtos, grantService.getGrantPapers(Collections.singletonList(ID)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAllUncompletedPapers() {
|
||||
when(paperService.findAllNotCompleted()).thenReturn(paperDtos);
|
||||
paperDtos.stream()
|
||||
.forEach(paperDto -> {
|
||||
paperDto.setTitle(StringUtils.abbreviate(paperDto.getTitle(), MAX_DISPLAY_SIZE));
|
||||
});
|
||||
|
||||
assertEquals(paperDtos, grantService.getAllUncompletedPapers());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delete() throws IOException {
|
||||
when(grantRepository.findOne(ID)).thenReturn(grantWithId);
|
||||
assertTrue(grantService.delete(grantWithId.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeDeadline() {
|
||||
GrantDto newGrantDto = new GrantDto();
|
||||
newGrantDto.getRemovedDeadlineIds().add(ID);
|
||||
grantDto.getDeadlines().add(deadline);
|
||||
GrantDto result = grantService.removeDeadline(grantDto, INDEX);
|
||||
|
||||
assertEquals(newGrantDto.getRemovedDeadlineIds(), result.getRemovedDeadlineIds());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findById() {
|
||||
when(grantRepository.findOne(ID)).thenReturn(grantWithId);
|
||||
Grant findGrant = grantService.findById(ID);
|
||||
|
||||
assertEquals(grantWithId.getId(), findGrant.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void attachPaper() {
|
||||
when(grantRepository.findOne(ID)).thenReturn(grantWithId);
|
||||
when(paperService.findAllSelect(Collections.singletonList(ID))).thenReturn(paperDtos);
|
||||
GrantDto newGrantDto = new GrantDto(grantWithId);
|
||||
|
||||
if (!newGrantDto.getPaperIds().isEmpty()) {
|
||||
newGrantDto.getPapers().clear();
|
||||
newGrantDto.setPapers(paperDtos);
|
||||
} else {
|
||||
newGrantDto.getPapers().clear();
|
||||
}
|
||||
|
||||
assertEquals(newGrantDto.getPapers(), grantService.attachPaper(grantDto));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filterEmptyDeadlines() {
|
||||
when(grantRepository.findOne(ID)).thenReturn(grantWithId);
|
||||
GrantDto newGrantDto = new GrantDto(grantWithId);
|
||||
|
||||
newGrantDto.setDeadlines(newGrantDto.getDeadlines().stream()
|
||||
.filter(dto -> dto.getDate() != null || !StringUtils.isEmpty(dto.getDescription()))
|
||||
.collect(Collectors.toList()));
|
||||
|
||||
assertEquals(newGrantDto.getDeadlines(), grantService.filterEmptyDeadlines(grantDto));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue