Exemplo n.º 1
0
def updateConsumer(admin, id):
    data = json_body()

    # Get corresponding consumer from the backend
    apiconsumer = api.get_consumer(id=id)

    # If roles are to be set, it must be ensured that the consumer
    # has already stored access data.
    if 'adminroles' in data and not apiconsumer.hasCredentials:
        raise exc.ConsumerNeedsCredentials

    # Create updateconsumer object
    updateconsumer = models.Consumer(id=id)

    # Handle new password
    if 'password' in data:
        if 'repeatpassword' not in data:
            raise exc.MissingData
        if not data['password'] == data['repeatpassword']:
            raise exc.PasswordsDoNotMatch
        if not apiconsumer.email:
            if 'email' not in data:
                raise exc.MissingData
        _pwhash = bcrypt.generate_password_hash(data['password'])
        del data['password']
        del data['repeatpassword']
        data['password'] = _pwhash

    # Check forbidden keys
    for key in ['credit', 'id']:
        if key in data:
            raise exc.ForbiddenField(key)

    # Handle adminroles
    if 'adminroles' in data:
        api_adminroles = api.getAdminroles(apiconsumer)
        for dep_id in data['adminroles'].keys():
            # Check if the consumer is already an administrator
            # for this department
            for api_role in api_adminroles:
                if api_role.department_id == int(dep_id):
                    # If the consumer is admin and the value is true, we can
                    # skip this modification
                    if data['adminroles'][dep_id]:
                        continue
            department = api.get_department(int(dep_id))
            api.setAdmin(apiconsumer, department, data['adminroles'][dep_id])

        del data['adminroles']

    # Handle remaining update data
    for key, value in data.items():
        setattr(updateconsumer, key, value)

    # Update consumer
    api.update_consumer(updateconsumer)

    return jsonify(result=True), 200
Exemplo n.º 2
0
    def test_update_consumer(self):
        consumer = self.api.get_consumer(id=1)
        self.assertEqual(consumer.name, 'William Jones')
        self.assertEqual(consumer.karma, 0)
        consumer = models.Consumer(id=1)
        self.api.update_consumer(consumer)
        consumer = self.api.get_consumer(id=1)
        self.assertEqual(consumer.name, 'William Jones')
        consumer = models.Consumer(id=1, karma=10)
        self.api.update_consumer(consumer)
        consumer = self.api.get_consumer(id=1)
        self.assertEqual(consumer.karma, 10)

        # test karmahistory
        consumer = models.Consumer(id=1, karma=9)
        self.api.update_consumer(consumer)

        consumer = models.Consumer(id=1, karma=8)
        self.api.update_consumer(consumer)

        consumer = models.Consumer(id=1, karma=7)
        self.api.update_consumer(consumer)

        c = models.Consumer(id=1, credit=1337)
        with self.assertRaises(exc.ForbiddenField):
            self.api.update_consumer(c)
        consumer = self.api.get_consumer(id=1)
        self.assertEqual(consumer.credit, 0)

        c = models.Consumer(active=False)
        with self.assertRaises(exc.FieldIsNone):
            self.api.update_consumer(c)
Exemplo n.º 3
0
def listConsumers(token):
    consumers = api.list_consumers()

    if token:
        consumers = list(map(validation.to_dict, consumers))
        for consumer in consumers:
            del consumer['password']
            cons = models.Consumer(id=consumer['id'])
            adminroles = list(map(validation.to_dict, api.getAdminroles(cons)))
            consumer['adminroles'] = adminroles

        return jsonify(consumers)

    return jsonify(
        convertMinimal(consumers,
                       ['name', 'id', 'active', 'isAdmin', 'hasCredentials']))
