Repository navigation
COG-1154: W1 · UserResource enable/disable → POST (server + Angular caller) #338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package com.userFront.resource; | ||
|
|
||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
| 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.MockMvcResultMatchers.status; | ||
|
|
||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.runners.MockitoJUnitRunner; | ||
| import org.springframework.test.web.servlet.MockMvc; | ||
| import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
|
|
||
| import com.userFront.service.TransactionService; | ||
| import com.userFront.service.UserService; | ||
|
|
||
| @RunWith(MockitoJUnitRunner.class) | ||
| public class UserResourceTest { | ||
|
|
||
| @Mock | ||
| private UserService userService; | ||
|
|
||
| @Mock | ||
| private TransactionService transactionService; | ||
|
|
||
| @InjectMocks | ||
| private UserResource userResource; | ||
|
|
||
| private MockMvc mockMvc; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| mockMvc = MockMvcBuilders.standaloneSetup(userResource).build(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is on purpose. |
||
| } | ||
|
|
||
| @Test | ||
| public void postEnableEnablesUser() throws Exception { | ||
| mockMvc.perform(post("/api/user/alice/enable")).andExpect(status().isOk()); | ||
| verify(userService).enableUser("alice"); | ||
| } | ||
|
|
||
| @Test | ||
| public void postDisableDisablesUser() throws Exception { | ||
| mockMvc.perform(post("/api/user/alice/disable")).andExpect(status().isOk()); | ||
| verify(userService).disableUser("alice"); | ||
| } | ||
|
|
||
| @Test | ||
| public void getEnableIsMethodNotAllowed() throws Exception { | ||
| mockMvc.perform(get("/api/user/alice/enable")).andExpect(status().isMethodNotAllowed()); | ||
| verify(userService, never()).enableUser("alice"); | ||
| } | ||
|
|
||
| @Test | ||
| public void getDisableIsMethodNotAllowed() throws Exception { | ||
| mockMvc.perform(get("/api/user/alice/disable")).andExpect(status().isMethodNotAllowed()); | ||
| verify(userService, never()).disableUser("alice"); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 CSRF cookie blocks admin account actions
When
xsrfHeadersfinds an XSRF cookie, both account actions fail from the Angular portal. The custom header triggers a preflight, but RequestFilter excludesx-xsrf-tokenfrom allowed headers.Learn more
The admin portal runs on localhost:4200 and sends credentialed requests to localhost:8080. An
XSRF-TOKENcookie makesxsrfHeadersadd a custom header to each POST. Browsers then send an OPTIONS preflight before the POST. RequestFilter responds with an allow-list that omitsx-xsrf-token, so the browser never sends the action. The new standalone MVC tests do not exercise browser CORS behavior.Example: An admin loads the portal after localhost:8080 issues an
XSRF-TOKENcookie. Clicking Disable sends an OPTIONS request asking to usex-xsrf-token; the response rejects the header, so the user remains enabled.Recommended fix: Add
x-xsrf-tokentoRequestFilter's preflightAccess-Control-Allow-Headerslist in the same change as the new header, and add a test for an OPTIONS request withAccess-Control-Request-Headers: x-xsrf-token.Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Intentionally not changed here.
XSRF-TOKENis only issued once W2 turns CSRF on (CookieCsrfTokenRepository). CSRF is disabled on this branch, soxsrfHeaders()returns empty headers and the POST stays a CORS simple request with no preflight. I checked this live: POST from Originhttp://localhost:4200returns 200 with the ACAO/ACAC headers (evidence in the PR body). The allow-list fix is owned by W2 sibling COG-1165, which replacesRequestFilterwith aCorsConfigurationSourcewhoseallowedHeadersalready includesx-xsrf-token. EditingRequestFilterhere would break the file-disjoint slicing and conflict with that ticket.