Skip to content

Instantly share code, notes, and snippets.

@pasystem
Last active October 1, 2020 08:55
Show Gist options
  • Save pasystem/9c8e9a39cc4d6646c1096a3ea0d0b8ed to your computer and use it in GitHub Desktop.
Save pasystem/9c8e9a39cc4d6646c1096a3ea0d0b8ed to your computer and use it in GitHub Desktop.
def test_api():
import json
import requests
from random import choice
from rest_framework.test import APIClient
from tws.sms.models import OTP
from apps.partners.models import GoodsCategory
api = APIClient()
def get_token():
data = {
"mobile_phone": "+77051972515",
"password": "qwerty123"
}
response = api.post('/api/auth/', data, format='json')
assert response.status_code == 200, ValueError("request error")
return f"JWT {response.data['access']}"
def step1_apply(iin: str, mobile_phone: str) -> str:
data = {
"iin": iin,
"mobile_phone": mobile_phone
}
response = api.post('/api/scoring/apply', data, format='json')
print('step1_apply:', response)
assert response.status_code == 201, f"Не удалось создать заявку: {json.dumps(data)}"
return response.data['uuid']
def step2_verify(uuid):
otp = OTP.objects.last()
data = {
'code': otp.code,
}
response = api.post(f'/api/scoring/{uuid}/verify', data, format='json')
print('step2_verify:', response)
assert response.status_code == 200, ValueError("Error:", response.data)
def step3_get_programs(uuid):
category_1 = GoodsCategory.objects.filter(purpose_code='4R').only('id').first()
category_2 = GoodsCategory.objects.filter(purpose_code='13K').only('id').first()
data: dict = {
"goods": [
{"category": category_1.id, "amount": 37900},
{"category": category_2.id, "amount": 24100},
{"category": category_2.id, "amount": 21100},
],
"down_payment": 0,
"period": 9
}
data['totalSumma'] = sum(g['amount'] for g in data.get('goods', []))
response = api.put(f'/api/scoring/{uuid}/set-params', data, format='json')
print('step3_get_programs:', response)
assert response.status_code == 200 and len(response.data), ValueError("Не удалось получить список тарифов")
return response.data[0]['id']
def step4_set_program(uuid: str, selected_product: int):
data = {
"selected_products": [selected_product]
}
response = api.put(f'/api/scoring/{uuid}/set-program', data, format='json')
print('step4_set_program:', response)
assert response.status_code == 200, ValueError("Не удалось выбрать программу")
def step5_get_graphs(uuid: str):
pass
def step6_and_step7_update_borrower(uuid: str):
from tws.people import MaritalStatus
response = api.get(f'/api/scoring/{uuid}/borrower', format='json')
assert response.status_code == 200, response.data
print('step5_get_borrower:', response)
address = response.data['borrower_data']['reg_address']
#address['postal_code'] = '060000'
data = {
"borrower_data": {
# "id": response.data['borrower_data']['id'],
"reg_address": address,
"real_address": address,
"contacts": [
{
"name": "супруга",
"mobile_phone": "+77051972510",
"relationship": "RELATIVE"
}
],
"marital_status": MaritalStatus.MARRIED,
"education": "high",
"job_place": address,
"job_phone": "7272911668",
"job_joined": "2018-05-01",
"job_title": "ДЕТСКИЙ ДОМ \"ЖАН УЯ\"",
"official_income": 420_000,
"additional_income": 0,
"occupation_id": 1, # Работник сферы торговли/услуг
"organization_type_id": 3 # Частная
}
}
# print('data:', json.dumps(data, indent=4))
response = api.patch(f'/api/scoring/{uuid}/borrower', data, format='json')
print('step6_update_borrower:', response)
assert response.status_code == 200, response.data
def step8_start_pipeline(uuid: str):
response = api.post(f'/api/scoring/{uuid}/start', None, format='json')
print('step7_start_pipeline:', response, response.data)
assert response.status_code == 200, response.data
def step9_upload_images(uuid: str):
test_photo_path = 'tests/test_photo.jpg'
test_photo_url = 'https://pbs.twimg.com/profile_images/669881624666898433/lJHEM3fv_400x400.jpg'
res = requests.get(test_photo_url)
open(test_photo_path, 'wb').write(res.content)
images = [
{
"type_image": "BORROWER_IMAGE",
"image": open(test_photo_path, "rb")
}, {
"type_image": "DOCUMENT_FRONT_IMAGE",
"image": open(test_photo_path, "rb")
}, {
"type_image": "DOCUMENT_BACK_IMAGE",
"image": open(test_photo_path, "rb")
}
]
for data in images:
response = api.post(f'/api/scoring/{uuid}/upload', data)
print('step9_upload_images:', response)
def step10_match_images(uuid: str):
response = api.post(f'/api/scoring/{uuid}/match-images', None, format='json')
assert response.status_code == 200, response.data
print('step10_match_images:', response)
def step11_get_decision(uuid: str):
response = api.put(f'/api/scoring/{uuid}/get-decision', None, format='json')
assert response.status_code == 200, response.data
print('step11_get_decision:', response.data)
return response.data
def step12_client_decision_true(uuid: str, decision: dict):
data = {
"decision": True,
"decision_id": decision['decision_id']
}
response = api.put(f'/api/scoring/{uuid}/client-decision', data, format='json')
assert response.status_code == 200, response.data
print('step12_client_decision_true:', response.data)
token = get_token()
api.credentials(HTTP_AUTHORIZATION=token)
iin = choice(['870929450493', '900124301526', '780529403355', '940628300109'])
iin = '781103400476'
uuid = step1_apply(iin=iin, mobile_phone="+77051972515")
print('uuid:', uuid)
step2_verify(uuid)
selected_product = step3_get_programs(uuid)
step4_set_program(uuid, selected_product)
step6_and_step7_update_borrower(uuid)
step8_start_pipeline(uuid)
step9_upload_images(uuid)
step10_match_images(uuid)
res = step11_get_decision(uuid)
# if len(res):
# decision = res[0]
# step12_client_decision_true(uuid, decision)
test_api()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment