###### tags: `EC` `cathay` # Cathay VEC Project of Recording # Configure Settings ## Start service 部署方式 參照README 大致的流程: start source -> into docker -> into virtualenv -> install submodule -> run flask 再次特別提醒: git submodule update --init --recursive 要執行此模組才會加入library ## Db configure IDE: Dbeaver Other infomation: open the ved-project -> envfile and envfile-infra you can find the below information: url, username, password & dbname call the : "vec_at_ut" ## Projects ### Routing API routing add to the decorator. Then "factory.py" is controller any api put in here if you have use the api routing in decorator. ### Middleware Decorator 的區域 有需要驗證的紀錄於此 1. request_id 2. auto verify (vec_api_auth) 3. treemall_user ### Environment 存放全域變數的地方 使用 os.environ 可抓取主機內資訊和記錄變數 ### Misc = 非主流的資訊 1. misc exception 將class 使用exception function 去撰寫錯誤資訊拋給前端 2. format.py 放入轉至的functions ### Rules ## POSTMAN ### ID & Pw Path 路徑 位置:google driver searching:資源 Filename : vec資源.xls workspace: vec ### environment Development : 開發環境 Prod : 正式環境 UAT : QA 測試環境 UT : 測試環境 ### Update GET/POST 更新POSTMAN 1. branch 要在development 2. WIP(Work In Progress) 代表還需要修改 ## request_id Description 從aws 請求時會給予request_id. # Project Structure ## Serverless Description 此 framework 是與aws之間做橋樑, 將開發的api自動連接. PR(Pull Request) 之後會更新lambda api. 更新之後,重新commit不用特別PR. API framework : flask AWS Connection framework : serverless Database : progressdb [serverless frame building](https://serverless.com/framework/docs/providers/aws/guide/quick-start/) serverless work way serverless-api : 一種開啟的呼叫 serverless-wrk : 啓動器, 入口 ## ORM 利用sqlalchemy 方式去建立模組 不具有flask model 的模組新增 使用testing 方式去測試資料連結ORM是否正確 ## Serverless Configure serverless.yml introcdution wsgi : 外部API為觸發器, 觸發lambda consumer : SQS為觸發器, 觸發lambda,為內部觸發. orderdetail : cron為觸發器, 觸發方式 cron(0 3,11,19 * * ? *) return-quantity-consumer : rate為觸發器, 觸發方式rate(5 minutes) ## lambda Introduction Trigger 在lambda 上的表示方式 第一種 API Gateway 第二種 SQS 第三種 cron(linux timer) 第四種 rate(frequency) ### Logger Logger 是寫在lambda P.S 但在正式環境上才會紀錄 ### Db checking join syntax 在environment.py 上可以做test view SQL syntax 利用testing 的方式去show SQL Syntax # AWS ## Normal Introduction 一種雲端機器概念 上頭有提供很多function ## Account 在此專案上需root 授權帳號 ```csv ``` ## AWS Switch 此專案有兩個 Workspace 1. Development 2. Production 如需要Switch 需要使用 "Switch Role" For Example: ``` Account: 066******* Role: SuperAdminForDev ``` --- # Applications ## Concept & Task 1. Return ERROR status code define 2. Return data need to revise 3. Redis cache usable 4. Dummy remove 5. Environment variable call ```python= # for example call the REGION from .. import environment print(environment.API_HUB_APP_REGION) ``` 6. Call DB need to add the seesion ```python= with session_context(db.session(), True) as session: ... ``` ## Task ```python= # succed Response { "Response": { "StatusCode": "SUCCESS", "Value": { "Element": [], "EquivalentAmount": "0", "MerchantName": "com.orca", "PointBalance": "0", "PointName": "小樹點", "ProviderKey": "TreePoints", "Status": "AVAILABLE" } } } # Change to { "data" : { "element" : [], "equivalent_amout" : "0", "merchantname" : "com.orca", "point_balance" : "0", "point_name" : "小樹點", "provider_key" : "TreePoints", "status" : "AVAILABLE" } } # ERROR Response # UNAUTHORIZED 401 | BAD_REQUEST 400 | POINT_GET_FAILED 500 { "Response": { "StatusCode": "UNAUTHORIZED" } } ``` 7. Task: Finish pytest case ## 閃購商品頁 1. 從原本 Mall 去修改 2. mall_msic 來篩選該拋出的值 3. 獲取 product_id 4. 判斷是否時間內 5. 是否上架 6. 判斷是否為閃購商品 7. 拋回既定的值 ## Redis Reference : auth/misc.py ## GP (Get-Point) 流程 VEC -> API Hub -> Point VEC : 我們這端 API Hub : 中間API 交流端 Point : 點數存放位置 ### How to get the point? Verify identidy 1. API Hub from the lambda 2. rpc (Remote Procedure Call) 遠端資源獲取 3. get aws credentials by self 4. double check aws credentials session *Any session suggest double-check.* ### Get Point Document [get point format](https://gitlab.com/api-hub/rpc-contract/-/wikis/pages) aws.session.invoke // gp_pb2 API 來源 # pytest for this project ## Folder Stracture Basic Introduction 1. conftest.py 首要先執行此fixture給其他的unittest可以帶入 2. contants.py 此為基本參數的設定 3. environment.py 此也為基本參數的設定 ... 其餘為unit test time 時間 = 毫秒 ## How to start test 1. 開啟docker-compose 確保local db and redis 已經打開 2. os.envior 內部資料是透過 serverless.yml 設定上獲取 然而內部設定是連結於aws lambda and DB 所以確保於local端需要以下步驟: **unset DATABASE_URI** **unset CACHE_URI** 3. 確認環境已修正 ```bash $ printenv # or quick search $ printenv | grep URI ``` 6. 啟動執行pytest 6.1 使用 pytest --tb=line #只會出現錯誤資訊 6.2 使用 pytest --tb=no #不會出現任何錯誤 ## Test 驗證邏輯 1. 使用requests_mock對該API routing requestment 驗證 2. API正確需回傳 status == 200 3. 驗證資料流,檢驗回傳資訊正確(就是使用資料庫連接) ```python= assert {check model object} ```` ### Project Trick Introduction [mock.path document](https://docs.python.org/3/library/unittest.mock.html#unittest.mock.patch) ```python= with mock.patch('application.middleware.auth'): pass # or @mock.patch('application.middleware.auth') def test_whatever(): pass ``` ### requests_mock [Install](https://pypi.org/project/requests-mock/) [Document](https://requests-mock.readthedocs.io/en/latest/pytest.html) 簡單來說:此requests mock 是一個fixture的一個方法,可直接使用於function parameter. ```pytnon= def test_for_example(request_mock): request_mock.post(url,parameter='') ``` 用此方式可以直接模擬request獲得相關的資料. ### Unittest 多參數原因 e.g. ```python= def test_get_details_negative( requests_mock, app, client, account_user, cathayholdings_account_provider_account, cathayholdings_account_provider_account_user_association, product_product, product_variant, account_address, order_orderstatuscode, order_cart, order_order, order_orderline, order_orderinvoiceapplytype, order_orderinvoice, order_orderpaymentbalance, order_orderdelivery, order_orderdeliveryhistory, db_tables): ``` 帶入相關的參數,在流程執行的時候可能會動用到相關的參數. 所以將有相關參數都帶入最為保險. # UAT & UT 正式資料測試 ## Concept 由local 端access to 線上測試端 拿取正式測試的資料來確認 ## 測試步驟 ### LOCAL 執行的部分 -> OPEN README.md -> source ./devtools-python.bash -> docker-compose up -d -> py-shell --publish 1337:1337 -> source venv/bin/activate -> ./switch-env at-ut/v1 # 注意!!! 如果要UAT 測試的話就更改為 at-uat -> source envfile -> source envfile-infra -> flask run --host 0.0.0.0 --port 1337 ### POSTMAN 部分 從這邊access 獲取使用者的正確資料 Workspace: vec Folder: Membership Enviroment: UAT # 如果要使用ut 的話 flask 要改重改at-ut -> POST: GetID -> POST: GetSso #複製 sso 後面的資訊全部!! 已得到 jwt 可以知道該使用者資訊 Switch Folder: Treemall Enviroment: Development -> GET: 登入 sign-on(SSO) # 把sso資訊貼上,access get api token會自動放入evn -> POST: 使用者資訊 Key #單純查看使用者資訊 ### Django 後端位置 Google Drive Searching: VEC 資源 Sheet: API-Hub 使用UAT資料 -> 點選網址 -> 登入 -> ECommerces -> MR -> Component SSO Gateway -> 修改at-ut # 就會變成ut資料 進去之後選擇