Commit 7ec89829 by PaulWattenberger

Pass Sailthru campaign id cookie to ecommerce

parent 33ae93ec
......@@ -3,6 +3,7 @@ from datetime import datetime, timedelta
import json
import itertools
from uuid import uuid4
import httpretty
import ddt
from django.conf import settings
......@@ -28,6 +29,7 @@ from student.models import CourseEnrollment
from student.tests.factories import CourseModeFactory
from student.tests.tests import EnrollmentEventTestMixin
from xmodule.modulestore.django import modulestore
from commerce.api.v0.views import SAILTHRU_CAMPAIGN_COOKIE
@attr(shard=1)
......@@ -51,6 +53,8 @@ class BasketsViewTests(EnrollmentEventTestMixin, UserMixin, ModuleStoreTestCase)
}
if marketing_email_opt_in:
payload["email_opt_in"] = True
self.client.cookies[SAILTHRU_CAMPAIGN_COOKIE] = 'sailthru id'
return self.client.post(self.url, payload)
def assertResponseMessage(self, response, expected_msg):
......@@ -173,6 +177,9 @@ class BasketsViewTests(EnrollmentEventTestMixin, UserMixin, ModuleStoreTestCase)
else:
self.assertResponsePaymentData(response)
# make sure ecommerce API call forwards Sailthru cookie
self.assertIn('{}=sailthru id'.format(SAILTHRU_CAMPAIGN_COOKIE), httpretty.last_request().headers['cookie'])
@ddt.data(True, False)
def test_course_with_honor_seat_sku(self, user_is_active):
"""
......
""" API v0 views. """
import logging
import requests
from edx_rest_api_client import exceptions
from opaque_keys import InvalidKeyError
......@@ -26,6 +27,7 @@ from util.json_request import JsonResponse
log = logging.getLogger(__name__)
SAILTHRU_CAMPAIGN_COOKIE = 'sailthru_bid'
class BasketsView(APIView):
......@@ -135,7 +137,8 @@ class BasketsView(APIView):
# Setup the API
try:
api = ecommerce_api_client(user)
api_session = requests.Session()
api = ecommerce_api_client(user, session=api_session)
except ValueError:
self._enroll(course_key, user)
msg = Messages.NO_ECOM_API.format(username=user.username, course_id=unicode(course_key))
......@@ -146,6 +149,15 @@ class BasketsView(APIView):
# Make the API call
try:
# Pass along Sailthru campaign id
campaign_cookie = request.COOKIES.get(SAILTHRU_CAMPAIGN_COOKIE)
if campaign_cookie:
cookie = {SAILTHRU_CAMPAIGN_COOKIE: campaign_cookie}
if api_session.cookies:
requests.utils.add_dict_to_cookiejar(api_session.cookies, cookie)
else:
api_session.cookies = requests.utils.cookiejar_from_dict(cookie)
response_data = api.baskets.post({
'products': [{'sku': default_enrollment_mode.sku}],
'checkout': True,
......
......@@ -32,7 +32,7 @@ def is_commerce_service_configured():
return bool(ecommerce_api_url and ecommerce_api_signing_key)
def ecommerce_api_client(user):
def ecommerce_api_client(user, session=None):
""" Returns an E-Commerce API client setup with authentication for the specified user. """
jwt_auth = configuration_helpers.get_value("JWT_AUTH", settings.JWT_AUTH)
return EdxRestApiClient(
......@@ -43,5 +43,6 @@ def ecommerce_api_client(user):
user.email,
tracking_context=create_tracking_context(user),
issuer=jwt_auth['JWT_ISSUER'],
expires_in=jwt_auth['JWT_EXPIRATION']
expires_in=jwt_auth['JWT_EXPIRATION'],
session=session
)
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