def test_get_all_transactions_no_admin(self): with self.app.app_context(): user = UsersModel("test", "*****@*****.**", role=Roles.User) user.hash_password("test") user.save_to_db() res = self.client.post("/api/login", data={ "email": user.email, "password": "******" }) token = json.loads(res.data)["token"] book = BooksModel(1, 1, 1, "book1") book.save_to_db() book2 = BooksModel(2, 2, 13.1, "book2") book2.save_to_db() isbns = [book.isbn, book2.isbn] prices = [book.precio, book2.precio] quantities = [1, 1] TransactionsModel.save_transaction(user.id, isbns, prices, quantities) res = self.client.get( "/api/allTransactions", headers={ "Authorization": 'Basic ' + base64.b64encode( (token + ":").encode('ascii')).decode('ascii') }) self.assertEqual(403, res.status_code)
def test_get_all_transactions(self): with self.app.app_context(): self.basic_setup() book2 = BooksModel(2, 2, 13.1, "book2") book2.save_to_db() isbns = [self.book.isbn, book2.isbn] prices = [self.book.precio, book2.precio] quantities = [1, 1] TransactionsModel.save_transaction(self.user.id, isbns, prices, quantities) res = self.client.get( "/api/allTransactions", headers={ "Authorization": 'Basic ' + base64.b64encode( (self.token + ":").encode('ascii')).decode('ascii') }) self.assertEqual(200, res.status_code) allTransactions = TransactionsModel.query.all() for i, transaction in enumerate(allTransactions): self.assertEqual(transaction.json(), json.loads(res.data)['transactions'][0][i])
def test_model_add_no_stock(self): with self.app.app_context(): self.basic_setup() book2 = BooksModel(2, 1, 1, "book2") book2.save_to_db() with self.assertRaises(Exception): TransactionsModel.save_transaction(self.user.id, [self.book.isbn], [self.book.precio], [100])
def test_model_delete(self): with self.app.app_context(): self.basic_setup() TransactionsModel.save_transaction(self.user.id, [self.book.isbn], [self.book.precio], [1]) self.assertEqual(1, len(TransactionsModel.query.all())) TransactionsModel.query.filter_by( id_transaction=1).first().delete_from_db() self.assertEqual(0, len(TransactionsModel.query.all()))
def test_model_invalid_update(self): with self.app.app_context(): self.basic_setup() TransactionsModel.save_transaction(self.user.id, [self.book.isbn], [self.book.precio], [1]) self.assertEqual(1, len(TransactionsModel.query.all())) data = {"id_transaction": 'string'} # must be an integer with self.assertRaises(Exception): TransactionsModel.query.filter_by( id_transaction=1).first().update_from_db(data)
def test_model_add(self): with self.app.app_context(): self.basic_setup() book2 = BooksModel(2, 1, 1, "book2") book2.save_to_db() expected = TransactionsModel.save_transaction( self.user.id, [self.book.isbn], [self.book.precio], [1]) self.assertEqual( expected[0], TransactionsModel.find_by_id_and_isbn(1, self.book.isbn).json()) expected = TransactionsModel.save_transaction( self.user.id, [book2.isbn], [self.book.precio], [1]) self.assertEqual( expected[0], TransactionsModel.find_by_id_and_isbn(2, book2.isbn).json())
def test_model_update(self): with self.app.app_context(): self.basic_setup() TransactionsModel.save_transaction(self.user.id, [self.book.isbn], [self.book.precio], [1]) self.assertEqual(1, len(TransactionsModel.query.all())) data = {"id_transaction": 10} # id = 10 TransactionsModel.query.filter_by( id_transaction=1).first().update_from_db(data) expected_output = { 'date': dt.datetime.today().strftime("%d-%m-%Y"), 'id_transaction': 10, 'user_id': 1, 'isbn': self.book.isbn, 'price': self.book.precio, 'book': self.book.json(), 'quantity': 1 } self.assertEqual( expected_output, TransactionsModel.find_by_id(data["id_transaction"])[0].json())
def post(self): data = parse_transaction() with lock: user = UsersModel.find_by_email(data['email']) if user is None: return {"message": f"User with ['email': {data['email']}] Not Found"}, 404 if user != g.user: return {"message": "Invalid transaction, can only post yours"}, 401 for isbn, quantity in zip(data['isbns'], data['quantities']): book = BooksModel.find_by_isbn(isbn) if book is None: return {"message": "Book with ['isbn': " + str(isbn) + "] Not Found"}, 404 if quantity > book.stock: return {"message": "Not enough stock for book with 'isbn': " + str(isbn) + "only " + str(book.stock) + " available"}, 404 try: transactions = TransactionsModel.save_transaction(user.id, data['isbns'], data['prices'], data['quantities']) except Exception as ex: return {'message': str(ex)}, 500 return {'transactions': transactions}, 201
def add_transactions_user2(self): isbns = [self.book.isbn, self.book2.isbn] prices = [self.book.precio, self.book2.precio] quantities = [3, 1] TransactionsModel.save_transaction(self.user2.id, isbns, prices, quantities)