|
| 1 | +# Spring User Framework - Comprehensive Test Improvement Plan |
| 2 | + |
| 3 | +## Executive Summary |
| 4 | +This plan addresses the critical need to improve test coverage from the current 27% to 80%+ for the Spring User Framework library. The strategy focuses on systematic implementation of meaningful tests that validate real functionality rather than just mocking behavior. |
| 5 | + |
| 6 | +## Current State Analysis |
| 7 | +- **Overall Coverage**: 27% (Critical Gap) |
| 8 | +- **Service Layer**: Minimal coverage with only UserService partially tested |
| 9 | +- **Controller Layer**: No test coverage |
| 10 | +- **Security Components**: Untested (High Risk) |
| 11 | +- **Event System**: No test coverage |
| 12 | + |
| 13 | +## Test Infrastructure Foundation (Completed) |
| 14 | +``` |
| 15 | +[✓] Test Directory Structure |
| 16 | +[✓] BaseTestConfiguration with common beans |
| 17 | +[✓] SecurityTestConfiguration for auth testing |
| 18 | +[✓] DatabaseTestConfiguration with H2 |
| 19 | +[✓] OAuth2TestConfiguration for OAuth2/OIDC |
| 20 | +[✓] Test Data Builders (User, Role, Token) |
| 21 | +[✓] Custom Test Annotations (@ServiceTest, @IntegrationTest) |
| 22 | +[✓] Mock Email Infrastructure |
| 23 | +``` |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## PHASE 1: Critical Security & Authentication Components |
| 28 | + |
| 29 | +### 1. DSUserDetailsService Testing |
| 30 | +**Priority**: CRITICAL - Authentication Foundation |
| 31 | + |
| 32 | +#### Test Cases: |
| 33 | +``` |
| 34 | +1. loadUserByUsername() Tests: |
| 35 | + - Valid email returns DSUserDetails |
| 36 | + - Non-existent email throws UsernameNotFoundException |
| 37 | + - Locked account returns user with locked status |
| 38 | + - Unverified account returns user with disabled status |
| 39 | + - User with multiple roles loads all authorities |
| 40 | +
|
| 41 | +2. OAuth2 Integration Tests: |
| 42 | + - loadUser() with OAuth2 token creates new user |
| 43 | + - loadUser() links OAuth2 to existing email |
| 44 | + - loadUser() updates existing OAuth2 user info |
| 45 | + - Different providers (Google, GitHub) handled correctly |
| 46 | +
|
| 47 | +3. OIDC Integration Tests: |
| 48 | + - OidcUser creation from ID token |
| 49 | + - Claims mapping to user attributes |
| 50 | + - Email verification from OIDC claims |
| 51 | +
|
| 52 | +4. Security Context Tests: |
| 53 | + - Authorities properly mapped |
| 54 | + - Custom attributes preserved |
| 55 | + - Session management integration |
| 56 | +``` |
| 57 | + |
| 58 | +### 2. AuthorityService Testing |
| 59 | +**Priority**: CRITICAL - Authorization Foundation |
| 60 | + |
| 61 | +#### Test Structure: |
| 62 | +``` |
| 63 | +Role Management: |
| 64 | +├── createRole() |
| 65 | +├── updateRole() |
| 66 | +├── deleteRole() |
| 67 | +├── findRoleByName() |
| 68 | +└── getAllRoles() |
| 69 | +
|
| 70 | +Authority Assignment: |
| 71 | +├── assignRoleToUser() |
| 72 | +├── removeRoleFromUser() |
| 73 | +├── getUserRoles() |
| 74 | +└── getUsersWithRole() |
| 75 | +
|
| 76 | +Authority Checking: |
| 77 | +├── hasRole() |
| 78 | +├── hasAnyRole() |
| 79 | +├── hasAllRoles() |
| 80 | +└── isAdmin() |
| 81 | +``` |
| 82 | + |
| 83 | +### 3. AuthController Testing |
| 84 | +**Priority**: HIGH - Entry Point Security |
| 85 | + |
| 86 | +#### Endpoint Tests: |
| 87 | +``` |
| 88 | +Authentication Flow: |
| 89 | +├── POST /login |
| 90 | +│ ├── Valid credentials → Success |
| 91 | +│ ├── Invalid credentials → 401 |
| 92 | +│ ├── Locked account → 423 |
| 93 | +│ └── Unverified account → 403 |
| 94 | +├── POST /logout |
| 95 | +│ └── Session invalidation |
| 96 | +└── OAuth2 /oauth2/authorization/{provider} |
| 97 | + ├── Redirect handling |
| 98 | + └── Callback processing |
| 99 | +``` |
| 100 | + |
| 101 | +### 4. LoginAttemptService Testing |
| 102 | +**Priority**: HIGH - Brute Force Protection |
| 103 | + |
| 104 | +#### Test Scenarios: |
| 105 | +``` |
| 106 | +Attempt Tracking: |
| 107 | +├── Failed attempts increment counter |
| 108 | +├── Successful login resets counter |
| 109 | +├── Account locks after max attempts |
| 110 | +└── IP-based tracking for distributed attacks |
| 111 | +``` |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +## PHASE 2: User Management Components |
| 116 | + |
| 117 | +### 5. UserService Enhancement |
| 118 | +**Current**: 6 tests passing |
| 119 | +**Target**: Comprehensive coverage |
| 120 | + |
| 121 | +#### Additional Tests Needed: |
| 122 | +``` |
| 123 | +User Lifecycle: |
| 124 | +├── createUser() with all validation rules |
| 125 | +├── updateUser() with partial updates |
| 126 | +├── deleteUser() soft delete |
| 127 | +├── findByEmail() edge cases |
| 128 | +├── Password management scenarios |
| 129 | +└── Account state transitions |
| 130 | +``` |
| 131 | + |
| 132 | +### 6. RegistrationController Testing |
| 133 | +**Priority**: HIGH - User Onboarding |
| 134 | + |
| 135 | +#### Test Flow: |
| 136 | +``` |
| 137 | +Registration Process: |
| 138 | +├── POST /user/registration |
| 139 | +│ ├── Valid registration → User created |
| 140 | +│ ├── Duplicate email → 409 |
| 141 | +│ └── Invalid data → 400 |
| 142 | +├── GET /user/registration/confirm |
| 143 | +│ ├── Valid token → Account activated |
| 144 | +│ ├── Expired token → Error |
| 145 | +│ └── Invalid token → 404 |
| 146 | +└── Password Reset Flow |
| 147 | + ├── Request reset → Email sent |
| 148 | + └── Reset with token → Password changed |
| 149 | +``` |
| 150 | + |
| 151 | +### 7. UserController Testing |
| 152 | +**Priority**: MEDIUM - User Features |
| 153 | + |
| 154 | +#### Endpoints: |
| 155 | +``` |
| 156 | +Profile Management: |
| 157 | +├── GET /user/profile → User data |
| 158 | +├── PUT /user/profile → Update profile |
| 159 | +├── DELETE /user/account → Soft delete |
| 160 | +└── Security validation on all endpoints |
| 161 | +``` |
| 162 | + |
| 163 | +### 8. PasswordResetTokenService Testing |
| 164 | +**Priority**: MEDIUM - Account Recovery |
| 165 | + |
| 166 | +#### Token Lifecycle: |
| 167 | +``` |
| 168 | +Token Management: |
| 169 | +├── Token generation with entropy |
| 170 | +├── Token validation and expiry |
| 171 | +├── One-time use enforcement |
| 172 | +└── Concurrent token handling |
| 173 | +``` |
| 174 | + |
| 175 | +--- |
| 176 | + |
| 177 | +## PHASE 3: Communication & Event System |
| 178 | + |
| 179 | +### 9. UserEmailService Testing |
| 180 | +**Priority**: HIGH - User Communication |
| 181 | + |
| 182 | +#### Email Scenarios: |
| 183 | +``` |
| 184 | +Email Types: |
| 185 | +├── Registration Confirmation |
| 186 | +│ ├── Correct token in URL |
| 187 | +│ ├── User data in template |
| 188 | +│ └── HTML/Text formats |
| 189 | +├── Password Reset |
| 190 | +│ ├── Secure token handling |
| 191 | +│ └── Expiration notice |
| 192 | +├── Account Status |
| 193 | +│ ├── Account locked notification |
| 194 | +│ ├── Account unlocked notification |
| 195 | +│ └── Welcome after verification |
| 196 | +└── Infrastructure |
| 197 | + ├── Template rendering |
| 198 | + ├── Error handling |
| 199 | + └── Mock mail verification |
| 200 | +``` |
| 201 | + |
| 202 | +### 10. Event System Testing |
| 203 | +**Priority**: MEDIUM - Async Processing |
| 204 | + |
| 205 | +#### Event Types: |
| 206 | +``` |
| 207 | +Event Publishing: |
| 208 | +├── UserRegistrationEvent |
| 209 | +├── PasswordResetEvent |
| 210 | +├── LoginEvent (success/failure) |
| 211 | +├── AccountLockEvent |
| 212 | +└── Transactional consistency |
| 213 | +``` |
| 214 | + |
| 215 | +--- |
| 216 | + |
| 217 | +## PHASE 4: Integration & Quality Assurance |
| 218 | + |
| 219 | +### 11. Security Integration Tests |
| 220 | +**Priority**: HIGH - Cross-cutting Concerns |
| 221 | + |
| 222 | +#### Security Scenarios: |
| 223 | +``` |
| 224 | +Security Features: |
| 225 | +├── Method-level @PreAuthorize |
| 226 | +├── CSRF protection validation |
| 227 | +├── Session management |
| 228 | +├── Remember-me functionality |
| 229 | +└── Concurrent session control |
| 230 | +``` |
| 231 | + |
| 232 | +### 12. Data Validation & Edge Cases |
| 233 | +**Priority**: MEDIUM - Robustness |
| 234 | + |
| 235 | +#### Validation Tests: |
| 236 | +``` |
| 237 | +Input Validation: |
| 238 | +├── Bean validation on DTOs |
| 239 | +├── Custom validators |
| 240 | +├── XSS prevention |
| 241 | +├── SQL injection prevention |
| 242 | +└── Edge case handling |
| 243 | +``` |
| 244 | + |
| 245 | +--- |
| 246 | + |
| 247 | +## Implementation Strategy |
| 248 | + |
| 249 | +### Test Generation Workflow |
| 250 | +``` |
| 251 | +For Each Component: |
| 252 | +1. Analyze with zen testgen |
| 253 | + └── Provide: Interface + Implementation + DTOs |
| 254 | +2. Generate comprehensive tests |
| 255 | + └── Review and enhance output |
| 256 | +3. Add integration layer |
| 257 | + └── Test with real dependencies |
| 258 | +4. Verify quality metrics |
| 259 | + └── Coverage + Meaningful assertions |
| 260 | +``` |
| 261 | + |
| 262 | +### Quality Standards Checklist |
| 263 | +``` |
| 264 | +[ ] Real implementations over mocks |
| 265 | +[ ] Database tests use @Transactional |
| 266 | +[ ] Clear, specific assertions |
| 267 | +[ ] Edge cases covered |
| 268 | +[ ] Security scenarios included |
| 269 | +[ ] No flaky tests |
| 270 | +[ ] Runs on JDK 17 & 21 |
| 271 | +``` |
| 272 | + |
| 273 | +### Success Metrics |
| 274 | +``` |
| 275 | +Coverage Targets: |
| 276 | +├── Service Layer: 90%+ |
| 277 | +├── Controllers: 85%+ |
| 278 | +├── Security: 95%+ |
| 279 | +└── Overall: 80%+ |
| 280 | +
|
| 281 | +Execution Standards: |
| 282 | +├── Total runtime < 5 minutes |
| 283 | +├── Zero flaky tests |
| 284 | +└── Deterministic results |
| 285 | +``` |
| 286 | + |
| 287 | +--- |
| 288 | + |
| 289 | +## Immediate Next Steps |
| 290 | + |
| 291 | +1. **Begin with DSUserDetailsService** |
| 292 | + - Implement 13 identified test cases |
| 293 | + - Use zen testgen for comprehensive coverage |
| 294 | + - Establish patterns for remaining services |
| 295 | + |
| 296 | +2. **Create Test Utilities** |
| 297 | + - Authentication test helpers |
| 298 | + - Security context builders |
| 299 | + - Enhanced OAuth2 mocks |
| 300 | + |
| 301 | +3. **Document Patterns** |
| 302 | + - Test structure standards |
| 303 | + - Naming conventions |
| 304 | + - Assertion patterns |
| 305 | + |
| 306 | +## Progress Tracking |
| 307 | + |
| 308 | +### Phase 1 Progress |
| 309 | +- [x] DSUserDetailsService Tests (Completed - Unit & Integration) |
| 310 | +- [x] AuthorityService Tests (Completed - Unit & Integration) |
| 311 | +- [ ] AuthController Tests |
| 312 | +- [ ] LoginAttemptService Tests |
| 313 | + |
| 314 | +### Phase 2 Progress |
| 315 | +- [ ] UserService Enhancement |
| 316 | +- [ ] RegistrationController Tests |
| 317 | +- [ ] UserController Tests |
| 318 | +- [ ] PasswordResetTokenService Tests |
| 319 | + |
| 320 | +### Phase 3 Progress |
| 321 | +- [ ] UserEmailService Tests |
| 322 | +- [ ] Event System Tests |
| 323 | + |
| 324 | +### Phase 4 Progress |
| 325 | +- [ ] Security Integration Tests |
| 326 | +- [ ] Data Validation Tests |
0 commit comments