def test_parser_request_all_events(self): """This test lists all of the mocked events in the test/fixtures/events directory, and correspondingly kicks off a parse, retrieve and render integration test. This means, to add a new event, all you need to do is drop in the JSON representation and this test will pick it up.""" httpretty.register_uri( httpretty.GET, "https://api.stripe.com/v1/customers/cus_id_fake", body=self.fixture('customer.json')) event_types = glob.glob("test/fixtures/events/*.json") for event_path in event_types: event = json.load(open(event_path)) try: event_type = event["type"].replace(".", "_") except KeyError: raise Exception( "Your fixture is badly formatted and does not contain a type: %s" % (event_path)) httpretty.register_uri( httpretty.GET, "https://api.stripe.com/v1/events/evt_id_fake", body=self.fixture('events/%s.json' % event_type)) parse_hook({"id": "evt_id_fake"})
def receieve_hook(): """ Path: /webhook/receive Method: POST """ # Abort if we're not sent JSON if not request.json: return jsonify_with_status(406, {'error': 'Requires application/json'}) # If the event doesn't have a id, it's not an event # https://stripe.com/docs/api#events if not request.json.get("id"): return jsonify_with_status(406, {'error': 'Does not have an id'}) try: parse_hook(request.json) except InvalidRequestError as e: # If the hook failed to parse, send back why to stripe # This will be visible in your dashboard return jsonify_with_status(406, {'error': str(e)}) except CleanParseException as e: # If the hook failed to parse, but we don't want it # to try again, send back why and a 200 so Stripe stops trying. return jsonify_with_status(200, {'error': str(e)}) return jsonify_with_status(200, {'success': True})
def test_parser_bad_id(self): "Tests parsing a bad id" httpretty.register_uri( httpretty.GET, "https://api.stripe.com/v1/events/evt_id_fake", body=self.fixture('not_found.json'), status=404) with pytest.raises(InvalidRequestError): parse_hook({"id": "evt_id_fake"})
def test_parser_bad_id(self): "Tests parsing a bad id" httpretty.register_uri(httpretty.GET, "https://api.stripe.com/v1/events/evt_id_fake", body=self.fixture('not_found.json'), status=404) with pytest.raises(InvalidRequestError): parse_hook({"id": "evt_id_fake"})
def test_parser_event_no_email(self): """This tests an event with a customer attached that does not have an email address.""" httpretty.register_uri( httpretty.GET, "https://api.stripe.com/v1/customers/cus_id_fake", body=self.fixture('customer_no_email.json')) httpretty.register_uri( httpretty.GET, "https://api.stripe.com/v1/events/evt_id_fake", body=self.fixture('events/customer_card_created.json')) # Should raise a "safe" clean parse exception with pytest.raises(CleanParseException): parse_hook({"id": "evt_id_fake"})
def test_parser_no_id(self): "Tests parsing no id" with pytest.raises(InvalidRequestError): parse_hook({})