예제 #1
0
파일: views.py 프로젝트: kave/cfgov-django
def govdelivery_subscribe(request):
    """
    View that checks to see if the request is AJAX, attempts to subscribe
    the user, then either redirects to an error/success page (non-AJAX) or
    in the case of AJAX, returns some JSON to tell the front-end.
    """
    is_ajax = request.is_ajax()
    if is_ajax:
        passing_response = JsonResponse({'result': 'pass'})
        failing_response = JsonResponse({'result': 'fail'})
    else:
        passing_response = redirect('govdelivery:success')
        failing_response = redirect('govdelivery:server_error')
    for required_param in REQUIRED_PARAMS:
        if required_param not in request.POST or not request.POST[required_param]:
            return failing_response if is_ajax else \
                redirect('govdelivery:user_error')
    email_address = request.POST['email']
    codes = request.POST.getlist('code')
    gd = GovDelivery(account_code=ACCOUNT_CODE)
    try:
        subscription_response = gd.set_subscriber_topics(email_address, codes)
        if subscription_response.status_code != 200:
            return failing_response
    except Exception:
        return failing_response
    answers = extract_answers_from_request(request)
    for question_id, answer_text in answers:
        response = gd.set_subscriber_answers_to_question(email_address,
                                                         question_id,
                                                         answer_text)
    return passing_response
예제 #2
0
    def subscribe(self, email, code):
        try:
            logger.info('subscribing to GovDelivery')
            gd = GovDelivery(account_code=settings.ACCOUNT_CODE)

            subscription_response = gd.set_subscriber_topics(
                email_address=email, topic_codes=[code])

            subscription_response.raise_for_status()
        except Exception:
            logger.exception('error subscribing to GovDelivery')
            return False

        return True
예제 #3
0
    def subscribe(self, email, code):
        try:
            logger.info('subscribing to GovDelivery')
            gd = GovDelivery(account_code=settings.ACCOUNT_CODE)

            subscription_response = gd.set_subscriber_topics(
                email_address=email,
                topic_codes=[code]
            )

            subscription_response.raise_for_status()
        except Exception:
            logger.exception('error subscribing to GovDelivery')
            return False

        return True
예제 #4
0
import sys
import json

from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
from django.conf import settings

from govdelivery.api import GovDelivery

from .models import SubscriptionAttempt

ACCOUNT_CODE = settings.GOVDELIVERY_ACCOUNT_CODE
gd = GovDelivery(account_code=ACCOUNT_CODE)


def fail_with_code_and_message(code, message):
    response = HttpResponse(message)
    response.status_code = code
    return response


def fail_with_missing_parameter(missing_parameter):
    text = 'Required parameter "%s" is missing' % missing_parameter
    response = fail_with_code_and_message(400, text)
    return response


def extract_answers_from_request(request):
    answers = []
    for param, value in request.POST.items():
        if param.startswith('questionid'):
예제 #5
0
 def setUp(self):
     self.gd = GovDelivery(
         username='******',
         password='******',
         account_code='XYZ'
     )
예제 #6
0
class TestGovDelivery(unittest.TestCase):
    def setUp(self):
        self.gd = GovDelivery(
            username='******',
            password='******',
            account_code='XYZ'
        )

    def test_translate_path(self):
        path = self.gd.translate_path('/api/account/$account_code/topics.xml')
        self.assertEqual(path, '/api/account/XYZ/topics.xml')

    @responses.activate
    def test_call_api_no_parser_returns_unparsed(self):
        path = self.gd.translate_path('/api/account/$account_code/topics.xml')
        mockResponseBody = load_data('list_all_topics.xml')
        responses.add(
            responses.GET,
            get_full_url_to_call(path),
            body=mockResponseBody,
            status=200
        )
        response = self.gd.call_api(path, 'get')
        self.assertIsInstance(response, Response)

    @responses.activate
    def test_call_api_with_parser_returns_parsed(self):
        path = self.gd.translate_path('/api/account/$account_code/topics.xml')
        mockResponseBody = load_data('list_all_topics.xml')
        responses.add(
            responses.GET,
            get_full_url_to_call(path),
            body=mockResponseBody,
            status=200
        )
        parsed_response = self.gd.call_api(
            path,
            'get',
            response_parser=topic_xml_as_dict
        )
        self.assertEqual(
            parsed_response,
            {'XYZ_998': 'Test Unlisted Topic', 'XYZ_999': 'Test Topic'}
        )

    @responses.activate
    def test_get_all_topics(self):
        path = self.gd.translate_path('/api/account/$account_code/topics.xml')
        mockResponseBody = load_data('list_all_topics.xml')
        responses.add(
            responses.GET,
            get_full_url_to_call(path),
            body=mockResponseBody,
            status=200
        )
        response = self.gd.get_all_topics()
        self.assertEqual(
            response,
            {'XYZ_998': 'Test Unlisted Topic', 'XYZ_999': 'Test Topic'}
        )

    @responses.activate
    def test_get_visible_topics(self):
        path = self.gd.translate_path('/api/account/$account_code/topics.xml')
        mockResponseBody = load_data('list_all_topics.xml')
        responses.add(
            responses.GET,
            get_full_url_to_call(path),
            body=mockResponseBody,
            status=200
        )
        response = self.gd.get_visible_topics()
        self.assertEqual(response, {'XYZ_999': 'Test Topic'})

    @responses.activate
    def test_get_subscriber_topics(self):
        email = '*****@*****.**'
        subscriber_id = base64.b64encode(email.encode('utf-8'))
        path = self.gd.translate_path(
            '/api/account/$account_code/subscribers/$subscriber_id/topics.xml',
            subscriber_id=subscriber_id
        )
        mockResponseBody = load_data('list_subscriber_topics.xml')
        responses.add(
            responses.GET,
            get_full_url_to_call(path),
            body=mockResponseBody,
            status=200
        )
        response = self.gd.get_subscriber_topics('*****@*****.**')
        self.assertIn('XYZ_999', response)