Пример #1
0
 def test_fail_on_production_if_connection_error(self, verify_mock):
     verify_mock.side_effect = [exceptions.ConnectionError, self.dummy_receipt]
     request = Request(self.receipt_data, password=self.password, use_production=True, use_sandbox=True)
     with pytest.raises(exceptions.ConnectionError):
         request.verify()
     calls = verify_mock.call_args_list
     assert calls == [mock.call(RECEIPT_PRODUCTION_VALIDATION_URL)]
Пример #2
0
    def test_responses(self):
        # We're going to mock the Apple's response and put 21007 status
        with patch.object(requests, 'post') as mock_post:
            iap_status_21007 = self.iap_response.copy()
            iap_status_21007['status'] = 21007
            mock_post.return_value.content = json.dumps(iap_status_21007)
            mock_post.return_value.status_code = 200
            set_verification_mode('production')
            request = Request('DummyReceipt')
            try:
                receipt = request.validate()
            except exceptions.InvalidReceipt as e:
                assert e.status == 21007
                assert e.description == e._descriptions[21007]

        # We're going to return an invalid http status code
        with patch.object(requests, 'post') as mock_post:
            mock_post.return_value.content = 'Not avaliable'
            mock_post.return_value.status_code = 500
            set_verification_mode('production')
            request = Request('DummyReceipt')
            try:
                receipt = request.validate()
            except exceptions.ItunesServerNotAvailable as e:
                assert e[0] == 500
                assert e[1] == 'Not avaliable'
Пример #3
0
 def test_request(self):
     set_verification_mode('production')
     request = Request(sandbox_receipt)
     try:
         receipt = request.validate()
         assert False
     except exceptions.InvalidReceipt as e:
         assert e.status == 21007
         assert e.description == e._descriptions[21007]
     set_verification_mode('review')
     request = Request(sandbox_receipt)
     receipt = request.validate()
     assert receipt
Пример #4
0
def test_request():
    try:
        from testdata import sandbox_receipt
    except ImportError:
        print 'No receipt data to test'
        return

    set_verification_mode('production')
    request = Request(sandbox_receipt)
    try:
        receipt = request.validate()
        assert False
    except exceptions.InvalidReceipt, e:
        assert e.status == 21007
        assert e.description == e._descriptions[21007]
Пример #5
0
class ItunesIapTestCase(ItunesIapMixin, unittest.TestCase):

    def setUp(self):
        super(ItunesIapTestCase, self).setUp()
        self.request = Request(self.receipt_data, password=self.password, use_production=False, use_sandbox=True)
        self.dummy_receipt = dict(receipt={}, status=0)

    @vcr.use_cassette('verified_receipt')
    def test_receipt_is_verified(self):
        receipt = self.request.verify()
        assert receipt.status == 0

    @mock.patch.object(Request, 'verify_from')
    def test_try_both_production_and_sandbox_mode(self, verify_mock):
        verify_mock.side_effect = [exceptions.InvalidReceipt, self.dummy_receipt]
        request = Request(self.receipt_data, password=self.password, use_production=True, use_sandbox=True)
        request.verify()
        calls = verify_mock.call_args_list
        assert calls == [mock.call(RECEIPT_PRODUCTION_VALIDATION_URL), mock.call(RECEIPT_SANDBOX_VALIDATION_URL)]

    @mock.patch.object(Request, 'verify_from')
    def test_fail_on_production_if_connection_error(self, verify_mock):
        verify_mock.side_effect = [exceptions.ConnectionError, self.dummy_receipt]
        request = Request(self.receipt_data, password=self.password, use_production=True, use_sandbox=True)
        with pytest.raises(exceptions.ConnectionError):
            request.verify()
        calls = verify_mock.call_args_list
        assert calls == [mock.call(RECEIPT_PRODUCTION_VALIDATION_URL)]

    @mock.patch.object(requests, 'post', side_effect=requests.HTTPError)
    def test_connection_error(self, post_mock):
        with pytest.raises(exceptions.ConnectionError):
            self.request.verify()

    @mock.patch.object(requests, 'post')
    def test_http_error(self, post_mock):
        post_mock.return_value = mock.MagicMock(status_code=404)
        with pytest.raises(exceptions.ItunesNotAvailable):
            self.request.verify()

    @mock.patch.object(requests.Response, 'content', mock.PropertyMock(return_value=b'Not available'))
    def test_json_error(self):
        with pytest.raises(exceptions.ItunesNotAvailable):
            self.request.verify()

    @vcr.use_cassette('invalid_receipt_data')
    def test_invalid_receipt(self):
        receipt_data = 'invalid'
        self.request = Request(receipt_data, password=self.password, use_production=False, use_sandbox=True)
        with pytest.raises(exceptions.InvalidReceipt):
            self.request.verify()
