LifeRPG_v2.0/modern/tests/test_oauth_refresh.py
TLimoges33 7fe4ae5365
🧙‍♂️ Transform LifeRPG into The Wizard's Grimoire - Production-Ready Application
 Major Features Added:
- Complete magical theming and rebranding from LifeRPG to The Wizard's Grimoire
- Production-grade React frontend with Tailwind CSS v4 and magical aesthetics
- Comprehensive analytics dashboard with Recharts integration (ScryingPortal)
- Push notifications system with PWA service worker support
- Drag & drop functionality using @dnd-kit for habit reordering
- Social features with friends system and leaderboards
- Performance optimization tools and monitoring
- Mobile app enhancement with PWA installation support

🏗️ Technical Infrastructure:
- Advanced service worker with offline support and background sync
- Zustand state management for scalable application state
- Production-ready UI component system with enhanced Button, Card, Input
- Progressive Web App (PWA) with manifest and app installation
- FastAPI backend with comprehensive API endpoints
- Docker containerization and CI/CD pipeline setup

📱 Progressive Web App Features:
- Offline functionality with intelligent caching
- Push notification support for habit reminders
- App installation on mobile and desktop platforms
- Background sync for offline data management
- Performance monitoring and optimization tools

🎨 User Experience:
- Magical wizard/grimoire theming throughout application
- Responsive design optimized for all device sizes
- Drag & drop habit management with smooth animations
- Interactive analytics with multiple chart types
- Social connectivity with friends and competitive features
- Comprehensive notification and performance settings

🔧 Developer Experience:
- Modern development stack with Vite and React
- Comprehensive testing setup and CI/CD pipelines
- Code quality tools with pre-commit hooks
- Docker development environment
- Detailed documentation and implementation guides

This represents a complete transformation from prototype to production-ready application with enterprise-grade features and magical user experience.
2025-08-30 17:32:42 +00:00

89 lines
3.3 KiB
Python

import time
import pytest
import importlib
# Do not import models or oauth at module level; tests must reload them after
# the `client` fixture sets DATABASE_URL so engines bind to the temp DB.
class DummyResp:
def __init__(self, status_code, data):
self.status_code = status_code
self._data = data
def json(self):
return self._data
# Note: DB is initialized by the `client` fixture in tests/conftest.py
def test_refresh_with_temp_session(client, monkeypatch):
# ensure we use the test DB created by the `client` fixture
import modern.backend.models as models
importlib.reload(models)
import modern.backend.oauth as oauth
importlib.reload(oauth)
refresh_google_token_if_needed = oauth.refresh_google_token_if_needed
db = models.SessionLocal()
try:
integration = models.Integration(user_id=1, provider='google', external_id='ext1', config='{}')
db.add(integration)
db.commit()
db.refresh(integration)
token_row = models.OAuthToken(integration_id=integration.id, access_token='old', refresh_token='enc_refresh', scope='read', expires_at=1)
db.add(token_row)
db.commit()
db.refresh(token_row)
def fake_post(url, data=None, timeout=10):
return DummyResp(200, {'access_token': 'new_access', 'refresh_token': 'new_refresh', 'expires_in': 3600, 'scope': 'read'})
monkeypatch.setattr('modern.backend.oauth.requests.post', fake_post)
# ensure client id/secret are set so helper proceeds
monkeypatch.setenv('GOOGLE_CLIENT_ID', 'test_client')
monkeypatch.setenv('GOOGLE_CLIENT_SECRET', 'test_secret')
# call helper with injected db (tests should pass a real session)
new_row = refresh_google_token_if_needed(token_row, db=db)
assert new_row is not None
assert new_row.access_token != token_row.access_token
finally:
db.close()
def test_refresh_with_injected_session(client, monkeypatch):
# ensure we use the test DB created by the `client` fixture
import modern.backend.models as models
importlib.reload(models)
import modern.backend.oauth as oauth
importlib.reload(oauth)
refresh_google_token_if_needed = oauth.refresh_google_token_if_needed
db = models.SessionLocal()
try:
integration = models.Integration(user_id=1, provider='google', external_id='ext2', config='{}')
db.add(integration)
db.commit()
db.refresh(integration)
token_row = models.OAuthToken(integration_id=integration.id, access_token='old2', refresh_token='enc_refresh2', scope='read', expires_at=1)
db.add(token_row)
db.commit()
db.refresh(token_row)
def fake_post(url, data=None, timeout=10):
return DummyResp(200, {'access_token': 'new_access2', 'refresh_token': 'new_refresh2', 'expires_in': 3600, 'scope': 'read'})
monkeypatch.setattr('modern.backend.oauth.requests.post', fake_post)
monkeypatch.setenv('GOOGLE_CLIENT_ID', 'test_client')
monkeypatch.setenv('GOOGLE_CLIENT_SECRET', 'test_secret')
new_row = refresh_google_token_if_needed(token_row, db=db)
assert new_row is not None
assert new_row.access_token != token_row.access_token
finally:
db.close()