Fakultas Ilmu Komputer UI

Commit 1df2f59b authored by Johanes Steven's avatar Johanes Steven

Add MockMVC with roles

parent e3029a06
Pipeline #81839 failed with stages
in 21 minutes and 14 seconds
......@@ -31,6 +31,7 @@ repositories {
}
dependencies {
implementation 'junit:junit:4.13.1'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
implementation group: 'org.springframework.boot', name: 'spring-boot-configuration-processor'
......@@ -50,6 +51,9 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation('org.springframework.boot:spring-boot-starter-security')
testImplementation 'org.springframework.security:spring-security-test:4.2.3.RELEASE'
}
test {
......
spring.datasource.url=jdbc:postgresql://localhost:5432/tasc
spring.datasource.username=postgres
spring.datasource.password=admin
spring.datasource.currentSchema=tasc
spring.datasource.url=jdbc:postgresql://ec2-3-212-75-25.compute-1.amazonaws.com:5432/dds939tlhunclk
spring.datasource.username=cnxgzqmgtjwgwt
spring.datasource.password=2e6f389654422dbd3406d431a243f61e5e5543ddc80a84d14afd6dcbe910f4d8
spring.datasource.currentSchema=public
spring.datasource.hikari.maximum-pool-size=10
logging.level.root=info
\ No newline at end of file
logging.level.root=info
package com.adpro.tasc.controller;
import org.junit.Before;
import org.junit.jupiter.api.Test;
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.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.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.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
......@@ -15,9 +21,20 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@AutoConfigureMockMvc
public class HelloControllerTest {
@Autowired
private WebApplicationContext context;
@Autowired
private MockMvc mockMvc;
@Before
public void setup() {
mockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
}
@Test
public void DefaultLoginMessage() throws Exception {
this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk())
......@@ -30,4 +47,26 @@ public class HelloControllerTest {
.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