Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion strr-api/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "strr-api"
version = "0.0.61"
version = "0.0.62"
description = ""
authors = ["thorwolpert <thor@wolpert.ca>"]
license = "BSD 3-Clause"
Expand Down
55 changes: 55 additions & 0 deletions strr-api/src/strr_api/resources/registrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@

import logging
import traceback
from datetime import datetime, timedelta
from http import HTTPStatus
from io import BytesIO

from dateutil.relativedelta import relativedelta
from flasgger import swag_from
from flask import Blueprint, g, jsonify, request, send_file
from flask_cors import cross_origin
Expand Down Expand Up @@ -312,6 +314,58 @@ def update_registration_status(registration_id):
return error_response(message=ErrorMessage.PROCESSING_ERROR.value, http_status=HTTPStatus.INTERNAL_SERVER_ERROR)


@bp.route("/<registration_id>/todos", methods=("GET",))
Comment thread
dimak1 marked this conversation as resolved.
@swag_from({"security": [{"Bearer": []}]})
@cross_origin(origin="*")
@jwt.requires_auth
def get_todos(registration_id):
"""
Get todos for a registration.
---
parameters:
- in: path
name: registration_id
type: integer
required: true
description: Registration Id
responses:
200:
description: Success
401:
description: Unauthorized to retrieve the todos for the registration.
500:
description: Unexpected error during processing.
"""
try:
account_id = request.headers.get("Account-Id")
user = User.get_or_create_user_by_jwt(g.jwt_oidc_token_info)
if not user:
raise AuthException()
registration = RegistrationService.get_registration(account_id, registration_id)
if not registration:
raise AuthException()

todos = []

if registration.status in [RegistrationStatus.ACTIVE, RegistrationStatus.EXPIRED]:
# Get the current time in UTC
current_time_utc = datetime.utcnow()
registration_expiry_datetime = registration.expiry_date

# Window in which todos should appear
threshold_datetime_start = registration_expiry_datetime - timedelta(days=30)
threshold_datetime_end = registration_expiry_datetime + relativedelta(years=3)

if threshold_datetime_start <= current_time_utc <= threshold_datetime_end:
todos.append({"task": {"type": "REGISTRATION_RENEWAL"}})

return {"todos": todos}, HTTPStatus.OK
except Exception as exception:
logger.error(exception)
logging.error("Traceback: %s", traceback.format_exc())
return error_response(message=ErrorMessage.PROCESSING_ERROR.value, http_status=HTTPStatus.INTERNAL_SERVER_ERROR)


