A minimalist web application for managing e-books with a FastAPI backend and a modern, responsive frontend featuring session-based authentication and real-time notifications. This is a more difficult version of Easier-E-Book-Manager.
- π Session-based authentication with Bearer token support
- β Add new books with comprehensive validation and duplicate checking (includes genre, price, and rating)
- π₯ Import books from a
books.jsonfile with progress feedback - π Enhanced book display showing cover, title, author, year, genre, price, and rating
- π Real-time notifications via snackbar alerts for all user actions
- π± Responsive, minimalist UI (plain JavaScript, CSS)
- ποΈ PostgreSQL database integration with full CRUD operations
- π§ͺ Comprehensive test suite for both frontend and backend
- π CORS support for cross-origin requests
β οΈ Robust error handling with user-friendly messages
- π Python 3.8 or higher
- π PostgreSQL database
- π’ Node.js (optional, for SASS compilation)
-
π¨ Create and activate virtual environment:
# Create virtual environment python -m venv .venv # Activate virtual environment # On Windows: .venv\Scripts\activate # On macOS/Linux: source .venv/bin/activate
-
π¦ Install dependencies:
cd backend pip install -r requirements.txt -
ποΈ Configure database:
- Ensure PostgreSQL is running and a database named
Booksexists with abookstable:- Columns:
isbn(PK),title,author,year,publisher,img_m,genre,price,rating
- Columns:
- Example SQL to create the table:
CREATE TABLE books ( isbn VARCHAR(20) PRIMARY KEY, title VARCHAR(500), author VARCHAR(200), year INTEGER, publisher VARCHAR(200), img_m VARCHAR(500), genre VARCHAR(50), price VARCHAR(10), rating VARCHAR(5) );
- Ensure PostgreSQL is running and a database named
-
π Set password:
- Edit
backend/config.pyand set your desired password:PASSWORD = "123"
- Edit
-
π Run the backend:
uvicorn main:app --reload
The backend will be available at
http://localhost:8000with interactive API docs athttp://localhost:8000/docs
-
π₯οΈ Open the application:
- Open
frontend/index.htmlin your browser - Or serve it from a local server for better CORS handling
- Open
-
π Login credentials:
- Username:
user - Password:
123(or whatever you set inbackend/config.py)
- Username:
- Edit styles in
src/style.scssand compile tofrontend/style.cssas needed.
- π Login: Use username
userand the password set inbackend/config.py - β Add Book: Click the
+button, fill out the form (all fields required), and submit - π₯ Import Books: Click "Import Books" to load books from
books.json - π¨οΈ Print: Click the "Print" button to print your book collection
- β Visual Feedback: Green snackbars for success, red for errors
- β° Session Management: Automatic logout on token expiration with clear notifications
POST /login- Authenticate with HTTP Basic AuthGET /books- Get all books (requires Bearer token)GET /books/{isbn}- Get specific book (requires Bearer token)POST /add-book- Add new book (requires Bearer token)POST /import-books- Import books from JSON (requires Bearer token)
- π§ Backend:
cd backend # Use the test runner (recommended) python run_tests.py list # List all available tests python run_tests.py all # Run key tests python run_tests.py test_new_fields # Run specific test # Or run tests directly from tests/ directory python tests/comprehensive_test.py # Complete system test python tests/test_new_fields.py # New fields functionality python tests/test_auth_flow.py # Authentication workflow
- π Frontend:
- Test Dashboard: Open
frontend/test/README.htmlfor organized test access - Unit Tests: Open
frontend/test/form-tests.htmlfor comprehensive form tests - Integration Tests: Open
frontend/test/real-backend-test.htmlfor backend integration - Debug Tools: Open
frontend/test/debug-form-test.htmlfor troubleshooting
- Test Dashboard: Open
- Purpose: Main integration test covering all system functionality
- Functionality:
- Complete end-to-end testing (server, auth, database)
- Book addition with all fields (genre, price, rating)
- Book retrieval and validation
- Error handling and edge cases
- Usage:
python tests/comprehensive_test.py
- Purpose: Tests enhanced book model with new fields
- Functionality:
- Genre/Gender field mapping (API "genre" β DB "gender")
- Price and rating field handling (string β numeric conversion)
- New fields validation and type conversion
- Usage:
python tests/test_new_fields.py
- Purpose: Authentication workflow testing
- Functionality:
- HTTP Basic Auth login process
- Bearer token generation and validation
- Protected endpoint access with tokens
- Session management and persistence
- Usage:
python tests/test_auth_flow.py
- Purpose: Database health check and structure verification
- Functionality:
- PostgreSQL connection and authentication
- Books table existence and schema validation
- Column types and constraints verification
- Sample data operations
- Usage:
python tests/check_database.py
- Purpose: Database CRUD operations testing
- Functionality:
- Book creation, reading, updating, deletion
- Database integrity verification
- Usage:
python tests/test_database.py
- Purpose: Troubleshoot 500 Internal Server Errors
- Functionality:
- Database connection issue diagnosis
- SQL execution problem identification
- Data type conversion error detection
- Field mapping problem diagnosis
- Usage:
python tests/diagnose_500_error.py
- Purpose: Session management and cleanup
- Functionality:
- Session file cleanup and validation
- Token testing and verification
- Usage:
python tests/clean_sessions.py
- Purpose: Session inspection and debugging
- Functionality: View current session state
- Usage:
python tests/read_sessions.py
- Purpose: Interactive test suite dashboard and main entry point
- Functionality:
- Organized access to all frontend tests
- Test categorization (Unit, Integration, Debug)
- Quick start guide and test descriptions
- Visual test navigation interface
- Usage: Open in browser - Recommended starting point
- Purpose: Comprehensive frontend form testing without backend dependency
- Functionality:
- Session token retrieval and validation
- Book object construction from form data
- Authorization header formatting
- UI state management testing
- Mock backend interaction testing
- Coverage: 12 comprehensive test cases
- Usage: Open in browser, tests run automatically
- Purpose: Professional Mocha/Chai testing framework
- Functionality:
- Structured test reporting using Mocha and Chai
- Book rendering and display testing
- Login/logout flow validation
- Session token management verification
- Usage: Open in browser (requires Mocha/Chai from CDN)
- Purpose: Complete backend integration testing
- Functionality:
- Live HTTP Basic Auth login testing
- Bearer token validation with real tokens
- Actual form submission to backend
- CORS and cross-origin request testing
- Real-time error diagnosis
- Usage: Open in browser (requires running backend)
- Purpose: Focused book addition functionality testing
- Functionality:
- Authentication flow testing
- Book data validation and submission
- Database persistence verification
- Error handling and validation
- Usage: Open in browser (requires running backend)
- Purpose: Backend connectivity and health check
- Functionality:
- Server availability testing
- API endpoint validation
- Response time monitoring
- Basic connectivity verification
- Usage: Open in browser (requires running backend)
- Purpose: Advanced form submission debugging and troubleshooting
- Functionality:
- Token validation testing
- Request/response logging and analysis
- Authentication troubleshooting
- Session management debugging
- Usage: Open in browser (requires running backend)
- UI state management testing
- Form prefill and cancellation logic
- Mock backend interaction testing
- Real form submission event simulation
- Error handling for various scenarios
- Coverage: 12 comprehensive test cases
- Usage: Open in browser, tests run automatically
- Purpose: Mocha-based testing framework setup
- Functionality:
- Professional test runner using Mocha and Chai
- Book rendering and display testing
- Login/logout flow validation
- Session token management verification
- Form input validation testing
- Usage: Open in browser (requires Mocha/Chai from CDN)
- Purpose: Real-time testing with actual backend
- Functionality:
- Backend connectivity verification
- Live HTTP Basic Auth login testing
- Bearer token validation with real tokens
- Actual form submission to backend
- Real-time error diagnosis
- CORS and network issue detection
- Usage: Open in browser (requires running backend)
- Purpose: Cross-origin request testing
- Functionality:
- Tests form submission from file:// protocol
- CORS compatibility verification
- Network error identification
- Same-origin vs. cross-origin behavior comparison
- Usage: Open in browser (requires running backend)
- Purpose: JavaScript unit test utilities
- Functionality:
- Reusable test functions for frontend components
- Mock data and helper functions
- Integration with test HTML pages
- Usage: Included in test HTML pages
| Test Area | Backend Tests | Frontend Tests | Coverage Level |
|---|---|---|---|
| π Authentication | β
test_auth_flow.pyβ test_auth_fix.pyβ token_test.py |
β
debug-form-test.htmlβ form-tests.html |
Complete |
| π Authorization | β
Bearer token validation β Session management |
β
Token handling β Header formatting |
Complete |
| ποΈ Database Operations | β
Book CRUD operations β Persistence validation β Duplicate checking |
β
Mock database tests β Real backend integration |
Complete |
| π Form Handling | β
Data validation β Error responses |
β
Form validation β Submission logic β Error handling |
Complete |
| π₯οΈ UI Interactions | N/A | β
State management β Visual feedback β Snackbar notifications |
Complete |
| π Network Communications | β
HTTP request/response β Error codes |
β
CORS handling β Network errors β Cross-origin requests |
Complete |
| β
Invalid auth β Database errors β Validation failures |
β
Network failures β Invalid tokens β Form errors |
Complete | |
| π€ User Experience | β
API response times β Error messages |
β
Notifications β Form prefilling β Visual feedback |
Complete |
cd backend
# Use the test runner (recommended)
python run_tests.py all # Run all key tests
python run_tests.py list # List available tests
python run_tests.py comprehensive_test # Main integration test
python run_tests.py test_new_fields # New fields test
python run_tests.py test_auth_flow # Authentication test
# Or run tests directly from tests/ directory
python tests/comprehensive_test.py # Complete system test
python tests/test_new_fields.py # New fields testing
python tests/test_auth_flow.py # Authentication testing
# Database tests
python tests/check_database.py # Database health check
python tests/test_database.py # Database operations
# Diagnostic tests
python tests/diagnose_500_error.py # Server error diagnosis
# Utility scripts
python tests/clean_sessions.py # Session cleanup
python tests/read_sessions.py # Session inspection- Test Dashboard:
- Open
frontend/test/README.htmlin browser - Interactive dashboard with organized test access
- Click on any test to launch it in a new tab
- Includes test descriptions and prerequisites
- Open
-
Comprehensive Form Tests:
- Open
frontend/test/form-tests.htmlin browser - Tests run automatically on page load
- Coverage: 12 comprehensive test cases
- View results in browser console and on page
- Open
-
Professional Test Runner:
- Open
frontend/test/test-runner.htmlin browser - Uses Mocha/Chai framework for structured testing
- Interactive test results display
- Open
-
Backend Connectivity Check:
- Start backend server:
uvicorn main:app --reload - Open
frontend/test/server-status-check.htmlin browser - Quick verification of backend availability
- Start backend server:
-
Complete Integration Testing:
- Open
frontend/test/real-backend-test.htmlin browser - Comprehensive backend integration tests
- Tests authentication, form submission, CORS
- Open
-
Book Addition Testing:
- Open
frontend/test/add-book-test.htmlin browser - Focused testing of book addition workflow
- Verifies database persistence
- Open
- Form Submission Debugging:
- Open
frontend/test/debug-form-test.htmlin browser - Advanced debugging tools for form issues
- Token validation and request logging
- Open
# Terminal 1: Start backend
cd backend
uvicorn main:app --reload
# Terminal 2: Run backend tests
cd backend
python run_tests.py all # Run all key tests with test runner
python run_tests.py comprehensive_test # Run comprehensive functionality test
# Browser: Open frontend test dashboard
# 1. frontend/test/README.html # Main test dashboard (recommended)
# 2. frontend/test/form-tests.html # Unit tests (no backend needed)
# 3. frontend/test/real-backend-test.html # Integration tests (backend required)
# 4. frontend/test/debug-form-test.html # Debug tools (if issues occur)python tests/diagnose_500_error.py # Database diagnostics python tests/check_database.py # Database health check
- frontend/test/form-tests.html
- frontend/debug-form-test.html
- frontend/test/real-backend-test.html
### π Test Debugging and Reporting
#### π§ Backend Test Output
- **Success Indicators:** HTTP 200 status codes, "Test passed" messages
- **Failure Indicators:** HTTP 4xx/5xx status codes, exception tracebacks
- **Debug Information:** Token values, request/response data, database states
#### π Frontend Test Output
- **Browser Console:** Detailed test results and error messages
- **Visual Indicators:** Green checkmarks for passed tests, red X for failures
- **Interactive Results:** Click on test results for detailed information
#### β οΈ Common Test Issues and Solutions
- **Backend Connection Failed:** Ensure `uvicorn main:app --reload` is running
- **401 Unauthorized:** Check password in `config.py` matches test scripts
- **CORS Errors:** Use a local server instead of opening HTML files directly
- **Database Errors:** Verify PostgreSQL is running and `Books` database exists
- **Token Validation Failed:** Check for expired sessions, restart backend if needed
#### π Test Data Management
- **Test Books:** Tests create books with ISBNs like `test-*`, `auth-test-*`, `manual-*`
- **Cleanup:** Test books persist in database - manually remove if needed
- **Sessions:** Backend maintains session tokens in `sessions.pkl` file
## π Project Structure
backend/ # FastAPI backend
βββ main.py # Main application with all endpoints
βββ config.py # Configuration (password)
βββ requirements.txt # Python dependencies
βββ sessions.pkl # Session token storage
βββ run_tests.py # Test runner script
βββ tests/ # Test files directory
βββ __init__.py # Python package initialization
βββ test_auth_flow.py # Authentication testing
βββ end_to_end_test.py # Complete API testing
βββ complete_test.py # Full flow testing
βββ test_new_fields.py # New fields testing
βββ diagnose_500_error.py # Database diagnostics
βββ *.py # Various other test scripts
frontend/ # π Frontend application
βββ index.html # π Main application page
βββ app.js # β‘ JavaScript application logic
βββ style.css # π¨ Compiled CSS styles
βββ test/ # π§ͺ Frontend test suite
βββ README.html # π Interactive test dashboard (entry point)
βββ README.md # π Test documentation
βββ form-tests.html # π§ Unit tests (no backend required)
βββ test-runner.html # πββοΈ Mocha/Chai test framework
βββ real-backend-test.html # π Integration tests
βββ add-book-test.html # β Book addition testing
βββ server-status-check.html # π Connectivity check
βββ debug-form-test.html # π Debug tools
βββ index.html # π Legacy test index
βββ app.test.js # π§° Test utilities
src/ # SASS source files
βββ style.scss # Main SASS source
books.json # Example books data for import Books.csv # Sample data in CSV format README.md # This file
## β
Recent Improvements
- **π Fixed authentication system** - Proper Bearer token handling
- **ποΈ Added real database persistence** - Books are actually saved to PostgreSQL
- **π Implemented snackbar notifications** - Visual feedback for all actions
- **β οΈ Enhanced error handling** - User-friendly error messages
- **π§ͺ Comprehensive testing** - Frontend and backend test suites
- **β° Session management** - Robust token validation and cleanup
- **π CORS support** - Cross-origin request handling
- **π§ Fixed field consistency** - Updated all tests to use "genre" field consistently
- **π Organized test structure** - All backend tests moved to dedicated `tests/` directory
- **πββοΈ Added test runner** - Convenient `run_tests.py` script for easy test execution
- **π¨οΈ Print functionality** - Added print button for book collection printing
- **π Database diagnostics** - Tools to troubleshoot database issues and field mapping
## π οΈ Troubleshooting
- **π« 401 Unauthorized errors:** Ensure you're logged in and have a valid session token
- **ποΈ Database connection issues:** Verify PostgreSQL is running and the database exists
- **π₯ Import failures:** Check that `books.json` exists and has the correct format
- **π CORS errors:** Serve the frontend from a local server instead of opening directly
- **β οΈ 500 Internal Server Errors when adding books:** The server may need to be restarted if it becomes unresponsive. Stop the server (Ctrl+C) and restart with `uvicorn main:app --reload`
- **π§ Field mapping issues:** The database has both 'genre' and 'genre' columns - the Book model uses 'genre' column for data storage
- **π Test book duplicates:** Test books persist in the database. Use unique ISBNs or manually clean test data if needed
## License
MIT
Used adjusted data set:
J. Schler, M. Koppel, S. Argamon and J. Pennebaker (2006). Effects of Age and Genre on Blogging in Proceedings of 2006 AAAI Spring Symposium on Computational Approaches for Analyzing Weblogs. URL: http://www.cs.biu.ac.il/~schlerj/schler_springsymp06.pdf