Exemple #1
0
    def test_update_user(self):
        """ Test the user/update API call.

            We attempt to update a user's details.
        """
        # Create a random user and open a session for that user.

        user = self.create_random_user()

        err_msg = self.open_session(user.user_id, user.password)
        self.assertEqual(err_msg, None)

        # Now try changing the user's name, password and PIN number.

        new_name       = utils.random_letters()
        new_password   = utils.random_letters()
        new_pin_number = utils.random_digits(min_length=4, max_length=4)

        changes = {"name"       : new_name,
                   "password"   : new_password,
                   "pin_number" : new_pin_number}
    
        success,response = self.call_api("tahua.api.user.update",
                                         include_session_key=True,
                                         pin_number=user.pin_number,
                                         changes=changes)
        if not success: self.fail(response)

        # Finally, check that the user has been updated.

        updated_user = User.objects.get(user_id=user.user_id)

        self.assertEqual(updated_user.name,       new_name)
        self.assertEqual(updated_user.password,   new_password)
        self.assertEqual(updated_user.pin_number, new_pin_number)
Exemple #2
0
    def test_new_user(self):
        """ Test the user/new API call.

            We attempt to create a new unique user, and then try creating a
            second user with the same user ID.
        """
        # Call the API to create a user.

        user_id    = self.random_user_id()
        name       = utils.random_letters()
        password   = utils.random_letters()
        pin_number = utils.random_digits(min_length=4, max_length=4)

        success,response = self.call_api("tahua.api.user.new",
                                         user_id=user_id,
                                         name=name,
                                         password=password,
                                         pin_number=pin_number)
        if not success: self.fail(response)

        # Check that the newly-created user actually exists.

        user = User.objects.get(user_id=user_id)

        self.assertEqual(user.user_id,    user_id)
        self.assertEqual(user.name,       name)
        self.assertEqual(user.password,   password)
        self.assertEqual(user.pin_number, pin_number)
Exemple #3
0
    def create_random_user(self):
        """ Create and return a new randomly-named User.

            Note that we create the user directly, rather than going via the
            API.
        """
        user = User()
        user.user_id    = self.random_user_id()
        user.name       = utils.random_letters()
        user.password   = utils.random_letters()
        user.pin_number = utils.random_digits(min_length=4, max_length=4)
        user.save()

        return user
Exemple #4
0
 def random_client_code(self):
     """ Return a random but unique code for a Client.
     """
     while True:
         code = utils.random_letters(max_length=20)
         if not Client.objects.filter(code=code).exists():
             return code
Exemple #5
0
 def random_client_name(self):
     """ Return a random but unique name for a Client.
     """
     while True:
         name = utils.random_letters()
         if not Client.objects.filter(name=name).exists():
             return name
Exemple #6
0
    def random_user_id(self):
        """ Return a random but unique ID for a User.
        """
        while True:
            prefix = utils.random_letters()
            suffix = utils.random_digits()
            user_id = prefix + ":" + suffix

            if not User.objects.filter(user_id=user_id).exists():
                return user_id
Exemple #7
0
    def test_pay_to_alias(self):
        """ Test the logic of making a payment to an alias.
        """
        # Create a source account and give it some funds.

        src_user = self.create_random_user()

        src_account = Account()
        src_account.user_id = src_user.user_id
        src_account.suffix  = "source account"
        src_account.save()

        transaction = Transaction()
        transaction.account = src_account
        transaction.timestamp = datetime.datetime.now()
        transaction.type      = Transaction.TYPE_DEPOSIT
        transaction.amount    = decimal.Decimal("100.00")
        transaction.save()

        # Create a user to make a payment to, and set up an alias for that
        # user.  Note that we store the user ID and alias in global variables,
        # so our testing alias resolver can resolve the alias.

        dst_user = self.create_random_user()

        global _test_alias_id, _test_user_id

        _test_user_id  = dst_user.user_id
        _test_alias_id = utils.random_letters()

        # Open a session as the source user, so we can make a payment from that
        # account.

        err_msg = self.open_session(src_user.user_id, src_user.password)
        self.assertEqual(err_msg, None)

        # Define our source and destination accounts, using the alias for the
        # destination account.

        src_account_id = {'user_id' : src_user.user_id,
                          'suffix'  : "source account"}

        dst_account_id = {'user_alias' : _test_alias_id}

        # Make a payment from the source to the destination account.

        resolver_path = "tahua.tests.test_payments.test_alias_resolver"
        with self.settings(TAHUA_USER_ALIAS_RESOLVER=resolver_path):
            success,response = self.call_api("tahua.api.pay.pay",
                                             include_session_key=True,
                                             from_account_id=src_account_id,
                                             to_account_id=dst_account_id,
                                             amount="10.00",
                                             pin_number=src_user.pin_number,
                                             meta_data={})
        if not success: self.fail(response)

        # Finally, check that the payment went to the right account.

        account_id = {'user_id' : dst_user.user_id}

        balance = account_helper.get_account_balance(account_id)
        self.assertEqual(balance, decimal.Decimal("10.00"))