Ejemplo n.º 1
0
    def setUp(self):
        super().setUp()
        db.create_all()

        admin_role = Role(name='admin')
        self.user = User(
            username='******',
            email='*****@*****.**',
            password_hash=
            'pbkdf2:sha256:150000$bwYY0rIO$320d11e791b3a0f1d0742038ceebf879b8182898cbefee7bf0e55b9c9e9e5576',
            enabled=True)
        self.admin = User(
            username='******',
            email='*****@*****.**',
            password_hash=
            'pbkdf2:sha256:150000$bwYY0rIO$320d11e791b3a0f1d0742038ceebf879b8182898cbefee7bf0e55b9c9e9e5576',
            enabled=True,
            roles=[admin_role])
        self.try_add_entities([
            self.user, self.admin, admin_role,
            Currency(code='USD', rate=0.5),
            Currency(code='RUR', rate=0.5),
            Country(id='c1', name='country1'),
            Product(id='0000', name='Test product', price=10, weight=10)
        ])
Ejemplo n.º 2
0
def save_currency(currency_id):
    ''' Creates or modifies existing currency '''
    payload = request.get_json()
    if not payload:
        abort(Response('No data was provided', status=400))

    if payload.get('rate'):
        try:
            float(payload['rate'])
        except:
            abort(Response('Not number', status=400))

    if currency_id is None:
        currency = Currency()
        currency.when_created = datetime.now()
        db.session.add(currency)
    else:
        currency = Currency.query.get(currency_id)
        if not currency:
            abort(
                Response(f'No currency <{currency_id}> was found', status=400))
    if 'rate' in payload.keys():
        today_rate = currency.history.filter_by(
            when_created=datetime.now().date()).first()
        if today_rate is None:
            currency.history.append(
                CurrencyHistoryEntry(code=currency.code,
                                     rate=float(payload['rate']),
                                     when_created=datetime.now().date()))
        else:
            today_rate.rate = float(payload['rate'])
    modify_object(currency, payload, ['code', 'name', 'rate'])

    db.session.commit()
    return jsonify(currency.to_dict())
Ejemplo n.º 3
0
 def setUp(self):
     super().setUp()
     db.create_all()
     self.user = User(
         id=100,
         username='******',
         email='*****@*****.**',
         password_hash=
         'pbkdf2:sha256:150000$bwYY0rIO$320d11e791b3a0f1d0742038ceebf879b8182898cbefee7bf0e55b9c9e9e5576',
         enabled=True)
     try:
         entities = [
             self.user,
             Country(id='c1', name='country1'),
             Currency(code='USD', name='US Dollar', rate=1),
             Currency(code='RUR', name='Russian rouble', rate=1),
             Country(id='c2', name='country2'),
             Shipping(id=1, name='Shipping1'),
             Shipping(id=2, name='Shipping2'),
             Shipping(id=3, name='Shipping3'),
             DHL(),
             ShippingRate(id=1,
                          shipping_method_id=1,
                          destination='c1',
                          weight=100,
                          rate=100),
             ShippingRate(id=5,
                          shipping_method_id=2,
                          destination='c1',
                          weight=200,
                          rate=110),
             ShippingRate(id=2,
                          shipping_method_id=2,
                          destination='c2',
                          weight=100,
                          rate=100),
             ShippingRate(id=3,
                          shipping_method_id=2,
                          destination='c2',
                          weight=1000,
                          rate=150),
             ShippingRate(id=4,
                          shipping_method_id=3,
                          destination='c2',
                          weight=2000,
                          rate=160)
         ]
         db.session.add_all(entities)
         db.session.commit()
     except:
         db.session.rollback()
Ejemplo n.º 4
0
    def setUp(self):
        super().setUp()
        db.create_all()

        admin_role = Role(id=10, name='admin')
        self.try_add_entities([
            User(
                id=0,
                username='******',
                email='*****@*****.**',
                password_hash=
                'pbkdf2:sha256:150000$bwYY0rIO$320d11e791b3a0f1d0742038ceebf879b8182898cbefee7bf0e55b9c9e9e5576',
                enabled=True,
                roles=[admin_role]),
            User(
                id=10,
                username='******',
                email='*****@*****.**',
                password_hash=
                'pbkdf2:sha256:150000$bwYY0rIO$320d11e791b3a0f1d0742038ceebf879b8182898cbefee7bf0e55b9c9e9e5576',
                enabled=True),
            User(
                id=20,
                username='******',
                email='*****@*****.**',
                password_hash=
                'pbkdf2:sha256:150000$bwYY0rIO$320d11e791b3a0f1d0742038ceebf879b8182898cbefee7bf0e55b9c9e9e5576',
                enabled=True), admin_role,
            Currency()
        ])
