"""
Tests against the sandbox Authorize.net API. Slow, requires internet.
"""

from datetime import date, timedelta
import os

from unittest2 import skipUnless, TestCase

from authorize import Address, AuthorizeClient, CreditCard
from authorize.exceptions import AuthorizeResponseError


# Authorize.net developer login for test
# user: authorizepy0
# pass: ny2qgWTw
# gateway id: 355553

SKIP_MESSAGE = 'Live tests only run if the AUTHORIZE_LIVE_TESTS ' \
    'environment variable is true.'
TEST_LOGIN_ID = '285tUPuS'
TEST_TRANSACTION_KEY = '58JKJ4T95uee75wd'

@skipUnless(os.environ.get('AUTHORIZE_LIVE_TESTS'), SKIP_MESSAGE)
class AuthorizeLiveTests(TestCase):
    def setUp(self):
        self.client = AuthorizeClient(TEST_LOGIN_ID, TEST_TRANSACTION_KEY)
        self.year = date.today().year + 10
        self.credit_card = CreditCard('4111111111111111', self.year, 1, '911',
            'Jeff', 'Schenck')
        self.address = Address('45 Rose Ave', 'Venice', 'CA', '90291')

    def test_credit_card(self):
        card = self.client.card(self.credit_card, self.address)
        transaction = card.auth(10)
        transaction.void()
        self.assertRaises(AuthorizeResponseError, transaction.settle)

    def test_saved_card(self):
        card = self.client.card(self.credit_card, self.address)
        saved = card.save()
        saved.auth(10).settle()
        saved.capture(10)
        saved.credit(10)
        saved_from_id = self.client.saved_card(saved.uid)
        saved_from_id.delete()

    def test_recurring(self):
        card = self.client.card(self.credit_card, self.address)
        start = date.today() + timedelta(days=7)
        recurring = card.recurring(10, start, months=1, occurrences=10)
        recurring.update(amount=20, trial_amount=10, trial_occurrences=3)
        recurring_from_id = self.client.recurring(recurring.uid)
        recurring_from_id.delete()

@@ -118,16 +118,30 @@
 mport os
+%0Aimport random
 %0A%0Afrom u
@@ -685,32 +685,369 @@
 ef setUp(self):%0A
+        # Random in testing feels gross, otherwise running the same test%0A        # suite in quick succession produces failures because Authorize.net%0A        # thinks the transactions are duplicates and rejects them%0A        self.amount1 = random.randrange(100, 100000) / 100.0%0A        self.amount2 = random.randrange(100, 100000) / 100.0%0A
         self.cli
@@ -1458,18 +1458,28 @@
 rd.auth(
+self.amount
 1
-0
 )%0A      
@@ -1712,18 +1712,28 @@
 ed.auth(
+self.amount
 1
-0
 ).settle
@@ -1757,18 +1757,28 @@
 capture(
+self.amount
 1
-0
 )%0A      
@@ -1792,18 +1792,28 @@
 .credit(
+self.amount
 1
-0
 )%0A      
@@ -2074,18 +2074,28 @@
 curring(
+self.amount
 1
-0
 , start,
@@ -2157,10 +2157,20 @@
 unt=
+self.amount
 2
-0
 , tr
@@ -2180,18 +2180,34 @@
 _amount=
-10
+self.amount2 - 0.5
 , trial_

Use differing charge amounts in live test suite to avoid duplicate charge errors