Skip to content
This repository has been archived by the owner on Sep 22, 2023. It is now read-only.

Tests for user, level and chapter features in backend #17

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
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 @@ -32,12 +32,14 @@ public LevelController(LevelService levelService, ScoreService scoreService, Cod
}

@GetMapping("/{level_id}/leaderboards")
public List<Score> getLevelLeaderboard(@PathVariable(value = "level_id") ObjectId levelId, Settings settings) {
public List<Score> getLevelLeaderboard(@PathVariable(value = "level_id") String levelIdStr, @RequestBody Settings settings) {
ObjectId levelId = new ObjectId(levelIdStr);
return scoreService.getScoresByLevelAndSettings(levelId, settings);
}

@GetMapping("/{level_id}/documentation")
public List<Documentation> getLevelDocumentation(@PathVariable(value = "level_id") ObjectId levelId, ProgrammingLanguage language) {
public List<Documentation> getLevelDocumentation(@PathVariable(value = "level_id") String levelIdStr, ProgrammingLanguage language) {
ObjectId levelId = new ObjectId(levelIdStr);
Level level = levelService.findByLevelId(levelId);
return level.getDocumentation();
}
Expand All @@ -48,7 +50,8 @@ public List<Level> getLevelsList(ProgrammingLanguage programmingLanguage, SkillL
}

@GetMapping("/{level_id}/solutions")
public List<Solution> getLevelSolutions(@PathVariable(value = "level_id") ObjectId levelId, ProgrammingLanguage language) {
public List<Solution> getLevelSolutions(@PathVariable(value = "level_id") String levelIdStr, ProgrammingLanguage language) {
ObjectId levelId = new ObjectId(levelIdStr);
Level level = levelService.findByLevelId(levelId);
return level.getSolutions();
}
Expand All @@ -74,7 +77,8 @@ public MessageResponse submitLevelSolution(@PathVariable(value = "level_id") Str
}

@GetMapping("/{level_id}")
public Level getCurrentLevel(@PathVariable(value = "level_id") ObjectId levelId) {
public Level getCurrentLevel(@PathVariable(value = "level_id") String levelIdStr) {
ObjectId levelId = new ObjectId(levelIdStr);
return levelService.findByLevelId(levelId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import pt.ua.deti.codespell.codespellbackend.model.Achievement;
import pt.ua.deti.codespell.codespellbackend.model.Game;
import pt.ua.deti.codespell.codespellbackend.model.User;
import pt.ua.deti.codespell.codespellbackend.request.ChangeNameRequest;
import pt.ua.deti.codespell.codespellbackend.request.ChangePasswordRequest;
import pt.ua.deti.codespell.codespellbackend.request.MessageResponse;
import pt.ua.deti.codespell.codespellbackend.service.UserService;

Expand Down Expand Up @@ -50,18 +53,20 @@ public List<Achievement> getUserAchievements(@PathVariable(value = "username") S
}

@PutMapping("/{username}/password")
public MessageResponse changePassword(@PathVariable(value = "username") String username, String newPassword) {
public MessageResponse changePassword(@PathVariable(value = "username") String username, @RequestBody ChangePasswordRequest changePasswordRequest) {
User user = getUserDetails(username);
String newPassword = changePasswordRequest.getPassword();
user.setPassword(passwordEncoder.encode(newPassword));

userService.updateUser(user);
return new MessageResponse(Date.from(Instant.now()), "Your password was successfully changed.");
}

@PutMapping("/{username}/name")
public MessageResponse changeName(@PathVariable(value = "username") String username, String newName) {
public MessageResponse changeName(@PathVariable(value = "username") String username, @RequestBody ChangeNameRequest changeNameRequest) {
User user = getUserDetails(username);
String newName = changeNameRequest.getName();
user.setName(newName);

userService.updateUser(user);
return new MessageResponse(Date.from(Instant.now()), "Your name was successfully changed.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package pt.ua.deti.codespell.codespellbackend.request;

import lombok.Data;
import lombok.Generated;

@Data
@Generated
public class ChangeNameRequest {
private final String email;
private final String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package pt.ua.deti.codespell.codespellbackend.request;

import lombok.Data;
import lombok.Generated;

@Data
@Generated
public class ChangePasswordRequest {
private final String email;
private final String password;
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@ public void registerUser(User user) {

}

public void updateUser(User user) {

if (!userRepository.existsByUsername(user.getUsername()))
throw new UserNotFoundException(String.format("The user %s could not be found.", user.getUsername()));
userRepository.save(user);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package pt.ua.deti.codespell.codespellbackend.controller;

import java.util.Collections;
import java.util.List;

import org.bson.types.ObjectId;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import pt.ua.deti.codespell.codespellbackend.model.*;
import pt.ua.deti.codespell.codespellbackend.repository.ChapterRepository;
import pt.ua.deti.codespell.codespellbackend.request.LoginRequest;
import pt.ua.deti.codespell.codespellbackend.request.MessageResponse;
import pt.ua.deti.codespell.codespellbackend.security.auth.AuthTokenResponse;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureDataMongo
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class ChapterControllerTest {

@Autowired
private TestRestTemplate restTemplate;

@Autowired
private ChapterRepository chapterRepository;

private HttpEntity<String> jwtEntity;

@BeforeAll
void beforeAll() {
LoginRequest loginRequest = new LoginRequest("[email protected]", "artur123");
restTemplate.postForEntity("/api/auth/register", new User("artur", "[email protected]", "artur123", Collections.emptyList()), MessageResponse.class);
ResponseEntity<AuthTokenResponse> loginResponse = restTemplate.postForEntity("/api/auth/login", loginRequest, AuthTokenResponse.class);
AuthTokenResponse authTokenResponse = loginResponse.getBody();

String token = "Bearer " + authTokenResponse.getToken();
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", token);
jwtEntity = new HttpEntity<String>(headers);
}

@BeforeEach
void setUp() {
Chapter chapter1 = new Chapter(new ObjectId(), "Variables", "Learn the basics about Java variables.",
1, Collections.emptyList(), Collections.emptyList(), new Settings(ProgrammingLanguage.JAVA, SkillLevel.NOVICE));
Chapter chapter2 = new Chapter(new ObjectId(), "Object Oriented Programming", "Learn the basics about Java OOP.",
2, Collections.emptyList(), Collections.emptyList(), new Settings(ProgrammingLanguage.JAVA, SkillLevel.NOVICE));

chapterRepository.save(chapter1);
chapterRepository.save(chapter2);
}

@AfterEach
void tearDown() {
chapterRepository.deleteAll();
}

@Test
@DisplayName("Return the list of all chapters")
void getAllChapters() {
String url = "/api/chapter/";

ResponseEntity<List<Chapter>> response = restTemplate
.exchange(url, HttpMethod.GET, jwtEntity, new ParameterizedTypeReference<List<Chapter>>() {
});

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getBody()).extracting(Chapter::getTitle).hasSize(2).doesNotContainNull();
assertThat(response.getBody()).extracting(Chapter::getTitle).containsOnly("Variables", "Object Oriented Programming");
assertThat(response.getBody()).extracting(Chapter::getDescription).containsOnly("Learn the basics about Java variables.", "Learn the basics about Java OOP.");
}
}
Loading