Exemplo n.º 4
0
    def test_insert_consumer(self):
        # insert correctly
        c = models.Consumer(name='Hans Müller', email='*****@*****.**')
        self.api.insert_consumer(c)
        consumer = self.api.get_consumer(id=5)
        self.assertEqual(consumer.name, 'Hans Müller')
        self.assertEqual(consumer.email, '*****@*****.**')
        self.assertEqual(consumer.credit, 0)
        self.assertEqual(consumer.karma, 0)
        self.assertTrue(consumer.active)

        # insert second consumer
        c = models.Consumer(name='Peter Meier', email='*****@*****.**')
        with self.assertRaises(exc.DuplicateObject):
            self.api.insert_consumer(c)

        # missing fields
        with self.assertRaises(exc.FieldIsNone):
            c = models.Consumer()
            self.api.insert_consumer(c)

        # insert wrong types
        with self.assertRaises(exc.WrongType):
            c = models.Consumer(name=2)

        # id should be forbidden
        c = models.Consumer(id=13, name='Hans')
        with self.assertRaises(exc.ForbiddenField):
            self.api.insert_consumer(c)

        # credit should be forbidden
        c = models.Consumer(name='Hans', credit=12)
        with self.assertRaises(exc.ForbiddenField):
            self.api.insert_consumer(c)

        # duplicate names should be rejected
        c = models.Consumer(name='Hans Müller')
        with self.assertRaises(exc.DuplicateObject):
            self.api.insert_consumer(c)
Exemplo n.º 5
0
    def test_adminroles(self):
        consumer = self.api.get_consumer(id=3)
        department = self.api.get_department(id=1)

        # Make sure, that the consumer is no admin
        adminroles = self.api.getAdminroles(consumer)
        self.assertEqual(len(adminroles), 0)

        # Make sure, that the consumer needs email and password to be admin
        with self.assertRaises(exc.ConsumerNeedsCredentials):
            self.api.setAdmin(consumer, department, True)

        # Update the consumer, so he can be admin
        upconsumer = models.Consumer(id=consumer.id)
        upconsumer.email = '*****@*****.**'
        upconsumer.password = '******'.encode()

        self.api.update_consumer(upconsumer)
        consumer = self.api.get_consumer(id=3)

        # Make consumer admin for department 1
        self.api.setAdmin(consumer, department, True)
        adminroles = self.api.getAdminroles(consumer)
        self.assertEqual(adminroles[0].department_id, 1)
        self.assertEqual(len(adminroles), 1)

        # Make consumer admin for the same department. Nothing should happen
        self.api.setAdmin(consumer, department, True)
        adminroles = self.api.getAdminroles(consumer)
        self.assertEqual(adminroles[0].department_id, 1)
        self.assertEqual(len(adminroles), 1)

        # Delete consumer admin role for department 1
        self.api.setAdmin(consumer, department, False)
        adminroles = self.api.getAdminroles(consumer)
        self.assertEqual(len(adminroles), 0)
Exemplo n.º 6
0
    def setUp(self):
        app, api = set_app(config.UnittestConfig)
        self.client = self.app.test_client()
        api.create_tables()
        self.api = api
        self.bcrypt = bcrypt

        # Create default consumers
        names = ['William Jones', 'Mary Smith', 'Bryce Jones', 'Daniel Lee']
        for name in names:
            consumer = models.Consumer(name=name)
            self.api.insert_consumer(consumer)

        # Create default departments
        names = ['Drinks', 'Sweets', 'Food']
        for name in names:
            department = models.Department(name=name, budget=20000)
            self.api.insert_department(department)

        # Create default products
        products = [{
            'name': 'Coffee',
            'department_id': 1,
            'price': 25
        }, {
            'name': 'Twix',
            'department_id': 2,
            'price': 100
        }, {
            'name': 'Pizza',
            'department_id': 3,
            'price': 400
        }]
        for product in products:
            p = models.Product(name=product['name'],
                               countable=True,
                               price=product['price'],
                               revocable=True,
                               department_id=product['department_id'])
            self.api.insert_product(p)

        self.consumerpasswords = ['secret1', 'secret2', 'secret3', 'secret4']
        self.consumeremails = [
            '*****@*****.**',
            '*****@*****.**',
            '*****@*****.**',
            '*****@*****.**',
        ]

        pwds = generate_passwords(self.consumerpasswords)
        c = models.Consumer(id=1)
        c.email = self.consumeremails[0]
        c.password = pwds[0]
        self.api.update_consumer(c)

        c = models.Consumer(id=2)
        c.email = self.consumeremails[1]
        c.password = pwds[1]
        self.api.update_consumer(c)

        consumer = self.api.get_consumer(id=1)
        department = self.api.get_department(id=1)
        self.api.setAdmin(consumer, department, True)
