Skip to content

Commit 00a4627

Browse files
Merge pull request #5 from CodeSignal/feature/existing-users
Added user seeding
2 parents 2b9e21b + c2514d0 commit 00a4627

File tree

5 files changed

+44
-12
lines changed

5 files changed

+44
-12
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ COPY requirements.txt .
77
# Copy configuration and initial data files
88
COPY auth_config.yml .
99
COPY initial_todos.json .
10+
COPY initial_users.json .
1011

1112
# Install Python dependencies listed in requirements.txt
1213
RUN pip install --no-cache-dir -r requirements.txt

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ The API's authentication is configured through `auth_config.yml` in the root dir
6161

6262
## Initial Data
6363

64-
The project comes with `initial_todos.json`, which contains a set of predefined todo items that serve as sample data. This file includes example todos with various states (completed and pending) to help you get started with testing the API immediately.
64+
The project comes with initial data, seeded at startup:
65+
- Todos: `initial_todos.json`
66+
- Users: `initial_users.json`
6567

6668
## Running the API
6769

app/main.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from routes.notes import notes_bp
66
from routes.auth import auth_bp, init_auth_routes
77
from middleware.auth_middleware import AuthMiddleware
8-
from utils.config import load_config, load_initial_todos
8+
from utils.config import load_config, load_initial_todos, load_initial_users
99
from utils.auth import setup_auth_config
10-
from services.auth_service import init_auth_service
10+
from services.auth_service import init_auth_service, add_user
1111
from flasgger import Swagger
1212
import secrets
1313

@@ -75,13 +75,19 @@ def create_app(auth_config):
7575

7676
return app
7777

78+
def seed_users():
79+
for user_data in load_initial_users():
80+
if not add_user(user_data["username"], user_data["password"]):
81+
print(f"Error: Failed to add user on init")
82+
7883
if __name__ == "__main__":
7984
try:
8085
# Load authentication configuration from config file
8186
auth_method, secret = load_config()
8287

8388
# Set up authentication based on configuration
8489
auth_config = setup_auth_config(auth_method, secret)
90+
seed_users()
8591

8692
# Initialize auth service
8793
init_auth_service(auth_config)

app/utils/config.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import yaml
22
import os
33
import json
4+
from pathlib import Path
5+
from typing import List
6+
7+
INITIAL_USERS_FILE = "initial_users.json"
8+
INITIAL_TODOS_FILE = "initial_todos.json"
9+
410

511
def load_config():
612
"""Load configuration from auth_config.yml file."""
@@ -38,18 +44,27 @@ def load_config():
3844
print("Using default configuration (no auth)")
3945
return "none", None
4046

47+
4148
def load_initial_todos():
4249
"""Load initial todos from the configuration file."""
43-
todos_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'initial_todos.json')
44-
45-
if not os.path.exists(todos_path):
46-
print(f"Warning: initial_todos.json not found at {todos_path}, using empty todos list")
50+
path = Path(__file__).resolve().parents[2] / INITIAL_TODOS_FILE
51+
return load_initial_data(path, "todos")
52+
53+
def load_initial_users() -> List:
54+
path = Path(__file__).resolve().parents[2] / INITIAL_USERS_FILE
55+
return load_initial_data(path)
56+
57+
def load_initial_data(path: Path, data_key: str = "data") -> List:
58+
filename = path.name
59+
60+
if not path.exists():
61+
print(f"Warning: {filename} not found at {path}, using empty list")
4762
return []
48-
63+
4964
try:
50-
with open(todos_path, 'r') as f:
65+
with open(path, 'r') as f:
5166
data = json.load(f)
52-
return data.get('todos', [])
67+
return data.get(data_key, [])
5368
except Exception as e:
54-
print(f"Error loading initial_todos.json: {e}")
55-
return []
69+
print(f"Error loading {filename}: {e}")
70+
return []

initial_users.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"data": [
3+
{
4+
"username": "johnsmith",
5+
"password": "testpass123"
6+
}
7+
]
8+
}

0 commit comments

Comments
 (0)