From 0b02367cc3a97a67e7e9969a8ed3e1480689f1cf Mon Sep 17 00:00:00 2001 From: IndexZero Date: Wed, 19 Feb 2025 18:31:47 +0300 Subject: [PATCH] MORE UNIT TESTS --- .../finals/EmployeeControllerTests.java | 18 +++ .../finals/EntranceControllerTests.java | 124 ++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 src/test/java/com/indexzero/finals/EntranceControllerTests.java diff --git a/src/test/java/com/indexzero/finals/EmployeeControllerTests.java b/src/test/java/com/indexzero/finals/EmployeeControllerTests.java index a0063bf..a75f93a 100644 --- a/src/test/java/com/indexzero/finals/EmployeeControllerTests.java +++ b/src/test/java/com/indexzero/finals/EmployeeControllerTests.java @@ -118,6 +118,24 @@ class EmployeeControllerTests { .andExpect(status().isForbidden()); } + @Test + void getByLogin() throws Exception { + this.mockMvc.perform( + get("/api/employee/pivanov") + .with(httpBasic("pivanov", "HelloWorld1234"))) + .andDo(print()) + .andExpect(status().isOk()); + } + + @Test + void getByLoginForbidden() throws Exception { + this.mockMvc.perform( + get("/api/employee/pivanov") + .with(httpBasic("afedorov", "HelloWorld1234"))) + .andDo(print()) + .andExpect(status().isForbidden()); + } + } diff --git a/src/test/java/com/indexzero/finals/EntranceControllerTests.java b/src/test/java/com/indexzero/finals/EntranceControllerTests.java new file mode 100644 index 0000000..9b861ac --- /dev/null +++ b/src/test/java/com/indexzero/finals/EntranceControllerTests.java @@ -0,0 +1,124 @@ +package com.indexzero.finals; + +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 static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc +class EntranceControllerTests { + @Autowired + private MockMvc mockMvc; + + @Test + void getUserEntries() throws Exception { + this.mockMvc.perform( + get("/api/entrance") + .with(httpBasic("pivanov", "HelloWorld1234")) + ) + .andDo(print()) + .andExpect(status().isOk() + ); + } + + @Test + void getUserEntriesUnathorized() throws Exception { + this.mockMvc.perform( + get("/api/entrance") + .with(httpBasic("pivanov", "HelloWorld12345")) + ) + .andDo(print()) + .andExpect(status().isUnauthorized()); + } + + @Test + void getUserLastEntry() throws Exception { + this.mockMvc.perform( + get("/api/entrance/last") + .with(httpBasic("pivanov", "HelloWorld1234")) + ) + .andDo(print()) + .andExpect(status().isOk() + ); + } + + @Test + void getUserLastEntryUnathorized() throws Exception { + this.mockMvc.perform( + get("/api/entrance/last") + .with(httpBasic("pivanov", "HelloWorld12345")) + ) + .andDo(print()) + .andExpect(status().isUnauthorized()); + } + + @Test + void getUserAllEntries() throws Exception { + this.mockMvc.perform( + get("/api/entrance/all") + .with(httpBasic("pivanov", "HelloWorld1234")) + ) + .andDo(print()) + .andExpect(status().isOk() + ); + } + + @Test + void getUserAllEntriesForbidden() throws Exception { + this.mockMvc.perform( + get("/api/entrance/all") + .with(httpBasic("afedorov", "HelloWorld1234")) + ) + .andDo(print()) + .andExpect(status().isForbidden()); + } + + @Test + void getUserAllEntriesUnauthorized() throws Exception { + this.mockMvc.perform( + get("/api/entrance/all") + .with(httpBasic("pivanov", "HelloWorld12345")) + ) + .andDo(print()) + .andExpect(status().isUnauthorized()); + } + + @Test + void getUserEntriesByLogin() throws Exception { + this.mockMvc.perform( + get("/api/entrance/pivanov") + .with(httpBasic("pivanov", "HelloWorld1234")) + ) + .andDo(print()) + .andExpect(status().isOk()); + } + + @Test + void getUserEntriesByLoginNotFound() throws Exception { + this.mockMvc.perform( + get("/api/entrance/pivanova") + .with(httpBasic("pivanov", "HelloWorld1234")) + ) + .andDo(print()) + .andExpect(status().isNotFound()); + } + + @Test + void getUserEntriesByLoginForbidden() throws Exception { + this.mockMvc.perform( + get("/api/entrance/pivanov") + .with(httpBasic("afedorov", "HelloWorld1234")) + ) + .andDo(print()) + .andExpect(status().isForbidden()); + } + +}