Exemplo n.º 1
0
    def test_picture(self):
        """ Test uploading a product picture through the admin interface. """
        self.add_product(self.TESTPRODUCT1, 1)

        # Test the default picture
        with app.test_request_context():
            app.preprocess_request()
            new_picture = streck.models.product.Product(
                self.TESTPRODUCT1['barcode']).picture()
        assert new_picture == '../img/NoneProduct.png'

        # Update a user
        rv = self.app.post('/admin/product/%s/update' %
                           self.TESTPRODUCT1['barcode'],
                           data=dict(picture=(StringIO(unhexlify(self.IMAGE)),
                                              'picture.png')),
                           buffered=True,
                           follow_redirects=True)
        with app.test_request_context():
            app.preprocess_request()
            new_picture = streck.models.product.Product(
                self.TESTPRODUCT1['barcode']).picture()
        assert new_picture != '../img/NoneProduct.png'

        # Compare the resulting picture
        rv = self.app.get('/images/%s' % new_picture)
        assert rv.status_code == 200
Exemplo n.º 2
0
    def test_create_product(self):
        """ Test creating products through the admin interface. """
        # Create product
        rv = self.add_product(self.TESTPRODUCT1, 1)
        assert b'Produkten "%s" tillagd.' % self.TESTPRODUCT1['name'] in rv.data
        with app.test_request_context():
            app.preprocess_request()
            product = streck.models.product.Product(self.TESTPRODUCT1['barcode'])
            assert product.exists()
            assert product.price() == self.TESTPRODUCT1['price']
            assert product.category() == (self.CATEGORIES[1]).decode('utf-8')

        # Create another product
        rv = self.add_product(self.TESTPRODUCT2, 2)
        assert b'Produkten "%s" tillagd.' % self.TESTPRODUCT2['name'] in rv.data
        with app.test_request_context():
            app.preprocess_request()
            product = streck.models.product.Product(self.TESTPRODUCT2['barcode'])
            assert product.exists()
            assert product.price() == self.TESTPRODUCT2['price']
            assert product.category() == (self.CATEGORIES[2]).decode('utf-8')

        # Create already existing product
        rv = self.add_product(self.TESTPRODUCT1, 1)
        assert b'Produktens ID är ej unikt!' in rv.data

        # Create already existing product with different category
        rv = self.add_product(self.TESTPRODUCT1, 2)
        assert b'Produktens ID är ej unikt!' in rv.data
Exemplo n.º 3
0
    def test_create_product(self):
        """ Test creating products through the admin interface. """
        # Create product
        rv = self.add_product(self.TESTPRODUCT1, 1)
        assert b'Produkten "%s" tillagd.' % self.TESTPRODUCT1[
            'name'] in rv.data
        with app.test_request_context():
            app.preprocess_request()
            product = streck.models.product.Product(
                self.TESTPRODUCT1['barcode'])
            assert product.exists()
            assert product.price() == self.TESTPRODUCT1['price']
            assert product.category() == (self.CATEGORIES[1]).decode('utf-8')

        # Create another product
        rv = self.add_product(self.TESTPRODUCT2, 2)
        assert b'Produkten "%s" tillagd.' % self.TESTPRODUCT2[
            'name'] in rv.data
        with app.test_request_context():
            app.preprocess_request()
            product = streck.models.product.Product(
                self.TESTPRODUCT2['barcode'])
            assert product.exists()
            assert product.price() == self.TESTPRODUCT2['price']
            assert product.category() == (self.CATEGORIES[2]).decode('utf-8')

        # Create already existing product
        rv = self.add_product(self.TESTPRODUCT1, 1)
        assert b'Produktens ID är ej unikt!' in rv.data

        # Create already existing product with different category
        rv = self.add_product(self.TESTPRODUCT1, 2)
        assert b'Produktens ID är ej unikt!' in rv.data
