class IssueView(BaseView): """ Gets an issue by its id """ url = "/issues/{id}" with_issue = match_getter(get_issue, "issue", iid="id") @with_issue @docs(summary="Get An Issue") @returns(JSendSchema.of(issue=IssueSchema())) async def get(self, issue): return { "status": JSendStatus.SUCCESS, "data": {"issue": issue.serialize(self.request.app.router)} } @with_issue @docs(summary="Modify An Issue") @expects(IssueUpdateSchema()) @returns(JSendSchema.of(issue=IssueSchema())) async def patch(self, issue): issue = await update_issue( issue, self.request["data"].get("status"), self.request["data"].get("resolution", None) ) return { "status": JSendStatus.SUCCESS, "data": {"issue": issue.serialize(self.request.app.router)} }
class UserIssuesView(BaseView): """ Gets or adds to the users' list of issues. """ url = f"/users/{{id:{USER_IDENTIFIER_REGEX}}}/issues" name = "user_issues" with_issues = match_getter(get_issues, "issues", user='******') with_user = match_getter(get_user, 'user', user_id='id') @with_user @with_issues @docs(summary="Get All Issues For User") @requires(UserMatchesToken() | UserIsAdmin()) @returns(JSendSchema.of(issues=Many(IssueSchema()))) async def get(self, user, issues): return { "status": JSendStatus.SUCCESS, "data": { "issues": [issue.serialize(self.request.app.router) for issue in issues] } } @with_user @docs(summary="Open Issue For User") @requires(UserMatchesToken() | UserIsAdmin()) @expects(IssueSchema(only=('description', 'bike_identifier'))) @returns( JSendSchema.of(issue=IssueSchema(only=('id', 'user_id', 'user_url', 'bike_identifier', 'description', 'opened_at')))) async def post(self, user): issue_data = { "description": self.request["data"]["description"], "user": user } if "bike_identifier" in self.request["data"]: issue_data["bike"] = await get_bike( identifier=self.request["data"]["bike_identifier"]) issue = await open_issue(**issue_data) return { "status": JSendStatus.SUCCESS, "data": { "issue": issue.serialize(self.request.app.router) } }
async def test_get_issues(self, client, random_admin): schema = JSendSchema.of(issues=Many(IssueSchema())) await open_issue(random_admin, "test issue!") resp = await client.get( '/api/v1/issues', headers={"Authorization": f"Bearer {random_admin.firebase_id}"}) data = schema.load(await resp.json()) assert len(data["data"]["issues"]) == 1
async def test_get_issue(self, client, random_admin): issue = await open_issue(random_admin, "An issue!") response = await client.get( f"/api/v1/issues/{issue.id}", headers={"Authorization": f"Bearer {random_admin.firebase_id}"}) response_data = JSendSchema.of(issue=IssueSchema()).load( await response.json()) assert response_data["status"] == JSendStatus.SUCCESS assert response_data["data"]["issue"]["id"] == issue.id
class BikeIssuesView(BaseView): url = f"/bikes/{{identifier:{BIKE_IDENTIFIER_REGEX}}}/issues" with_issues = match_getter(partial(get_issues, is_active=True), 'issues', bike=('identifier', str)) with_bike = match_getter(get_bike, 'bike', identifier=('identifier', str)) with_user = match_getter(get_user, "user", firebase_id=GetFrom.AUTH_HEADER) @with_issues @with_user @docs(summary="Get All Open Issues On Bike") @requires(UserIsAdmin()) @returns(JSendSchema.of(issues=Many(IssueSchema()))) async def get(self, issues: List[Issue], user): return { "status": JSendStatus.SUCCESS, "data": { "issues": [issue.serialize(self.request.app.router) for issue in issues] } } @with_user @with_bike @docs(summary="Open A New Issue About Bike") @expects(IssueSchema(only=('description', ))) @returns( JSendSchema.of(issue=IssueSchema(only=('id', 'user_id', 'user_url', 'bike_identifier', 'description', 'opened_at')))) async def post(self, user, bike): issue = await open_issue( description=self.request["data"]["description"], user=user, bike=bike) return { "status": JSendStatus.SUCCESS, "data": { "issue": issue.serialize(self.request.app.router) } }
async def test_create_issue_for_bike(self, client, random_user, random_bike): response = await client.post( f"/api/v1/bikes/{random_bike.identifier}/issues", headers={"Authorization": f"Bearer {random_user.firebase_id}"}, json={"description": "I HATE IT"}) response_data = JSendSchema.of(issue=IssueSchema()).load( await response.json()) assert response_data["data"]["issue"][ "bike_identifier"] == random_bike.identifier
async def test_get_issues_for_bike(self, client, random_admin, random_bike): await Issue.create(user=random_admin, bike=random_bike, description="OMG AWFUL") response = await client.get( f"/api/v1/bikes/{random_bike.identifier}/issues", headers={"Authorization": f"Bearer {random_admin.firebase_id}"}) response_data = JSendSchema.of(issues=Many(IssueSchema())).load( await response.json()) assert response_data["status"] == JSendStatus.SUCCESS assert "issues" in response_data["data"] assert all(issue["bike_identifier"] == random_bike.identifier for issue in response_data["data"]["issues"])
class IssuesView(BaseView): """ Gets the list of issues or adds a new issue. """ url = "/issues" with_issues = match_getter(partial(get_issues, is_active=True), "issues") with_admin = match_getter(get_user, "user", firebase_id=GetFrom.AUTH_HEADER) @with_admin @with_issues @docs(summary="Get All Issues") @requires(UserIsAdmin()) @returns(JSendSchema.of(issues=Many(IssueSchema(exclude=('user', 'bike'))))) async def get(self, user, issues: List[Issue]): return { "status": JSendStatus.SUCCESS, "data": {"issues": [issue.serialize(self.request.app.router) for issue in issues]} }