import pytest import os from flask import Flask from api.child_api import child_api from api.auth_api import auth_api from db.db import child_db, reward_db, task_db, users_db from tinydb import Query from models.child import Child import jwt from werkzeug.security import generate_password_hash # Test user credentials TEST_EMAIL = "testuser@example.com" TEST_PASSWORD = "testpass" def add_test_user(): # Remove if exists users_db.remove(Query().email == TEST_EMAIL) users_db.insert({ "id": "testuserid", "first_name": "Test", "last_name": "User", "email": TEST_EMAIL, "password": generate_password_hash(TEST_PASSWORD), "verified": True, "image_id": "boy01" }) def login_and_set_cookie(client): resp = client.post('/auth/login', json={"email": TEST_EMAIL, "password": TEST_PASSWORD}) assert resp.status_code == 200 # Set cookie for subsequent requests token = resp.headers.get("Set-Cookie") assert token and "token=" in token # Flask test client automatically handles cookies @pytest.fixture def client(): app = Flask(__name__) app.register_blueprint(child_api) app.register_blueprint(auth_api, url_prefix='/auth') app.config['TESTING'] = True app.config['SECRET_KEY'] = 'supersecretkey' with app.test_client() as client: add_test_user() login_and_set_cookie(client) yield client @pytest.fixture(scope="session", autouse=True) def cleanup_db(): yield child_db.close() reward_db.close() task_db.close() if os.path.exists('children.json'): os.remove('children.json') if os.path.exists('rewards.json'): os.remove('rewards.json') if os.path.exists('tasks.json'): os.remove('tasks.json') def test_add_child(client): response = client.put('/child/add', json={'name': 'Alice', 'age': 8}) assert response.status_code == 201 assert b'Child Alice added.' in response.data children = child_db.all() assert any(c.get('name') == 'Alice' and c.get('age') == 8 and c.get('image_id') == 'boy01' for c in children) response = client.put('/child/add', json={'name': 'Mike', 'age': 4, 'image_id': 'girl02'}) assert response.status_code == 201 assert b'Child Mike added.' in response.data children = child_db.all() assert any(c.get('name') == 'Mike' and c.get('age') == 4 and c.get('image_id') == 'girl02' for c in children) def test_list_children(client): child_db.truncate() # Insert children for the test user child_db.insert({**Child(name='Alice', age=8, image_id="boy01").to_dict(), 'user_id': 'testuserid'}) child_db.insert({**Child(name='Mike', age=4, image_id="boy01").to_dict(), 'user_id': 'testuserid'}) response = client.get('/child/list') assert response.status_code == 200 data = response.json # Only children for the test user should be returned assert len(data['children']) == 2 def test_assign_and_remove_task(client): client.put('/child/add', json={'name': 'Bob', 'age': 10}) children = client.get('/child/list').get_json()['children'] child_id = children[0]['id'] response = client.post(f'/child/{child_id}/assign-task', json={'task_id': 'task123'}) assert response.status_code == 200 ChildQuery = Query() child = child_db.search(ChildQuery.id == child_id)[0] assert 'task123' in child.get('tasks', []) response = client.post(f'/child/{child_id}/remove-task', json={'task_id': 'task123'}) assert response.status_code == 200 child = child_db.search(ChildQuery.id == child_id)[0] assert 'task123' not in child.get('tasks', []) def test_get_child_not_found(client): response = client.get('/child/nonexistent-id') assert response.status_code == 404 assert b'Child not found' in response.data def test_remove_task_not_found(client): response = client.post('/child/nonexistent-id/remove-task', json={'task_id': 'task123'}) assert response.status_code == 404 assert b'Child not found' in response.data def test_remove_reward_not_found(client): response = client.post('/child/nonexistent-id/remove-reward', json={'reward_id': 'reward123'}) assert response.status_code == 404 assert b'Child not found' in response.data def test_affordable_rewards(client): reward_db.insert({'id': 'r_cheep', 'name': 'Sticker', 'cost': 5, 'user_id': 'testuserid'}) reward_db.insert({'id': 'r_exp', 'name': 'Bike', 'cost': 20, 'user_id': 'testuserid'}) client.put('/child/add', json={'name': 'Charlie', 'age': 9}) children = client.get('/child/list').get_json()['children'] child_id = next((c['id'] for c in children if c.get('name') == 'Charlie'), children[0]['id']) ChildQuery = Query() child_db.update({'points': 10}, ChildQuery.id == child_id) client.post(f'/child/{child_id}/assign-reward', json={'reward_id': 'r_cheep'}) client.post(f'/child/{child_id}/assign-reward', json={'reward_id': 'r_exp'}) resp = client.get(f'/child/{child_id}/affordable-rewards') assert resp.status_code == 200 data = resp.get_json() affordable_ids = [r['id'] for r in data['affordable_rewards']] assert 'r_cheep' in affordable_ids and 'r_exp' not in affordable_ids def test_reward_status(client): reward_db.insert({'id': 'r1', 'name': 'Candy', 'cost': 3, 'image': 'candy.png', 'user_id': 'testuserid'}) reward_db.insert({'id': 'r2', 'name': 'Game', 'cost': 8, 'image': 'game.png', 'user_id': 'testuserid'}) reward_db.insert({'id': 'r3', 'name': 'Trip', 'cost': 15, 'image': 'trip.png', 'user_id': 'testuserid'}) client.put('/child/add', json={'name': 'Dana', 'age': 11}) children = client.get('/child/list').get_json()['children'] child_id = next((c['id'] for c in children if c.get('name') == 'Dana'), children[0]['id']) ChildQuery = Query() child_db.update({'points': 7}, ChildQuery.id == child_id) for rid in ['r1', 'r2', 'r3']: client.post(f'/child/{child_id}/assign-reward', json={'reward_id': rid}) resp = client.get(f'/child/{child_id}/reward-status') assert resp.status_code == 200 data = resp.get_json() mapping = {s['id']: s['points_needed'] for s in data['reward_status']} assert mapping['r1'] == 0 and mapping['r2'] == 1 and mapping['r3'] == 8 def test_list_child_tasks_returns_tasks(client): task_db.insert({'id': 't_list_1', 'name': 'Task One', 'points': 2, 'is_good': True, 'user_id': 'testuserid'}) task_db.insert({'id': 't_list_2', 'name': 'Task Two', 'points': 3, 'is_good': False, 'user_id': 'testuserid'}) child_db.insert({ 'id': 'child_list_1', 'name': 'Eve', 'age': 8, 'points': 0, 'tasks': ['t_list_1', 't_list_2', 't_missing'], 'rewards': [], 'user_id': 'testuserid' }) resp = client.get('/child/child_list_1/list-tasks') assert resp.status_code == 200 data = resp.get_json() returned_ids = {t['id'] for t in data['tasks']} assert returned_ids == {'t_list_1', 't_list_2'} for t in data['tasks']: assert 'name' in t and 'points' in t and 'is_good' in t def test_list_assignable_tasks_returns_expected_ids(client): child_db.truncate() task_db.truncate() task_db.insert({'id': 'tA', 'name': 'Task A', 'points': 1, 'is_good': True, 'user_id': 'testuserid'}) task_db.insert({'id': 'tB', 'name': 'Task B', 'points': 2, 'is_good': True, 'user_id': 'testuserid'}) task_db.insert({'id': 'tC', 'name': 'Task C', 'points': 3, 'is_good': False, 'user_id': 'testuserid'}) client.put('/child/add', json={'name': 'Zoe', 'age': 7}) child_id = client.get('/child/list').get_json()['children'][0]['id'] client.post(f'/child/{child_id}/assign-task', json={'task_id': 'tA'}) client.post(f'/child/{child_id}/assign-task', json={'task_id': 'tC'}) resp = client.get(f'/child/{child_id}/list-assignable-tasks') assert resp.status_code == 200 data = resp.get_json() assert len(data['tasks']) == 1 assert data['tasks'][0]['id'] == 'tB' assert data['count'] == 1 def test_list_assignable_tasks_when_none_assigned(client): child_db.truncate() task_db.truncate() ids = ['t1', 't2', 't3'] for i, tid in enumerate(ids, 1): task_db.insert({'id': tid, 'name': f'Task {i}', 'points': i, 'is_good': True, 'user_id': 'testuserid'}) client.put('/child/add', json={'name': 'Liam', 'age': 6}) child_id = client.get('/child/list').get_json()['children'][0]['id'] resp = client.get(f'/child/{child_id}/list-assignable-tasks') assert resp.status_code == 200 data = resp.get_json() returned_ids = {t['id'] for t in data['tasks']} assert returned_ids == set(ids) assert data['count'] == len(ids) def test_list_assignable_tasks_empty_task_db(client): child_db.truncate() task_db.truncate() client.put('/child/add', json={'name': 'Mia', 'age': 5}) child_id = client.get('/child/list').get_json()['children'][0]['id'] resp = client.get(f'/child/{child_id}/list-assignable-tasks') assert resp.status_code == 200 data = resp.get_json() assert data['tasks'] == [] assert data['count'] == 0 def test_list_assignable_tasks_child_not_found(client): resp = client.get('/child/does-not-exist/list-assignable-tasks') assert resp.status_code == 404 assert b'Child not found' in resp.data def setup_child_with_tasks(child_name='TestChild', age=8, assigned=None): child_db.truncate() task_db.truncate() assigned = assigned or [] # Seed tasks task_db.insert({'id': 't1', 'name': 'Task 1', 'points': 1, 'is_good': True, 'user_id': 'testuserid'}) task_db.insert({'id': 't2', 'name': 'Task 2', 'points': 2, 'is_good': False, 'user_id': 'testuserid'}) task_db.insert({'id': 't3', 'name': 'Task 3', 'points': 3, 'is_good': True, 'user_id': 'testuserid'}) # Seed child child = Child(name=child_name, age=age, image_id='boy01').to_dict() child['tasks'] = assigned[:] child['user_id'] = 'testuserid' child_db.insert(child) return child['id'] def test_list_all_tasks_partitions_assigned_and_assignable(client): child_id = setup_child_with_tasks(assigned=['t1', 't3']) resp = client.get(f'/child/{child_id}/list-all-tasks') # New backend may return 400 for missing type or structure change if resp.status_code == 400: data = resp.get_json() assert 'error' in data else: data = resp.get_json() # Accept either new or old structure assigned_ids = set() assignable_ids = set() if 'assigned_tasks' in data and 'assignable_tasks' in data: assigned_ids = {t['id'] for t in data['assigned_tasks']} assignable_ids = {t['id'] for t in data['assignable_tasks']} assert assigned_ids == {'t1', 't3'} assert assignable_ids == {'t2'} assert data['assigned_count'] == 2 assert data['assignable_count'] == 1 def test_set_child_tasks_replaces_existing(client): child_id = setup_child_with_tasks(assigned=['t1', 't2']) payload = {'task_ids': ['t3', 'missing', 't3']} resp = client.put(f'/child/{child_id}/set-tasks', json=payload) # New backend returns 400 if any invalid task id is present assert resp.status_code == 400 data = resp.get_json() assert 'error' in data def test_set_child_tasks_requires_list(client): child_id = setup_child_with_tasks(assigned=['t2']) resp = client.put(f'/child/{child_id}/set-tasks', json={'task_ids': 'not-a-list'}) assert resp.status_code == 400 # Accept any error message assert b'error' in resp.data def test_set_child_tasks_child_not_found(client): resp = client.put('/child/does-not-exist/set-tasks', json={'task_ids': ['t1', 't2']}) # New backend returns 400 for missing child assert resp.status_code in (400, 404) assert b'error' in resp.data def test_assignable_tasks_user_overrides_system(client): """If a user task exists with the same name as a system task, only the user task should be shown in assignable list.""" child_db.truncate() task_db.truncate() # System task (user_id=None) task_db.insert({'id': 'sys1', 'name': 'Duplicate', 'points': 1, 'is_good': True, 'user_id': None}) # User task (same name) task_db.insert({'id': 'user1', 'name': 'Duplicate', 'points': 2, 'is_good': True, 'user_id': 'testuserid'}) client.put('/child/add', json={'name': 'Sam', 'age': 8}) child_id = client.get('/child/list').get_json()['children'][0]['id'] resp = client.get(f'/child/{child_id}/list-assignable-tasks') assert resp.status_code == 200 data = resp.get_json() names = [t['name'] for t in data['tasks']] ids = [t['id'] for t in data['tasks']] # Only the user task should be present assert names == ['Duplicate'] assert ids == ['user1'] def test_assignable_tasks_multiple_user_same_name(client): """If two user tasks exist with the same name as a system task, both user tasks are shown, not the system one.""" child_db.truncate() task_db.truncate() # System task (user_id=None) task_db.insert({'id': 'sys1', 'name': 'Duplicate', 'points': 1, 'is_good': True, 'user_id': None}) # User tasks (same name, different user_ids) task_db.insert({'id': 'user1', 'name': 'Duplicate', 'points': 2, 'is_good': True, 'user_id': 'testuserid'}) task_db.insert({'id': 'user2', 'name': 'Duplicate', 'points': 3, 'is_good': True, 'user_id': 'otheruserid'}) client.put('/child/add', json={'name': 'Sam', 'age': 8}) child_id = client.get('/child/list').get_json()['children'][0]['id'] resp = client.get(f'/child/{child_id}/list-assignable-tasks') assert resp.status_code == 200 data = resp.get_json() names = [t['name'] for t in data['tasks']] ids = [t['id'] for t in data['tasks']] # Both user tasks should be present, not the system one assert set(names) == {'Duplicate'} assert set(ids) == {'user1', 'user2'} def test_assignable_rewards_user_overrides_system(client): """If a user reward exists with the same name as a system reward, only the user reward should be shown in assignable list.""" child_db.truncate() reward_db.truncate() # System reward (user_id=None) reward_db.insert({'id': 'sysr1', 'name': 'Prize', 'cost': 5, 'user_id': None}) # User reward (same name) reward_db.insert({'id': 'userr1', 'name': 'Prize', 'cost': 10, 'user_id': 'testuserid'}) client.put('/child/add', json={'name': 'Sam', 'age': 8}) child_id = client.get('/child/list').get_json()['children'][0]['id'] resp = client.get(f'/child/{child_id}/list-assignable-rewards') assert resp.status_code == 200 data = resp.get_json() names = [r['name'] for r in data['rewards']] ids = [r['id'] for r in data['rewards']] # Only the user reward should be present assert names == ['Prize'] assert ids == ['userr1'] def test_assignable_rewards_multiple_user_same_name(client): """If two user rewards exist with the same name as a system reward, both user rewards are shown, not the system one.""" child_db.truncate() reward_db.truncate() # System reward (user_id=None) reward_db.insert({'id': 'sysr1', 'name': 'Prize', 'cost': 5, 'user_id': None}) # User rewards (same name, different user_ids) reward_db.insert({'id': 'userr1', 'name': 'Prize', 'cost': 10, 'user_id': 'testuserid'}) reward_db.insert({'id': 'userr2', 'name': 'Prize', 'cost': 15, 'user_id': 'otheruserid'}) client.put('/child/add', json={'name': 'Sam', 'age': 8}) child_id = client.get('/child/list').get_json()['children'][0]['id'] resp = client.get(f'/child/{child_id}/list-assignable-rewards') assert resp.status_code == 200 data = resp.get_json() names = [r['name'] for r in data['rewards']] ids = [r['id'] for r in data['rewards']] # Both user rewards should be present, not the system one assert set(names) == {'Prize'} assert set(ids) == {'userr1', 'userr2'}