Exemplo n.º 7
0
def insertConsumer(admin):
    c = models.Consumer(**json_body())
    api.insert_consumer(c)
    return jsonify(result='created'), 201
Exemplo n.º 8
0
    def test_insert_purchase(self):
        department = self.api.get_department(id=1)
        self.assertEqual(department.id, 1)
        self.assertEqual(department.name, "Drinks")
        self.assertEqual(department.income_base, 0)
        self.assertEqual(department.income_karma, 0)
        self.assertEqual(department.expenses, 0)
        self.assertEqual(department.budget, 20000)

        # insert consumer and product
        c2 = models.Consumer(id=2, karma=10)
        c3 = models.Consumer(id=3, karma=-10)

        self.api.update_consumer(c2)
        self.api.update_consumer(c3)

        # check consumers
        consumers = self.api.list_consumers()
        self.assertEqual(len(consumers), 4)
        self.assertEqual(consumers[0].credit, 0)
        self.assertEqual(consumers[1].credit, 0)
        self.assertEqual(consumers[2].credit, 0)
        self.assertEqual(consumers[3].credit, 0)

        self.assertEqual(consumers[0].karma, 0)
        self.assertEqual(consumers[1].karma, 10)
        self.assertEqual(consumers[2].karma, -10)
        self.assertEqual(consumers[3].karma, 0)

        # check, if the objects are correct
        product = self.api.get_product(id=1)
        self.assertEqual(product.price, 25)

        # Turn on use karma
        self.api.configuration['USE_KARMA'] = True

        pur1 = models.Purchase(consumer_id=1,
                               product_id=1,
                               amount=1,
                               comment="good dude buys something")

        pur2 = models.Purchase(consumer_id=2,
                               product_id=1,
                               amount=1,
                               comment="awesome dude buys something")

        pur3 = models.Purchase(consumer_id=3,
                               product_id=1,
                               amount=1,
                               comment="bad dude buys something")

        self.api.insert_purchase(pur1)
        self.api.insert_purchase(pur2)
        self.api.insert_purchase(pur3)

        # get purchases
        purchases = self.api.list_purchases()
        self.assertEqual(len(purchases), 3)

        self.assertEqual(purchases[0].paid_base_price_per_product, 25)
        self.assertEqual(purchases[0].paid_karma_per_product, 5)
        self.assertEqual(purchases[0].amount, 1)
        self.assertEqual(purchases[0].comment, "good dude buys something")
        self.assertIsNotNone(purchases[0].timestamp)

        self.assertEqual(purchases[1].paid_base_price_per_product, 25)
        self.assertEqual(purchases[1].paid_karma_per_product, 0)
        self.assertEqual(purchases[1].amount, 1)
        self.assertEqual(purchases[1].comment, "awesome dude buys something")
        self.assertIsNotNone(purchases[1].timestamp)

        self.assertEqual(purchases[2].paid_base_price_per_product, 25)
        self.assertEqual(purchases[2].paid_karma_per_product, 10)
        self.assertEqual(purchases[2].amount, 1)
        self.assertEqual(purchases[2].comment, "bad dude buys something")
        self.assertIsNotNone(purchases[2].timestamp)

        # check consumers
        consumers = self.api.list_consumers()
        self.assertEqual(len(consumers), 4)
        self.assertEqual(consumers[0].credit, -30)
        self.assertEqual(consumers[1].credit, -25)
        self.assertEqual(consumers[2].credit, -35)
        self.assertEqual(consumers[3].credit, 0)

        department = self.api.get_department(id=1)
        self.assertEqual(department.id, 1)
        self.assertEqual(department.name, "Drinks")
        self.assertEqual(department.income_base, 75)
        self.assertEqual(department.income_karma, 15)
        self.assertEqual(department.expenses, 0)
        self.assertEqual(department.budget, 20000)

        # now we revoke the purchases
        pur = models.Purchase(id=1, revoked=True)
        self.api.update_purchase(pur)
        department = self.api.get_department(id=1)
        self.assertEqual(department.income_base, 50)
        self.assertEqual(department.income_karma, 10)

        pur = models.Purchase(id=2, revoked=True)
        self.api.update_purchase(pur)
        department = self.api.get_department(id=1)
        self.assertEqual(department.income_base, 25)
        self.assertEqual(department.income_karma, 10)

        pur = models.Purchase(id=3, revoked=True)
        self.api.update_purchase(pur)
        department = self.api.get_department(id=1)
        self.assertEqual(department.income_base, 0)
        self.assertEqual(department.income_karma, 0)

        # test with wrong foreign key consumer_id
        pur4 = models.Purchase(consumer_id=5,
                               product_id=1,
                               amount=1,
                               comment="purchase done by unittest")
        with self.assertRaises(exc.ForeignKeyNotExisting):
            self.api.insert_purchase(pur4)

        # no new purchase should have been created
        self.assertEqual(len(self.api.list_purchases()), 3)

        # test with wrong foreign key product_id
        pur5 = models.Purchase(consumer_id=1,
                               product_id=4,
                               amount=1,
                               comment="purchase done by unittest")
        with self.assertRaises(exc.ForeignKeyNotExisting):
            self.api.insert_purchase(pur5)

        # no new purchase should have been created
        self.assertEqual(len(self.api.list_purchases()), 3)

        # the credit of the consumer must not have to be changed
        consumer = self.api.get_consumer(id=1)
        self.assertEqual(consumer.credit, 0)

        # purchase.id should be forbidden
        pur6 = models.Purchase(consumer_id=1,
                               product_id=1,
                               id=1337,
                               amount=1,
                               comment="purchase done by unittest")
        with self.assertRaises(exc.ForbiddenField):
            self.api.insert_purchase(pur6)

        # purchase.paid_base_price_per_product and paid_karma_per_product
        # should be forbidden
        pur7 = models.Purchase(consumer_id=1,
                               product_id=1,
                               paid_base_price_per_product=200,
                               paid_karma_per_product=200,
                               amount=1,
                               comment="purchase done by unittest")
        with self.assertRaises(exc.ForbiddenField):
            self.api.insert_purchase(pur7)

        # purchase.revoked should be forbidden
        pur8 = models.Purchase(consumer_id=1,
                               product_id=1,
                               revoked=True,
                               amount=1,
                               comment="purchase done by unittest")
        with self.assertRaises(exc.ForbiddenField):
            self.api.insert_purchase(pur8)

        # purchase.revoked should be forbidden
        pur9 = models.Purchase(consumer_id=1,
                               product_id=1,
                               amount=1,
                               timestamp=datetime.datetime.now(),
                               comment="purchase done by unittest")
        with self.assertRaises(exc.ForbiddenField):
            self.api.insert_purchase(pur9)
