Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Adler committed Aug 19, 2024
1 parent ba9dae4 commit f9cf396
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 26 deletions.
11 changes: 11 additions & 0 deletions app/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,14 @@ def set_password(self, password):

def check_password(self, password):
return check_password_hash(self.password_hash, password)

def get_item_names(self):
[item.to_mongo().to_dict()['name'] for item in self.items]

def to_dict(self):
user_dict = self.to_mongo().to_dict()

# Convert the 'items' field to a list of serialized Item documents
user_dict['items'] = [item.to_mongo().to_dict() for item in self.items]

return user_dict
4 changes: 2 additions & 2 deletions app/views/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def register():
def login():
auth_header = request.headers.get('Authorization')

if not auth_header or not auth_header.startswith('Basic '):
return jsonify({'message': 'Authorization header is missing or invalid'}), 401
# if not auth_header or not auth_header.startswith('Basic '):
# return jsonify({'message': 'Authorization header is missing or invalid'}), 401

# Decode the Basic Auth header
try:
Expand Down
4 changes: 2 additions & 2 deletions app/views/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ def clear_session(**kwargs):
@token_required(dm_required=True)
def export_session(**kwargs):
session = {
"user": [user.to_mongo().to_dict() for user in User.objects()],
"user": [user.to_dict() for user in User.objects()],
"item": [item.to_mongo().to_dict() for item in Item.objects()]
}
return json.dumps(session), 200
return jsonify(session), 200


@session_bp.route('/import', methods=['POST'])
Expand Down
71 changes: 49 additions & 22 deletions tests/views/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,53 +95,80 @@ def test_clear_session_without_token(self):
self.assertEqual(response.status_code, 403) # Expecting Forbidden

def test_export_session(self):
# Add mock data
user = User(username="john_doe", items=["item1", "item2"]).save()
item = Item(name="Laptop", available=False, claimed=True).save()

# Call the export_session route
response = self.client.get('/export')

response = self.client.get('/session/export', headers={
'Authorization': f'Bearer {self.token}'
})
assert response.status_code == 200

session_data = json.loads(response.data)

assert len(session_data['user']) == 1
assert len(session_data['item']) == 1
assert session_data['user'][0]['username'] == "john_doe"
assert session_data['item'][0]['name'] == "Laptop"
assert len(session_data['user']) == 2
assert len(session_data['item']) == 2
assert session_data['user'][0]['username'] == 'dmuser'
assert session_data['item'][0]['name'] == 'Sword of Testing'
assert session_data['user'][1]['username'] == 'regularuser'
assert session_data['item'][1]['name'] == 'Shield of Testing'

def test_import_session(self):
# Add initial mock data to be imported
import_data = {
"user": [
'item': [
{
"username": "john_doe",
"items": ["item1", "item2"]
'_id': '66c2d78b33a0a189170c43e0',
'available': False,
'claimed': True
},
{
'_id': '66c2d78b33a0a189170c43e1',
'available': False,
'claimed': True
}
],
"item": [
'user': [
{
"_id": "1",
"name": "Laptop",
"available": False,
"claimed": True
'username': 'dmuser',
'items': []
},
{
'username': 'regularuser',
'items': [
{
'_id': '66c2d78b33a0a189170c43e0',
'available': False,
'claimed': True
},
{
'_id': '66c2d78b33a0a189170c43e1',
'available': False,
'claimed': True
}
]
}
]
}

# Call the import_session route
response = self.client.post('/import', data=json.dumps(import_data), content_type='application/json')

response = self.client.post('/session/import',
data=json.dumps(import_data),
content_type='application/json',
headers={
'Authorization': f'Bearer {self.token}'
}
)
assert response.status_code == 200

# Check the response message
response_data = json.loads(response.data)
assert response_data['message'] == "1 users and 1 items imported"
assert response_data['message'] == "2 users and 2 items imported"

# Verify the imported data
user = User.objects.get(username="john_doe")
assert user.items == ["item1", "item2"]
dmuser = User.objects.get(username="dmuser")
assert dmuser.get_item_names() == []

regularuser = User.objects.get(username="regularuser")
assert regularuser.get_item_names() == ['Sword of Testing', 'Shield of Testing']

item = Item.objects.get(name="Laptop")
assert item.available is False
Expand Down

0 comments on commit f9cf396

Please sign in to comment.