Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Spring Boot CI/CD

on:
push:
branches: [ "dev", "feature/*", "fix/*" ]
branches: [ "dev", "feature/*", "fix/*", test/*" ]
pull_request:
branches: [ "main", "dev" ]
branches: [ "main" ]

jobs:
build-and-test:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ public class UserService {
public User registerStudent(String username, String password, String email,
String studentId, String phone, String address) {

if (username == null || username.trim().isEmpty()) {
throw new IllegalArgumentException("Username cannot be empty");
}
if (password == null || password.trim().isEmpty()) {
throw new IllegalArgumentException("Password cannot be empty");
}
if (email == null || email.trim().isEmpty()) {
throw new IllegalArgumentException("Email cannot be empty");
}
if (studentId == null || studentId.trim().isEmpty()) {
throw new IllegalArgumentException("Student ID cannot be empty");
}

if (userRepository.existsByUsername(username)) {
throw new RuntimeException("Username already exists");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,12 @@ void registerTeacher_Post() throws Exception {
.andExpect(redirectedUrl("/login"))
.andExpect(flash().attributeExists("success"));
}

// Test unauthenticated user accessing protected page
@Test
void unauthenticatedUser_AccessingDashboard_ShouldRedirectToLogin() throws Exception {
mockMvc.perform(get("/dashboard"))
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrlPattern("**/login"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,35 @@ void updateProfile() throws Exception {
.andExpect(redirectedUrl("/student/dashboard"))
.andExpect(flash().attributeExists("success"));
}

@Test
@WithMockUser(roles = "TEACHER")
void teacher_AccessingStudentDashboard_ShouldBeForbidden() throws Exception {
mockMvc.perform(get("/student/dashboard"))
.andExpect(status().isForbidden());
}

@Test
@WithMockUser(username = "student2", roles = "STUDENT")
void dashboard_WithIncompleteStudentData_ShouldStillLoad() throws Exception {
User mockUser = new User();
mockUser.setId(2L);
mockUser.setUsername("student2");

// Create StudentDTO with DEFAULT values (not null)
StudentDTO mockStudent = new StudentDTO();
mockStudent.setUsername("student2");
mockStudent.setStudentId(""); // Empty but not null
mockStudent.setEmail(""); // Empty but not null
mockStudent.setPhone(""); // Empty but not null
mockStudent.setAddress(""); // Empty but not null
mockStudent.setCourseCodes(new ArrayList<>()); // Empty list, not null

when(userService.getUserByUsername("student2")).thenReturn(mockUser);
when(userService.getStudentDTOByUserId(2L)).thenReturn(mockStudent);

mockMvc.perform(get("/student/dashboard"))
.andExpect(status().isOk())
.andExpect(view().name("student/dashboard"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class TeacherControllerTest {
@MockBean
private StudentRepository studentRepository;

// ESSENTIAL METHODS (Filled)
@Test
@WithMockUser(roles = "TEACHER")
void dashboard() throws Exception {
Expand Down Expand Up @@ -83,6 +82,33 @@ void enrollStudent() throws Exception {
.andExpect(flash().attributeExists("success"));
}

@Test
@WithMockUser(roles = "STUDENT")
void student_AccessingTeacherDashboard_ShouldBeForbidden() throws Exception {
mockMvc.perform(get("/teacher/dashboard"))
.andExpect(status().isForbidden());
}

@Test
@WithMockUser(roles = "TEACHER")
void deleteStudent_WithInvalidId_ShouldHandleGracefully() throws Exception {
mockMvc.perform(get("/teacher/delete-student/9999"))
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("/teacher/students"));
}

@Test
@WithMockUser(roles = "TEACHER")
void enrollStudent_WithInvalidIds_ShouldShowError() throws Exception {
mockMvc.perform(post("/teacher/enroll-student")
.with(csrf())
.param("studentId", "9999")
.param("courseId", "9999"))
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("/teacher/enroll-student"));

}

// NON-ESSENTIAL METHODS (Blank)
@Test
void showAddStudentForm() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,4 @@ void findByTeacherId() {
.containsExactlyInAnyOrder("MATH201", "PHY201");
}

// NON-ESSENTIAL METHODS (None needed for this file)
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ void setUp() {
testStudent.setStudentId("S1001");
}

// ESSENTIAL METHODS
@Test
void registerStudent() {
when(userRepository.existsByUsername("newstudent")).thenReturn(false);
Expand Down Expand Up @@ -98,6 +97,35 @@ void enrollStudentInCourse() {
assertThat(testStudent.getCourses()).contains(testCourse);
}

@Test
void enrollStudentInCourse_CourseNotFound_ShouldThrowException() {
when(studentRepository.findById(1L)).thenReturn(Optional.of(testStudent));
when(courseRepository.findById(999L)).thenReturn(Optional.empty());

assertThatThrownBy(() ->
userService.enrollStudentInCourse(1L, 999L))
.isInstanceOf(RuntimeException.class)
.hasMessageContaining("not found");
}

@Test
void enrollStudentInCourse_StudentNotFound_ShouldThrowException() {
when(studentRepository.findById(999L)).thenReturn(Optional.empty());

assertThatThrownBy(() ->
userService.enrollStudentInCourse(999L, 1L))
.isInstanceOf(RuntimeException.class)
.hasMessageContaining("not found");
}

@Test
void registerStudent_WithEmptyFields_ShouldFail() {

assertThatThrownBy(() ->
userService.registerStudent("", "", "", "", "", ""))
.isInstanceOf(Exception.class);
}

// NON-ESSENTIAL METHODS (Blank)
@Test
void getUserByUsername() { }
Expand Down