Ejemplo n.º 5
0
 def setUp(self):
     super().setUp()
     db.create_all()
     
     admin_role = Role(name='admin')
     self.user = User(username='******', email='*****@*****.**',
         password_hash='pbkdf2:sha256:150000$bwYY0rIO$320d11e791b3a0f1d0742038ceebf879b8182898cbefee7bf0e55b9c9e9e5576', 
         enabled=True)
     self.admin = User(username='******', email='*****@*****.**',
         password_hash='pbkdf2:sha256:150000$bwYY0rIO$320d11e791b3a0f1d0742038ceebf879b8182898cbefee7bf0e55b9c9e9e5576',
         enabled=True, roles=[admin_role])
     self.try_add_entities([
         self.user, self.admin, admin_role,
         Currency(code='USD', rate=0.5),
         Currency(code='RUR', rate=0.5)
     ])
Ejemplo n.º 6
0
 def test_delete_currency(self):
     gen_id = f'{__name__}-{int(datetime.now().timestamp())}'
     self.try_add_entities(
         [Currency(code=gen_id, name='Currency_1', rate=1)])
     res = self.try_admin_operation(
         lambda: self.client.delete(f'/api/v1/admin/currency/{gen_id}'))
     self.assertEqual(res.status_code, 200)
     currency = Currency.query.get(gen_id)
     self.assertEqual(currency, None)
Ejemplo n.º 7
0
 def test_get_currencies(self):
     self.try_add_entities(
         [Currency(code='0001', name='Currency_1', rate=1)])
     res = self.try_user_operation(
         lambda: self.client.get('/api/v1/admin/currency'))
     self.assertEqual(res.json[0], {
         'code': '0001',
         'name': 'Currency_1',
         'rate': 1.0
     })
Ejemplo n.º 8
0
    def test_save_currency(self):
        gen_id = f'{__name__}-{int(datetime.now().timestamp())}'
        self.try_add_entities(
            [Currency(code=gen_id, name='Currency_1', rate=1)])
        res = self.try_admin_operation(lambda: self.client.post(
            f'/api/v1/admin/currency/{gen_id}', json={'rate': 2}))
        self.assertEqual(res.status_code, 200)
        currency = Currency.query.get(gen_id)
        self.assertEqual(currency.rate, 2)
        self.assertEqual(CurrencyHistoryEntry.query.count(), 1)

        res = self.client.post(f'/api/v1/admin/currency/{gen_id}',
                               json={'rate': '2@'})
        self.assertEqual(res.status_code, 400)
Ejemplo n.º 9
0
 def setUp(self):
     super().setUp()
     db.create_all()
     
     admin_role = Role(id=10, name='admin')
     self.try_add_entities([
         User(username='******', email='*****@*****.**',
             password_hash='pbkdf2:sha256:150000$bwYY0rIO$320d11e791b3a0f1d0742038ceebf879b8182898cbefee7bf0e55b9c9e9e5576',
             enabled=True, roles=[admin_role]),
         User(id=10, username='******', email='*****@*****.**',
             password_hash='pbkdf2:sha256:150000$bwYY0rIO$320d11e791b3a0f1d0742038ceebf879b8182898cbefee7bf0e55b9c9e9e5576', 
             enabled=True),
         admin_role,
         Country(id='c1', name='country1'),
         Currency(code='USD', rate=0.5),
         Product(id='SHIPPING', name='Shipping', weight=0, available=False,
             synchronize=False, separate_shipping=False, purchase=False)
     ])
Ejemplo n.º 10
0
 def test_get_currency(self):
     self.try_add_entities([Currency(code=1)])
     self.try_admin_operation(lambda: self.client.get('/admin/currencies/'))
Ejemplo n.º 11
0
 def test_get_invoice(self):
     self.try_add_entities([Invoice(id=1), Currency(code='USD', rate=1)])
     self.try_admin_operation(lambda: self.client.get('/admin/invoices/1'))