# TODO: Certificates are not supported for the MVP release. This functionality will be supported in a future release.
# @bp.route("/<registration_id>/certificate", methods=("POST",))
# @swag_from({"security": [{"Bearer": []}]})
Expand Down Expand Up @@ -364,6 +418,7 @@ def update_registration_status(registration_id):
@swag_from({"security": [{"Bearer": []}]})
@cross_origin(origin="*")
@jwt.requires_auth
@jwt.has_one_of_roles([Role.STRR_EXAMINER.value, Role.SYSTEM.value])
def create_registration_for_permit_validation():
"""
Create minimum registration for permit validation.
Expand Down
41 changes: 38 additions & 3 deletions strr-api/tests/postman/strr-api.postman_collection.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info": {
"_postman_id": "16206b52-8feb-4cbd-a845-6c17fa6a3e90",
"_postman_id": "f3154729-f428-4ff9-861f-39292b2d04fb",
"name": "strr-api",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "31792407"
Expand Down Expand Up @@ -345,7 +345,7 @@
}
],
"url": {
"raw": "{{api_url}}/applications?sortBy=application_date&sortOrder=asc&recordNumber=123&registrationType=HOST&registrationType=PLATFORM&registrationStatus=ACTIVE&registrationStatus=SUSPENDED&status=PAID&status=AUTO_APPROVED&assignee={{username}}",
"raw": "{{api_url}}/applications?sortBy=application_date&sortOrder=asc&recordNumber=123&registrationType=HOST&registrationType=PLATFORM&registrationStatus=ACTIVE&registrationStatus=SUSPENDED&status=PAID&assignee={{username}}",
"host": [
"{{api_url}}"
],
Expand Down Expand Up @@ -756,7 +756,7 @@
}
],
"url": {
"raw": "{{api_url}}/applications/search?text=018-851-570&limit=10&page=1&sortBy=application_date&sortOrder=asc&recordNumber=123&registrationType=HOST&registrationType=PLATFORM&registrationStatus=ACTIVE&registrationStatus=SUSPENDED&status=PAID&status=AUTO_APPROVED&assignee={{username}}",
"raw": "{{api_url}}/applications/search?text=018-851-570&limit=10&page=1&sortBy=application_date&sortOrder=asc&recordNumber=123&registrationType=HOST&registrationType=PLATFORM&registrationStatus=ACTIVE&registrationStatus=SUSPENDED&status=PAID&assignee={{username}}",
"host": [
"{{api_url}}"
],
Expand Down Expand Up @@ -1659,6 +1659,41 @@
}
},
"response": []
},
{
"name": "Get ToDos",
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{token}}",
"type": "string"
}
]
},
"method": "GET",
"header": [
{
"key": "Account-Id",
"value": "{{account_id}}",
"type": "text"
}
],
"url": {
"raw": "{{api_url}}/registrations/{{registration_id}}/todos",
"host": [
"{{api_url}}"
],
"path": [
"registrations",
"{{registration_id}}",
"todos"
]
}
},
"response": []
}
]
},
Expand Down
134 changes: 132 additions & 2 deletions strr-api/tests/unit/resources/test_registrations.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import json
import os
from datetime import datetime
import random
from datetime import datetime, timedelta
from http import HTTPStatus
from unittest.mock import patch

import pytest
from dateutil.relativedelta import relativedelta

from strr_api.enums.enum import PaymentStatus, RegistrationStatus
from strr_api.exceptions import ExternalServiceException
from strr_api.models import Application, Events
from strr_api.models import Application, Events, Registration, User
from tests.unit.utils.auth_helpers import PUBLIC_USER, STRR_EXAMINER, create_header
from tests.unit.utils.mocks import (
fake_document,
Expand Down Expand Up @@ -378,3 +380,131 @@ def test_cancel_registration(session, client, jwt):
assert HTTPStatus.OK == rv.status_code
response_json = rv.json
assert response_json.get("status") == RegistrationStatus.CANCELLED.value


def test_get_expired_registration_todos_in_renewal_window(session, client, jwt):
headers = create_header(jwt, [PUBLIC_USER], "Account-Id")
headers["Account-Id"] = ACCOUNT_ID

current_utc = datetime.utcnow()
registration_end_date = current_utc - relativedelta(years=1)
user = User(
username="testUser",
firstname="Test",
lastname="User",
iss="test",
sub=f"sub{random.randint(0, 99999)}",
idp_userid="testUserID",
login_source="testLogin",
)
user.save()

registration = Registration(
start_date=registration_end_date - relativedelta(years=1),
expiry_date=registration_end_date,
status=RegistrationStatus.ACTIVE,
registration_type="HOST",
sbc_account_id=ACCOUNT_ID,
registration_number="H1234567",
user_id=user.id,
)
registration.save()
rv = client.get(f"/registrations/{registration.id}/todos", headers=headers)
response_json = rv.json
assert response_json.get("todos")[0].get("task") is not None


def test_get_expired_registration_todos_outside_renewal_window(session, client, jwt):
headers = create_header(jwt, [PUBLIC_USER], "Account-Id")
headers["Account-Id"] = ACCOUNT_ID

current_utc = datetime.utcnow()
registration_end_date = current_utc + relativedelta(years=4)
user = User(
username="testUser",
firstname="Test",
lastname="User",
iss="test",
sub=f"sub{random.randint(0, 99999)}",
idp_userid="testUserID",
login_source="testLogin",
)
user.save()

registration = Registration(
start_date=registration_end_date - relativedelta(years=5),
expiry_date=registration_end_date,
status=RegistrationStatus.ACTIVE,
registration_type="HOST",
sbc_account_id=ACCOUNT_ID,
registration_number="H1234567",
user_id=user.id,
)
registration.save()
rv = client.get(f"/registrations/{registration.id}/todos", headers=headers)
response_json = rv.json
assert response_json.get("todos") == []


def test_get_active_registration_todos_in_renewal_window(session, client, jwt):
headers = create_header(jwt, [PUBLIC_USER], "Account-Id")
headers["Account-Id"] = ACCOUNT_ID

current_utc = datetime.utcnow()
registration_end_date = current_utc + timedelta(days=24)
user = User(
username="testUser",
firstname="Test",
lastname="User",
iss="test",
sub=f"sub{random.randint(0, 99999)}",
idp_userid="testUserID",
login_source="testLogin",
)
user.save()

registration = Registration(
start_date=registration_end_date - timedelta(days=340),
expiry_date=registration_end_date,
status=RegistrationStatus.ACTIVE,
registration_type="HOST",
sbc_account_id=ACCOUNT_ID,
registration_number="H1234567",
user_id=user.id,
)
registration.save()
rv = client.get(f"/registrations/{registration.id}/todos", headers=headers)
response_json = rv.json
assert response_json.get("todos")[0].get("task") is not None


def test_get_active_registration_todos_outside_renewal_window(session, client, jwt):
headers = create_header(jwt, [PUBLIC_USER], "Account-Id")
headers["Account-Id"] = ACCOUNT_ID

current_utc = datetime.utcnow()
registration_end_date = current_utc + timedelta(days=65)
user = User(
username="testUser",
firstname="Test",
lastname="User",
iss="test",
sub=f"sub{random.randint(0, 99999)}",
idp_userid="testUserID",
login_source="testLogin",
)
user.save()

registration = Registration(
start_date=registration_end_date - timedelta(days=300),
expiry_date=registration_end_date,
status=RegistrationStatus.ACTIVE,
registration_type="HOST",
sbc_account_id=ACCOUNT_ID,
registration_number="H1234567",
user_id=user.id,
)
registration.save()
rv = client.get(f"/registrations/{registration.id}/todos", headers=headers)
response_json = rv.json
assert response_json.get("todos") == []