1+ package com .digitalsanctuary .spring .user .service ;
2+
3+ import static org .junit .jupiter .api .Assertions .assertFalse ;
4+ import static org .junit .jupiter .api .Assertions .assertTrue ;
5+
6+ import java .util .List ;
7+ import java .util .Locale ;
8+
9+ import org .junit .jupiter .api .Test ;
10+ import org .springframework .beans .factory .annotation .Autowired ;
11+ import org .springframework .boot .test .context .SpringBootTest ;
12+ import org .springframework .test .context .TestPropertySource ;
13+
14+ /**
15+ * Integration test for PasswordPolicyService that tests with actual property loading.
16+ * This ensures the properties file is correctly formatted and parsed.
17+ */
18+ @ SpringBootTest
19+ @ TestPropertySource (locations = "classpath:config/dsspringuserconfig.properties" )
20+ class PasswordPolicyServiceIntegrationTest {
21+
22+ @ Autowired
23+ private PasswordPolicyService passwordPolicyService ;
24+
25+ private static final Locale LOCALE = Locale .ENGLISH ;
26+
27+ @ Test
28+ void testSpecialCharactersFromPropertiesFile () {
29+ // Test that quotes are NOT required as special characters
30+ // This password has all requirements except quotes
31+ List <String > errors1 = passwordPolicyService .validate (null , "Password1!" , null , LOCALE );
32+ assertTrue (errors1 .isEmpty (), "Password with ! should be valid, but got errors: " + errors1 );
33+
34+ // Test various special characters from the configured list
35+ String [] validPasswords = {
36+ "Password1~" , // tilde
37+ "Password1!" , // exclamation
38+ "Password1@" , // at sign
39+ "Password1#" , // hash
40+ "Password1$" , // dollar
41+ "Password1%" , // percent
42+ "Password1^" , // caret
43+ "Password1&" , // ampersand
44+ "Password1*" , // asterisk
45+ "Password1(" , // left paren
46+ "Password1)" , // right paren
47+ "Password1_" , // underscore
48+ "Password1-" , // hyphen
49+ "Password1+" , // plus
50+ "Password1=" , // equals
51+ "Password1{" , // left brace
52+ "Password1}" , // right brace
53+ "Password1[" , // left bracket
54+ "Password1]" , // right bracket
55+ "Password1|" , // pipe
56+ "Password1\\ " , // backslash
57+ "Password1:" , // colon
58+ "Password1;" , // semicolon
59+ "Password1\" " , // double quote (should work as special char, not required)
60+ "Password1'" , // single quote
61+ "Password1<" , // less than
62+ "Password1>" , // greater than
63+ "Password1," , // comma
64+ "Password1." , // period
65+ "Password1?" , // question mark
66+ "Password1/" // forward slash
67+ };
68+
69+ for (String password : validPasswords ) {
70+ List <String > errors = passwordPolicyService .validate (null , password , null , LOCALE );
71+ assertTrue (errors .isEmpty (),
72+ "Password '" + password + "' should be valid, but got errors: " + errors );
73+ }
74+
75+ // Test that password without any special character fails
76+ // Note: This may throw NoSuchMessageException in test context, but that's OK
77+ // We're testing that the validation logic works, not the message formatting
78+ try {
79+ List <String > errors2 = passwordPolicyService .validate (null , "Password1" , null , LOCALE );
80+ assertFalse (errors2 .isEmpty (), "Password without special character should fail" );
81+ assertTrue (errors2 .stream ().anyMatch (e -> e .contains ("INSUFFICIENT_SPECIAL" ) || e .contains ("special" )),
82+ "Should have special character error, but got: " + errors2 );
83+ } catch (Exception e ) {
84+ // If we get a NoSuchMessageException, it means the validation detected the missing special char
85+ // which is what we want to test
86+ assertTrue (e .getMessage ().contains ("INSUFFICIENT_SPECIAL" ),
87+ "Exception should be about missing special character: " + e .getMessage ());
88+ }
89+ }
90+
91+ @ Test
92+ void testQuotesNotRequiredAfterFix () {
93+ // This test specifically verifies that quotes are not required
94+ // A password without quotes but with other special chars should be valid
95+ List <String > errors = passwordPolicyService .validate (null , "Password1@" , null , LOCALE );
96+ assertTrue (errors .isEmpty (),
97+ "Password without quotes should be valid when it has other special chars. Errors: " + errors );
98+ }
99+ }
0 commit comments