Exemplo n.º 9
0
    def test_create_deposit(self):
        # Make consumer 1 administrator
        upconsumer = models.Consumer(id=1)
        upconsumer.email = '*****@*****.**'
        upconsumer.password = '******'.encode()
        self.api.update_consumer(upconsumer)

        admin = self.api.get_consumer(id=1)
        departments = self.api.list_departments()
        self.api.setAdmin(admin, departments[0], True)

        # check the consumers credit
        consumer = self.api.get_consumer(1)
        self.assertEqual(consumer.credit, 0)

        # create deposit
        dep = models.Deposit(consumer_id=1, amount=250, comment="testcomment")
        self.api.insert_deposit(dep)

        # check, if the consumers credit has been increased
        consumer = self.api.get_consumer(id=1)
        self.assertEqual(consumer.credit, 250)

        # check the results
        deposit = self.api.get_deposit(id=1)
        self.assertEqual(deposit.amount, 250)
        self.assertEqual(deposit.comment, "testcomment")
        self.assertEqual(deposit.consumer_id, consumer.id)

        # revoke deposit
        dep = models.Deposit(id=1, revoked=True)
        self.api.update_deposit(dep, admin)

        deposits = self.api.list_deposits()
        self.assertEqual(len(deposits), 1)
        self.assertTrue(deposits[0].revoked)
        consumer = self.api.get_consumer(id=1)
        self.assertEqual(consumer.credit, 0)

        # Un-revoke deposit again to make sure this works too
        dep = models.Deposit(id=1, revoked=False)
        self.api.update_deposit(dep, admin)
        consumer = self.api.get_consumer(id=1)
        self.assertEqual(consumer.credit, 250)

        # Check revokehistory
        deposits = self.api.list_deposits()
        self.assertEqual(len(deposits), 1)
        self.assertFalse(deposits[0].revoked)
        self.assertEqual(len(deposits[0].revoke_history), 2)
        self.assertTrue(deposits[0].revoke_history[0].revoked)
        self.assertFalse(deposits[0].revoke_history[1].revoked)

        # test with wrong foreign_key consumer_id
        dep = models.Deposit(consumer_id=5, amount=240, comment="testcomment")
        with self.assertRaises(exc.ForeignKeyNotExisting):
            self.api.insert_deposit(dep)

        # deposit.id should be forbidden
        dep = models.Deposit(consumer_id=2,
                             amount=20,
                             id=12,
                             comment="testcomment")
        with self.assertRaises(exc.ForbiddenField):
            self.api.insert_deposit(dep)

        # deposit.timestamp should be forbidden
        dep = models.Deposit(consumer_id=2,
                             amount=20,
                             comment="testcomment",
                             timestamp=datetime.datetime.now())
        with self.assertRaises(exc.ForbiddenField):
            self.api.insert_deposit(dep)
