Repository navigation
COG-1142: W0 smoke suite — signup, login, logout, remember-me (MockMvc) #109
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
Open
patrickbradley-cog
wants to merge
2
commits into
devin/cog-1141-w0-testcontainers-mysql-harness-for-springboottest-boot-154
Choose a base branch
from
devin/1790261326-cog-1142-auth-smoke-suite
base: devin/cog-1141-w0-testcontainers-mysql-harness-for-springboottest-boot-154
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
214 changes: 214 additions & 0 deletions
214
UserFront/src/test/java/com/userFront/AuthSmokeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| package com.userFront; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertTrue; | ||
| 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.redirectedUrl; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
|
||
| import javax.persistence.EntityManager; | ||
| import javax.persistence.PersistenceContext; | ||
| import javax.servlet.http.Cookie; | ||
|
|
||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.mock.web.MockHttpSession; | ||
| import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
| import org.springframework.test.util.AopTestUtils; | ||
| import org.springframework.test.util.ReflectionTestUtils; | ||
| import org.springframework.test.web.servlet.MvcResult; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import com.userFront.dao.PrimaryAccountDao; | ||
| import com.userFront.dao.RoleDao; | ||
| import com.userFront.dao.SavingsAccountDao; | ||
| import com.userFront.dao.UserDao; | ||
| import com.userFront.domain.PrimaryAccount; | ||
| import com.userFront.domain.SavingsAccount; | ||
| import com.userFront.domain.User; | ||
| import com.userFront.domain.security.Role; | ||
| import com.userFront.domain.security.UserRole; | ||
| import com.userFront.service.AccountService; | ||
|
|
||
| /** | ||
| * Regression contract for signup, form login, logout and remember-me as they behave on | ||
| * Boot 1.5.4. Later migration waves re-run this class unchanged. | ||
| */ | ||
| @Transactional | ||
| public class AuthSmokeTest extends AbstractIntegrationTest { | ||
|
|
||
| private static final String USERNAME = "authsmoke-user"; | ||
| private static final String PASSWORD = "authsmoke-pass"; | ||
| private static final String EMAIL = "authsmoke-user@example.com"; | ||
|
|
||
| @Autowired | ||
| private UserDao userDao; | ||
|
|
||
| @Autowired | ||
| private RoleDao roleDao; | ||
|
|
||
| @Autowired | ||
| private PrimaryAccountDao primaryAccountDao; | ||
|
|
||
| @Autowired | ||
| private SavingsAccountDao savingsAccountDao; | ||
|
|
||
| @Autowired | ||
| private AccountService accountService; | ||
|
|
||
| @Autowired | ||
| private BCryptPasswordEncoder passwordEncoder; | ||
|
|
||
| @PersistenceContext | ||
| private EntityManager entityManager; | ||
|
|
||
| @Before | ||
| public void seedRole() { | ||
| if (roleDao.findByName("ROLE_USER") == null) { | ||
| Role role = new Role(); | ||
| role.setRoleId(1); | ||
| role.setName("ROLE_USER"); | ||
| roleDao.save(role); | ||
| } | ||
| advanceAccountNumberPastExistingAccounts(); | ||
| } | ||
|
|
||
| /** | ||
| * Signup numbers accounts from a static counter in {@code AccountServiceImpl} that ignores rows already in | ||
| * the database (COG-1179), so rows committed by other suites in this JVM can make signup generate a | ||
| * duplicate number and fail. Moving the counter past the highest existing number keeps this class | ||
| * independent of test order. | ||
| */ | ||
| private void advanceAccountNumberPastExistingAccounts() { | ||
| int highest = 0; | ||
| for (PrimaryAccount account : primaryAccountDao.findAll()) { | ||
| highest = Math.max(highest, account.getAccountNumber()); | ||
| } | ||
| for (SavingsAccount account : savingsAccountDao.findAll()) { | ||
| highest = Math.max(highest, account.getAccountNumber()); | ||
| } | ||
| Object target = AopTestUtils.getUltimateTargetObject(accountService); | ||
| Integer next = (Integer) ReflectionTestUtils.getField(target, "nextAccountNumber"); | ||
| if (next < highest) { | ||
| ReflectionTestUtils.setField(target, "nextAccountNumber", highest); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void signupCreatesUserWithHashedPasswordRoleAndBothAccounts() throws Exception { | ||
| mockMvc.perform(post("/signup") | ||
| .param("username", USERNAME) | ||
| .param("password", PASSWORD) | ||
| .param("firstName", "Smoke") | ||
| .param("lastName", "User") | ||
| .param("email", EMAIL) | ||
| .param("phone", "5551234567")) | ||
| .andExpect(status().is3xxRedirection()) | ||
| .andExpect(redirectedUrl("/")); | ||
|
|
||
| entityManager.flush(); | ||
| entityManager.clear(); | ||
|
|
||
| User user = userDao.findByUsername(USERNAME); | ||
| assertNotNull(user); | ||
| assertEquals(EMAIL, user.getEmail()); | ||
| assertFalse(PASSWORD.equals(user.getPassword())); | ||
| assertTrue(user.getPassword().startsWith("$2a$")); | ||
| assertTrue(passwordEncoder.matches(PASSWORD, user.getPassword())); | ||
|
|
||
| assertEquals(1, user.getUserRoles().size()); | ||
| UserRole userRole = user.getUserRoles().iterator().next(); | ||
| assertEquals("ROLE_USER", userRole.getRole().getName()); | ||
|
|
||
| assertNotNull(user.getPrimaryAccount()); | ||
| assertNotNull(user.getSavingsAccount()); | ||
| } | ||
|
|
||
| @Test | ||
| public void loginWithValidCredentialsRedirectsToUserFront() throws Exception { | ||
| signup(); | ||
|
|
||
| mockMvc.perform(post("/index") | ||
| .param("username", USERNAME) | ||
| .param("password", PASSWORD)) | ||
| .andExpect(status().is3xxRedirection()) | ||
| .andExpect(redirectedUrl("/userFront")); | ||
| } | ||
|
|
||
| @Test | ||
| public void loginWithBadPasswordRedirectsToIndexError() throws Exception { | ||
| signup(); | ||
|
|
||
| mockMvc.perform(post("/index") | ||
| .param("username", USERNAME) | ||
| .param("password", "wrong-password")) | ||
| .andExpect(status().is3xxRedirection()) | ||
| .andExpect(redirectedUrl("/index?error")); | ||
| } | ||
|
|
||
| @Test | ||
| public void logoutIsAGetAndDeletesRememberMeCookie() throws Exception { | ||
| signup(); | ||
|
|
||
| MvcResult login = mockMvc.perform(post("/index") | ||
| .param("username", USERNAME) | ||
| .param("password", PASSWORD) | ||
| .param("remember-me", "on")) | ||
| .andExpect(redirectedUrl("/userFront")) | ||
| .andReturn(); | ||
|
|
||
| Cookie issued = login.getResponse().getCookie("remember-me"); | ||
| assertNotNull(issued); | ||
|
|
||
| // GET, not POST: W2 turns CSRF on and the logoutRequestMatcher must keep this working. | ||
| MvcResult logout = mockMvc.perform(get("/logout") | ||
| .cookie(issued) | ||
| .session((MockHttpSession) login.getRequest().getSession())) | ||
| .andExpect(status().is3xxRedirection()) | ||
| .andExpect(redirectedUrl("/index?logout")) | ||
| .andReturn(); | ||
|
devin-ai-integration[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| Cookie rememberMe = logout.getResponse().getCookie("remember-me"); | ||
| assertNotNull(rememberMe); | ||
| assertEquals(0, rememberMe.getMaxAge()); | ||
|
|
||
| // Today's behaviour: remember-me is token-based, so a copy of the cookie kept by the | ||
| // client still authenticates after logout. Captured, not endorsed. | ||
| mockMvc.perform(get("/userFront").cookie(issued).session(new MockHttpSession())) | ||
| .andExpect(status().isOk()); | ||
| } | ||
|
|
||
| @Test | ||
| public void rememberMeCookieReauthenticatesOnAFreshSession() throws Exception { | ||
| signup(); | ||
|
|
||
| MvcResult login = mockMvc.perform(post("/index") | ||
| .param("username", USERNAME) | ||
| .param("password", PASSWORD) | ||
| .param("remember-me", "on")) | ||
| .andExpect(redirectedUrl("/userFront")) | ||
| .andReturn(); | ||
|
|
||
| Cookie rememberMe = login.getResponse().getCookie("remember-me"); | ||
| assertNotNull(rememberMe); | ||
| assertTrue(rememberMe.getMaxAge() > 0); | ||
|
|
||
| mockMvc.perform(get("/userFront").cookie(rememberMe).session(new MockHttpSession())) | ||
| .andExpect(status().isOk()); | ||
| } | ||
|
|
||
| private void signup() throws Exception { | ||
| mockMvc.perform(post("/signup") | ||
| .param("username", USERNAME) | ||
| .param("password", PASSWORD) | ||
| .param("firstName", "Smoke") | ||
| .param("lastName", "User") | ||
| .param("email", EMAIL) | ||
| .param("phone", "5551234567")) | ||
| .andExpect(redirectedUrl("/")); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.