1
0
Fork 0
mirror of https://codeberg.org/beerbrawl/beerbrawl.git synced 2024-09-23 05:40:51 +02:00

added testbase delete db, confirmdialog, updated userendpoint

This commit is contained in:
rafael 2024-05-19 11:24:07 +02:00
parent 64cdb3a2c6
commit b9e9b37700
6 changed files with 103 additions and 5 deletions

View file

@ -125,8 +125,10 @@ public class UserEndpoint {
/**
* Get Username and password for user.
* The target user is retrieved through the JWT
*
* @param username target user to extract more information
*/
@GetMapping("/detail/{username}")
@GetMapping("/{username}")
@ResponseStatus(HttpStatus.OK)
@PreAuthorize("isAuthenticated()")
@Operation(summary = "Get detailed information about user and their tournaments.", security = @SecurityRequirement(name = "apiKey"))

View file

@ -0,0 +1,18 @@
package at.ac.tuwien.sepr.groupphase.backend.basetest;
import at.ac.tuwien.sepr.groupphase.backend.repository.*;
import org.junit.jupiter.api.AfterEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
public abstract class TestBase {
@Autowired
private UserRepository userRepository;
@AfterEach
public void clearDatabase() throws Exception {
userRepository.deleteAll(); // cascading should clear all for test user
}
}

View file

@ -4,11 +4,15 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.InstanceOfAssertFactories.stream;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import at.ac.tuwien.sepr.groupphase.backend.basetest.TestBase;
import at.ac.tuwien.sepr.groupphase.backend.basetest.TestData;
import at.ac.tuwien.sepr.groupphase.backend.endpoint.UserEndpoint;
import at.ac.tuwien.sepr.groupphase.backend.endpoint.dto.UserLoginDto;
import at.ac.tuwien.sepr.groupphase.backend.entity.ApplicationUser;
import at.ac.tuwien.sepr.groupphase.backend.repository.UserRepository;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ -16,6 +20,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.security.web.header.Header;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ -30,13 +36,16 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
@AutoConfigureMockMvc
public class UserEndpointTest implements TestData {
public class UserEndpointTest extends TestBase implements TestData {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private UserRepository userRepository;
@Test
public void tryRegisterNewAccountAndTryAuthenticateWithIt() throws Exception {
var dto = UserLoginDto.UserLoginDtoBuilder.anUserLoginDto()
@ -72,5 +81,69 @@ public class UserEndpointTest implements TestData {
.isNotNull()
.hasSizeGreaterThanOrEqualTo(1)
.startsWith("Bearer ");
//Todo: Prune database after every test run (automate)
userRepository.deleteAll();
}
@Test
public void deleteUser_SuccessfulDeletion_ReturnsNoContent() throws Exception {
var dto = UserLoginDto.UserLoginDtoBuilder.anUserLoginDto()
.withUsername("foo")
.withPassword("12345678")
.build();
var response = mockMvc
.perform(MockMvcRequestBuilders
.post("/api/v1/user/register")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(this.objectMapper.writeValueAsString(dto)))
.andExpect(status().isCreated())
.andReturn().getResponse();
var bearer_token = response.getContentAsString();
mockMvc.perform(delete("/api/v1/user/{username}", dto.getUsername())
.header("Authorization", bearer_token)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
}
@Test
public void deleteUser_UnsuccessfulDeletion_ReturnsForbidden() throws Exception {
var dto = UserLoginDto.UserLoginDtoBuilder.anUserLoginDto()
.withUsername("foo")
.withPassword("12345678")
.build();
mockMvc.perform(MockMvcRequestBuilders
.post("/api/v1/user/register")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(this.objectMapper.writeValueAsString(dto)))
.andExpect(status().isCreated())
.andReturn().getResponse();
var dto2 = UserLoginDto.UserLoginDtoBuilder.anUserLoginDto()
.withUsername("faa")
.withPassword("12341234")
.build();
var response = mockMvc
.perform(MockMvcRequestBuilders
.post("/api/v1/user/register")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(this.objectMapper.writeValueAsString(dto2)))
.andExpect(status().isCreated())
.andReturn().getResponse();
var wrong_token = response.getContentAsString();
mockMvc.perform(delete("/api/v1/user/{username}", dto.getUsername())
.header("Authorization", wrong_token)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isForbidden());
//userRepository.deleteAll();
}
}

View file

@ -1,5 +1,6 @@
package at.ac.tuwien.sepr.groupphase.backend.unittests;
import at.ac.tuwien.sepr.groupphase.backend.basetest.TestBase;
import at.ac.tuwien.sepr.groupphase.backend.basetest.TestData;
import at.ac.tuwien.sepr.groupphase.backend.endpoint.dto.UserLoginDto;
import at.ac.tuwien.sepr.groupphase.backend.entity.ApplicationUser;
@ -39,7 +40,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
@Transactional
public class UserDetailServiceTest implements TestData {
public class UserDetailServiceTest extends TestBase implements TestData {
@Autowired
private UserService userService;

View file

@ -207,7 +207,7 @@ export class UserEndpointService {
}
}
let localVarPath = `/api/v1/user/detail/${this.configuration.encodeParam({name: "username", value: username, in: "path", style: "simple", explode: false, dataType: "string", dataFormat: undefined})}`;
let localVarPath = `/api/v1/user/${this.configuration.encodeParam({name: "username", value: username, in: "path", style: "simple", explode: false, dataType: "string", dataFormat: undefined})}`;
return this.httpClient.request<UserDetailDto>('get', `${this.configuration.basePath}${localVarPath}`,
{
context: localVarHttpContext,

View file

@ -7,7 +7,11 @@
<p><strong>Closed Registrations:</strong> {{ closedTournaments }}</p>
<p><strong>List of Tournaments:</strong></p>
<ul>
<li *ngFor="let tournament of tournaments">{{ tournament }}</li>
@for (tournament of tournaments; track tournament) {
<li>{{ tournament }}</li>
} @empty {
<li>Currently no tournaments exist.</li>
}
</ul>
</mat-card-content>
<mat-card-actions>