Пример #6
0
 def test_context(self):
     request = Request(sandbox_receipt)
     configs = request.use_production, request.use_sandbox
     with request.verification_mode('production'):
         try:
             request.verify()
             assert False
         except exceptions.InvalidReceipt as e:
             assert e.status == 21007
         with request.verification_mode('sandbox'):
             request.verify()
         try:
             request.verify()
             assert False
         except exceptions.InvalidReceipt as e:
             assert e.status == 21007
     assert configs == (request.use_production, request.use_sandbox)
Пример #7
0
    def test_extract_receipt(self):
        """
            Testing the extract receipt function.
            The function which helps to put the last 'in_app's fields' in the
            'receipt dictionary'
        """

        # Test IAP Response without in_app list
        request = Request('DummyReceipt', use_production=True)
        ext_receipt = request._extract_receipt(self.iap_response)

        assert ext_receipt['status'] == 0  # 0 is normal
        assert ext_receipt['receipt']['product_id'] == u'TestProduction1'
        assert ext_receipt['receipt']['original_transaction_id'] == u'1000000012345678'  # original transaction id
        assert ext_receipt['receipt']['quantity'] == u'1'  # check quantity

        # Test IAP Response with in_app list
        request = Request('DummyReceipt', use_production=True)
        ext_receipt = request._extract_receipt(self.iap_response_in_app)

        assert ext_receipt['status'] == 0  # 0 is normal
        assert ext_receipt['receipt']['product_id'] == u'org.itunesiap'
        assert ext_receipt['receipt']['original_transaction_id'] == u'1000000155715958'  # original transaction id
        assert ext_receipt['receipt']['quantity'] == u'1'  # check quantity
Пример #8
0
def test_context():
    try:
        from testdata import sandbox_receipt
    except ImportError:
        print 'No receipt data to test'
        return
    request = Request(sandbox_receipt)
    configs = request.use_production, request.use_sandbox
    with request.verification_mode('production'):
        try:
            request.verify()
            assert False
        except exceptions.InvalidReceipt, e:
            assert e.status == 21007
        with request.verification_mode('sandbox'):
            request.verify()
        try:
            request.verify()
            assert False
        except exceptions.InvalidReceipt, e:
            assert e.status == 21007
Пример #9
0
 def test_invalid_receipt(self):
     receipt_data = 'invalid'
     self.request = Request(receipt_data, password=self.password, use_production=False, use_sandbox=True)
     with pytest.raises(exceptions.InvalidReceipt):
         self.request.verify()
Пример #10
0
 def test_try_both_production_and_sandbox_mode(self, verify_mock):
     verify_mock.side_effect = [exceptions.InvalidReceipt, self.dummy_receipt]
     request = Request(self.receipt_data, password=self.password, use_production=True, use_sandbox=True)
     request.verify()
     calls = verify_mock.call_args_list
     assert calls == [mock.call(RECEIPT_PRODUCTION_VALIDATION_URL), mock.call(RECEIPT_SANDBOX_VALIDATION_URL)]
Пример #11
0
 def setUp(self):
     super(ItunesIapTestCase, self).setUp()
     self.request = Request(self.receipt_data, password=self.password, use_production=False, use_sandbox=True)
     self.dummy_receipt = dict(receipt={}, status=0)
Пример #12
0
    try:
        from testdata import sandbox_receipt
    except ImportError:
        print 'No receipt data to test'
        return

    set_verification_mode('production')
    request = Request(sandbox_receipt)
    try:
        receipt = request.validate()
        assert False
    except exceptions.InvalidReceipt, e:
        assert e.status == 21007
        assert e.description == e._descriptions[21007]
    set_verification_mode('review')
    request = Request(sandbox_receipt)
    receipt = request.validate()
    assert receipt

def test_context():
    try:
        from testdata import sandbox_receipt
    except ImportError:
        print 'No receipt data to test'
        return
    request = Request(sandbox_receipt)
    configs = request.use_production, request.use_sandbox
    with request.verification_mode('production'):
        try:
            request.verify()
            assert False