mirror of
https://github.com/usatiuk/y.git
synced 2025-10-28 18:37:47 +01:00
some more tests
This commit is contained in:
@@ -50,12 +50,12 @@ public class Person {
|
|||||||
@JoinTable(name = "person_follows",
|
@JoinTable(name = "person_follows",
|
||||||
joinColumns = @JoinColumn(name = "follower"),
|
joinColumns = @JoinColumn(name = "follower"),
|
||||||
inverseJoinColumns = @JoinColumn(name = "followee"))
|
inverseJoinColumns = @JoinColumn(name = "followee"))
|
||||||
private Collection<Person> following;
|
private Collection<Person> following = new ArrayList<>();
|
||||||
|
|
||||||
@ManyToMany(mappedBy = "following")
|
@ManyToMany(mappedBy = "following")
|
||||||
private Collection<Person> followers;
|
private Collection<Person> followers = new ArrayList<>();
|
||||||
|
|
||||||
@ManyToMany(mappedBy = "members")
|
@ManyToMany(mappedBy = "members")
|
||||||
private Collection<Chat> chats;
|
private Collection<Chat> chats = new ArrayList<>();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package com.usatiuk.tjv.y.server.repository;
|
||||||
|
|
||||||
|
import com.usatiuk.tjv.y.server.entity.Person;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||||
|
|
||||||
|
@DataJpaTest
|
||||||
|
public class PersonRepositoryTest {
|
||||||
|
@Autowired
|
||||||
|
private PersonRepository personRepository;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void findByUsernameOrUuidTest() {
|
||||||
|
var person = personRepository.save(new Person().setUsername("u1").setFullName("fn").setPassword("pass"));
|
||||||
|
|
||||||
|
Assertions.assertEquals(person, personRepository.findByUsernameOrId("u1").get());
|
||||||
|
Assertions.assertEquals(person, personRepository.findByUsernameOrId(person.getUuid()).get());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,159 @@
|
|||||||
|
package com.usatiuk.tjv.y.server.service;
|
||||||
|
|
||||||
|
import com.usatiuk.tjv.y.server.dto.PersonCreateTo;
|
||||||
|
import com.usatiuk.tjv.y.server.dto.converters.PersonMapper;
|
||||||
|
import com.usatiuk.tjv.y.server.entity.Person;
|
||||||
|
import com.usatiuk.tjv.y.server.repository.PersonRepository;
|
||||||
|
import org.junit.jupiter.api.*;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.security.access.AccessDeniedException;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
import org.springframework.security.test.context.support.TestExecutionEvent;
|
||||||
|
import org.springframework.security.test.context.support.WithUserDetails;
|
||||||
|
import org.springframework.test.jdbc.JdbcTestUtils;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
@TestInstance(PER_CLASS)
|
||||||
|
@Transactional
|
||||||
|
public class PersonServiceImplIntegrationTest {
|
||||||
|
@Autowired
|
||||||
|
private PersonServiceImpl personService;
|
||||||
|
@Autowired
|
||||||
|
private PersonRepository personRepository;
|
||||||
|
@Autowired
|
||||||
|
private JdbcTemplate jdbcTemplate;
|
||||||
|
@Autowired
|
||||||
|
private PasswordEncoder passwordEncoder;
|
||||||
|
@Autowired
|
||||||
|
private PersonMapper personMapper;
|
||||||
|
|
||||||
|
protected static final String person1Password = "p1p";
|
||||||
|
protected Person person1;
|
||||||
|
protected static final String person2Password = "p2p";
|
||||||
|
protected Person person2;
|
||||||
|
protected static final String person3Password = "p3p";
|
||||||
|
protected Person person3;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
void setup() {
|
||||||
|
person1 = personRepository.save(
|
||||||
|
new Person()
|
||||||
|
.setUsername("person1")
|
||||||
|
.setFullName("Person 1")
|
||||||
|
.setPassword(passwordEncoder.encode(person1Password)));
|
||||||
|
person2 = personRepository.save(
|
||||||
|
new Person()
|
||||||
|
.setUsername("person2")
|
||||||
|
.setFullName("Person 2")
|
||||||
|
.setPassword(passwordEncoder.encode(person2Password)).setFollowing(new ArrayList<>(List.of(person1))));
|
||||||
|
person3 = personRepository.save(
|
||||||
|
new Person()
|
||||||
|
.setUsername("person3")
|
||||||
|
.setFullName("Person 3")
|
||||||
|
.setPassword(passwordEncoder.encode(person3Password))
|
||||||
|
.setFollowing(List.of(person2, person1)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
void erase() {
|
||||||
|
JdbcTestUtils.deleteFromTables(jdbcTemplate, "person_follows", "chat_person", "post", "message", "chat", "person");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithUserDetails(value = "person1", setupBefore = TestExecutionEvent.TEST_EXECUTION)
|
||||||
|
void shouldAddFollowing() {
|
||||||
|
var auth = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
|
||||||
|
Assertions.assertEquals(0, personService.getFollowing(auth).size());
|
||||||
|
personService.addFollower(auth, person2.getUuid());
|
||||||
|
Assertions.assertIterableEquals(List.of(personMapper.makeDto(person2)), personService.getFollowing(auth));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithUserDetails(value = "person2", setupBefore = TestExecutionEvent.TEST_EXECUTION)
|
||||||
|
void shouldRemoveFollowing() {
|
||||||
|
var auth = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
|
||||||
|
Assertions.assertEquals(1, personService.getFollowing(auth).size());
|
||||||
|
personService.removeFollower(auth, person1.getUuid());
|
||||||
|
Assertions.assertEquals(0, personService.getFollowing(auth).size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void firstSignedUpShouldBeAdmin() {
|
||||||
|
personService.signup(new PersonCreateTo("newadmin", "full name admin", "adminpass"));
|
||||||
|
var created = personService.readByUsername("newadmin");
|
||||||
|
Assertions.assertTrue(created.isAdmin());
|
||||||
|
Assertions.assertTrue(personService.getAdmins().contains(created));
|
||||||
|
|
||||||
|
personService.signup(new PersonCreateTo("newnotadmin", "full name notadmin", "notadminpass"));
|
||||||
|
var created2 = personService.readByUsername("newnotadmin");
|
||||||
|
Assertions.assertFalse(created2.isAdmin());
|
||||||
|
Assertions.assertFalse(personService.getAdmins().contains(created2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@TestInstance(PER_CLASS)
|
||||||
|
@Transactional
|
||||||
|
class AdminTests {
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
void setup() {
|
||||||
|
personRepository.save(person1.setAdmin(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
void teardown() {
|
||||||
|
personRepository.save(person1.setAdmin(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithUserDetails(value = "person1", setupBefore = TestExecutionEvent.TEST_EXECUTION)
|
||||||
|
void shouldAddRemoveAdminIfAdmin() {
|
||||||
|
personService.addAdmin(person2.getUuid());
|
||||||
|
var updatedP2 = personService.readByUsername(person2.getUsername());
|
||||||
|
Assertions.assertTrue(updatedP2.isAdmin());
|
||||||
|
Assertions.assertTrue(personService.getAdmins().contains(updatedP2));
|
||||||
|
|
||||||
|
personService.removeAdmin(person2.getUuid());
|
||||||
|
updatedP2 = personService.readByUsername(person2.getUsername());
|
||||||
|
Assertions.assertFalse(updatedP2.isAdmin());
|
||||||
|
Assertions.assertFalse(personService.getAdmins().contains(updatedP2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithUserDetails(value = "person2", setupBefore = TestExecutionEvent.TEST_EXECUTION)
|
||||||
|
void shouldNotAddRemoveAdminIfNotAdmin() {
|
||||||
|
Assertions.assertThrows(AccessDeniedException.class, () ->
|
||||||
|
personService.addAdmin(person2.getUuid())
|
||||||
|
);
|
||||||
|
var updatedP = personService.readByUsername(person2.getUsername());
|
||||||
|
Assertions.assertFalse(updatedP.isAdmin());
|
||||||
|
Assertions.assertFalse(personService.getAdmins().contains(updatedP));
|
||||||
|
|
||||||
|
Assertions.assertThrows(AccessDeniedException.class, () ->
|
||||||
|
personService.addAdmin(person3.getUuid())
|
||||||
|
);
|
||||||
|
var updatedP3 = personService.readByUsername(person3.getUsername());
|
||||||
|
Assertions.assertFalse(updatedP3.isAdmin());
|
||||||
|
Assertions.assertFalse(personService.getAdmins().contains(updatedP3));
|
||||||
|
|
||||||
|
Assertions.assertThrows(AccessDeniedException.class, () ->
|
||||||
|
personService.removeAdmin(person1.getUuid())
|
||||||
|
);
|
||||||
|
var updatedP1 = personService.readByUsername(person1.getUsername());
|
||||||
|
Assertions.assertTrue(updatedP1.isAdmin());
|
||||||
|
Assertions.assertTrue(personService.getAdmins().contains(updatedP1));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user