Skip to content

Commit 5098f82

Browse files
committed
fix(test): use dynamic dates in AdminRoleAccessControlTest
Replace hardcoded date strings with dynamically generated future dates to prevent test failures when dates pass. The tests were using hardcoded date "2025-12-31" which failed validation against @FutureOrPresent constraint after that date passed. Added a futureDate() helper method that returns LocalDate.now().plusYears(1) to ensure dates are always valid.
1 parent bc86e3b commit 5098f82

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

src/test/java/com/digitalsanctuary/spring/user/concurrent/AdminRoleAccessControlTest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.springframework.transaction.annotation.Propagation;
2020
import org.springframework.transaction.annotation.Transactional;
2121
import com.digitalsanctuary.spring.demo.UserDemoApplication;
22+
import java.time.LocalDate;
2223
import com.digitalsanctuary.spring.user.persistence.model.Role;
2324
import com.digitalsanctuary.spring.user.persistence.repository.RoleRepository;
2425
import com.digitalsanctuary.spring.user.test.annotations.IntegrationTest;
@@ -51,6 +52,11 @@ class AdminRoleAccessControlTest {
5152
private TestUserManager userManager;
5253
private static final String TEST_PREFIX = "role.access";
5354

55+
/** Returns a date one year in the future to satisfy @FutureOrPresent validation */
56+
private static String futureDate() {
57+
return LocalDate.now().plusYears(1).toString();
58+
}
59+
5460
@BeforeEach
5561
void setUp() {
5662
String uniquePrefix = TEST_PREFIX + "." + System.currentTimeMillis();
@@ -122,7 +128,7 @@ class EventAccessControlTests {
122128
@DisplayName("Admin can perform all event operations")
123129
void testAdminEventOperations() throws Exception {
124130
// Admin can create events
125-
String validEventJson = "{\"name\": \"Admin Event\", \"description\": \"Created by admin\", \"location\": \"Test Location\", \"date\": \"2025-12-31\", \"time\": \"14:30\"}";
131+
String validEventJson = "{\"name\": \"Admin Event\", \"description\": \"Created by admin\", \"location\": \"Test Location\", \"date\": \"" + futureDate() + "\", \"time\": \"14:30\"}";
126132
mockMvc.perform(post("/api/events").contentType(MediaType.APPLICATION_JSON)
127133
.content(validEventJson).with(csrf())).andExpect(status().isOk());
128134
}
@@ -132,7 +138,7 @@ void testAdminEventOperations() throws Exception {
132138
@DisplayName("Regular user can only register for events")
133139
void testUserEventLimitations() throws Exception {
134140
// User CANNOT create events
135-
String validEventJson = "{\"name\": \"User Event\", \"description\": \"Should fail\", \"location\": \"Test Location\", \"date\": \"2025-12-31\", \"time\": \"14:30\"}";
141+
String validEventJson = "{\"name\": \"User Event\", \"description\": \"Should fail\", \"location\": \"Test Location\", \"date\": \"" + futureDate() + "\", \"time\": \"14:30\"}";
136142
mockMvc.perform(post("/api/events").contentType(MediaType.APPLICATION_JSON)
137143
.content(validEventJson).with(csrf())).andExpect(status().isForbidden());
138144
}
@@ -141,7 +147,7 @@ void testUserEventLimitations() throws Exception {
141147
@WithMockUser(authorities = {})
142148
@DisplayName("No authorities should deny access")
143149
void testNoAuthoritiesAccess() throws Exception {
144-
String validEventJson = "{\"name\": \"Unauthorized\", \"description\": \"Should fail\", \"location\": \"Test Location\", \"date\": \"2025-12-31\", \"time\": \"14:30\"}";
150+
String validEventJson = "{\"name\": \"Unauthorized\", \"description\": \"Should fail\", \"location\": \"Test Location\", \"date\": \"" + futureDate() + "\", \"time\": \"14:30\"}";
145151
mockMvc.perform(post("/api/events").contentType(MediaType.APPLICATION_JSON)
146152
.content(validEventJson).with(csrf())).andExpect(status().isForbidden());
147153
}
@@ -180,7 +186,7 @@ void testMultiplePermissionLevels() {
180186
@DisplayName("Partial admin permissions should work correctly")
181187
void testPartialAdminPermissions() throws Exception {
182188
// User with some admin privileges can create events
183-
String validEventJson = "{\"name\": \"Partial Admin Event\", \"description\": \"Test\", \"location\": \"Test Location\", \"date\": \"2025-12-31\", \"time\": \"14:30\"}";
189+
String validEventJson = "{\"name\": \"Partial Admin Event\", \"description\": \"Test\", \"location\": \"Test Location\", \"date\": \"" + futureDate() + "\", \"time\": \"14:30\"}";
184190
mockMvc.perform(post("/api/events").contentType(MediaType.APPLICATION_JSON)
185191
.content(validEventJson).with(csrf())).andExpect(status().isOk());
186192
}

0 commit comments

Comments
 (0)