test_request.py 1.67 KB
from pyramid import testing


def test_create_va():
    import os
    from datetime import timedelta
    from base64 import b64encode
    from pyramid_snap.structure import (
        RC_OK,
        SERVICE_VA_CREATE,
    )
    from pyramid_snap.view import (
        create_now,
        time_to_str,
        create_rc,
    )
    from winpay.signature import generator
    from snap_server.views import API
    private_file = os.path.join('..', 'private.pem')
    with open(private_file, 'rb') as f:
        private_key = f.read()
    timestamp = create_now()
    expired_timestamp = timestamp + timedelta(1)
    timestamp_str = time_to_str(timestamp)
    expired_str = time_to_str(expired_timestamp)
    trx_id = '122005'
    channel = 'MANDIRI'
    data = {
        'customerNo': '08123456789',
        'virtualAccountName': 'Iwan Gunawan',
        'trxId': trx_id,
        'totalAmount': {
            'value': 10000,
            'currency': 'IDR'},
        'virtualAccountTrxType': 'c',
        'expiredDate': expired_str,
        'additionalInfo': {'channel': channel}}
    signature = generator(private_key, data, timestamp)
    signature_b64 = b64encode(signature)
    signature_b64 = signature_b64.decode('utf-8')
    headers = {
        'X-Timestamp': timestamp_str,
        'X-Signature': signature_b64,
        'X-Partner-Id': '1234',
        'X-External-Id': trx_id,
        'Channel-Id': channel}
    request = testing.DummyRequest(
        client_addr='127.0.0.1', method='POST', json_body=data,
        headers=headers)
    api = API(request)
    resp = api.create_va()
    assert resp.status_code == RC_OK[0]
    assert resp.json_body['responseCode'] == create_rc(
        RC_OK, SERVICE_VA_CREATE)