def test_hardware_checkin_proj_not_found(client, auth_token): project_id, hardware_id = db_helper() checkout_data = { "project_id": str(project_id), "amount": 50 } res = client.post(f'/hardware/check-out/{hardware_id}', json=checkout_data, headers={ "Authorization": auth_token }) Projects.drop_collection() checkin_data = { "project_id": str(project_id), "amount": 10 } res = client.post(f'/hardware/check-in/{hardware_id}', json=checkin_data, headers={ "Authorization": auth_token }) assert res.status_code == 404 assert b"Project not found" in res.data
def test_project_delete_not_found(client, auth_token): user = User.objects.first() project_data = { "name": "New Project", "description": "Project description here", "creator_id": user.pk, "project_id": "unique-id" } Projects(**project_data).save() project_id = Projects.objects.first().pk Projects.drop_collection() res = client.delete(f'/projects/{project_id}', headers={"Authorization": auth_token}) assert res.status_code == 404 assert b"not found" in res.data
def projects_create(): """POST projects/ Desc: Creates a new project Returns: 201: newly created project 422: validation errors """ req = request.get_json() req["creator_id"] = get_jwt_identity()["_id"]["$oid"] try: new_project = Projects(**req).save() return new_project.to_json(), 201 except ValidationError as e: return parse_error(e), 422 except NotUniqueError as e: return {'msg': str(e)}, 422
def projects_read(): """GET projects/ Desc: Gets all available projects associated with the current user Returns: 200: all projects in the database """ for project in Projects.objects(creator_id=get_jwt_identity()["_id"]["$oid"]): project_hardware = list() for hardware in project.hardware: price = HardwareSet.objects(id=hardware["_id"]).first()["price"] old_datetime = hardware["checkout_time"] # price * time diff in hours hardware["cost"] = price * \ ((datetime.now() - old_datetime).seconds / 3600) project_hardware.append(hardware) project.update(hardware=project_hardware) project.reload() return Projects.objects(creator_id=get_jwt_identity()["_id"]["$oid"]).to_json(), 200
def test_project_read_id_not_found(client, auth_token): user = User.objects.first() project_data = [{ "name": "New Project 1", "description": "Project description here", "creator_id": user.pk, "project_id": "unique-id" }, { "name": "New Project 2", "description": "Project description here", "creator_id": user.pk, "project_id": "unique-id-2" }] for project in project_data: Projects(**project).save() project = Projects.objects.first() Projects.objects(id=project.pk).delete() res = client.get(f'/projects/{project.project_id}', headers={"Authorization": auth_token}) assert res.status_code == 404 assert b"not found" in res.data
def test_hardware_read_id_success(client, auth_token): user = User.objects.first() project_data = { "name": "New Project 1", "description": "Project description here", "creator_id": user.pk, "project_id": "unique-id-3" } hardware_data = [ { "name": "Read Hardware ID 1", "capacity": 2000, "available": 200, "price": 20 }, { "name": "Read Hardware ID 2", "capacity": 2000, "available": 200, "price": 20 } ] Projects(**project_data).save() HardwareSet(**hardware_data[0]).save() HardwareSet(**hardware_data[1]).save() project = Projects.objects.first() hardware1 = HardwareSet.objects(name="Read Hardware ID 1").first() hardware2 = HardwareSet.objects(name="Read Hardware ID 2").first() checkout_data = { "project_id": str(project.pk), "amount": 50 } res = client.post(f'/hardware/check-out/{hardware1.pk}', json=checkout_data, headers={ "Authorization": auth_token }) res = client.get(f'/hardware/{project.pk}', headers={ "Authorization": auth_token }) hardware1_id = str(hardware1.pk) hardware2_id = str(hardware2.pk) assert res.status_code == 200 assert hardware1_id in str(res.data) assert hardware2_id not in str(res.data)
def test_project_delete_invalid_input(client, auth_token): user = User.objects.first() project_data = { "name": "New Project", "description": "Project description here", "creator_id": user.pk, "project_id": "unique-id" } Projects(**project_data).save() project_id = str(Projects.objects.first().pk)[:-1] res = client.delete(f'/projects/{project_id}', headers={"Authorization": auth_token}) assert res.status_code == 422 assert b"not a valid ObjectId" in res.data
def projects_delete(id): """DELETE projects/<id> Desc: Deletes a specific project from the database Returns: 200: success message 404: project not found 422: validation errors """ try: if Projects.objects(id=id).delete() > 0: return {'msg': 'Projects set successfully deleted'}, 200 else: return {'msg': 'Project not found'}, 404 except ValidationError as e: return parse_error(e), 422
def projects_read_id(id): """GET projects/<id> Desc: Gets a project by project id Returns: 200: project found in the database and returned 404: project not found 422: validation errors """ try: curr_project = Projects.objects(project_id=id).first() if curr_project: return curr_project.to_json(), 200 else: return {'msg': 'Project not found'}, 404 except ValidationError as e: return parse_error(e), 422
def test_project_update_empty(client, auth_token): user = User.objects.first() project_data = { "name": "New Project", "description": "Project description here", "creator_id": user.pk, "project_id": "unique-id" } Projects(**project_data).save() project_id = Projects.objects.first().pk del project_data["name"] del project_data["creator_id"] res = client.put(f'/projects/{project_id}', json=project_data, headers={"Authorization": auth_token}) assert res.status_code == 200
def projects_update(id): """PUT projects/<id> Desc: Updates a specific projects Returns: 200: updated projects object 404: project not found 422: validation errors """ req = request.get_json() try: curr_project = Projects.objects(id=id).first() if curr_project: curr_project.update(**req) curr_project.reload() return curr_project.to_json(), 200 else: return {'msg': 'Project not found'}, 404 except ValidationError as e: return parse_error(e), 422
def test_project_read(client, auth_token): user = User.objects.first() project_data = [{ "name": "New Project 1", "description": "Project description here", "creator_id": user.pk, "project_id": "unique-id" }, { "name": "New Project 2", "description": "Project description here", "creator_id": user.pk, "project_id": "unique-id-2" }] for project in project_data: Projects(**project).save() res = client.get('/projects/', headers={"Authorization": auth_token}) assert res.status_code == 200 assert b"New Project 1" in res.data assert b"New Project 2" in res.data
def test_hardware_read_id_not_found(client, auth_token): user = User.objects.first() project_data = { "name": "New Project 1", "description": "Project description here", "creator_id": user.pk, "project_id": "unique-id-3" } hardware_data = { "name": "Read Hardware ID 1", "capacity": 2000, "available": 200, "price": 20 } Projects(**project_data).save() HardwareSet(**hardware_data).save() project = Projects.objects.first() hardware = HardwareSet.objects.first() checkout_data = { "project_id": str(project.pk), "amount": 50 } invalid_proj_id = str(project.pk)[1:] + str(project.pk)[0] res = client.post(f'/hardware/check-out/{hardware.pk}', json=checkout_data, headers={ "Authorization": auth_token }) res = client.get(f'/hardware/{invalid_proj_id}', headers={ "Authorization": auth_token }) assert res.status_code == 404 assert b'Project not found' in res.data
def db_helper(): user = User.objects.first() project_data = { "name": "Project 1", "description": "Project description here", "creator_id": user.pk, "project_id": "unique-id" } hardware_data = { "name": "Hardware Set 1", "capacity": 2000, "available": 200, "price": 20 } Projects(**project_data).save() HardwareSet(**hardware_data).save() project_id = Projects.objects.first().pk hardware_id = HardwareSet.objects.first().pk return project_id, hardware_id
def clean_db(): User.drop_collection() Projects.drop_collection() HardwareSet.drop_collection()