Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void loadComponents() {
}

@Test
public void testGetComponent() {
void testGetComponent() {
Component component = componentsRepository.save(new Component("Intel i5 12400F",
"Procesador de 6 núcleos y 12 hilos", ComponentType.CPU, "Intel", BigDecimal.valueOf(153.97), 14));

Expand All @@ -135,12 +135,12 @@ public void testGetComponent() {
}

@Test
public void testGetComponentNotFound() {
void testGetComponentNotFound() {
assertThrows(ResponseStatusException.class, () -> componentsService.getComponent(99L));
}

@Test
public void testGetComponentImage() throws IOException, SQLException {
void testGetComponentImage() throws IOException, SQLException {
Component component = componentsRepository.findAll().get(0);
Resource expectedImage = new ClassPathResource("/sample_images/ryzen-7.webp");
component.setImage(imageService.createImage(expectedImage.getInputStream()));
Expand All @@ -151,7 +151,7 @@ public void testGetComponentImage() throws IOException, SQLException {
}

@Test
public void testGetNullComponentImage() throws SQLException {
void testGetNullComponentImage() throws SQLException {
Resource actualImage = componentsService.getImageFile(1L);

assertEquals(null, actualImage);
Expand All @@ -162,6 +162,7 @@ void postComponentAsNoRegisteredUser() {
Component component = new Component("Intel i5 12400F", "Procesador de 6 núcleos y 12 hilos", ComponentType.CPU,
"Intel", BigDecimal.valueOf(153.97), 14);
assertThrows(AuthenticationCredentialsNotFoundException.class, () -> componentsService.save(component));
assertEquals(2, componentsRepository.count());
}

@Test
Expand All @@ -176,7 +177,7 @@ void postComponentAsNoAdmin() {
Component component = new Component("Intel i5 12400F", "Procesador de 6 núcleos y 12 hilos", ComponentType.CPU,
"Intel", BigDecimal.valueOf(153.97), 14);
assertThrows(AuthorizationDeniedException.class, () -> componentsService.save(component));

assertEquals(2, componentsRepository.count());
}

@Test
Expand All @@ -191,7 +192,73 @@ void postComponentAsAdmin() {
Component component = new Component("Intel i5 12400F", "Procesador de 6 núcleos y 12 hilos", ComponentType.CPU,
"Intel", BigDecimal.valueOf(153.97), 14);
assertDoesNotThrow(() -> componentsService.save(component));
assertEquals(3, componentsRepository.count());
}

@Test
void postVoidComponent() {
User admin = usersRepository.findByEmail("testadmin1@example.com").orElseThrow();

SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(
org.springframework.security.core.userdetails.User.withUsername(admin.getId().toString())
.password("").authorities("ROLE_ADMIN").build(),
null, java.util.List.of(new SimpleGrantedAuthority("ROLE_ADMIN"))));

Component newComponent = new Component("", "", ComponentType.CPU, "", BigDecimal.valueOf(100.00), 5);

assertThrows(IllegalArgumentException.class,
() -> componentsService.save(newComponent));
}

@Test
void postNullComponent() {
User admin = usersRepository.findByEmail("testadmin1@example.com").orElseThrow();

SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(
org.springframework.security.core.userdetails.User.withUsername(admin.getId().toString())
.password("").authorities("ROLE_ADMIN").build(),
null, java.util.List.of(new SimpleGrantedAuthority("ROLE_ADMIN"))));

Component newComponent = new Component(null, null, ComponentType.CPU, null, BigDecimal.valueOf(100.00), 5);

assertThrows(IllegalArgumentException.class,
() -> componentsService.save(newComponent));
}

@Test
void postExistingComponent() {
User admin = usersRepository.findByEmail("testadmin1@example.com").orElseThrow();

SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(
org.springframework.security.core.userdetails.User.withUsername(admin.getId().toString())
.password("").authorities("ROLE_ADMIN").build(),
null, java.util.List.of(new SimpleGrantedAuthority("ROLE_ADMIN"))));

Component newComponent = new Component("NVIDIA GeForce RTX 5060 WINDFORCE MAX OC", "sampleDescription",
ComponentType.CPU, "sampleBrand", BigDecimal.valueOf(100.00), 5);

assertThrows(IllegalArgumentException.class,
() -> componentsService.save(newComponent));
}

@Test
void postBadFormatComponent() {
User admin = usersRepository.findByEmail("testadmin1@example.com").orElseThrow();

SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(
org.springframework.security.core.userdetails.User.withUsername(admin.getId().toString())
.password("").authorities("ROLE_ADMIN").build(),
null, java.util.List.of(new SimpleGrantedAuthority("ROLE_ADMIN"))));

Component newComponent = new Component("sampleName", "sampleDescription", ComponentType.CPU, "sampleBrand",
BigDecimal.valueOf(-100.00), -5);

assertThrows(IllegalArgumentException.class,
() -> componentsService.save(newComponent));
}

@Test
Expand Down Expand Up @@ -359,9 +426,8 @@ void postImageWithInvalidFile() {

@Test
void deleteComponentAsNotLoggedIn() {

assertThrows(AuthenticationCredentialsNotFoundException.class, () -> componentsService.delete(1L));

assertEquals(2, componentsRepository.count());
}

@Test
Expand All @@ -377,7 +443,6 @@ void deleteComponentAsLoggedInNoAdmin() {

assertThrows(AuthorizationDeniedException.class, () -> componentsService.delete(component.getId()));
assertEquals(2, componentsRepository.count());

}

@Test
Expand All @@ -393,7 +458,6 @@ void deleteComponentAsAdmin() {

assertDoesNotThrow(() -> componentsService.delete(component.getId()));
assertEquals(1, componentsRepository.count());

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ void deleteReviewAsNotLoggedIn() {

assertThrows(AuthenticationCredentialsNotFoundException.class,
() -> reviewsService.delete(component.getId(), review.getId()));
assertEquals(2, reviewsRepository.count());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,8 @@ void updateImagesAsAdmin() throws IOException, SQLException {

@Test
void deleteUserAsNotLoggedIn() {

assertThrows(AuthenticationCredentialsNotFoundException.class, () -> usersService.deleteUser(1L));

assertEquals(2, usersRepository.count());
}

@Test
Expand Down
12 changes: 6 additions & 6 deletions frontend/app/services/components-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function getComponents(page: number): Promise<ComponentsPageResult>
const res = await fetch(url.toString());

if (!res.ok) {
throw new Error("Error fetching components");
throw new Error("Hubo un error al cargar los componentes.");
}

const data = await res.json();
Expand All @@ -46,7 +46,7 @@ export async function getRecentComponents(): Promise<ComponentDTO[]> {
const res = await fetch(url.toString());

if (!res.ok) {
throw new Error("Error fetching recent components");
throw new Error("Hubo un error al cargar los componentes.");
}

const data = await res.json();
Expand All @@ -67,7 +67,7 @@ export async function getComponent(id: number): Promise<ComponentDTO> {
const res = await fetch(url.toString());

if (!res.ok) {
throw new Error("Error fetching component");
throw new Error("Hubo un error al cargar el componente.");
}

return await res.json();
Expand Down Expand Up @@ -99,7 +99,7 @@ export async function createComponent (name: string, description: string, type:
message = undefined;
}

throw new Error(message ?? "There was an error while creating the component.");
throw new Error(message ?? "Hubo un error al crear el componente.");
}

return await res.json();
Expand Down Expand Up @@ -131,7 +131,7 @@ export async function deleteComponent(id: number): Promise<void> {
});

if (!response.ok) {
throw new Error("Error deleting component");
throw new Error("Hubo un error al eliminar el componente.");
}

}
Expand Down Expand Up @@ -174,7 +174,7 @@ export async function getComponentTypes(): Promise<string[]> {
const response = await fetch(url.toString());

if (!response.ok) {
throw new Error("No se pudieron obtener los tipos de componentes");
throw new Error("No se pudieron obtener los tipos de componentes.");
}

return await response.json();
Expand Down
6 changes: 3 additions & 3 deletions frontend/app/services/users-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export async function logIn(email: string, pass: string): Promise<void> {
});

if (!res.ok) {
throw new Error("There was an error while logging in.");
throw new Error("Hubo un error al iniciar sesión.");
}

}
Expand All @@ -58,7 +58,7 @@ export async function logOut(): Promise<void> {
});

if (!res.ok) {
throw new Error("There was an error while logging out.");
throw new Error("Hubo un error al cerrar sesión.");
}

}
Expand Down Expand Up @@ -89,7 +89,7 @@ export async function register(name: string, surname: string, username: string,
message = undefined;
}

throw new Error(message ?? "There was an error while registering.");
throw new Error(message ?? "Hubo un error al registrarse.");
}

return await res.json();
Expand Down
Loading
Loading