-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added support for message provider (#251)
* feat: message provider implementation * feat: add basic verify flow * feat: add missing flask config * feat: initial flask start setup * feat: and sample provider test, rename MessageProvider constructor * feat: move handler to provider * feat: pass handler as python argument * feat: pass handler as python argument * feat: create setup endpoint for message handlers, add setup_state fn, refactor test * feat: enable context manager in message provider, allow provider to pass Proxy port param, new endpoints to stop/check proxy * fix: flake8 * fix: revert bad merge to http_proxy; add pydocstyle * feat: parse content, update readme and test * test: add missing tests for message provider * fix: check the pact files exists before running the vefivication * fix: flake8 * feat: rebase feature from master branch * fix: remove dead code * feat: add http_proxy test, replace print with log, use flask localstack to store states * fix: change PROXY_PORT to 1234 to fix broken build * fix: flake8 * chore: skip provider test to make the build pass (troubleshooting) * chore: skip 2 tests that causes BrokenPipeError for investigation * chore: comment out the broken tests * fix: change default proxy port to 1234 * feat: updated message consumer test to be compatible with message provider test * fix: Updated tests that where failing on master branch as well * feat: ported message_provider to fastapi * fix: fixed comment * fix: removed unnecessary pact file updated flask * fix: added detectcontentlambda-contentprovider.json to .gitignore Co-authored-by: Tuan Pham <q.tuan.p@gmail.com> Co-authored-by: William Infante <williaminfante@gmail.com>
- Loading branch information
1 parent
2c81029
commit 903371b
Showing
16 changed files
with
535 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import pytest | ||
from pact import MessageProvider | ||
|
||
|
||
def document_created_handler(): | ||
return { | ||
"event": "ObjectCreated:Put", | ||
"documentName": "document.doc", | ||
"creator": "TP", | ||
"documentType": "microsoft-word" | ||
} | ||
|
||
|
||
def document_deleted_handler(): | ||
return { | ||
"event": "ObjectCreated:Delete", | ||
"documentName": "document.doc", | ||
"creator": "TP", | ||
"documentType": "microsoft-word" | ||
} | ||
|
||
|
||
def test_verify_success(): | ||
provider = MessageProvider( | ||
message_providers={ | ||
'A document created successfully': document_created_handler, | ||
'A document deleted successfully': document_deleted_handler | ||
}, | ||
provider='ContentProvider', | ||
consumer='DetectContentLambda', | ||
pact_dir='pacts' | ||
|
||
) | ||
with provider: | ||
provider.verify() | ||
|
||
|
||
def test_verify_failure_when_a_provider_missing(): | ||
provider = MessageProvider( | ||
message_providers={ | ||
'A document created successfully': document_created_handler, | ||
}, | ||
provider='ContentProvider', | ||
consumer='DetectContentLambda', | ||
pact_dir='pacts' | ||
|
||
) | ||
|
||
with pytest.raises(AssertionError): | ||
with provider: | ||
provider.verify() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
"""Http Proxy to be used as provider url in verifier.""" | ||
from fastapi import FastAPI, status, Request, HTTPException | ||
import uvicorn as uvicorn | ||
import logging | ||
log = logging.getLogger(__name__) | ||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
app = FastAPI() | ||
PROXY_PORT = 1234 | ||
UVICORN_LOGGING_LEVEL = "error" | ||
items = { | ||
"states": None | ||
} | ||
|
||
|
||
def _match_states(payload): | ||
"""Match states in payload against stored message handlers.""" | ||
log.debug(f'Find handler from payload: {payload}') | ||
handlers = items["states"] | ||
states = handlers['messageHandlers'] | ||
log.debug(f'Setup states: {handlers}') | ||
provider_states = payload['providerStates'] | ||
|
||
for state in provider_states: | ||
matching_state = state['name'] | ||
if matching_state in states: | ||
return states[matching_state] | ||
raise HTTPException(status_code=500, detail='No matched handler.') | ||
|
||
|
||
@app.post("/") | ||
async def root(request: Request): | ||
"""Match states with provided message handlers.""" | ||
payload = await request.json() | ||
message = _match_states(payload) | ||
return {'contents': message} | ||
|
||
|
||
@app.get('/ping', status_code=status.HTTP_200_OK) | ||
def ping(): | ||
"""Check whether the server is available before setting up states.""" | ||
return {"ping": "pong"} | ||
|
||
|
||
@app.post("/setup", status_code=status.HTTP_201_CREATED) | ||
async def setup(request: Request): | ||
"""Endpoint to setup states. | ||
Use localstack to store payload. | ||
""" | ||
payload = await request.json() | ||
items["states"] = payload | ||
return items["states"] | ||
|
||
|
||
def run_proxy(): | ||
"""Rub HTTP Proxy.""" | ||
uvicorn.run("pact.http_proxy:app", port=PROXY_PORT, log_level=UVICORN_LOGGING_LEVEL) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.