Exemplo n.º 1
0
    def test_display_balance(self):
        Database.db.clear_db()
        test_username = "******"
        dne_username = '******'

        UsersController.add_user(test_username)
        # Make call to display balance method and should return True
        self.assertTrue(UsersController.display_balance(test_username))

        # Try to get balance for user that doesn't exist
        with self.assertRaises(Exception) as cm:
            UsersController.display_balance(dne_username)
        exception = cm.exception
        self.assertIn("doesn't exist", exception.message)
Exemplo n.º 2
0
    def test_display_balance(self):
        Database.db.clear_db()
        test_username = "******"
        dne_username = '******'

        UsersController.add_user(test_username)
        # Make call to display balance method and should return True
        self.assertTrue(UsersController.display_balance(test_username))

        # Try to get balance for user that doesn't exist
        with self.assertRaises(Exception) as cm:
            UsersController.display_balance(dne_username)
        exception = cm.exception
        self.assertIn("doesn't exist", exception.message)
Exemplo n.º 3
0
    def test_add_user(self):
        Database.db.clear_db()
        test_username = "******"
        invalid_user = "******"
        invalid_user2 = 'abc'

        # Add a new valid user and expect no problems
        self.assertTrue(UsersController.add_user(test_username))
        user = Database.db.lookup_user(test_username)
        self.assertEqual(test_username, user.name)

        # Add invalid characters username
        with self.assertRaises(Exception) as cm:
            UsersController.add_user(invalid_user)
        exception = cm.exception
        self.assertIn("Invalid characters", exception.message)

        # Add invalid length username
        with self.assertRaises(Exception) as cm:
            UsersController.add_user(invalid_user2)
        exception = cm.exception
        self.assertIn("Invalid length", exception.message)

        # Add an existing user
        with self.assertRaises(Exception) as cm:
            UsersController.add_user(test_username)
        exception = cm.exception
        self.assertIn("already exists", exception.message)
Exemplo n.º 4
0
    def test_add_user(self):
        Database.db.clear_db()
        test_username = "******"
        invalid_user = "******"
        invalid_user2 = 'abc'

        # Add a new valid user and expect no problems
        self.assertTrue(UsersController.add_user(test_username))
        user = Database.db.lookup_user(test_username)
        self.assertEqual(test_username, user.name)

        # Add invalid characters username
        with self.assertRaises(Exception) as cm:
            UsersController.add_user(invalid_user)
        exception = cm.exception
        self.assertIn("Invalid characters", exception.message)

        # Add invalid length username
        with self.assertRaises(Exception) as cm:
            UsersController.add_user(invalid_user2)
        exception = cm.exception
        self.assertIn("Invalid length", exception.message)

        # Add an existing user
        with self.assertRaises(Exception) as cm:
            UsersController.add_user(test_username)
        exception = cm.exception
        self.assertIn("already exists", exception.message)
Exemplo n.º 5
0
    def test_display_feed(self):
        Database.db.clear_db()
        valid_card = "4111111111111111"
        valid_card = "4111111111111111"
        note = "This is a test note"
        valid_amount = '$66.60'

        actor_username = "******"
        target_username = '******'
        dne_username = '******'

        UsersController.add_user(actor_username)
        UsersController.add_user(target_username)
        UsersController.add_credit_card(actor_username, valid_card)
        TransactionsController.create_transaction(
            actor_username, target_username, valid_amount, note
        )

        # Display feed should return true for both users
        # If True, the feeds get printed to the screen
        self.assertTrue(
            TransactionsController.display_feed(actor_username)
        )
        self.assertTrue(
            TransactionsController.display_feed(target_username)
        )

        # Nonexistant user trying to get feed
        with self.assertRaises(Exception) as cm:
            TransactionsController.display_feed(dne_username)
        exception = cm.exception
        self.assertIn("doesn't exist", exception.message)
Exemplo n.º 6
0
    def test_add_credit_card(self):
        Database.db.clear_db()
        valid_card = "4111111111111111"
        invalid_card = "A12345678910111213"

        test_username = "******"
        test_username2 = 'test_user'
        dne_username = '******'

        UsersController.add_user(test_username)
        UsersController.add_user(test_username2)
        user1 = Database.db.lookup_user(test_username)
        user2 = Database.db.lookup_user(test_username2)

        # Add invalid card to existing user
        with self.assertRaises(Exception) as cm:
            UsersController.add_credit_card(test_username, invalid_card)
        exception = cm.exception
        self.assertEqual("ERROR: This card is invalid", exception.message)
        self.assertNotEqual(user1.card_number, invalid_card)

        # Add valid card to existing user
        self.assertTrue(UsersController.add_credit_card(test_username,
                                                        valid_card))
        self.assertEqual(user1.card_number, valid_card)

        # Add a valid card to a user that already has a card
        with self.assertRaises(Exception) as cm:
            UsersController.add_credit_card(test_username, valid_card)
        exception = cm.exception
        self.assertEqual(
            "ERROR: this user already has a valid credit card",
            exception.message
        )

        # Add valid card that belongs to someone else
        with self.assertRaises(Exception) as cm:
            UsersController.add_credit_card(test_username2, valid_card)
        exception = cm.exception
        self.assertEqual(
            ("ERROR: that card number has already been added by"
             " another user, reported for fraud!"),
            exception.message
        )
        self.assertNotEqual(user2.card_number, valid_card)

        # Add a valid card to a user that isn't in the database
        with self.assertRaises(Exception) as cm:
            UsersController.add_credit_card(dne_username, valid_card)
        exception = cm.exception
        self.assertIn("doesn't exist", exception.message)
Exemplo n.º 7
0
    def test_add_credit_card(self):
        Database.db.clear_db()
        valid_card = "4111111111111111"
        invalid_card = "A12345678910111213"

        test_username = "******"
        test_username2 = 'test_user'
        dne_username = '******'

        UsersController.add_user(test_username)
        UsersController.add_user(test_username2)
        user1 = Database.db.lookup_user(test_username)
        user2 = Database.db.lookup_user(test_username2)

        # Add invalid card to existing user
        with self.assertRaises(Exception) as cm:
            UsersController.add_credit_card(test_username, invalid_card)
        exception = cm.exception
        self.assertEqual("ERROR: This card is invalid", exception.message)
        self.assertNotEqual(user1.card_number, invalid_card)

        # Add valid card to existing user
        self.assertTrue(
            UsersController.add_credit_card(test_username, valid_card))
        self.assertEqual(user1.card_number, valid_card)

        # Add a valid card to a user that already has a card
        with self.assertRaises(Exception) as cm:
            UsersController.add_credit_card(test_username, valid_card)
        exception = cm.exception
        self.assertEqual("ERROR: this user already has a valid credit card",
                         exception.message)

        # Add valid card that belongs to someone else
        with self.assertRaises(Exception) as cm:
            UsersController.add_credit_card(test_username2, valid_card)
        exception = cm.exception
        self.assertEqual(("ERROR: that card number has already been added by"
                          " another user, reported for fraud!"),
                         exception.message)
        self.assertNotEqual(user2.card_number, valid_card)

        # Add a valid card to a user that isn't in the database
        with self.assertRaises(Exception) as cm:
            UsersController.add_credit_card(dne_username, valid_card)
        exception = cm.exception
        self.assertIn("doesn't exist", exception.message)
Exemplo n.º 8
0
    def test_create_transaction(self):
        Database.db.clear_db()
        valid_card = "4111111111111111"
        invalid_card = "12345678910111213"
        note = "This is a test note"
        valid_amount = '$66.60'
        invalid_amount = '66F.60'
        invalid_amount2 = '$-66.60'

        actor_username = "******"
        target_username = '******'
        dne_username = '******'

        UsersController.add_user(actor_username)
        UsersController.add_user(target_username)
        UsersController.add_credit_card(actor_username, valid_card)

        # A valid transaction. Check to see the balance
        # incremented  Also check to see that the transaction
        # is stored for both users
        target = Database.db.lookup_user(target_username)
        old_balance = target.balance
        self.assertTrue(
            TransactionsController.create_transaction(
                actor_username, target_username, valid_amount, note
            )
        )
        float_amount = float(valid_amount.split('$')[-1])
        self.assertEqual(target.balance-old_balance, float_amount)

        actor_trans, target_trans = tuple(map(
            Database.db.lookup_transactions,
            [actor_username, target_username]
        ))

        total_transactions = list(set(actor_trans).union(set(target_trans)))
        transaction = list({
            transaction for transaction in total_transactions
            if all(
                [
                    transaction.actor == actor_username,
                    transaction.target == target_username,
                    transaction.amount == float_amount,
                    transaction.note == note
                ]
            )})
        # Checking to see that the transaction exists for both users
        self.assertIn(transaction[0], actor_trans)
        self.assertIn(transaction[0], target_trans)

        # Nonexistant user trying to pay
        with self.assertRaises(Exception) as cm:
            TransactionsController.create_transaction(
                dne_username, target_username, valid_amount, note
            )
        exception = cm.exception
        self.assertIn("doesn't exist", exception.message)

        # Paying themselves
        with self.assertRaises(Exception) as cm:
            TransactionsController.create_transaction(
                actor_username, actor_username, valid_amount, note
            )
        exception = cm.exception
        self.assertEqual(exception.message, "ERROR: users cannot pay themselves")

        # Paying without credit card
        with self.assertRaises(Exception) as cm:
            TransactionsController.create_transaction(
                target_username, actor_username, valid_amount, note
            )
        exception = cm.exception
        self.assertEqual(exception.message, "ERROR: this user does not have a credit card")

        # invalid amount passed
        with self.assertRaises(Exception) as cm:
            TransactionsController.create_transaction(
                actor_username, target_username, invalid_amount, note
            )
        exception = cm.exception
        self.assertEqual(exception.message, "ERROR: Invalid Amount Entered")

        # a negative amount passed
        with self.assertRaises(Exception) as cm:
            TransactionsController.create_transaction(
                actor_username, target_username, invalid_amount2, note
            )
        exception = cm.exception
        self.assertEqual(exception.message, "ERROR: Invalid Amount Entered")