Skip to content

Commit

Permalink
Merge pull request #131 from rebeccacremona/separate-route
Browse files Browse the repository at this point in the history
Move purchase history to its own route.
  • Loading branch information
bensteinberg committed Aug 7, 2020
2 parents 7121473 + 93d61a0 commit f98c6bd
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 20 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Expand Up @@ -5,7 +5,7 @@ services:
image: postgres:9.6.2
web:
build: ./perma-payments
image: perma-payments:0.25
image: perma-payments:0.26
tty: true
command: bash
volumes:
Expand Down
44 changes: 30 additions & 14 deletions perma-payments/perma_payments/tests/test_routes.py
Expand Up @@ -94,6 +94,17 @@ def acknowledge_purchase():
return data


@pytest.fixture
def purchase_history():
return {
'route': '/purchase-history/',
'valid_data': {
'customer_pk': SENTINEL['customer_pk'],
'customer_type': SENTINEL['customer_type'],
}
}


@pytest.fixture
def subscribe():
data = {
Expand Down Expand Up @@ -1326,8 +1337,7 @@ def test_subscription_post_no_standing_subscription(client, subscription, mocker
'customer_type': subscription['valid_data']['customer_type'],
'subscription': None,
'timestamp': mocker.sentinel.timestamp,
'purchases': [],
'purchase_history': []
'purchases': []
})
r = response.json()
assert r and list(r.keys()) == ['encrypted_data']
Expand Down Expand Up @@ -1363,8 +1373,7 @@ def test_subscription_post_standard_standing_subscription(client, subscription,
'paid_through': complete_standing_sa.paid_through,
},
'timestamp': mocker.sentinel.timestamp,
'purchases': [],
'purchase_history': []
'purchases': []
})
r = response.json()
assert r and list(r.keys()) == ['encrypted_data']
Expand Down Expand Up @@ -1443,14 +1452,22 @@ def test_subscription_multiple_purchases_to_acknowledge(client, subscription, ge
"link_quantity": prr2.related_request.link_quantity
}]


def test_subscription_other_methods(client, subscription):
get_not_allowed(client, subscription['route'])
put_patch_delete_not_allowed(client, subscription['route'])


# purchase history

@pytest.mark.django_db
def test_subscription_single_purchase_in_history(client, subscription, get_prr_for_user, mocker):
mocker.patch('perma_payments.views.process_perma_transmission', autospec=True, return_value=subscription['valid_data'])
def test_single_purchase_in_history(client, purchase_history, get_prr_for_user, mocker):
mocker.patch('perma_payments.views.process_perma_transmission', autospec=True, return_value=purchase_history['valid_data'])
prepped = mocker.patch('perma_payments.views.prep_for_perma', autospec=True, return_value=SENTINEL['bytes'])
prr = get_prr_for_user(SENTINEL['customer_pk'], SENTINEL['customer_type'])

# request
response = client.post(subscription['route'])
response = client.post(purchase_history['route'])

assert response.status_code == 200
purchase_history = prepped.mock_calls[0][1][0]['purchase_history']
Expand All @@ -1462,15 +1479,15 @@ def test_subscription_single_purchase_in_history(client, subscription, get_prr_f


@pytest.mark.django_db
def test_subscription_multiple_purchases_in_history(client, subscription, get_prr_for_user, mocker):
mocker.patch('perma_payments.views.process_perma_transmission', autospec=True, return_value=subscription['valid_data'])
def test_multiple_purchases_in_history(client, purchase_history, get_prr_for_user, mocker):
mocker.patch('perma_payments.views.process_perma_transmission', autospec=True, return_value=purchase_history['valid_data'])
prepped = mocker.patch('perma_payments.views.prep_for_perma', autospec=True, return_value=SENTINEL['bytes'])
prr1 = get_prr_for_user(SENTINEL['customer_pk'], SENTINEL['customer_type'])
prr2 = get_prr_for_user(SENTINEL['customer_pk'], SENTINEL['customer_type'])
get_prr_for_user(SENTINEL['customer_pk'], SENTINEL['customer_type'], inform_perma=False)

# request
response = client.post(subscription['route'])
response = client.post(purchase_history['route'])

assert response.status_code == 200
purchase_history = prepped.mock_calls[0][1][0]['purchase_history']
Expand All @@ -1485,10 +1502,9 @@ def test_subscription_multiple_purchases_in_history(client, subscription, get_pr
}]



def test_subscription_other_methods(client, subscription):
get_not_allowed(client, subscription['route'])
put_patch_delete_not_allowed(client, subscription['route'])
def test_purchase_history_other_methods(client, purchase_history):
get_not_allowed(client, purchase_history['route'])
put_patch_delete_not_allowed(client, purchase_history['route'])


# cancellation request
Expand Down
1 change: 1 addition & 0 deletions perma-payments/perma_payments/urls.py
Expand Up @@ -8,6 +8,7 @@
url(r'^cybersource-callback/$', views.cybersource_callback, name='cybersource_callback'),
url(r'^purchase/$', views.purchase, name='purchase'),
url(r'^acknowledge-purchase/$', views.acknowledge_purchase, name='acknowledge_purchase'),
url(r'^purchase-history/$', views.purchase_history, name='purchase_history'),
url(r'^subscribe/$', views.subscribe, name='subscribe'),
url(r'^subscription/$', views.subscription, name='subscription'),
url(r'^update-statuses/$', views.update_statuses, name='update_statuses'),
Expand Down
29 changes: 24 additions & 5 deletions perma-payments/perma_payments/views.py
Expand Up @@ -616,16 +616,35 @@ def subscription(request):
# Mention any bonus links that have been purchased, but not yet acknowledged
purchases = PurchaseRequestResponse.customer_unacknowledged(data['customer_pk'], data['customer_type'])

# Send the customer's full purchase history too.
purchase_history = PurchaseRequestResponse.customer_history(data['customer_pk'], data['customer_type'])

response = {
'customer_pk': data['customer_pk'],
'customer_type': data['customer_type'],
'subscription': subscription,
'timestamp': datetime.utcnow().timestamp(),
'purchases': purchases,
'purchase_history': purchase_history
'purchases': purchases
}
return JsonResponse({'encrypted_data': prep_for_perma(response).decode('ascii')})


@csrf_exempt
@require_http_methods(["POST"])
@sensitive_post_parameters('encrypted_data')
def purchase_history(request):
"""
Returns a customer's one-time purchase history.
"""
try:
data = process_perma_transmission(request.POST, FIELDS_REQUIRED_FROM_PERMA['subscription'])
except InvalidTransmissionException:
return bad_request(request)

purchase_history = PurchaseRequestResponse.customer_history(data['customer_pk'], data['customer_type'])

response = {
'customer_pk': data['customer_pk'],
'customer_type': data['customer_type'],
'purchase_history': purchase_history,
'timestamp': datetime.utcnow().timestamp()
}
return JsonResponse({'encrypted_data': prep_for_perma(response).decode('ascii')})

Expand Down

0 comments on commit f98c6bd

Please sign in to comment.