Exemplo n.º 10
0
    def test_create_payoff(self):

        # Check bank balance
        bank = self.api.get_bank()
        self.assertEqual(bank.credit, 0)

        # Check department expenses
        departments = self.api.list_departments()
        self.assertEqual(departments[0].expenses, 0)
        self.assertEqual(departments[1].expenses, 0)
        self.assertEqual(departments[2].expenses, 0)

        # Check payoffs
        payoffs = self.api.list_payoffs()
        self.assertEqual(len(payoffs), 0)

        # Make consumer 2 administrator for department 2
        upconsumer = models.Consumer(id=2)
        upconsumer.email = '*****@*****.**'
        upconsumer.password = '******'.encode()
        self.api.update_consumer(upconsumer)
        consumer = self.api.get_consumer(upconsumer.id)
        self.api.setAdmin(consumer, departments[1], True)

        # Check consumer admin states
        consumers = self.api.list_consumers()
        self.assertEqual(len(consumers), 4)
        self.assertTrue(consumers[0].isAdmin)
        self.assertTrue(consumers[1].isAdmin)
        self.assertFalse(consumers[2].isAdmin)
        self.assertFalse(consumers[3].isAdmin)

        # Check adminroles of consumer 1
        adminroles = self.api.getAdminroles(consumers[0])
        self.assertEqual(len(adminroles), 1)
        self.assertEqual(adminroles[0].consumer_id, consumers[0].id)
        self.assertEqual(adminroles[0].department_id, departments[0].id)

        # Check adminroles of consumer 2
        adminroles = self.api.getAdminroles(consumers[1])
        self.assertEqual(len(adminroles), 1)
        self.assertEqual(adminroles[0].consumer_id, consumers[1].id)
        self.assertEqual(adminroles[0].department_id, departments[1].id)

        # Consumer No. 1 is administrator for department 1 and should
        # therefore be able to insert a payoff
        payoff_1 = models.Payoff(department_id=1,
                                 comment='Should work',
                                 amount=10000,
                                 admin_id=1)
        self.api.insert_payoff(payoff_1)

        # Now it is necessary to check whether even a payoff and an admin
        # event have been created and whether all values are set correctly.
        payoffs = self.api.list_payoffs()
        self.assertEqual(len(payoffs), 1)
        self.assertEqual(payoffs[0].department_id, payoff_1.department_id)
        self.assertEqual(payoffs[0].comment, payoff_1.comment)
        self.assertEqual(payoffs[0].amount, payoff_1.amount)

        # Check bank balance
        bank = self.api.get_bank()
        self.assertEqual(bank.credit, -10000)

        # Check that the expenses of the departments are correct
        departments = self.api.list_departments()
        self.assertEqual(departments[0].expenses, 10000)
        self.assertEqual(departments[1].expenses, 0)
        self.assertEqual(departments[2].expenses, 0)

        # Try to set revoke to false, this should fail
        p = models.Payoff(id=1, revoked=False, admin_id=1)
        with self.assertRaises(exc.NothingHasChanged):
            self.api.update_payoff(p)

        # revoke payoff
        p = models.Payoff(id=1, revoked=True, admin_id=1)
        self.api.update_payoff(p)

        bank = self.api.get_bank()
        self.assertEqual(bank.credit, 0)
        departments = self.api.list_departments()
        self.assertEqual(departments[0].expenses, 0)
        self.assertEqual(departments[1].expenses, 0)
        self.assertEqual(departments[2].expenses, 0)

        with self.assertRaises(exc.CanOnlyBeRevokedOnce):
            self.api.update_payoff(p)

        departments = self.api.list_departments()
        self.assertEqual(departments[0].expenses, 0)
        self.assertEqual(departments[1].expenses, 0)
        self.assertEqual(departments[2].expenses, 0)

        # Try to un-revoke payoff, this should fail
        p = models.Payoff(id=1, revoked=False, admin_id=1)
        with self.assertRaises(exc.RevokeIsFinal):
            self.api.update_payoff(p)

        departments = self.api.list_departments()
        self.assertEqual(departments[0].expenses, 0)
        self.assertEqual(departments[1].expenses, 0)
        self.assertEqual(departments[2].expenses, 0)
