Fakultas Ilmu Komputer UI

Commit 761492f8 authored by Johanes Steven's avatar Johanes Steven

Merge branch 'johanes' into 'master'

Add MockMVC with roles

See merge request !9
parents 90223aed 1df2f59b
Pipeline #81846 failed with stages
in 20 minutes and 50 seconds
...@@ -31,6 +31,7 @@ repositories { ...@@ -31,6 +31,7 @@ repositories {
} }
dependencies { dependencies {
implementation 'junit:junit:4.13.1'
compileOnly 'org.projectlombok:lombok' compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok'
implementation group: 'org.springframework.boot', name: 'spring-boot-configuration-processor' implementation group: 'org.springframework.boot', name: 'spring-boot-configuration-processor'
...@@ -50,6 +51,9 @@ dependencies { ...@@ -50,6 +51,9 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation('org.springframework.boot:spring-boot-starter-security') implementation('org.springframework.boot:spring-boot-starter-security')
testImplementation 'org.springframework.security:spring-security-test:4.2.3.RELEASE'
} }
test { test {
......
spring.datasource.url=jdbc:postgresql://localhost:5432/tasc spring.datasource.url=jdbc:postgresql://ec2-3-212-75-25.compute-1.amazonaws.com:5432/dds939tlhunclk
spring.datasource.username=postgres spring.datasource.username=cnxgzqmgtjwgwt
spring.datasource.password=admin spring.datasource.password=2e6f389654422dbd3406d431a243f61e5e5543ddc80a84d14afd6dcbe910f4d8
spring.datasource.currentSchema=tasc spring.datasource.currentSchema=public
spring.datasource.hikari.maximum-pool-size=10 spring.datasource.hikari.maximum-pool-size=10
logging.level.root=info logging.level.root=info
\ No newline at end of file
package com.adpro.tasc.controller; package com.adpro.tasc.controller;
import org.junit.Before;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
...@@ -15,9 +21,20 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. ...@@ -15,9 +21,20 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@AutoConfigureMockMvc @AutoConfigureMockMvc
public class HelloControllerTest { public class HelloControllerTest {
@Autowired
private WebApplicationContext context;
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
@Before
public void setup() {
mockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
}
@Test @Test
public void DefaultLoginMessage() throws Exception { public void DefaultLoginMessage() throws Exception {
this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk()) this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk())
...@@ -30,4 +47,26 @@ public class HelloControllerTest { ...@@ -30,4 +47,26 @@ public class HelloControllerTest {
.andExpect(content().string(containsString("Register"))); .andExpect(content().string(containsString("Register")));
} }
@Test
public void loginWithAdminTest() throws Exception {
this.mockMvc
.perform(get("/home-admin").with(user("admin").password("{noop}123").roles("ADMIN")))
.andDo(print()).andExpect(status().isOk());
}
@Test
public void loginWithStudentTest() throws Exception {
this.mockMvc
.perform(get("/home-student")
.with(user("student").password("{noop}123").roles("STUDENT")))
.andDo(print()).andExpect(status().isOk());
}
@Test
public void loginWithTATest() throws Exception {
this.mockMvc
.perform(get("/home-TA").with(user("ta").password("{noop}123").roles("TEACHING_ASSISTANT")))
.andDo(print()).andExpect(status().isOk());
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment