Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api test cases #3

Merged
merged 8 commits into from
Jun 20, 2024
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
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
from openg2p_spar_mapper_api.app import Initializer

from openg2p_fastapi_common.ping import PingInitializer
from openg2p_fastapi_common.context import app_registry


initializer = Initializer()
PingInitializer()

app = main_init.return_app()
app = initializer.return_app()

if __name__ == "__main__":
initializer.main()
2 changes: 2 additions & 0 deletions src/openg2p_spar_mapper_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
_config = Settings.get_config()

from openg2p_fastapi_common.app import Initializer as BaseInitializer
from openg2p_g2pconnect_common_lib.oauth_token import OAuthTokenService

from .controllers import (
AsyncMapperController,
Expand All @@ -28,6 +29,7 @@ class Initializer(BaseInitializer):
def initialize(self, **kwargs):
super().initialize()

OAuthTokenService()
SessionInitializer()
MapperService()
IdFaMappingValidations()
Expand Down
2 changes: 1 addition & 1 deletion src/openg2p_spar_mapper_api/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional

from openg2p_fastapi_common.config import Settings as BaseSettings
from openg2p_g2pconnect_common_lib.config import Settings as BaseSettings
from pydantic import AnyUrl
from pydantic_settings import SettingsConfigDict

Expand Down
68 changes: 63 additions & 5 deletions src/openg2p_spar_mapper_api/controllers/async_mapper_controller.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import asyncio
import logging
import uuid
from typing import Annotated

import httpx
from fastapi import Depends
from openg2p_fastapi_common.controller import BaseController
from openg2p_g2pconnect_common_lib.jwt_signature_validator import JWTSignatureValidator
from openg2p_g2pconnect_common_lib.schemas import (
AsyncCallbackRequest,
AsyncResponse,
Expand Down Expand Up @@ -89,8 +92,21 @@ def __init__(self, **kwargs):
methods=["POST"],
)

async def link_async(self, link_request: LinkRequest):
async def link_async(
self,
link_request: LinkRequest,
is_signature_valid: Annotated[bool, Depends(JWTSignatureValidator())],
):
correlation_id = str(uuid.uuid4())
try:
RequestValidation.get_component().validate_signature(is_signature_valid)
except RequestValidationException as e:
error_response = (
AsyncResponseHelper.get_component().construct_error_async_response(
link_request, e
)
)
return error_response
await asyncio.create_task(
self.handle_service_and_link_callback(link_request, correlation_id, "link")
)
Expand All @@ -99,8 +115,21 @@ async def link_async(self, link_request: LinkRequest):
correlation_id,
)

async def update_async(self, update_request: UpdateRequest):
async def update_async(
self,
update_request: UpdateRequest,
is_signature_valid: Annotated[bool, Depends(JWTSignatureValidator())],
):
correlation_id = str(uuid.uuid4())
try:
RequestValidation.get_component().validate_signature(is_signature_valid)
except RequestValidationException as e:
error_response = (
AsyncResponseHelper.get_component().construct_error_async_response(
update_request, e
)
)
return error_response
await asyncio.create_task(
self.handle_service_and_update_callback(
update_request, correlation_id, "update"
Expand All @@ -111,8 +140,21 @@ async def update_async(self, update_request: UpdateRequest):
correlation_id,
)

async def resolve_async(self, resolve_request: ResolveRequest):
async def resolve_async(
self,
resolve_request: ResolveRequest,
is_signature_valid: Annotated[bool, Depends(JWTSignatureValidator())],
):
correlation_id = str(uuid.uuid4())
try:
RequestValidation.get_component().validate_signature(is_signature_valid)
except RequestValidationException as e:
error_response = (
AsyncResponseHelper.get_component().construct_error_async_response(
resolve_request, e
)
)
return error_response
await asyncio.create_task(
self.handle_service_and_resolve_callback(
resolve_request, correlation_id, "resolve"
Expand All @@ -123,8 +165,21 @@ async def resolve_async(self, resolve_request: ResolveRequest):
correlation_id,
)

async def unlink_async(self, unlink_request: UnlinkRequest):
async def unlink_async(
self,
unlink_request: UnlinkRequest,
is_signature_valid: Annotated[bool, Depends(JWTSignatureValidator())],
):
correlation_id = str(uuid.uuid4())
try:
RequestValidation.get_component().validate_signature(is_signature_valid)
except RequestValidationException as e:
error_response = (
AsyncResponseHelper.get_component().construct_error_async_response(
unlink_request, e
)
)
return error_response
try:
RequestValidation.get_component().validate_request(unlink_request)
RequestValidation.get_component().validate_unlink_async_request_header(
Expand All @@ -148,7 +203,10 @@ async def unlink_async(self, unlink_request: UnlinkRequest):
)

async def handle_service_and_link_callback(
self, link_request: LinkRequest, correlation_id: str, action: str
self,
link_request: LinkRequest,
correlation_id: str,
action: str,
):
try:
RequestValidation.get_component().validate_async_request(link_request)
Expand Down
32 changes: 28 additions & 4 deletions src/openg2p_spar_mapper_api/controllers/sync_mapper_controller.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from typing import Annotated

from fastapi import Depends
from openg2p_fastapi_common.controller import BaseController
from openg2p_g2pconnect_common_lib.jwt_signature_validator import JWTSignatureValidator
from openg2p_g2pconnect_mapper_lib.schemas import (
LinkRequest,
LinkResponse,
Expand Down Expand Up @@ -55,8 +59,13 @@ def __init__(self, **kwargs):
methods=["POST"],
)

async def link_sync(self, link_request: LinkRequest):
async def link_sync(
self,
link_request: LinkRequest,
is_signature_valid: Annotated[bool, Depends(JWTSignatureValidator())],
):
try:
RequestValidation.get_component().validate_signature(is_signature_valid)
RequestValidation.get_component().validate_request(link_request)
RequestValidation.get_component().validate_link_request_header(link_request)
except RequestValidationException as e:
Expand All @@ -75,8 +84,13 @@ async def link_sync(self, link_request: LinkRequest):
single_link_responses,
)

async def update_sync(self, update_request: UpdateRequest):
async def update_sync(
self,
update_request: UpdateRequest,
is_signature_valid: Annotated[bool, Depends(JWTSignatureValidator())],
):
try:
RequestValidation.get_component().validate_signature(is_signature_valid)
RequestValidation.get_component().validate_request(update_request)
RequestValidation.get_component().validate_update_request_header(
update_request
Expand All @@ -99,8 +113,13 @@ async def update_sync(self, update_request: UpdateRequest):
)
)

async def resolve_sync(self, resolve_request: ResolveRequest):
async def resolve_sync(
self,
resolve_request: ResolveRequest,
is_signature_valid: Annotated[bool, Depends(JWTSignatureValidator())],
):
try:
RequestValidation.get_component().validate_signature(is_signature_valid)
RequestValidation.get_component().validate_request(resolve_request)
RequestValidation.get_component().validate_resolve_request_header(
resolve_request
Expand All @@ -123,8 +142,13 @@ async def resolve_sync(self, resolve_request: ResolveRequest):
)
)

async def unlink_sync(self, unlink_request: UnlinkRequest):
async def unlink_sync(
self,
unlink_request: UnlinkRequest,
is_signature_valid: Annotated[bool, Depends(JWTSignatureValidator())],
):
try:
RequestValidation.get_component().validate_signature(is_signature_valid)
RequestValidation.get_component().validate_request(unlink_request)
RequestValidation.get_component().validate_unlink_request_header(
unlink_request
Expand Down
14 changes: 11 additions & 3 deletions src/openg2p_spar_mapper_api/services/request_validations.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
from openg2p_fastapi_common.service import BaseService
from openg2p_g2pconnect_common_lib.schemas import (
SyncResponseStatusReasonCodeEnum,
)
from openg2p_g2pconnect_common_lib.schemas.async_schemas import (
AsyncResponseStatusReasonCodeEnum,
SecurityErrorCodes,
SyncResponseStatusReasonCodeEnum,
)

from .exceptions import RequestValidationException


class RequestValidation(BaseService):
def validate_signature(self, is_signature_valid) -> None:
if not is_signature_valid:
raise RequestValidationException(
code=SecurityErrorCodes.INVALID_JWT_SIGNATURE,
message=SecurityErrorCodes.INVALID_JWT_SIGNATURE,
)

return None

def validate_link_request_header(self, request) -> None:
if request.header.action != "link":
raise RequestValidationException(
Expand Down
Loading
Loading