-
Notifications
You must be signed in to change notification settings - Fork 219
Description
The Issue:
It's usually frustrating when setting up development and testing environments requires manual insertion of user data. Without an automated endpoint for seeding user data, we face inconsistencies and delays when testing features that depend on a populated user database.
My Proposal:
I propose implementing a POST /api/v1/seed endpoint that specifically seeds the database with sample user data. This endpoint should:
-
Insert a predefined set of user records (e.g., default users, test accounts) into the database.
-
Return a JSON response that includes details such as the number of records inserted and key user information.
-
Be secured so that it is only accessible in development or via proper administrative authorization in other environments.
Alternatives:
Running an external script to seed the user data manually each time, which adds overhead and increases the risk of inconsistent test data.
Utilizing ORM-based seeding mechanisms that aren’t integrated into the API, making the process less transparent and harder to trigger in automated CI/CD pipelines.
Purpose:
To streamline the process of populating our database with user data for development and testing purposes. It would help ensure that every environment starts with a consistent set of user records, reducing setup time and potential errors. A detailed documentation update outlining the sample user data structure, usage instructions, and security considerations (such as environment restrictions) would also be beneficial.
Requirements:
-
Develop server-side logic to parse the provided JSON data and insert it into the database.
-
Implement authentication and authorization to restrict access to this endpoint.
-
Optimize database operations for efficient insertion.
-
Return a structured JSON response indicating the result of the operation.
-
Write unit tests to validate the functionality.
Expected Outcome:
Developers and testers should be able to populate the database with consistent user data using a single API request.
The API should provide clear feedback on the seeding process.
Tasks:
Create an endpoint (POST: /api/v1/seed).
Validate user authentication and authorization before processing the request.
Parse the JSON data from the request body.
Insert the user data into the database.
Return a JSON response with the number of records inserted and any relevant information.
Handle edge cases, such as invalid JSON data or database errors.
Write comprehensive unit tests.
Example Request [With Auth Token and JSON Payload]:
curl -X POST {rootdomain}/api/v1/seed \ -H "Authorization: Bearer YOUR_ADMIN_ACCESS_TOKEN" \ -H "Content-Type: application/json" \ -d @seed_data.json Response [Success]:
JSON
{
"status_code": 201,
"message": "Successfully seeded database with user data.",
"records_inserted": 1,
"seeded_users": [
{
"id": "user-12345",
"created_at": "2023-11-20T10:00:00Z",
"updated_at": "2023-11-21T14:30:00Z",
"first_name": "Alice",
"last_name": "Johnson",
"email": "[email protected]",
"status": "active",
"password": "hashed_password_123",
"phone": "+23412345678",
"is_active": true,
"backup_codes": [
"code123",
"code456"
],
"attempts_left": 3,
"time_left": 0,
"secret": "2FA_SECRET_KEY",
"is_2fa_enabled": true,
"deletedAt": null,
"owned_organisations": [
{
"id": "org-67890",
"created_at": "2023-11-15T09:00:00Z",
"updated_at": "2023-11-16T11:45:00Z",
"name": "HNG tech.",
"description": "Providing innovative tech solutions.",
"email": "[email protected]",
"industry": "Technology",
"type": "Private",
"country": "NG",
"address": "123 Main St, Lagos",
"owner": "user-12345",
"members": [
"user-12345",
"user-98765"
],
"state": "LG",
"isDeleted": false,
"products": [
{
"id": "prod-101112",
"created_at": "2023-11-18T13:00:00Z",
"updated_at": "2023-11-19T15:20:00Z",
"name": "product A",
"description": "example product description.",
"category": "Software",
"image": "https://example.com/images/cloud-storage.jpg",
"price": 49.99,
"cost_price": 25.00,
"quantity": 100,
"size": "N/A",
"stock_status": "in stock",
"org": "org-67890",
"deletedAt": null,
"comments": [
{
"id": "comment-131415",
"created_at": "2023-11-20T16:00:00Z",
"updated_at": "2023-11-20T16:00:00Z",
"comment": "Great product!",
"product": "prod-101112",
"user": "user-98765",
"model_id": "prod-101112",
"model_type": "Product"
}
],
"orderItems": [
{
"id": "order-item-161718",
"created_at": "2023-11-21T10:00:00Z",
"updated_at": "2023-11-21T10:00:00Z",
"order": "order-192021",
"product": "prod-101112",
"quantity": 2,
"total_price": 99.98
}
],
"cart": [
{
"id": "cart-222324",
"created_at": "2023-11-21T12:00:00Z",
"updated_at": "2023-11-21T12:00:00Z",
"quantity": 1,
"product": "prod-101112",
"user": "user-98765"
}
]
}
],
"preferences": [
{
"id": "pref-252627",
"created_at": "2023-11-17T14:00:00Z",
"updated_at": "2023-11-17T14:00:00Z",
"name": "Theme",
"value": "Dark",
"organisation": "org-67890"
}
],
"invites": [
{
"id": "invite-282930",
"created_at": "2023-11-19T10:00:00Z",
"updated_at": "2023-11-19T10:00:00Z",
"token": "invite_token_123",
"email": "[email protected]",
"isGeneric": false,
"isAccepted": true,
"organisation": "org-67890"
}
]
}
],
"organisations": [],
"jobs": [
{
"id": "job-313233",
"created_at": "2023-11-20T10:00:00Z",
"updated_at": "2023-11-20T10:00:00Z",
"title": "Senior Frontend Developer",
"description": "Develop user interfaces for web applications.",
"location": "Remote",
"deadline": "2024-01-31T23:59:59Z",
"salary_range": "80,000 - 120,000",
"job_type": "full-time",
"job_mode": "remote",
"company_name": "Tech Solutions Inc.",
"qualifications": [
"Bachelor's Degree in Computer Science",
"5+ years of experience in React"
],
"key_responsibilities": [
"Develop and maintain frontend components",
"Participate in code reviews"
],
"benefits": [
"Health insurance",
"Unlimited PTO"
],
"experience_level": "Senior Developer",
"job_application": []
}
],
"profile": {
"id": "profile-343536",
"created_at": "2023-11-21T08:00:00Z",
"updated_at": "2023-11-21T08:00:00Z",
"username": "alicej",
"jobTitle": "CEO",
"pronouns": "she/her",
"department": "Executive",
"email": "[email protected]",
"bio": "Tech enthusiast and entrepreneur.",
"social_links": [
"https://linkedin.com/in/alicejohnson"
],
"language": "en",
"region": "US",
"timezones": "America/Los_Angeles",
"profile_pic_url": "https://example.com/images/alice.jpg",
"deletedAt": null
},
"testimonials": [
{
"id": "test-373839",
"created_at": "2023-11-21T11:00:00Z",
"updated_at": "2023-11-21T11:00:00Z",
"user": "user-98765",
"name": "Bob Smith",
"content": "Alice is a fantastic leader!"
}
],
}
Response [Errors]:
Unauthorized Access:
{ "status_code": 401, "message": "Not authorized" }
Invalid JSON Data:
{ "status_code": 400, message": "Invalid JSON data provided." }
Server Error:
{ "status_code": 500, "message": "An unexpected error occurred." }
Testing:
-
Write unit tests to verify user data seeding.
-
Test scenarios with valid and invalid JSON data.
-
Ensure authentication and authorization checks are enforced.
-
Test database error handling.
-
Updated Seed JSON (with remaining fields):
JSON
[
{
"id": "user-12345",
"created_at": "2023-11-20T10:00:00Z",
"updated_at": "2023-11-21T14:30:00Z",
"first_name": "Alice",
"last_name": "Johnson",
"email": "[email protected]",
"status": "active",
"password": "hashed_password_123",
"phone": "+23412345678",
"is_active": true,
"backup_codes": [
"code123",
"code456"
],
"attempts_left": 3,
"time_left": 0,
"secret": "2FA_SECRET_KEY",
"is_2fa_enabled": true,
"deletedAt": null,
"owned_organisations": [
{
"id": "org-67890",
"created_at": "2023-11-15T09:00:00Z",
"updated_at": "2023-11-16T11:45:00Z",
"name": "HNG tech.",
"description": "Providing innovative tech solutions.",
"email": "[email protected]",
"industry": "Technology",
"type": "Private",
"country": "NG",
"address": "123 Main St, Lagos",
"owner": "user-12345",
"members": [
"user-12345",
"user-98765"
],
"state": "LG",
"isDeleted": false,
"products": [
{
"id": "prod-101112",
"created_at": "2023-11-18T13:00:00Z",
"updated_at": "2023-11-19T15:20:00Z",
"name": "product A",
"description": "example product description.",
"category": "Software",
"image": "https://example.com/images/cloud-storage.jpg",
"price": 49.99,
"cost_price": 25.00,
"quantity": 100,
"size": "N/A",
"stock_status": "in stock",
"org": "org-67890",
"deletedAt": null,
"comments": [
{
"id": "comment-131415",
"created_at": "2023-11-20T16:00:00Z",
"updated_at": "2023-11-20T16:00:00Z",
"comment": "Great product!",
"product": "prod-101112",
"user": "user-98765",
"model_id": "prod-101112",
"model_type": "Product"
}
],
"orderItems": [
{
"id": "order-item-161718",
"created_at": "2023-11-21T10:00:00Z",
"updated_at": "2023-11-21T10:00:00Z",
"order": "order-192021",
"product": "prod-101112",
"quantity": 2,
"total_price": 99.98
}
],
"cart": [
{
"id": "cart-222324",
"created_at": "2023-11-21T12:00:00Z",
"updated_at": "2023-11-21T12:00:00Z",
"quantity": 1,
"product": "prod-101112",
"user": "user-98765"
}
]
}
],
"preferences": [
{
"id": "pref-252627",
"created_at": "2023-11-17T14:00:00Z",
"updated_at": "2023-11-17T14:00:00Z",
"name": "Theme",
"value": "Dark",
"organisation": "org-67890"
}
],
"invites": [
{
"id": "invite-282930",
"created_at": "2023-11-19T10:00:00Z",
"updated_at": "2023-11-19T10:00:00Z",
"token": "invite_token_123",
"email": "[email protected]",
"isGeneric": false,
"isAccepted": true,
"organisation": "org-67890"
}
]
}
],
"organisations": [],
"jobs": [
{
"id": "job-313233",
"created_at": "2023-11-20T10:00:00Z",
"updated_at": "2023-11-20T10:00:00Z",
"title": "Senior Frontend Developer",
"description": "Develop user interfaces for web applications.",
"location": "Remote",
"deadline": "2024-01-31T23:59:59Z",
"salary_range": "80,000 - 120,000",
"job_type": "full-time",
"job_mode": "remote",
"company_name": "Tech Solutions Inc.",
"qualifications": [
"Bachelor's Degree in Computer Science",
"5+ years of experience in React"
],
"key_responsibilities": [
"Develop and maintain frontend components",
"Participate in code reviews"
],
"benefits": [
"Health insurance",
"Unlimited PTO"
],
"experience_level": "Senior Developer",
"job_application": []
}
],
"profile": {
"id": "profile-343536",
"created_at": "2023-11-21T08:00:00Z",
"updated_at": "2023-11-21T08:00:00Z",
"username": "alicej",
"jobTitle": "CEO",
"pronouns": "she/her",
"department": "Executive",
"email": "[email protected]",
"bio": "Tech enthusiast and entrepreneur.",
"social_links": [
"https://linkedin.com/in/alicejohnson"
],
"language": "en",
"region": "US",
"timezones": "America/Los_Angeles",
"profile_pic_url": "https://example.com/images/alice.jpg",
"deletedAt": null
},
"testimonials": [
{
"id": "test-373839",
"created_at": "2023-11-21T11:00:00Z",
"updated_at": "2023-11-21T11:00:00Z",
"user": "user-98765",
"name": "Bob Smith",
"content": "Alice is a fantastic leader!"
}
],