Skip to content

Commit

Permalink
Improve reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
Nonononoki committed May 12, 2024
1 parent b52fc02 commit 3430cb3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 45 deletions.
20 changes: 11 additions & 9 deletions src/main/java/com/nonononoki/alovoa/service/AdminService.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
Expand Down Expand Up @@ -95,16 +94,19 @@ public void sendMailAll(MailDto dto) throws AlovoaException, MessagingException,
public void deleteReport(long id) throws AlovoaException {
checkRights();

UserReport report = userReportRepo.findById(id).orElse(null);
if (report == null) {
UserReport r = userReportRepo.findById(id).orElse(null);
if (r == null) {
throw new AlovoaException("report_not_found");
}
try {
User u = report.getUserFrom();
u.getReported().remove(report);
userRepo.saveAndFlush(u);
} catch (Exception e) {
userReportRepo.delete(report);

for (UserReport report : userReportRepo.findByUserTo(r.getUserTo())) {
try {
User u = report.getUserFrom();
u.getReported().remove(report);
userRepo.saveAndFlush(u);
} catch (Exception e) {
userReportRepo.delete(report);
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/static/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ function viewProfileMedia(uuid) {
window.open(url, '_blank').focus();
}

function deleteReport(id) {
function deleteReport(id, idReal) {
$.ajax({
type: "POST",
url: "/admin/report/delete/" + id,
success: function() {
$("#report" + id).hide();
$(".user" + idReal).hide();
},
error: function(e) {
console.log(e);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ <h4 th:text="${report.userTo.firstName}"></h4>
class="fa fa-user"></i></button>
<button class="button" th:onclick="viewProfileMedia([[${report.toUuid}]])"><i
class="fa fa-photo-video"></i></button>
<button class="button" th:onclick="deleteReport([[${report.id}]])"
<button class="button" th:onclick="deleteReport([[${report.id}]], [[${report.userTo.id}]])"
th:text="#{admin.delete-report}"></button>
<button class="button" th:onclick="removeDescription([[${report.toUuid}]])"
th:text="#{admin.remove-description}"></button>
Expand Down
67 changes: 33 additions & 34 deletions src/test/java/com/nonononoki/alovoa/service/AdminServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
package com.nonononoki.alovoa.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.transaction.annotation.Transactional;

import com.nonononoki.alovoa.Tools;
import com.nonononoki.alovoa.component.TextEncryptorConverter;
import com.nonononoki.alovoa.entity.Captcha;
Expand All @@ -26,11 +8,28 @@
import com.nonononoki.alovoa.entity.user.UserReport;
import com.nonononoki.alovoa.model.ContactDto;
import com.nonononoki.alovoa.model.MailDto;
import com.nonononoki.alovoa.model.UserDto;
import com.nonononoki.alovoa.repo.ContactRepository;
import com.nonononoki.alovoa.repo.ConversationRepository;
import com.nonononoki.alovoa.repo.UserReportRepository;
import com.nonononoki.alovoa.repo.UserRepository;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.transaction.annotation.Transactional;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;

@SpringBootTest
@ActiveProfiles("test")
Expand Down Expand Up @@ -107,6 +106,7 @@ void test() throws Exception {
User adminUser = allUsers.get(0);
User user1 = testUsers.get(0);
User user2 = testUsers.get(1);
User user3 = testUsers.get(2);

Mockito.when(authService.getCurrentUser()).thenReturn(adminUser);
Mockito.when(authService.getCurrentUser(true)).thenReturn(adminUser);
Expand All @@ -124,14 +124,20 @@ void test() throws Exception {
adminService.sendMailSingle(mailDto);

adminService.hideContact(contactTest().getId());
adminService.deleteReport(reportTest(testUsers, adminUser).getId());

assertEquals(0, userReportRepo.count());
reportTest(user3, user2, adminUser);
assertEquals(1, userReportRepo.count());
UserReport report = reportTest(user1, user2, adminUser);
user2 = userRepo.findByEmail(user2.getEmail());
adminService.deleteReport(report.getId());
assertEquals(0, userReportRepo.count());

adminService.banUser(user1.getUuid());
User bannedUser = userRepo.findById(user1.getId()).get();
assertEquals(true, bannedUser.isDisabled());

assertEquals(true, adminService.userExists(URLDecoder.decode(user2.getEmail(), StandardCharsets.UTF_8)));

assertTrue(bannedUser.isDisabled());
assertTrue(adminService.userExists(URLDecoder.decode(user2.getEmail(), StandardCharsets.UTF_8)));

double donationAmount = 10.00;
adminService.addDonation(URLDecoder.decode(user2.getEmail(), StandardCharsets.UTF_8), donationAmount);
assertEquals(donationAmount, user2.getTotalDonations());
Expand All @@ -153,19 +159,12 @@ private Contact contactTest() throws Exception {
return c;
}

private UserReport reportTest(List<User> testUsers, User adminUser) throws Exception {

User user1 = testUsers.get(0);
User user2 = testUsers.get(1);

private UserReport reportTest(User user1, User user2, User adminUser) throws Exception {
Mockito.when(authService.getCurrentUser()).thenReturn(user1);

assertEquals(0, userReportRepo.count());
Mockito.when(authService.getCurrentUser(true)).thenReturn(user1);
UserReport report = userService.reportUser(user2.getUuid(), "report");
assertEquals(1, userReportRepo.count());

Mockito.when(authService.getCurrentUser()).thenReturn(adminUser);

Mockito.when(authService.getCurrentUser(true)).thenReturn(adminUser);
return report;
}

Expand Down

0 comments on commit 3430cb3

Please sign in to comment.