Exemplo n.º 11
0
    def test_departmentpurchase(self):
        # Make consumer 1 administrator
        upconsumer = models.Consumer(id=1)
        upconsumer.email = '*****@*****.**'
        upconsumer.password = '******'.encode()
        self.api.update_consumer(upconsumer)

        consumer = self.api.get_consumer(id=1)
        departments = self.api.list_departments()
        self.api.setAdmin(consumer, departments[0], True)

        self.assertEqual(departments[0].expenses, 0)
        self.assertEqual(departments[1].expenses, 0)
        self.assertEqual(departments[2].expenses, 0)

        product = self.api.get_product(id=1)
        self.assertEqual(product.stock, 0)

        # List departmentpurchasecollections
        dpcollections = self.api.list_departmentpurchasecollections()
        self.assertEqual(len(dpcollections), 0)

        # Create departmentpurchasecollection
        dpcollection = models.DepartmentpurchaseCollection()
        dpcollection.department_id = departments[0].id
        dpcollection.admin_id = consumer.id

        # Insert departmentpurchasecollection
        self.api.insert_departmentpurchasecollection(dpcollection)
        dpcollections = self.api.list_departmentpurchasecollections()
        self.assertEqual(len(dpcollections), 1)

        # Get last departmentpurchasecollection
        dpcollection = self.api.get_last_departmentpurchasecollection()
        self.assertIsNotNone(dpcollection)
        self.assertEqual(dpcollection.id, 1)
        self.assertFalse(dpcollection.revoked)

        # Insert departmentpurchase
        dpurchase = models.Departmentpurchase()
        dpurchase.collection_id = dpcollection.id
        dpurchase.product_id = product.id
        dpurchase.amount = 5
        dpurchase.total_price = 50

        self.api.insert_departmentpurchase(dpurchase)
        dpurchases = self.api.list_departmentpurchases(collection_id=1)
        self.assertEqual(len(dpurchases), 1)

        product = self.api.get_product(id=1)
        self.assertEqual(product.stock, dpurchase.amount)

        departments = self.api.list_departments()
        self.assertEqual(departments[0].expenses, 50)
        self.assertEqual(departments[1].expenses, 0)
        self.assertEqual(departments[2].expenses, 0)

        # Revoke departmentpurchasecollection
        dpcollection = models.DepartmentpurchaseCollection()
        dpcollection.id = 1
        dpcollection.revoked = True
        self.api.update_departmentpurchasecollection(dpcollection, consumer)

        dpcollection = self.api.get_departmentpurchasecollection(id=1)
        self.assertEqual(dpcollection.id, 1)
        self.assertTrue(dpcollection.revoked)
        self.assertEqual(len(dpcollection.revoke_history), 1)

        departments = self.api.list_departments()
        self.assertEqual(departments[0].expenses, 0)
        self.assertEqual(departments[1].expenses, 0)
        self.assertEqual(departments[2].expenses, 0)

        dpcollections = self.api.list_departmentpurchasecollections()
        self.assertEqual(len(dpcollections), 1)
        self.assertTrue(dpcollections[0].revoked)
        self.assertEqual(len(dpcollections[0].revoke_history), 1)

        product = self.api.get_product(id=1)
        self.assertEqual(product.stock, 0)

        department = self.api.get_department(id=1)
        self.assertEqual(department.expenses, 0)

        # Revoke again, this should do nothing
        dpcollection = models.DepartmentpurchaseCollection()
        dpcollection.id = 1
        dpcollection.revoked = True
        with self.assertRaises(exc.NothingHasChanged):
            self.api.update_departmentpurchasecollection(
                dpcollection, consumer)

        departments = self.api.list_departments()
        self.assertEqual(departments[0].expenses, 0)
        self.assertEqual(departments[1].expenses, 0)
        self.assertEqual(departments[2].expenses, 0)

        dpcollections = self.api.list_departmentpurchasecollections()
        self.assertEqual(len(dpcollections), 1)
        self.assertTrue(dpcollections[0].revoked)

        # Un-revoke dpcollection
        dpcollection = models.DepartmentpurchaseCollection()
        dpcollection.id = 1
        dpcollection.revoked = False
        self.api.update_departmentpurchasecollection(dpcollection, consumer)

        departments = self.api.list_departments()
        self.assertEqual(departments[0].expenses, 50)
        self.assertEqual(departments[1].expenses, 0)
        self.assertEqual(departments[2].expenses, 0)

        dpcollections = self.api.list_departmentpurchasecollections()
        self.assertEqual(len(dpcollections), 1)
        self.assertFalse(dpcollections[0].revoked)
        self.assertEqual(len(dpcollections[0].revoke_history), 2)
        self.assertTrue(dpcollections[0].revoke_history[0].revoked)
        self.assertFalse(dpcollections[0].revoke_history[1].revoked)

        # Check, weather an incorrect departmentpurchase causes a dead
        # departmentpurchasecollection

        dpcollection = models.DepartmentpurchaseCollection()
        dpcollection.department_id = departments[0].id
        dpcollection.admin_id = consumer.id
        self.api.insert_departmentpurchasecollection(dpcollection)
        # List departmentpurchasecollections
        dpcollections = self.api.list_departmentpurchasecollections()
        self.assertEqual(len(dpcollections), 2)
        # Get the last dpcollection
        dpcollection = self.api.get_last_departmentpurchasecollection()
        self.assertEqual(dpcollection.id, 2)

        # Insert defective departmentpurchase
        dpurchase = models.Departmentpurchase()
        dpurchase.collection_id = dpcollection.id
        dpurchase.product_id = product.id
        # amount is missing
        dpurchase.total_price = 50
        with self.assertRaises(exc.InvalidDepartmentpurchase):
            self.api.insert_departmentpurchase(dpurchase)

        dpcollections = self.api.list_departmentpurchasecollections()
        self.assertEqual(len(dpcollections), 1)