Exemplo n.º 4
0
    def test_undo_disabled(self):
        """ Test undoing a transaction using a disabled user. """
        # Hack a product into the database so we have something to undo
        with app.test_request_context():
            app.preprocess_request()
            streck.models.transaction.Transaction('jane', '0012345678905', 5.0).perform()
            assert streck.models.user.User('jane').debt() == 5.0

        # Try to undo it
        self.app.post('/user/jane/buy', data=dict(barcode=app.config['UNDO_BARCODE']), follow_redirects=True)
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User('jane').debt() == 5.0
Exemplo n.º 5
0
    def test_paid_disabled(self):
        """ Test resetting the debt of a disabled user. """
        # Make sure we have a debt
        with app.test_request_context():
            app.preprocess_request()
            streck.models.transaction.Transaction('jane', '0012345678905', 5.0).perform()
            streck.models.transaction.Transaction('jane', '0012345678905', 5.0).perform()
            assert streck.models.user.User('jane').debt() == 10.0

        # Test paying off the debt
        self.app.post('/user/jane/buy', data=dict(barcode=app.config['PAID_BARCODE']), follow_redirects=True)
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User('jane').debt() == 0.0
Exemplo n.º 6
0
    def test_undo_after_paid(self):
        """ Test undoing a transaction when the last one was a debt reset. """
        # Make sure we have a debt, and that it's paid
        with app.test_request_context():
            app.preprocess_request()
            streck.models.transaction.Transaction('john', '0012345678905', 5.0).perform()
            streck.models.transaction.Transaction('john', '0012345678905', 5.0).perform()
            streck.models.transaction.Transaction('john', paid=True).perform()
            assert streck.models.user.User('john').last_paid_id() != -1
            assert streck.models.user.User('john').debt() == 0.0

        # Try to undo
        self.app.post('/user/john/buy', data=dict(barcode=app.config['UNDO_BARCODE']), follow_redirects=True)
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User('john').debt() == 10.0 # by design apparently?
Exemplo n.º 7
0
 def test_undo_empty(self):
     """ Test undoing the last transaction when there isn't one. """
     # Try to undo it
     self.app.post('/user/john/buy', data=dict(barcode=app.config['UNDO_BARCODE']), follow_redirects=True)
     with app.test_request_context():
         app.preprocess_request()
         assert streck.models.user.User('john').debt() == 0.0
Exemplo n.º 8
0
    def test_undo_disabled(self):
        """ Test undoing a transaction using a disabled user. """
        # Hack a product into the database so we have something to undo
        with app.test_request_context():
            app.preprocess_request()
            streck.models.transaction.Transaction('jane', '0012345678905',
                                                  5.0).perform()
            assert streck.models.user.User('jane').debt() == 5.0

        # Try to undo it
        self.app.post('/user/jane/buy',
                      data=dict(barcode=app.config['UNDO_BARCODE']),
                      follow_redirects=True)
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User('jane').debt() == 5.0
Exemplo n.º 9
0
    def test_top_user_total(self):
        """ Tests :meth:Stats.top_user_total and :meth:Stats.toplist_user_alltime. """
        self.do_transactions(20, self.U_J, self.P_A) # Jobbmat, not counted
        self.do_transactions(2,  self.U_B, self.P_A)
        self.do_transactions(10, self.U_A, self.P_A)

        # Make sure the total top user is correct
        with app.test_request_context():
            app.preprocess_request()
            expect_name = self.U_A.name()
            top_name, total = streck.models.stats.Stats.top_user_total()
            assert expect_name == top_name
            assert total == 10.0
            toplist = streck.models.stats.Stats.toplist_user_alltime()
            assert [v[0] for v in toplist] == [self.U_J.name(), self.U_A.name(), self.U_B.name()]
            assert [v[1] for v in toplist] == [20, 10, 2]
            assert [v[2] for v in toplist] == [20.0, 10.0, 2.0]

        # Make sure it's still correct after a debt payment
        with app.test_request_context():
            app.preprocess_request()
            self.do_transactions(1, self.U_A, self.P_N, notes='paid', price=-self.U_A.debt())
            self.do_transactions(1, self.U_B, self.P_N, notes='paid', price=-self.U_B.debt())
        with app.test_request_context():
            app.preprocess_request()
            expect_name = self.U_A.name()
            top_name, total = streck.models.stats.Stats.top_user_total()
            assert expect_name == top_name
            assert total == 10.0
            toplist = streck.models.stats.Stats.toplist_user_alltime()
            assert [v[0] for v in toplist] == [self.U_J.name(), self.U_A.name(), self.U_B.name()]
            assert [v[1] for v in toplist] == [20, 10, 2]
            assert [v[2] for v in toplist] == [20.0, 10.0, 2.0]

        # And after more transactions
        self.do_transactions(10, self.U_B, self.P_A)
        self.do_transactions(1,  self.U_A, self.P_A)
        with app.test_request_context():
            app.preprocess_request()
            expect_name = self.U_B.name()
            top_name, total = streck.models.stats.Stats.top_user_total()
            assert expect_name == top_name
            assert total == 12.0
            toplist = streck.models.stats.Stats.toplist_user_alltime()
            assert [v[0] for v in toplist] == [self.U_J.name(), self.U_B.name(), self.U_A.name()]
            assert [v[1] for v in toplist] == [20, 12, 11]
            assert [v[2] for v in toplist] == [20.0, 12.0, 11.0]
Exemplo n.º 10
0
    def test_undo(self):
        """ Test undoing something. """
        # Hack a product into the database so we have something to undo
        with app.test_request_context():
            app.preprocess_request()
            streck.models.transaction.Transaction('john', '0012345678905', 5.0).perform()
            assert streck.models.user.User('john').debt() == 5.0

        # Make sure we can undo the transaction
        self.app.post('/user/john/buy', data=dict(barcode=app.config['UNDO_BARCODE']), follow_redirects=True)
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User('john').debt() == 0.0

        # Make sure it appears on the user page
        rv = self.app.get('/user/john', follow_redirects=True)
        assert b'The Product' not in rv.data
Exemplo n.º 11
0
    def test_update_product(self):
        """ Test updating products through the admin interface. """
        new_name = 'Cheaper Product'
        new_price = 0.5
        self.add_product(self.TESTPRODUCT1, 1)

        # Update product name
        rv = self.app.post('/admin/product/%s/update' %
                           self.TESTPRODUCT1['barcode'],
                           data=dict(name=new_name),
                           follow_redirects=True)
        assert new_name in rv.data
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.product.Product(
                self.TESTPRODUCT1['barcode']).name() == new_name

        # Update product price
        rv = self.app.post('/admin/product/%s/update' %
                           self.TESTPRODUCT1['barcode'],
                           data=dict(price=new_price),
                           follow_redirects=True)
        assert str(new_price) in rv.data
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.product.Product(
                self.TESTPRODUCT1['barcode']).price() == new_price

        # Update product category
        rv = self.app.post('/admin/product/%s/update' %
                           self.TESTPRODUCT1['barcode'],
                           data=dict(category=2),
                           follow_redirects=True)
        assert self.CATEGORIES[2] in rv.data
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.product.Product(
                self.TESTPRODUCT1['barcode']).category() == (
                    self.CATEGORIES[2]).decode('utf-8')

        # Update a nonexistent product
        rv = self.app.post('/admin/product/nothing/update',
                           data=dict(name='Irrelevant'),
                           follow_redirects=True)
        assert b'Produkten existerar inte!' in rv.data
Exemplo n.º 12
0
 def test_undo_empty(self):
     """ Test undoing the last transaction when there isn't one. """
     # Try to undo it
     self.app.post('/user/john/buy',
                   data=dict(barcode=app.config['UNDO_BARCODE']),
                   follow_redirects=True)
     with app.test_request_context():
         app.preprocess_request()
         assert streck.models.user.User('john').debt() == 0.0
Exemplo n.º 13
0
    def test_paid_disabled(self):
        """ Test resetting the debt of a disabled user. """
        # Make sure we have a debt
        with app.test_request_context():
            app.preprocess_request()
            streck.models.transaction.Transaction('jane', '0012345678905',
                                                  5.0).perform()
            streck.models.transaction.Transaction('jane', '0012345678905',
                                                  5.0).perform()
            assert streck.models.user.User('jane').debt() == 10.0

        # Test paying off the debt
        self.app.post('/user/jane/buy',
                      data=dict(barcode=app.config['PAID_BARCODE']),
                      follow_redirects=True)
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User('jane').debt() == 0.0
Exemplo n.º 14
0
    def test_undo(self):
        """ Test undoing something. """
        # Hack a product into the database so we have something to undo
        with app.test_request_context():
            app.preprocess_request()
            streck.models.transaction.Transaction('john', '0012345678905',
                                                  5.0).perform()
            assert streck.models.user.User('john').debt() == 5.0

        # Make sure we can undo the transaction
        self.app.post('/user/john/buy',
                      data=dict(barcode=app.config['UNDO_BARCODE']),
                      follow_redirects=True)
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User('john').debt() == 0.0

        # Make sure it appears on the user page
        rv = self.app.get('/user/john', follow_redirects=True)
        assert b'The Product' not in rv.data
Exemplo n.º 15
0
    def test_remove_not_allowed(self):
        """ Test removing something disallowed as jobbmat. """
        # Make sure we can't remove the product
        self.app.post('/user/%s/buy' % app.config['REMOVE_JOBBMAT_BARCODE'], data=dict(barcode=self.TESTPRODUCT_BAD['barcode']), follow_redirects=True)
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User(app.config['JOBBMAT_BARCODE']).debt() == 0.0

        # Make sure it appears on the user page
        rv = self.app.get('/user/%s' % app.config['JOBBMAT_BARCODE'], follow_redirects=True)
        assert self.TESTPRODUCT_BAD['name'] not in rv.data
Exemplo n.º 16
0
    def test_buy_disabled(self):
        """ Test buying something using a disabled user. """
        # Make sure we can't buy the product
        self.app.post('/user/jane/buy', data=dict(barcode='0012345678905'), follow_redirects=True)
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User('jane').debt() == 0.0

        # Make sure it appears on the user page
        rv = self.app.get('/user/jane', follow_redirects=True)
        assert b'The Product' not in rv.data
Exemplo n.º 17
0
    def setUp(self):
        """ Set up test case.

        Inserts a product and a user into the database
        """
        GenericStreckTestCase.setUp(self)
    	with app.test_request_context():
            app.preprocess_request()
            streck.models.product.Product.add('0012345678905', 'The Product', 5.0, 1, '../img/NoneProduct.png')
            streck.models.user.User.add('john', 'User 1', '../img/NoneUser.png')
            streck.models.user.User.add('jane', 'User 2', '../img/NoneUser.png').disable()
Exemplo n.º 18
0
    def setUp(self):
        """ Set up test case.

        Inserts two products into the database and sets the allowed jobbmat category
        """
        GenericStreckTestCase.setUp(self)
    	with app.test_request_context():
            app.preprocess_request()
            streck.models.product.Product.add(self.TESTPRODUCT_GOOD['barcode'], self.TESTPRODUCT_GOOD['name'], self.TESTPRODUCT_GOOD['price'], 2, '../img/NoneProduct.png')
            streck.models.product.Product.add(self.TESTPRODUCT_BAD['barcode'], self.TESTPRODUCT_BAD['name'], self.TESTPRODUCT_BAD['price'], 1, '../img/NoneProduct.png')
        app.config['ALLOWED_JOBBMAT_CATEGORIES'] = [(self.CATEGORIES[2]).decode('utf-8')]
Exemplo n.º 19
0
    def test_buy_allowed(self):
        """ Test booking something allowed as jobbmat. """
        # Make sure we can buy the product
        self.app.post('/user/%s/buy' % app.config['JOBBMAT_BARCODE'], data=dict(barcode=self.TESTPRODUCT_GOOD['barcode']), follow_redirects=True)
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User(app.config['JOBBMAT_BARCODE']).debt() == self.TESTPRODUCT_GOOD['price']

        # Make sure it appears on the user page
        rv = self.app.get('/user/%s' % app.config['JOBBMAT_BARCODE'], follow_redirects=True)
        assert self.TESTPRODUCT_GOOD['name'] in rv.data
Exemplo n.º 20
0
    def test_user_disabling(self):
        """ Test disabling and enabling users through the admin interface. """
        self.add_user(self.TESTUSER)

        # Disable a user
        rv = self.app.get('/admin/user/%s/disable' % self.TESTUSER['barcode'],
                          follow_redirects=True)
        assert b'Användaren är nu avstängd!' in rv.data
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User(self.TESTUSER['barcode']).disabled()

        # Disable a user again
        rv = self.app.get('/admin/user/%s/disable' % self.TESTUSER['barcode'],
                          follow_redirects=True)
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User(self.TESTUSER['barcode']).disabled()

        # Enable a user
        rv = self.app.get('/admin/user/%s/enable' % self.TESTUSER['barcode'],
                          follow_redirects=True)
        assert b'Användaren är inte längre avstängd!' in rv.data
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User(self.TESTUSER['barcode']).enabled()

        # Enable a user again
        rv = self.app.get('/admin/user/%s/enable' % self.TESTUSER['barcode'],
                          follow_redirects=True)
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User(self.TESTUSER['barcode']).enabled()

        # Disable a nonexistent user
        rv = self.app.get('/admin/user/nobody/disable', follow_redirects=True)
        assert b'Användaren existerar inte!' in rv.data

        # Enable a nonexistent user
        rv = self.app.get('/admin/user/nobody/enable', follow_redirects=True)
        assert b'Användaren existerar inte!' in rv.data
Exemplo n.º 21
0
    def test_undo_after_paid(self):
        """ Test undoing a transaction when the last one was a debt reset. """
        # Make sure we have a debt, and that it's paid
        with app.test_request_context():
            app.preprocess_request()
            streck.models.transaction.Transaction('john', '0012345678905',
                                                  5.0).perform()
            streck.models.transaction.Transaction('john', '0012345678905',
                                                  5.0).perform()
            streck.models.transaction.Transaction('john', paid=True).perform()
            assert streck.models.user.User('john').last_paid_id() != -1
            assert streck.models.user.User('john').debt() == 0.0

        # Try to undo
        self.app.post('/user/john/buy',
                      data=dict(barcode=app.config['UNDO_BARCODE']),
                      follow_redirects=True)
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User(
                'john').debt() == 10.0  # by design apparently?
Exemplo n.º 22
0
    def test_create_user(self):
        """ Test creating users through the admin interface. """
        # Create user
        rv = self.add_user(self.TESTUSER)
        assert b'Användaren "%s" tillagd.' % self.TESTUSER['name'] in rv.data
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User(self.TESTUSER['barcode']).exists()

        # Create already existing user
        rv = self.add_user(self.TESTUSER)
        assert b'Användarens ID är ej unikt!' in rv.data
Exemplo n.º 23
0
    def test_remove_allowed(self):
        """ Test removing some jobbmat. """
        # Make sure we can remove the product
        self.app.post('/user/%s/buy' % app.config['REMOVE_JOBBMAT_BARCODE'], data=dict(barcode=self.TESTPRODUCT_GOOD['barcode']), follow_redirects=True)
        with app.test_request_context():
            app.preprocess_request()
            # Note negative sign. Removing even if there was no initial purchase is allowed by design
            assert streck.models.user.User(app.config['JOBBMAT_BARCODE']).debt() == -self.TESTPRODUCT_GOOD['price']

        # Make sure it appears on the user page
        rv = self.app.get('/user/%s' % app.config['JOBBMAT_BARCODE'], follow_redirects=True)
        assert self.TESTPRODUCT_GOOD['name'] in rv.data
Exemplo n.º 24
0
    def test_create_user(self):
        """ Test creating users through the admin interface. """
        # Create user
        rv = self.add_user(self.TESTUSER)
        assert b'Användaren "%s" tillagd.' % self.TESTUSER[
            'name'] in rv.data
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User(self.TESTUSER['barcode']).exists()

        # Create already existing user
        rv = self.add_user(self.TESTUSER)
        assert b'Användarens ID är ej unikt!' in rv.data
Exemplo n.º 25
0
    def test_picture(self):
        """ Test uploading a product picture through the admin interface. """
        self.add_product(self.TESTPRODUCT1, 1)

        # Test the default picture
        with app.test_request_context():
            app.preprocess_request()
            new_picture = streck.models.product.Product(self.TESTPRODUCT1['barcode']).picture()
        assert new_picture == '../img/NoneProduct.png'

        # Update a user
        rv = self.app.post('/admin/product/%s/update' % self.TESTPRODUCT1['barcode'], data=dict(
            picture=(StringIO(unhexlify(self.IMAGE)), 'picture.png')
        ), buffered=True, follow_redirects=True)
        with app.test_request_context():
            app.preprocess_request()
            new_picture = streck.models.product.Product(self.TESTPRODUCT1['barcode']).picture()
        assert new_picture != '../img/NoneProduct.png'

        # Compare the resulting picture
        rv = self.app.get('/images/%s' % new_picture)
        assert rv.status_code == 200
Exemplo n.º 26
0
    def test_buy_disabled(self):
        """ Test buying something using a disabled user. """
        # Make sure we can't buy the product
        self.app.post('/user/jane/buy',
                      data=dict(barcode='0012345678905'),
                      follow_redirects=True)
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User('jane').debt() == 0.0

        # Make sure it appears on the user page
        rv = self.app.get('/user/jane', follow_redirects=True)
        assert b'The Product' not in rv.data
Exemplo n.º 27
0
    def test_top_user_debt(self):
        """ Tests :meth:Stats.top_user_debt and :meth:Stats.toplist_user_now. """
        self.do_transactions(20, self.U_J, self.P_A) # Jobbmat, not counted
        self.do_transactions(2,  self.U_B, self.P_B)
        self.do_transactions(10, self.U_A, self.P_A)

        # Make sure the top user is correct
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.stats.Stats.top_user_debt().id() == self.U_B.id()
            toplist = streck.models.stats.Stats.toplist_user_now()
            assert [u.id() for u in toplist] == [self.U_B.id(), self.U_A.id()]
            assert [u.debt() for u in toplist] == [18.0, 10.0]
Exemplo n.º 28
0
    def test_top_product(self):
        """ Tests :meth:Stats.top_product. """
        self.do_transactions(20, self.U_J, self.P_A)  # Jobbmat, not counted
        self.do_transactions(2, self.U_A, self.P_A)
        self.do_transactions(10, self.U_A, self.P_B)

        # Make sure the top product is correct
        with app.test_request_context():
            app.preprocess_request()
            expect_name = self.P_B.name()
            top_name, count = streck.models.stats.Stats.top_product()
            assert top_name == expect_name
            assert count == 10
Exemplo n.º 29
0
    def test_top_product(self):
        """ Tests :meth:Stats.top_product. """
        self.do_transactions(20, self.U_J, self.P_A) # Jobbmat, not counted
        self.do_transactions(2,  self.U_A, self.P_A)
        self.do_transactions(10, self.U_A, self.P_B)

        # Make sure the top product is correct
        with app.test_request_context():
            app.preprocess_request()
            expect_name = self.P_B.name()
            top_name, count = streck.models.stats.Stats.top_product()
            assert top_name == expect_name
            assert count == 10
Exemplo n.º 30
0
    def test_update_product(self):
        """ Test updating products through the admin interface. """
        new_name = 'Cheaper Product'
        new_price = 0.5
        self.add_product(self.TESTPRODUCT1, 1)

        # Update product name
        rv = self.app.post('/admin/product/%s/update' % self.TESTPRODUCT1['barcode'], data=dict(
            name=new_name
        ), follow_redirects=True)
        assert new_name in rv.data
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.product.Product(self.TESTPRODUCT1['barcode']).name() == new_name

        # Update product price
        rv = self.app.post('/admin/product/%s/update' % self.TESTPRODUCT1['barcode'], data=dict(
            price=new_price
        ), follow_redirects=True)
        assert str(new_price) in rv.data
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.product.Product(self.TESTPRODUCT1['barcode']).price() == new_price

        # Update product category
        rv = self.app.post('/admin/product/%s/update' % self.TESTPRODUCT1['barcode'], data=dict(
            category=2
        ), follow_redirects=True)
        assert self.CATEGORIES[2] in rv.data
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.product.Product(self.TESTPRODUCT1['barcode']).category() == (self.CATEGORIES[2]).decode('utf-8')

        # Update a nonexistent product
        rv = self.app.post('/admin/product/nothing/update', data=dict(
            name='Irrelevant'
        ), follow_redirects=True)
        assert b'Produkten existerar inte!' in rv.data
Exemplo n.º 31
0
    def setUp(self):
        """ Set up test case.

        Inserts a product and a user into the database
        """
        GenericStreckTestCase.setUp(self)
        with app.test_request_context():
            app.preprocess_request()
            streck.models.product.Product.add('0012345678905', 'The Product',
                                              5.0, 1, '../img/NoneProduct.png')
            streck.models.user.User.add('john', 'User 1',
                                        '../img/NoneUser.png')
            streck.models.user.User.add('jane', 'User 2',
                                        '../img/NoneUser.png').disable()
Exemplo n.º 32
0
    def test_top_user_debt(self):
        """ Tests :meth:Stats.top_user_debt and :meth:Stats.toplist_user_now. """
        self.do_transactions(20, self.U_J, self.P_A)  # Jobbmat, not counted
        self.do_transactions(2, self.U_B, self.P_B)
        self.do_transactions(10, self.U_A, self.P_A)

        # Make sure the top user is correct
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.stats.Stats.top_user_debt().id(
            ) == self.U_B.id()
            toplist = streck.models.stats.Stats.toplist_user_now()
            assert [u.id() for u in toplist] == [self.U_B.id(), self.U_A.id()]
            assert [u.debt() for u in toplist] == [18.0, 10.0]
Exemplo n.º 33
0
    def setUp(self):
        """ Set up test case.

        Inserts two products, two users and some transactions
        """
        GenericStreckTestCase.setUp(self)
    	with app.test_request_context():
            app.preprocess_request()
            self.P_A = streck.models.product.Product.add('BC_P_A', 'Prod. A', 1.0, 2, '')
            self.P_B = streck.models.product.Product.add('BC_P_B', 'Prod. B', 9.0, 1, '')
            self.P_N = streck.models.product.Product(None)
            self.U_A = streck.models.user.User.add('BC_U_A', 'User A', '')
            self.U_B = streck.models.user.User.add('BC_U_B', 'User B', '')
            self.U_J = streck.models.user.User(app.config['JOBBMAT_BARCODE'])
Exemplo n.º 34
0
    def test_user_disabling(self):
        """ Test disabling and enabling users through the admin interface. """
        self.add_user(self.TESTUSER)

        # Disable a user
        rv = self.app.get('/admin/user/%s/disable' % self.TESTUSER['barcode'], follow_redirects=True)
        assert b'Användaren är nu avstängd!' in rv.data
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User(self.TESTUSER['barcode']).disabled()

        # Disable a user again
        rv = self.app.get('/admin/user/%s/disable' % self.TESTUSER['barcode'], follow_redirects=True)
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User(self.TESTUSER['barcode']).disabled()

        # Enable a user
        rv = self.app.get('/admin/user/%s/enable' % self.TESTUSER['barcode'], follow_redirects=True)
        assert b'Användaren är inte längre avstängd!' in rv.data
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User(self.TESTUSER['barcode']).enabled()

        # Enable a user again
        rv = self.app.get('/admin/user/%s/enable' % self.TESTUSER['barcode'], follow_redirects=True)
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User(self.TESTUSER['barcode']).enabled()

        # Disable a nonexistent user
        rv = self.app.get('/admin/user/nobody/disable', follow_redirects=True)
        assert b'Användaren existerar inte!' in rv.data

        # Enable a nonexistent user
        rv = self.app.get('/admin/user/nobody/enable', follow_redirects=True)
        assert b'Användaren existerar inte!' in rv.data
Exemplo n.º 35
0
    def test_buy_allowed(self):
        """ Test booking something allowed as jobbmat. """
        # Make sure we can buy the product
        self.app.post('/user/%s/buy' % app.config['JOBBMAT_BARCODE'],
                      data=dict(barcode=self.TESTPRODUCT_GOOD['barcode']),
                      follow_redirects=True)
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User(app.config['JOBBMAT_BARCODE']).debt(
            ) == self.TESTPRODUCT_GOOD['price']

        # Make sure it appears on the user page
        rv = self.app.get('/user/%s' % app.config['JOBBMAT_BARCODE'],
                          follow_redirects=True)
        assert self.TESTPRODUCT_GOOD['name'] in rv.data
