Skip to content

Commit

Permalink
Add initial tests for Authentication endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
GustavoDinizMonteiro committed Oct 29, 2018
1 parent ceb8fd6 commit a76c884
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 39 deletions.
52 changes: 30 additions & 22 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

Expand All @@ -15,7 +16,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
<relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
Expand All @@ -28,12 +29,12 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<exclusions>
<exclusion>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</exclusion>
</exclusions>
<exclusions>
<exclusion>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -78,28 +79,35 @@
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>

<!-- To allow use java 10 -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version><!--$NO-MVN-MAN-VER$-->
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version><!--$NO-MVN-MAN-VER$ -->
</dependency>

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
<scope>provided</scope>
</dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
</dependency>


<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>


</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

@CrossOrigin
@RestController
@RequestMapping(path = Endpoints.LOGIN_ENDPOINT)
@RequestMapping(Endpoints.LOGIN_ENDPOINT)
public class AuthenticationController {

@Autowired
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/quem/me/ajuda/models/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,12 @@ public class Student {
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "tutor_info_id")
private TutorInfo tutorInfo;

public Student(String registration, String name, String phone, String email, String password) {
this.registration = registration;
this.name = name;
this.phone = phone;
this.email = email;
this.password = password;
}
}
16 changes: 0 additions & 16 deletions src/test/java/quem/me/ajuda/QuemMeAjudaApplicationTests.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
*
*/
package quem.me.ajuda.controllers;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;

import com.fasterxml.jackson.databind.ObjectMapper;

import quem.me.ajuda.constants.Endpoints;
import quem.me.ajuda.models.Student;
import quem.me.ajuda.services.StudentService;

/**
*
*/
@SpringBootTest
@RunWith(JUnitPlatform.class)
@ExtendWith(SpringExtension.class)
@TestInstance(Lifecycle.PER_CLASS)
class AuthenticationControllerTest {
private final String ENDPOINT = "/".concat(Endpoints.LOGIN_ENDPOINT);

private Student testUser;

private MockMvc mockMvc;

@Autowired
private StudentService studentService;

@Autowired
private ObjectMapper objectMapper;

/**
*
*/
@BeforeAll
void setUpAll() {
this.mockMvc = standaloneSetup(AuthenticationController.class).build();
}

@BeforeEach
void setUp() {
testUser = new Student("user", "pass", "phone", "email", "password");
this.studentService.create(testUser);
}

/**
* @throws Exception
*/
@Test
void testLoginSucessfully() throws Exception {
mockMvc.perform(post(ENDPOINT)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(testUser)))
.andExpect(status().isOk());
}

}

0 comments on commit a76c884

Please sign in to comment.