-
Notifications
You must be signed in to change notification settings - Fork 4
Add 'Remember Me' Feature and Tests #105
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
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3eef32b
chore: add redis container setup in build_and_run_app.sh for local de…
tsviz 4c133df
chore: remove CSV export functionality from AppController and SalesDAO
tsviz 12d0891
Add 'Remember Me' checkbox to login form
tsviz c46ffa4
Add 'Remember Me' feature and corresponding tests
tsviz 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
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
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
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
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
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
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
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,48 @@ | ||
package net.codejava; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices; | ||
|
||
import javax.servlet.http.Cookie; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@SpringBootTest | ||
public class AppControllerTest { | ||
|
||
@Autowired | ||
private AppController appController; | ||
|
||
@Autowired | ||
private PersistentTokenBasedRememberMeServices rememberMeServices; | ||
|
||
@Test | ||
public void testRememberMeFunctionality() throws Exception { | ||
MockHttpServletRequest request = new MockHttpServletRequest(); | ||
MockHttpServletResponse response = new MockHttpServletResponse(); | ||
|
||
// Simulate login request with 'Remember Me' checked | ||
request.setParameter("username", "testuser"); | ||
request.setParameter("password", "testpassword"); | ||
request.setParameter("rememberMe", "on"); | ||
|
||
String view = appController.loginPost(request, response, null); | ||
|
||
// Assert that the user is redirected to the home page | ||
assertEquals("redirect:/", view); | ||
|
||
// Assert that the 'Remember Me' cookie is set | ||
Cookie rememberMeCookie = response.getCookie("rememberMe"); | ||
assertNotNull(rememberMeCookie); | ||
assertEquals("true", rememberMeCookie.getValue()); | ||
assertTrue(rememberMeCookie.getMaxAge() > 0); | ||
|
||
// Assert that the security context is populated | ||
assertNotNull(SecurityContextHolder.getContext().getAuthentication()); | ||
} | ||
} |
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,151 @@ | ||
package net.codejava; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
@SpringBootTest | ||
public class JUnit5ExampleTest12 { | ||
|
||
// Global variables to control test behavior | ||
private static boolean isFeatureEnabled = true; | ||
private static int maxRecordsPerPage = 20; | ||
private static String defaultSearchQuery = "Laptop"; | ||
private static String defaultItemName = "Smartphone"; | ||
private static double defaultItemPrice = 999.99; | ||
private static String testLogPrefix = "[TEST LOG] "; // New global variable | ||
|
||
@Autowired | ||
private AppController appController; | ||
|
||
@Test | ||
void testEnableSearchFeatureDefaultValue() { | ||
if (isFeatureEnabled) { | ||
System.out.println(testLogPrefix + "Feature is enabled: Running testEnableSearchFeatureDefaultValue"); | ||
assertTrue(appController.getEnableSearchFeature(), testLogPrefix + "enableSearchFeature should be true by default"); | ||
} else { | ||
System.out.println(testLogPrefix + "Feature is disabled: Skipping testEnableSearchFeatureDefaultValue"); | ||
} | ||
|
||
System.out.println(testLogPrefix + "Checking additional conditions..."); | ||
System.out.println(testLogPrefix + "Test completed successfully."); | ||
System.out.println(testLogPrefix + "Logging additional information."); | ||
System.out.println(testLogPrefix + "Feature flag value: " + isFeatureEnabled); | ||
System.out.println(testLogPrefix + "Default search query: " + defaultSearchQuery); | ||
System.out.println(testLogPrefix + "Default item name: " + defaultItemName); | ||
System.out.println(testLogPrefix + "Default item price: " + defaultItemPrice); | ||
System.out.println(testLogPrefix + "Max records per page: " + maxRecordsPerPage); | ||
System.out.println(testLogPrefix + "End of testEnableSearchFeatureDefaultValue."); | ||
} | ||
|
||
@Test | ||
void testMaxRecordsPerPage() { | ||
System.out.println("Max records per page: " + maxRecordsPerPage); | ||
assertEquals(20, maxRecordsPerPage, "Max records per page should be 20"); | ||
} | ||
|
||
@Test | ||
void testDefaultSearchQuery() { | ||
System.out.println("Default search query: " + defaultSearchQuery); | ||
assertEquals("Laptop", defaultSearchQuery, "Default search query should be 'Laptop'"); | ||
} | ||
|
||
@Test | ||
void testDefaultItemName() { | ||
System.out.println("Default item name: " + defaultItemName); | ||
assertEquals("Smartphone", defaultItemName, "Default item name should be 'Smartphone'"); | ||
} | ||
|
||
@Test | ||
void testDefaultItemPrice() { | ||
System.out.println("Default item price: " + defaultItemPrice); | ||
assertEquals(999.99, defaultItemPrice, "Default item price should be 999.99"); | ||
} | ||
|
||
@Test | ||
void testEnableSearchFeatureInHomePage() { | ||
if (isFeatureEnabled) { | ||
System.out.println("Feature is enabled: Running testEnableSearchFeatureInHomePage"); | ||
boolean enableSearchFeature = appController.getEnableSearchFeature(); | ||
System.out.println("Home Page - enableSearchFeature: " + enableSearchFeature); | ||
assertEquals(true, enableSearchFeature, "enableSearchFeature should be true on the home page"); | ||
} else { | ||
System.out.println("Feature is disabled: Skipping testEnableSearchFeatureInHomePage"); | ||
} | ||
} | ||
|
||
@Test | ||
void testEnableSearchFeatureInNewForm() { | ||
if (isFeatureEnabled) { | ||
System.out.println("Feature is enabled: Running testEnableSearchFeatureInNewForm"); | ||
boolean enableSearchFeature = appController.getEnableSearchFeature(); | ||
System.out.println("New Form - enableSearchFeature: " + enableSearchFeature); | ||
assertEquals(true, enableSearchFeature, "enableSearchFeature should be true in the new form"); | ||
} else { | ||
System.out.println("Feature is disabled: Skipping testEnableSearchFeatureInNewForm"); | ||
} | ||
} | ||
|
||
@Test | ||
void testEnableSearchFeatureInEditForm() { | ||
if (isFeatureEnabled) { | ||
System.out.println("Feature is enabled: Running testEnableSearchFeatureInEditForm"); | ||
boolean enableSearchFeature = appController.getEnableSearchFeature(); | ||
System.out.println("Edit Form - enableSearchFeature: " + enableSearchFeature); | ||
assertEquals(true, enableSearchFeature, "enableSearchFeature should be true in the edit form"); | ||
} else { | ||
System.out.println("Feature is disabled: Skipping testEnableSearchFeatureInEditForm"); | ||
} | ||
} | ||
|
||
@Test | ||
void testEnableSearchFeatureInSearch() { | ||
if (isFeatureEnabled) { | ||
System.out.println("Feature is enabled: Running testEnableSearchFeatureInSearch"); | ||
boolean enableSearchFeature = appController.getEnableSearchFeature(); | ||
System.out.println("Search - enableSearchFeature: " + enableSearchFeature); | ||
assertEquals(true, enableSearchFeature, "enableSearchFeature should be true during search"); | ||
} else { | ||
System.out.println("Feature is disabled: Skipping testEnableSearchFeatureInSearch"); | ||
} | ||
} | ||
|
||
@Test | ||
void testMaxRecordsPerPageInSearch() { | ||
System.out.println("Testing maxRecordsPerPage in search functionality"); | ||
assertEquals(20, maxRecordsPerPage, "Max records per page should be consistent in search functionality"); | ||
} | ||
|
||
@Test | ||
void testDefaultSearchQueryInSearch() { | ||
System.out.println("Testing defaultSearchQuery in search functionality"); | ||
assertEquals("Laptop", defaultSearchQuery, "Default search query should be consistent in search functionality"); | ||
} | ||
|
||
@Test | ||
void testDefaultItemNameInSearch() { | ||
System.out.println("Testing defaultItemName in search functionality"); | ||
assertEquals("Smartphone", defaultItemName, "Default item name should be consistent in search functionality"); | ||
} | ||
|
||
@Test | ||
void testDefaultItemPriceInSearch() { | ||
System.out.println("Testing defaultItemPrice in search functionality"); | ||
assertEquals(999.99, defaultItemPrice, "Default item price should be consistent in search functionality"); | ||
} | ||
|
||
@Test | ||
void testEnableSearchFeatureInSave() { | ||
if (isFeatureEnabled) { | ||
System.out.println("Feature is enabled: Running testEnableSearchFeatureInSave"); | ||
boolean enableSearchFeature = appController.getEnableSearchFeature(); | ||
System.out.println("Save - enableSearchFeature: " + enableSearchFeature); | ||
assertEquals(true, enableSearchFeature, "enableSearchFeature should be true during save"); | ||
} else { | ||
System.out.println("Feature is disabled: Skipping testEnableSearchFeatureInSave"); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
Check warning
Code scanning / CodeQL
Failure to use secure cookies Medium
Copilot Autofix
AI about 1 month ago
To fix the issue, the 'secure' flag must be explicitly set on the
rememberMeCookie
before it is added to the response. This ensures that the cookie is only transmitted over secure HTTPS connections. The fix involves calling thesetSecure(true)
method on therememberMeCookie
object before theresponse.addCookie()
call. This change does not alter the existing functionality but enhances the security of the application.