Commit 9701b396 authored by James Lawton's avatar James Lawton
Browse files

refactored folder structure and integrated testing

parent 7dacf811
......@@ -20,7 +20,14 @@
</component>
<component name="ChangeListManager">
<list default="true" id="b1ac61f1-92d2-42a3-9749-f0ab40feb902" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/app/cert_tools_api.py" beforeDir="false" afterPath="$PROJECT_DIR$/app/cert_tools_api.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/app/testing/generatePDF_test.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/app/testing/generate_pdf_1000.json" afterDir="false" />
<change afterPath="$PROJECT_DIR$/app/testing/valid_certificate_schema.json" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/cert_issuer_api.py" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/app/cert_issuer_test.py" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/app/cert_tools_api.py" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/certify-api.yml" beforeDir="false" afterPath="$PROJECT_DIR$/certify-api.yml" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
......@@ -47,12 +54,24 @@
<property name="ASKED_ADD_EXTERNAL_FILES" value="true" />
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/app/controller/cert_issuer" />
<property name="node.js.detected.package.eslint" value="true" />
<property name="node.js.path.for.package.eslint" value="project" />
<property name="node.js.selected.package.eslint" value="(autodetect)" />
<property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" />
</component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/app/controller/cert_issuer" />
<recent name="$PROJECT_DIR$/app" />
</key>
<key name="MoveFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/app/controller" />
<recent name="$PROJECT_DIR$/app/controller/cert_tools" />
<recent name="$PROJECT_DIR$/app/controller/cert_issuer" />
<recent name="$PROJECT_DIR$/app/testing" />
</key>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
......@@ -95,7 +114,12 @@
<workItem from="1611305635782" duration="1025000" />
<workItem from="1611675036251" duration="8405000" />
<workItem from="1611830252898" duration="4192000" />
<workItem from="1614155406218" duration="15128000" />
<workItem from="1614155406218" duration="16885000" />
<workItem from="1615202743441" duration="775000" />
<workItem from="1615204833386" duration="15297000" />
<workItem from="1615300454466" duration="81203000" />
<workItem from="1616495999312" duration="4000" />
<workItem from="1616499559069" duration="360000" />
</task>
<servers />
</component>
......@@ -115,26 +139,38 @@
<option name="oldMeFiltersMigrated" value="true" />
</component>
<component name="WindowStateProjectService">
<state x="898" y="188" width="1022" height="737" key="SettingsEditor" timestamp="1603879684033">
<screen x="72" y="27" width="1848" height="1053" />
<state x="666" y="249" key="#com.intellij.fileTypes.FileTypeChooser" timestamp="1615213496607">
<screen x="0" y="23" width="1440" height="835" />
</state>
<state x="666" y="249" key="#com.intellij.fileTypes.FileTypeChooser/0.23.1440.835@0.23.1440.835" timestamp="1615213496607" />
<state x="303" y="111" key="#com.intellij.openapi.keymap.impl.ui.EditKeymapsDialog" timestamp="1615300295546">
<screen x="0" y="23" width="1440" height="841" />
</state>
<state x="303" y="111" key="#com.intellij.openapi.keymap.impl.ui.EditKeymapsDialog/0.23.1440.841@0.23.1440.841" timestamp="1615300295546" />
<state x="483" y="167" key="RollbackChangesDialog" timestamp="1615300417210">
<screen x="0" y="23" width="1440" height="841" />
</state>
<state x="898" y="188" width="1022" height="737" key="SettingsEditor/72.27.1848.1053@72.27.1848.1053" timestamp="1603879684033" />
<state x="677" y="235" width="568" height="494" key="find.popup" timestamp="1611833482275">
<screen x="0" y="23" width="1440" height="832" />
<state x="483" y="167" key="RollbackChangesDialog/0.23.1440.841@0.23.1440.841" timestamp="1615300417210" />
<state x="518" y="361" key="com.intellij.openapi.vcs.update.UpdateOrStatusOptionsDialogupdate-v2" timestamp="1615300404728">
<screen x="0" y="23" width="1440" height="841" />
</state>
<state x="518" y="361" key="com.intellij.openapi.vcs.update.UpdateOrStatusOptionsDialogupdate-v2/0.23.1440.841@0.23.1440.841" timestamp="1615300404728" />
<state x="677" y="238" width="574" height="500" key="find.popup" timestamp="1616416083298">
<screen x="0" y="23" width="1440" height="842" />
</state>
<state x="677" y="233" width="568" height="490" key="find.popup/0.23.1440.827@0.23.1440.827" timestamp="1607609434359" />
<state x="677" y="233" width="568" height="491" key="find.popup/0.23.1440.828@0.23.1440.828" timestamp="1607596065749" />
<state x="677" y="234" width="568" height="492" key="find.popup/0.23.1440.829@0.23.1440.829" timestamp="1608120278680" />
<state x="677" y="235" width="568" height="494" key="find.popup/0.23.1440.832@0.23.1440.832" timestamp="1611833482274" />
<state x="941" y="295" width="620" height="522" key="find.popup/72.27.1848.1053@72.27.1848.1053" timestamp="1604654177267" />
<state x="677" y="238" width="574" height="500" key="find.popup/0.23.1440.841@0.23.1440.841" timestamp="1615368360254" />
<state x="677" y="238" width="574" height="500" key="find.popup/0.23.1440.842@0.23.1440.842" timestamp="1616416083298" />
<state width="600" height="428" key="javadoc.popup" timestamp="1615976487280">
<screen x="0" y="23" width="1440" height="842" />
</state>
<state width="600" height="428" key="javadoc.popup/0.23.1440.842@0.23.1440.842" timestamp="1615976487280" />
<state x="97" y="205" width="670" height="532" key="search.everywhere.popup" timestamp="1607508077041">
<screen x="0" y="23" width="1440" height="826" />
</state>
<state x="97" y="205" width="670" height="532" key="search.everywhere.popup/0.23.1440.826@0.23.1440.826" timestamp="1607508077041" />
<state x="197" y="259" width="672" height="678" key="search.everywhere.popup/72.27.1848.1053@72.27.1848.1053" timestamp="1603979580642" />
<state x="1150" y="445" width="540" height="223" key="vcs.readOnlyHandler.ReadOnlyStatusDialog" timestamp="1603879881310">
<screen x="72" y="27" width="1848" height="1053" />
</state>
<state x="1150" y="445" width="540" height="223" key="vcs.readOnlyHandler.ReadOnlyStatusDialog/72.27.1848.1053@72.27.1848.1053" timestamp="1603879881310" />
</component>
</project>
\ No newline at end of file
No preview for this file type
import pytest
from httpx import AsyncClient
from cert_tools_api import app
@pytest.mark.asyncio
async def test_root():
async with AsyncClient(app=app, base_url="http://localhost:7000") as ac:
response = await ac.post("/createBloxbergCertificate")
print(response)
assert response.status_code == 200
assert response.json() == {"message": "Tomato"}
\ No newline at end of file
def add_file_ipfs(cert_path):
ipfs_batch_file = "./data/meta_certificates/" + str(uuid.uuid1()) + '.json'
ipfs_object = {"file_certifications": []}
# Important to put name of IPFS container
client = ipfshttpclient.connect('/dns/ipfs/tcp/5001')
hash = client.add(cert_path)
return hash['Hash']
def update_ipfs_link(token_id, token_uri):
config = get_config()
print(config.unsigned_certificates_dir)
certificate_batch_handler, transaction_handler, connector = \
ethereum_sc.instantiate_blockchain_handlers(config)
# calling the smart contract to update the token uri for the token id
cert_issuer.issue_certificates.update_token_uri(config, certificate_batch_handler, transaction_handler, token_id,
token_uri)
return
##Experimental IPNS - IPNS is still in Alpha so it is relatively slow. Not recommended for production
# TODO: Implement key rotation
def add_file_ipns(ipfsHash, generateKey, newKey=None):
client = ipfshttpclient.connect('/dns/ipfs/tcp/5001')
if generateKey is True:
newKey = str(uuid.uuid1())
client.key.gen(newKey, "rsa")["Name"]
tempAddress = '/ipfs/' + ipfsHash
ipnshash = client.name.publish(tempAddress, key=newKey, timeout=300)
return ipnshash, newKey
\ No newline at end of file
from fastapi import APIRouter
from controller.cert_issuer import sign_certificate
router = APIRouter()
router.include_router(sign_certificate.router, tags=["sign"])
......@@ -12,23 +12,11 @@ import fitz
import cert_issuer.config
from cert_issuer.blockchain_handlers import ethereum_sc
import cert_issuer.issue_certificates
from fastapi import APIRouter
app = FastAPI()
router = APIRouter()
config = None
origins = [
"http://localhost",
"http://localhost:8080",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class createToken(BaseModel):
recipientPublickey: str
......@@ -53,36 +41,6 @@ def get_config():
return config
def update_ipfs_link(token_id, token_uri):
config = get_config()
print(config.unsigned_certificates_dir)
certificate_batch_handler, transaction_handler, connector = \
ethereum_sc.instantiate_blockchain_handlers(config)
# calling the smart contract to update the token uri for the token id
cert_issuer.issue_certificates.update_token_uri(config, certificate_batch_handler, transaction_handler, token_id,
token_uri)
return
def add_file_ipfs(cert_path):
# Important to put name of IPFS container
client = ipfshttpclient.connect('/dns/ipfs/tcp/5001')
hash = client.add(cert_path)
return hash['Hash']
##Experimental IPNS - IPNS is still in Alpha so it is relatively slow. Not recommended for production
# TODO: Implement key rotation
def add_file_ipns(ipfsHash, generateKey, newKey=None):
client = ipfshttpclient.connect('/dns/ipfs/tcp/5001')
if generateKey is True:
newKey = str(uuid.uuid1())
client.key.gen(newKey, "rsa")["Name"]
tempAddress = '/ipfs/' + ipfsHash
ipnshash = client.name.publish(tempAddress, key=newKey, timeout=300)
return ipnshash, newKey
async def issue_batch_to_blockchain(config, certificate_batch_handler, transaction_handler, recipientPublicKey,
tokenURI):
(tx_id, token_id) = cert_issuer.issue_certificates.issue(config, certificate_batch_handler, transaction_handler,
......@@ -91,7 +49,7 @@ async def issue_batch_to_blockchain(config, certificate_batch_handler, transacti
# Full Workflow - Called from cert_tools_api
@app.post("/issueBloxbergCertificate")
@router.post("/issueBloxbergCertificate")
async def issue(createToken: createToken, request: Request):
config = get_config()
certificate_batch_handler, transaction_handler, connector = \
......@@ -100,17 +58,13 @@ async def issue(createToken: createToken, request: Request):
# file that stores the ipfs hashes of the certificates in the batch
if createToken.enableIPFS is True:
try:
ipfs_batch_file = "./data/meta_certificates/" + str(uuid.uuid1()) + '.json'
ipfs_object = {"file_certifications": []}
ipfsHash = add_file_ipfs("./data/meta_certificates/.placeholder")
generateKey = True
ipnsHash, generatedKey = add_file_ipns(ipfsHash, generateKey)
print("Initial IPNS Commit")
tokenURI = 'http://ipfs.io/ipns/' + ipnsHash['Name']
print(tokenURI)
except Exception as e:
print(e)
return "Couldn't add file to IPFS"
raise HTTPException(status_code=400, detail=f"Couldn't add file to IPFS")
else:
tokenURI = 'https://bloxberg.org'
try:
......@@ -121,12 +75,8 @@ async def issue(createToken: createToken, request: Request):
#pr.disable()
#pr.print_stats(sort="tottime")
#pr.dump_stats('profileAPI.pstat')
# certificate_batch_handler.set_certificates_in_batch(request.json)
# delegating the issuing of the certificate to the respective transaction handler, that will call "createCertificate" on the smart contract
# (tx_id, token_id) = cert_issuer.issue_certificates.issue(config, certificate_batch_handler, transaction_handler, createToken.recipientPublickey, tokenURI)
except Exception as e:
print(e)
return status.HTTP_400_BAD_REQUEST
raise HTTPException(status_code=400, detail=f"Failed to issue certificate batch to the blockchain")
# Retrieve file path of certified transaction
blockchain_file_path = config.blockchain_certificates_dir
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment