Skip to content

Commit 661a6a3

Browse files
committed
all tests for both feats are passing
1 parent b12d9f5 commit 661a6a3

File tree

12 files changed

+559
-118
lines changed

12 files changed

+559
-118
lines changed
Binary file not shown.

backend/app/api/routes/universe.py

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def get_universe(universe_id):
8282
universe = Universe.get_by_id(session, universe_id)
8383

8484
if not universe:
85-
raise ValidationError('Universe not found')
85+
raise NotFoundError('Universe not found')
8686

8787
# Check if user has access (owner or public universe)
8888
if not (universe.is_public or str(universe.user_id) == str(current_user_id)):
@@ -94,7 +94,7 @@ def get_universe(universe_id):
9494

9595
return jsonify(response_data)
9696

97-
except (ValidationError, AuthorizationError) as e:
97+
except (ValidationError, AuthorizationError, NotFoundError) as e:
9898
session.rollback()
9999
raise
100100
except Exception as e:
@@ -110,6 +110,15 @@ def update_universe(universe_id):
110110
if not data:
111111
raise ValidationError('No input data provided')
112112

113+
# Check for invalid fields
114+
allowed_fields = {'name', 'description', 'is_public'}
115+
invalid_fields = set(data.keys()) - allowed_fields
116+
if invalid_fields:
117+
return jsonify({
118+
'error_code': 'ValidationError',
119+
'message': f'Invalid fields: {", ".join(invalid_fields)}'
120+
}), 400
121+
113122
with get_db() as db:
114123
universe = Universe.get_by_id(db, universe_id)
115124
if not universe:
@@ -121,7 +130,6 @@ def update_universe(universe_id):
121130
raise AuthorizationError('Not authorized to modify this universe')
122131

123132
# Update allowed fields
124-
allowed_fields = {'name', 'description', 'is_public'}
125133
update_data = {k: v for k, v in data.items() if k in allowed_fields}
126134

127135
for key, value in update_data.items():
@@ -187,27 +195,50 @@ def update_physics(universe_id):
187195

188196
data = request.get_json()
189197
if not data or 'physics_params' not in data:
190-
raise ValidationError('Invalid physics parameters')
191-
192-
# Update physics parameters
193-
universe.update_physics(data['physics_params'])
198+
return jsonify({
199+
'error_code': 'ValidationError',
200+
'message': 'Invalid physics parameters'
201+
}), 400
194202

195-
# Add universe to session and commit
196-
db.add(universe)
197-
db.commit()
198-
199-
# Return complete universe data with user role
200-
response_data = universe.to_dict()
201-
response_data['user_role'] = 'owner'
202-
return jsonify(response_data)
203-
204-
except (ValidationError, AuthorizationError) as e:
203+
try:
204+
# Update physics parameters
205+
universe.update_physics(data['physics_params'])
206+
207+
# Add universe to session and commit
208+
db.add(universe)
209+
db.commit()
210+
211+
# Return complete universe data with user role
212+
response_data = universe.to_dict()
213+
response_data['user_role'] = 'owner'
214+
return jsonify(response_data)
215+
216+
except ValueError as e:
217+
db.rollback()
218+
return jsonify({
219+
'error_code': 'ValidationError',
220+
'message': str(e)
221+
}), 400
222+
223+
except ValidationError as e:
205224
db.rollback()
206-
raise
225+
return jsonify({
226+
'error_code': 'ValidationError',
227+
'message': str(e)
228+
}), 400
229+
except AuthorizationError as e:
230+
db.rollback()
231+
return jsonify({
232+
'error_code': 'AuthorizationError',
233+
'message': str(e)
234+
}), 403
207235
except Exception as e:
208236
db.rollback()
209237
logger.error(f"Error updating physics: {str(e)}", exc_info=True)
210-
raise ValidationError(f"Error updating physics: {str(e)}")
238+
return jsonify({
239+
'error_code': 'InternalError',
240+
'message': 'An internal error occurred'
241+
}), 500
211242

212243
@universe_bp.route('/<uuid:universe_id>/harmony/', methods=['PUT'])
213244
@jwt_required()
Binary file not shown.
Binary file not shown.

backend/create_demo_user.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from app.db.session import get_db
2+
from app.models.user import User
3+
4+
def create_demo_user():
5+
with get_db() as db:
6+
# Delete existing demo user if exists
7+
demo_user = db.query(User).filter_by(email='[email protected]').first()
8+
if demo_user:
9+
print("Deleting existing demo user...")
10+
db.delete(demo_user)
11+
db.commit()
12+
print("Deleted demo user")
13+
14+
# Create new demo user
15+
demo_user = User(
16+
17+
username='demo',
18+
is_active=True
19+
)
20+
demo_user.set_password('demo123')
21+
db.add(demo_user)
22+
db.commit()
23+
print("Created demo user")
24+
25+
# Verify password works
26+
if demo_user.check_password('demo123'):
27+
print("Password verification successful")
28+
else:
29+
print("Password verification failed!")
30+
return demo_user
31+
32+
if __name__ == '__main__':
33+
create_demo_user()

0 commit comments

Comments
 (0)