-
Notifications
You must be signed in to change notification settings - Fork 575
Expand file tree
/
Copy pathStudentControllerTest.java
More file actions
63 lines (51 loc) · 2.71 KB
/
StudentControllerTest.java
File metadata and controls
63 lines (51 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.example.demo.student;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class StudentControllerTest {
private static final String URL = "api/v1/students/";
@Autowired
private WebApplicationContext context;
private MockMvc mvc;
@BeforeEach
public void setup() {
mvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
}
@ParameterizedTest
@CsvSource({"annasmith,password,200,true", "fred,password123,401,false"})
public void testOnlyCorrectlyAuthenticatedUsersCanObtainJwtToken(String username, String password, int statusCode, boolean checkToken) throws Exception {
String jWtToken = performLogin(username, password, statusCode);
if(checkToken) {
assertThat(jWtToken.startsWith("Bearer"));
} else {
assertThat(jWtToken).isNullOrEmpty();
}
}
@ParameterizedTest
@CsvSource({"annasmith,password,200", "linda,password,403", "tom,password,403"})
public void testOnlyAuthenticatedUserWithStudentRoleCanAccessAPI(String username, String password, int statusCode) throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/api/v1/students/1")
.header("Authorization", performLogin(username, password, 200)))
.andExpect(status().is(statusCode));
}
private String performLogin(String username, String password, int expectedStatusCode) throws Exception {
String body = "{\"username\":\"" + username + "\", \"password\":\"" + password + "\"}";
MvcResult result = mvc.perform(post("/login").content(body)).andExpect(status().is(expectedStatusCode)).andReturn();
return result.getResponse().getHeader("Authorization");
}
}