Exemplo n.º 36
0
    def test_remove_not_allowed(self):
        """ Test removing something disallowed as jobbmat. """
        # Make sure we can't remove the product
        self.app.post('/user/%s/buy' % app.config['REMOVE_JOBBMAT_BARCODE'],
                      data=dict(barcode=self.TESTPRODUCT_BAD['barcode']),
                      follow_redirects=True)
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User(
                app.config['JOBBMAT_BARCODE']).debt() == 0.0

        # Make sure it appears on the user page
        rv = self.app.get('/user/%s' % app.config['JOBBMAT_BARCODE'],
                          follow_redirects=True)
        assert self.TESTPRODUCT_BAD['name'] not in rv.data
Exemplo n.º 37
0
    def test_remove_allowed(self):
        """ Test removing some jobbmat. """
        # Make sure we can remove the product
        self.app.post('/user/%s/buy' % app.config['REMOVE_JOBBMAT_BARCODE'],
                      data=dict(barcode=self.TESTPRODUCT_GOOD['barcode']),
                      follow_redirects=True)
        with app.test_request_context():
            app.preprocess_request()
            # Note negative sign. Removing even if there was no initial purchase is allowed by design
            assert streck.models.user.User(app.config['JOBBMAT_BARCODE']).debt(
            ) == -self.TESTPRODUCT_GOOD['price']

        # Make sure it appears on the user page
        rv = self.app.get('/user/%s' % app.config['JOBBMAT_BARCODE'],
                          follow_redirects=True)
        assert self.TESTPRODUCT_GOOD['name'] in rv.data
Exemplo n.º 38
0
    def setUp(self):
        """ Set up test case.

        Inserts two products, two users and some transactions
        """
        GenericStreckTestCase.setUp(self)
        with app.test_request_context():
            app.preprocess_request()
            self.P_A = streck.models.product.Product.add(
                'BC_P_A', 'Prod. A', 1.0, 2, '')
            self.P_B = streck.models.product.Product.add(
                'BC_P_B', 'Prod. B', 9.0, 1, '')
            self.P_N = streck.models.product.Product(None)
            self.U_A = streck.models.user.User.add('BC_U_A', 'User A', '')
            self.U_B = streck.models.user.User.add('BC_U_B', 'User B', '')
            self.U_J = streck.models.user.User(app.config['JOBBMAT_BARCODE'])
Exemplo n.º 39
0
    def setUp(self):
        """ Set up test case.

        Inserts two products into the database and sets the allowed jobbmat category
        """
        GenericStreckTestCase.setUp(self)
        with app.test_request_context():
            app.preprocess_request()
            streck.models.product.Product.add(self.TESTPRODUCT_GOOD['barcode'],
                                              self.TESTPRODUCT_GOOD['name'],
                                              self.TESTPRODUCT_GOOD['price'],
                                              2, '../img/NoneProduct.png')
            streck.models.product.Product.add(self.TESTPRODUCT_BAD['barcode'],
                                              self.TESTPRODUCT_BAD['name'],
                                              self.TESTPRODUCT_BAD['price'], 1,
                                              '../img/NoneProduct.png')
        app.config['ALLOWED_JOBBMAT_CATEGORIES'] = [
            (self.CATEGORIES[2]).decode('utf-8')
        ]
Exemplo n.º 40
0
    def test_update_user(self):
        """ Test updating users through the admin interface. """
        new_user_name = 'Joe'
        self.add_user(self.TESTUSER)

        # Update a user
        rv = self.app.post('/admin/user/%s/update' % self.TESTUSER['barcode'], data=dict(
            name=new_user_name
        ), follow_redirects=True)
        assert new_user_name in rv.data
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User(self.TESTUSER['barcode']).name() == new_user_name

        # Update a nonexistent user
        rv = self.app.post('/admin/user/nobody/update', data=dict(
            name='Irrelevant'
        ), follow_redirects=True)
        assert b'Användaren existerar inte!' in rv.data
Exemplo n.º 41
0
    def test_update_user(self):
        """ Test updating users through the admin interface. """
        new_user_name = 'Joe'
        self.add_user(self.TESTUSER)

        # Update a user
        rv = self.app.post('/admin/user/%s/update' % self.TESTUSER['barcode'],
                           data=dict(name=new_user_name),
                           follow_redirects=True)
        assert new_user_name in rv.data
        with app.test_request_context():
            app.preprocess_request()
            assert streck.models.user.User(
                self.TESTUSER['barcode']).name() == new_user_name

        # Update a nonexistent user
        rv = self.app.post('/admin/user/nobody/update',
                           data=dict(name='Irrelevant'),
                           follow_redirects=True)
        assert b'Användaren existerar inte!' in rv.data
Exemplo n.º 42
0
                amt = float(line[3])
                g.db.execute(
                    'insert into transactions values (null, ?, ?, ?, ?, ?)',
                    [time,
                     u.id(),
                     Product(None).id(), -amt, 'converted pay'])
                print '%s paid debt!' % u.name()
            else:
                print 'Unknown flag %s' % flag
    # tab-separated, columns are:
    # timestamp flag user-barcode, product-barcode/paid amount, price, type
    # flag is *probably* 0 for buying and 2 for paying


if __name__ == '__main__':
    ctx = app.test_request_context()
    ctx.push()
    streck.models.setup_db()
    if len(sys.argv) < 2:
        sys.stderr.write('Usage: ./convert.py <old data path>')
        sys.exit(1)
    if not os.path.exists(sys.argv[1]):
        sys.stderr.write('Invalid path!')
        sys.exit(2)
    userpath = os.path.join(sys.argv[1], 'users/')
    productpath = os.path.join(sys.argv[1], 'products/')
    resourcepath = os.path.join(sys.argv[1], 'resources/')
    if not os.path.exists(userpath) or not os.path.exists(
            productpath) or not os.path.exists(resourcepath):
        sys.stderr.write('No data found!')
        sys.exit(3)
Exemplo n.º 43
0
    def test_top_user_total(self):
        """ Tests :meth:Stats.top_user_total and :meth:Stats.toplist_user_alltime. """
        self.do_transactions(20, self.U_J, self.P_A)  # Jobbmat, not counted
        self.do_transactions(2, self.U_B, self.P_A)
        self.do_transactions(10, self.U_A, self.P_A)

        # Make sure the total top user is correct
        with app.test_request_context():
            app.preprocess_request()
            expect_name = self.U_A.name()
            top_name, total = streck.models.stats.Stats.top_user_total()
            assert expect_name == top_name
            assert total == 10.0
            toplist = streck.models.stats.Stats.toplist_user_alltime()
            assert [v[0] for v in toplist
                    ] == [self.U_J.name(),
                          self.U_A.name(),
                          self.U_B.name()]
            assert [v[1] for v in toplist] == [20, 10, 2]
            assert [v[2] for v in toplist] == [20.0, 10.0, 2.0]

        # Make sure it's still correct after a debt payment
        with app.test_request_context():
            app.preprocess_request()
            self.do_transactions(1,
                                 self.U_A,
                                 self.P_N,
                                 notes='paid',
                                 price=-self.U_A.debt())
            self.do_transactions(1,
                                 self.U_B,
                                 self.P_N,
                                 notes='paid',
                                 price=-self.U_B.debt())
        with app.test_request_context():
            app.preprocess_request()
            expect_name = self.U_A.name()
            top_name, total = streck.models.stats.Stats.top_user_total()
            assert expect_name == top_name
            assert total == 10.0
            toplist = streck.models.stats.Stats.toplist_user_alltime()
            assert [v[0] for v in toplist
                    ] == [self.U_J.name(),
                          self.U_A.name(),
                          self.U_B.name()]
            assert [v[1] for v in toplist] == [20, 10, 2]
            assert [v[2] for v in toplist] == [20.0, 10.0, 2.0]

        # And after more transactions
        self.do_transactions(10, self.U_B, self.P_A)
        self.do_transactions(1, self.U_A, self.P_A)
        with app.test_request_context():
            app.preprocess_request()
            expect_name = self.U_B.name()
            top_name, total = streck.models.stats.Stats.top_user_total()
            assert expect_name == top_name
            assert total == 12.0
            toplist = streck.models.stats.Stats.toplist_user_alltime()
            assert [v[0] for v in toplist
                    ] == [self.U_J.name(),
                          self.U_B.name(),
                          self.U_A.name()]
            assert [v[1] for v in toplist] == [20, 12, 11]
            assert [v[2] for v in toplist] == [20.0, 12.0, 11.0]
Exemplo n.º 44
0
				amt = float(line[4])
				g.db.execute('insert into transactions values (null, ?, ?, ?, ?, ?)', [time, u.id(), p.id(), amt, 'converted'])
				print '%s bought "%s"!' % (u.name(), p.name())
				continue
			elif flag == 2:
				amt = float(line[3])
				g.db.execute('insert into transactions values (null, ?, ?, ?, ?, ?)', [time, u.id(), Product(None).id(), -amt, 'converted pay'])
				print '%s paid debt!' % u.name()
			else:
				print 'Unknown flag %s' % flag
	# tab-separated, columns are:
	# timestamp flag user-barcode, product-barcode/paid amount, price, type
	# flag is *probably* 0 for buying and 2 for paying

if __name__ == '__main__':
	ctx = app.test_request_context()
	ctx.push()
	streck.models.setup_db()
	if len(sys.argv) < 2:
		sys.stderr.write('Usage: ./convert.py <old data path>')
		sys.exit(1)
	if not os.path.exists(sys.argv[1]):
		sys.stderr.write('Invalid path!')
		sys.exit(2)
	userpath = os.path.join(sys.argv[1], 'users/')
	productpath = os.path.join(sys.argv[1], 'products/')
	resourcepath = os.path.join(sys.argv[1], 'resources/')
	if not os.path.exists(userpath) or not os.path.exists(productpath) or not os.path.exists(resourcepath):
		sys.stderr.write('No data found!')
		sys.exit(3)
	import_users(userpath)