コード例 #1
0
    def test_order_mail(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]
            dataTransaction = {
                "isbns": isbns,
                'prices': prices,
                'quantities': quantities,
                "email": self.user.email,
            }
            res = self.client.post(
                "/api/transaction",
                data=dataTransaction,
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(201, res.status_code)
            # hi ha dues transaccions amb id 1
            transactions = TransactionsModel.find_by_id(1)
            self.assertEqual(len(transactions), 2)

            # les dues transaccions equivalen als llibres que acabem de posar
            for i, isbn in enumerate(isbns):
                self.assertEqual(
                    TransactionsModel.find_by_id_and_isbn(1, isbn).json(),
                    json.loads(res.data)['transactions'][i])
コード例 #2
0
    def test_post_add_library(self):
        with self.app.app_context():
            self.basic_setup()
            book2 = BooksModel(2, 10, 13.1, "book2")
            book2.save_to_db()

            isbns = [self.book.isbn, book2.isbn]
            prices = [self.book.precio, book2.precio]
            quantities = [1, 1]
            dataTransaction = {
                "isbns": isbns,
                'prices': prices,
                'quantities': quantities,
                "email": self.user.email,
            }
            res = self.client.post(
                "/api/transaction",
                data=dataTransaction,
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(201, res.status_code)
            self.assertEqual(self.book.isbn, self.user.library[0].isbn)
            self.assertEqual(book2.isbn, self.user.library[1].isbn)
            self.assertEqual(2, len(self.user.library))
コード例 #3
0
ファイル: test_COM_4.py プロジェクト: UB-ES-2020-A/Grup-ES
    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)
コード例 #4
0
ファイル: test_COM_4.py プロジェクト: UB-ES-2020-A/Grup-ES
    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])
コード例 #5
0
ファイル: test_BIB_2.py プロジェクト: UB-ES-2020-A/Grup-ES
    def test_model_add(self):
        with self.app.app_context():
            book = BooksModel(1, 1, 1, "test")
            book.save_to_db()
            entry = LibraryModel(book.isbn, 1, LibraryType.Bought, State.Dropped)

            entry.save_to_db()
            self.assertEqual(entry, LibraryModel.find_by_id_and_isbn(1, 1))
コード例 #6
0
ファイル: test_AM_1.py プロジェクト: UB-ES-2020-A/Grup-ES
    def test_model_add(self):
        with self.app.app_context():
            date = datetime.now()
            book = BooksModel(1, 1, 1.0, "titulo", "autor", "editorial",
                              "sinposis", "url", date)

            book.save_to_db()
            self.assertEqual(book, BooksModel.find_by_isbn(book.isbn))
コード例 #7
0
ファイル: test_AM_1.py プロジェクト: UB-ES-2020-A/Grup-ES
    def test_model_delete(self):
        with self.app.app_context():
            date = datetime.now()
            book = BooksModel(1, 1, 1.0, "titulo", "autor", "editorial",
                              "sinposis", "url", date)

            book.save_to_db()
            book.delete_from_db()
            self.assertEqual(False, book.vendible)
コード例 #8
0
ファイル: test_BIB_2.py プロジェクト: UB-ES-2020-A/Grup-ES
    def test_model_delete(self):
        with self.app.app_context():
            book = BooksModel(1, 1, 1, "test")
            book.save_to_db()
            entry = LibraryModel(book.isbn, 1, LibraryType.Bought)
            entry.save_to_db()
            entry.delete_from_db()

            self.assertEqual(False, entry.visible)
コード例 #9
0
ファイル: test_BIB_2.py プロジェクト: UB-ES-2020-A/Grup-ES
    def test_model_add_duplicate(self):
        with self.app.app_context():
            book = BooksModel(1, 1, 1, "test")
            book.save_to_db()
            entry = LibraryModel(book.isbn, 1, LibraryType.WishList, State.Reading)
            entry.save_to_db()

            same_entry = LibraryModel(book.isbn, 1, LibraryType.WishList, State.Reading)
            with self.assertRaises(Exception):
                same_entry.save_to_db()
コード例 #10
0
    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])
コード例 #11
0
ファイル: test_BIB_2.py プロジェクト: UB-ES-2020-A/Grup-ES
    def test_model_invalid_update(self):
        with self.app.app_context():
            book = BooksModel(1, 1, 1, "test")
            book.save_to_db()
            entry = LibraryModel(book.isbn, 1, LibraryType.WishList, State.Pending)
            entry.save_to_db()

            with self.assertRaises(Exception):
                data = {"isbn": 1, "user_id": 1, "state": "Pending2", "visible": True, "library_type": "WishList"}
                entry.update_from_db(data)
コード例 #12
0
ファイル: test_AM_1.py プロジェクト: UB-ES-2020-A/Grup-ES
    def test_model_add_duplicate(self):
        with self.app.app_context():
            date = datetime.now()
            book = BooksModel(1, 1, 1.0, "titulo", "autor", "editorial",
                              "sinposis", "url", date)
            book.save_to_db()

            same_book = BooksModel(1, 1, 1.0, "titulo", "autor", "editorial",
                                   "sinposis", "url", date)
            with self.assertRaises(Exception):
                same_book.save_to_db()
コード例 #13
0
 def test_get_search_by_isbn(self):
     with self.app.app_context():
         book = BooksModel(9780553803716, 1, 1, "test")
         book.save_to_db()
         book = BooksModel(9780553803712, 1, 1, "test2")
         book.save_to_db()
         args = {
             "isbn": 9780553803716
         }
         res = self.client.get('/api/search', data=args)
         self.assertEqual(200, res.status_code)
         self.assertEqual(len(json.loads(res.data)["books"]), 1)
         list_books = list(map(lambda u: u.json(), BooksModel.query.filter_by(isbn=args['isbn']).all()))
         self.assertEqual(list_books, json.loads(res.data)["books"])
コード例 #14
0
 def test_get_search_by_editorial(self):
     with self.app.app_context():
         book = BooksModel(1, 1, 1, "test", "Isaac Asimov", "Bantam Books")
         book.save_to_db()
         book = BooksModel(2, 1, 1, "as")
         book.save_to_db()
         args = {
             'editorial': "Bantam Books"
         }
         res = self.client.get('/api/search', data=args)
         self.assertEqual(200, res.status_code)
         self.assertEqual(len(json.loads(res.data)["books"]), 1)
         list_books = list(map(lambda u: u.json(), BooksModel.query.filter_by(editorial=args['editorial']).all()))
         self.assertEqual(list_books, json.loads(res.data)["books"])
コード例 #15
0
    def test_get_transactions_without_login(self):
        with self.app.app_context():
            user = UsersModel('test', '*****@*****.**')
            user.hash_password('test')
            UsersModel.save_to_db(user)

            book = BooksModel(1, 1, 1.0, "titulo")
            book.save_to_db()

            # no login

            dataTransaction = {}

            res = self.client.post("/api/transaction", data=dataTransaction)
            self.assertEqual(401, res.status_code)
コード例 #16
0
    def test_model_add_some(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, book2.isbn],
                [self.book.precio, book2.precio], [1, 1])
            self.assertEqual(
                expected[0],
                TransactionsModel.find_by_id_and_isbn(1,
                                                      self.book.isbn).json())
            self.assertEqual(expected[1],
                             TransactionsModel.find_by_id_and_isbn(
                                 1, book2.isbn).json())  # same id
コード例 #17
0
ファイル: test_BIB_2.py プロジェクト: UB-ES-2020-A/Grup-ES
    def test_model_update(self):
        with self.app.app_context():
            book = BooksModel(1, 1, 1, "test")
            book.save_to_db()
            book2 = BooksModel(2, 1, 1, "test2")
            book2.save_to_db()

            entry = LibraryModel(book.isbn, 1, LibraryType.Bought, State.Finished)
            entry.save_to_db()

            data = {"isbn": book2.isbn, "user_id": 2, "state": "Reading", "visible": True, "library_type": "WishList"}
            entry.update_from_db(data)

            expected_json = data
            expected_json["book"] = BooksModel.find_by_isbn(book2.isbn).json()
            del expected_json["isbn"]
            self.assertEqual(expected_json, entry.json())
コード例 #18
0
    def test_get_transactions_user(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]
            dataTransaction = {
                "isbns": isbns,
                'prices': prices,
                'quantities': quantities,
                "email": self.user.email,
            }

            res = self.client.post(
                "/api/transaction",
                data=dataTransaction,
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(201, res.status_code)

            res = self.client.get(
                f"/api/transactions/{self.user.email}",
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(200, res.status_code)
            # l'usuari te dues transactions
            self.assertEqual(2, len(self.user.transactions))

            # comprovem que son les que hem acabat d'afegir
            expected = [
                transaction.json()
                for transaction in TransactionsModel.find_by_id(1)
            ]
            real_output = [
                transaction.json() for transaction in self.user.transactions
            ]
            self.assertEqual(expected, real_output)
コード例 #19
0
ファイル: books.py プロジェクト: UB-ES-2020-A/Grup-ES
    def post(self):
        data = parse_book()
        check_constraints_book(data)
        with lock:
            book = BooksModel.find_by_isbn(data["isbn"])
            if book:
                return {
                    "message":
                    f"A book with same isbn {data['isbn']} already exists"
                }, 409
            try:
                del data['vendible']  # always set to True when post
                book = BooksModel(**data)
                book.save_to_db()
            except Exception as e:
                return {"message": str(e)}, 500

        return book.json(), 201
コード例 #20
0
ファイル: test_BIB_2.py プロジェクト: UB-ES-2020-A/Grup-ES
    def test_post_entry(self):
        with self.app.app_context():
            user = UsersModel("test", "test")
            user.hash_password("test")
            user.save_to_db()

            book = BooksModel(1, 1, 1, "test")
            book.save_to_db()

            res = self.client.post("api/login", data={"email": user.email, "password": "******"})
            token = json.loads(res.data)["token"]

            parameters = {
                'isbn': book.isbn,
            }

            res = self.client.post(f"api/library/{user.email}", data=parameters, headers={
                "Authorization": 'Basic ' + base64.b64encode((token + ":").encode('ascii')).decode('ascii')
            })
            self.assertEqual(201, res.status_code)
            self.assertEqual(LibraryModel.find_by_id_and_isbn(user.id, 1).json(), json.loads(res.data))
コード例 #21
0
    def test_post_add_library_delete_from_wishlist(self):
        with self.app.app_context():
            self.basic_setup()
            book2 = BooksModel(2, 10, 13.1, "book2")
            book2.save_to_db()

            # enter book to wishlist
            entry = LibraryModel(self.book.isbn, 1, LibraryType.WishList,
                                 State.Pending)
            entry.save_to_db()
            self.assertEqual(entry, LibraryModel.find_by_id_and_isbn(1, 1))

            isbns = [self.book.isbn, book2.isbn]
            prices = [self.book.precio, book2.precio]
            quantities = [1, 1]
            dataTransaction = {
                "isbns": isbns,
                'prices': prices,
                'quantities': quantities,
                "email": self.user.email,
            }
            res = self.client.post(
                "/api/transaction",
                data=dataTransaction,
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(201, res.status_code)

            # check if book is in library
            self.assertEqual(self.book.isbn, self.user.library[0].isbn)
            self.assertEqual(book2.isbn, self.user.library[1].isbn)

            # check if type of book2 has changed: was in wishlist, now bought
            self.assertEqual(LibraryType.Bought,
                             self.user.library[1].library_type)

            self.assertEqual(2, len(self.user.library))
コード例 #22
0
ファイル: test_BIB_2.py プロジェクト: UB-ES-2020-A/Grup-ES
    def test_get_entry(self):
        with self.app.app_context():
            user = UsersModel("test", "test")
            user.hash_password("test")
            user.save_to_db()

            book = BooksModel(1, 1, 1, "test")
            book.save_to_db()
            book2 = BooksModel(2, 1, 1, "test")
            book2.save_to_db()
            book3 = BooksModel(3, 1, 1, "test")
            book3.save_to_db()

            entry = LibraryModel(book.isbn, user.id, LibraryType.Bought, State.Pending)
            entry.save_to_db()
            entry2 = LibraryModel(book2.isbn, user.id, LibraryType.WishList, State.Pending)
            entry2.save_to_db()
            entry3 = LibraryModel(book3.isbn, user.id, LibraryType.Bought, State.Reading)
            entry3.save_to_db()

            res = self.client.post("api/login", data={"email": user.email, "password": "******"})
            token = json.loads(res.data)["token"]

            res = self.client.get(f"api/userLibrary/{user.email}", headers={
                "Authorization": 'Basic ' + base64.b64encode((token + ":").encode('ascii')).decode('ascii')
            })
            self.assertEqual(200, res.status_code)
            expectedRes = list(map(lambda e: e.json(), [entry, entry3]))
            self.assertEqual(expectedRes, json.loads(res.data)["library"])
コード例 #23
0
ファイル: test_US_GRR_1.py プロジェクト: UB-ES-2020-A/Grup-ES
class UnitTestOfUS(BaseTest):
    def basic_setup(self):
        password = "******"
        self.admin = UsersModel("admin", "admin", Roles.Admin)
        self.admin.hash_password(password)
        self.admin.save_to_db()

        self.user = UsersModel("test", "test")
        self.user.hash_password("test")
        self.user.save_to_db()

        self.book = BooksModel(2, 2, 2, "test")
        self.book.save_to_db()

        res = self.client.post("/api/login",
                               data={
                                   "email": self.admin.email,
                                   "password": password
                               })
        self.token = json.loads(res.data)["token"]

    def test_delete_review_admin(self):
        with self.app.app_context():
            self.basic_setup()

            review = ReviewsModel(self.book.isbn, self.user.id, 2, "test")
            review.save_to_db()

            res = self.client.delete(
                f"api/review/{self.user.id}/{self.book.isbn}",
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(200, res.status_code)
            self.assertEqual(
                None,
                ReviewsModel.find_by_isbn_user_id(self.book.isbn,
                                                  self.user.id))
コード例 #24
0
    def test_get_transactions_other_user(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]
            dataTransaction = {
                "isbns": isbns,
                'prices': prices,
                'quantities': quantities,
                "email": self.user.email,
            }

            res = self.client.post(
                "/api/transaction",
                data=dataTransaction,
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(201, res.status_code)
            user2 = UsersModel('test2', '*****@*****.**')
            user2.hash_password('test2')
            UsersModel.save_to_db(user2)

            res = self.client.get(
                '/api/transactions/' + user2.email,
                headers={  # user tries to get user2 transactions
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(401, res.status_code)
コード例 #25
0
ファイル: test_BIB_1.py プロジェクト: UB-ES-2020-A/Grup-ES
class UnitTestOfUS(BaseTest):
    def basic_setup(self):
        password = "******"
        self.user = UsersModel("test", "test")
        self.user.hash_password(password)
        self.user.save_to_db()

        self.book = BooksModel(1, 10, 10, "test")
        self.book.save_to_db()

        self.entry = LibraryModel(self.book.isbn, self.user.id,
                                  LibraryType.Bought)
        self.entry.save_to_db()

        res = self.client.post("api/login",
                               data={
                                   "email": self.user.email,
                                   "password": password
                               })
        self.token = json.loads(res.data)["token"]

    # TEST TASK 1
    def test_delete_library_visibility(self):
        with self.app.app_context():
            self.basic_setup()

            res = self.client.delete(
                f"api/library/{self.user.email}/visibility/{self.book.isbn}",
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })

            self.assertEqual(200, res.status_code)
            self.assertEqual(
                False,
                LibraryModel.find_by_id_and_isbn(self.user.id,
                                                 self.book.isbn).visible)

    def test_post_library_visibility(self):
        with self.app.app_context():
            self.basic_setup()
            self.entry.change_visible_db(False)

            res = self.client.post(
                f"api/library/{self.user.email}/visibility/{self.book.isbn}",
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })

            self.assertEqual(200, res.status_code)
            self.assertEqual(
                True,
                LibraryModel.find_by_id_and_isbn(self.user.id,
                                                 self.book.isbn).visible)

    # Endpoint independent
    def test_library_visibility_invalid_email(self):
        with self.app.app_context():
            self.basic_setup()

            res = self.client.delete(
                f"api/library/fails/visibility/{self.book.isbn}",
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })

            self.assertEqual(404, res.status_code)
            # Checks visibility doesn't change
            self.assertEqual(
                self.entry.visible,
                LibraryModel.find_by_id_and_isbn(self.user.id,
                                                 self.book.isbn).visible)

    def test_library_visibility_invalid_isbn(self):
        with self.app.app_context():
            self.basic_setup()

            res = self.client.delete(
                f"api/library/{self.user.email}/visibility/fails",
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })

            self.assertEqual(404, res.status_code)
            # Checks visibility doesn't change
            self.assertEqual(
                self.entry.visible,
                LibraryModel.find_by_id_and_isbn(self.user.id,
                                                 self.book.isbn).visible)

    def test_library_visibility_modification_other_user(self):
        with self.app.app_context():
            self.basic_setup()

            user2 = UsersModel("test2", "test2")
            user2.hash_password("test2")
            user2.save_to_db()

            library = self.entry = LibraryModel(self.book.isbn, user2.id,
                                                LibraryType.Bought)
            library.save_to_db()

            res = self.client.delete(
                f"api/library/{user2.email}/visibility/{self.book.isbn}",
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })

            self.assertEqual(401, res.status_code)
            # Checks visibility doesn't change
            self.assertEqual(
                self.entry.visible,
                LibraryModel.find_by_id_and_isbn(self.user.id,
                                                 self.book.isbn).visible)

    def test_library_visibility_not_logged(self):
        with self.app.app_context():
            self.basic_setup()

            res = self.client.delete(
                f"api/library/{self.user.email}/visibility/{self.book.isbn}")

            self.assertEqual(401, res.status_code)
            # Checks visibility doesn't change
            self.assertEqual(
                self.entry.visible,
                LibraryModel.find_by_id_and_isbn(self.user.id,
                                                 self.book.isbn).visible)
コード例 #26
0
class UnitTestOfUS(BaseTest):

    def basic_setup(self):
        password = "******"
        self.user = UsersModel("test", "test")
        self.user.hash_password(password)
        self.user.save_to_db()

        self.book = BooksModel(1, 1, 1, "test")
        self.book.save_to_db()

        res = self.client.post("/api/login", data={"email": self.user.email, "password": password})
        self.token = json.loads(res.data)["token"]

    def test_model_update_one_score(self):
        with self.app.app_context():
            self.basic_setup()

            review = ReviewsModel(self.book.isbn, self.user.id, 2)
            review.save_to_db()

            data = {"score": 3}
            review.update_from_db(data)

            self.assertEqual(data["score"], review.score)
            self.assertEqual(data["score"], ScoresModel.find_by_isbn(self.book.isbn).score)

    def test_model_update_several_score(self):
        with self.app.app_context():
            self.basic_setup()

            user2 = UsersModel("test2", "test2")
            user2.hash_password("test2")
            user2.save_to_db()

            review = ReviewsModel(self.book.isbn, self.user.id, 2)
            review.save_to_db()

            review2 = ReviewsModel(self.book.isbn, user2.id, 4)
            review2.save_to_db()

            data = {"score": 3}
            review.update_from_db(data)

            self.assertEqual(data["score"], review.score)
            self.assertEqual((data["score"] + review2.score)/2, ScoresModel.find_by_isbn(self.book.isbn).score)

    def test_model_update_all_parameters(self):
        with self.app.app_context():
            self.basic_setup()

            review = ReviewsModel(self.book.isbn, self.user.id, 2)
            review.save_to_db()

            data = {"score": 3, "review": "test review"}
            review.update_from_db(data)

            # Checks all items have been updated
            self.assertDictEqual(data, {k: v for k, v in review.json().items() if k in data})
            self.assertEqual(data["score"], ScoresModel.find_by_isbn(self.book.isbn).score)

    def test_update_review(self):
        with self.app.app_context():
            self.basic_setup()

            review = ReviewsModel(self.book.isbn, self.user.id, 3, "test")
            review.save_to_db()

            data = {"score": 1, "review": "test review"}
            res = self.client.put(f"/api/review/{self.user.id}/{self.book.isbn}", data=data, headers={
                "Authorization": 'Basic ' + base64.b64encode((self.token + ":").encode('ascii')).decode('ascii')
            })

            self.assertEqual(200, res.status_code)
            # Checks all items have been updated
            self.assertDictEqual(data, {k: v for k, v in json.loads(res.data)["review"].items() if k in data})
コード例 #27
0
ファイル: test_ESCP_3.py プロジェクト: UB-ES-2020-A/Grup-ES
class UnitTestOfUS(BaseTest):
    def basic_setup(self):
        self.user = UsersModel("test", "*****@*****.**")
        self.user.hash_password("test")
        self.user.save_to_db()

        self.book = BooksModel(1, 1, 1, "book1")
        self.book.save_to_db()
        self.book2 = BooksModel(2, 100, 13.1, "book2")
        self.book2.save_to_db()

        res = self.client.post("/api/login",
                               data={
                                   "email": self.user.email,
                                   "password": "******"
                               })
        self.token = json.loads(res.data)["token"]

    def test_get_best_sellers(self):
        with self.app.app_context():
            self.basic_setup()

            isbns = [self.book.isbn, self.book2.isbn]
            prices = [self.book.precio, self.book2.precio]
            quantities = [1, 50]
            dataTransaction = {
                "isbns": isbns,
                'prices': prices,
                'quantities': quantities,
                "email": self.user.email,
            }
            res = self.client.post(
                "/api/transaction",
                data=dataTransaction,
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(201, res.status_code)
            # hi ha dues transaccions amb id 1
            transactions = TransactionsModel.find_by_id(1)
            self.assertEqual(len(transactions), 2)

            args = {
                "numBooks": 2,
            }
            res = self.client.get('/api/trending', data=args)
            self.assertEqual(200, res.status_code)

            self.assertEqual(
                2, len(json.loads(res.data)
                       ['books']))  # veiem que n'hi ha dos com li hem demanat
            self.assertEqual(self.book2.isbn,
                             json.loads(res.data)['books'][0]
                             ['isbn'])  # el més venut és el llibre amb isbn 2

    def test_get_best_sellers_2(self):
        with self.app.app_context():
            self.basic_setup()

            isbns = [self.book.isbn, self.book2.isbn]
            prices = [self.book.precio, self.book2.precio]
            quantities = [1, 50]
            dataTransaction = {
                "isbns": isbns,
                'prices': prices,
                'quantities': quantities,
                "email": self.user.email,
            }
            res = self.client.post(
                "/api/transaction",
                data=dataTransaction,
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(201, res.status_code)

            args = {
                "numBooks": 1,
            }
            res = self.client.get('/api/trending', data=args)
            self.assertEqual(200, res.status_code)

            self.assertEqual(1, len(json.loads(
                res.data)['books']))  # ens retorna només 1
            self.assertEqual(self.book2.isbn,
                             json.loads(res.data)['books'][0]
                             ['isbn'])  # el més venut és el llibre amb isbn 2

    def test_get_best_sellers_no_transactions(self):
        with self.app.app_context():
            self.basic_setup()
            args = {
                "numBooks": 1,
            }
            res = self.client.get('/api/trending', data=args)
            self.assertEqual(200, res.status_code)

            self.assertEqual(
                0, len(json.loads(res.data)['books'])
            )  # encara que el numBooks sigui 1, retorna 0 perque no
            # hi ha cap transaction

    def test_get_best_sellers_no_vendible(self):
        with self.app.app_context():
            self.basic_setup()

            self.book2.vendible = False

            isbns = [self.book.isbn, self.book2.isbn]
            prices = [self.book.precio, self.book2.precio]
            quantities = [1, 50]
            dataTransaction = {
                "isbns": isbns,
                'prices': prices,
                'quantities': quantities,
                "email": self.user.email,
            }
            res = self.client.post(
                "/api/transaction",
                data=dataTransaction,
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(201, res.status_code)

            args = {
                "numBooks": 1,
            }
            res = self.client.get('/api/trending', data=args)
            self.assertEqual(200, res.status_code)

            # el llibre amb isbn=2 no és vendible així que el llibre més venut és el isbn=1.
            self.assertEqual(self.book.isbn,
                             json.loads(res.data)['books'][0]['isbn'])
コード例 #28
0
ファイル: test_US_GRR_3.py プロジェクト: UB-ES-2020-A/Grup-ES
class UnitTestOfUS(BaseTest):
    def basic_setup(self):
        password = "******"
        self.user = UsersModel("test", "test")
        self.user.hash_password(password)
        self.user.save_to_db()

        self.book = BooksModel(1, 1, 1, "test")
        self.book.save_to_db()

        res = self.client.post("/api/login",
                               data={
                                   "email": self.user.email,
                                   "password": password
                               })
        self.token = json.loads(res.data)["token"]

    def test_delete_review(self):
        with self.app.app_context():
            self.basic_setup()

            review = ReviewsModel(self.book.isbn, self.user.id, 3, "test")
            review.save_to_db()

            res = self.client.delete(
                f"/api/review/{self.user.id}/{self.book.isbn}",
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(200, res.status_code)
            self.assertEqual(
                None,
                ReviewsModel.find_by_isbn_user_id(self.book.isbn,
                                                  self.user.id))

    def test_delete_review_other_user(self):
        with self.app.app_context():
            self.basic_setup()

            user2 = UsersModel("test2", "test2")
            user2.hash_password("test2")
            user2.save_to_db()

            review = ReviewsModel(self.book.isbn, user2.id, 3, "test")
            review.save_to_db()

            res = self.client.delete(
                f"/api/review/{user2.id}/{self.book.isbn}",
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })

            self.assertEqual(401, res.status_code)
            # Checks not modified
            self.assertEqual(
                review,
                ReviewsModel.find_by_isbn_user_id(self.book.isbn, user2.id))

    def test_delete_review_not_created(self):
        with self.app.app_context():
            self.basic_setup()

            res = self.client.delete(
                f"/api/review/{self.user.id}/{self.book.isbn}",
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(404, res.status_code)

    def test_delete_book_does_not_exist(self):
        with self.app.app_context():
            self.basic_setup()

            res = self.client.delete(
                f"/api/review/{self.user.id}/0",
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(404, res.status_code)

    def test_delete_user_does_not_exist(self):
        with self.app.app_context():
            self.basic_setup()

            res = self.client.delete(
                f"/api/review/0/{self.book.isbn}",
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(404, res.status_code)

    def test_delete_not_logged(self):
        with self.app.app_context():
            self.basic_setup()

            res = self.client.delete(
                f"/api/review/{self.user.id}/{self.book.isbn}")
            self.assertEqual(401, res.status_code)
コード例 #29
0
class UnitTestOfUS(BaseTest):
    def tearDown(self):
        super().tearDown()
        with self.app.app_context():
            TransactionsModel.it_transaction = 1

    def basic_setup(self):
        self.user = UsersModel("test", "*****@*****.**")
        self.user.hash_password("test")
        self.user.save_to_db()

        self.book = BooksModel(1, 1, 1, "book1")
        self.book.save_to_db()

        res = self.client.post("/api/login",
                               data={
                                   "email": self.user.email,
                                   "password": "******"
                               })
        self.token = json.loads(res.data)["token"]

    # TEST TASK 1
    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_add_some(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, book2.isbn],
                [self.book.precio, book2.precio], [1, 1])
            self.assertEqual(
                expected[0],
                TransactionsModel.find_by_id_and_isbn(1,
                                                      self.book.isbn).json())
            self.assertEqual(expected[1],
                             TransactionsModel.find_by_id_and_isbn(
                                 1, book2.isbn).json())  # same id

    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_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 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)

    # TEST TASK 2
    def test_post(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]
            dataTransaction = {
                "isbns": isbns,
                'prices': prices,
                'quantities': quantities,
                "email": self.user.email,
            }
            res = self.client.post(
                "/api/transaction",
                data=dataTransaction,
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(201, res.status_code)
            # hi ha dues transaccions amb id 1
            transactions = TransactionsModel.find_by_id(1)
            self.assertEqual(len(transactions), 2)

            # les dues transaccions equivalen als llibres que acabem de posar
            for i, isbn in enumerate(isbns):
                self.assertEqual(
                    TransactionsModel.find_by_id_and_isbn(1, isbn).json(),
                    json.loads(res.data)['transactions'][i])

    def test_post_add_library(self):
        with self.app.app_context():
            self.basic_setup()
            book2 = BooksModel(2, 10, 13.1, "book2")
            book2.save_to_db()

            isbns = [self.book.isbn, book2.isbn]
            prices = [self.book.precio, book2.precio]
            quantities = [1, 1]
            dataTransaction = {
                "isbns": isbns,
                'prices': prices,
                'quantities': quantities,
                "email": self.user.email,
            }
            res = self.client.post(
                "/api/transaction",
                data=dataTransaction,
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(201, res.status_code)
            self.assertEqual(self.book.isbn, self.user.library[0].isbn)
            self.assertEqual(book2.isbn, self.user.library[1].isbn)
            self.assertEqual(2, len(self.user.library))

    def test_post_add_library_delete_from_wishlist(self):
        with self.app.app_context():
            self.basic_setup()
            book2 = BooksModel(2, 10, 13.1, "book2")
            book2.save_to_db()

            # enter book to wishlist
            entry = LibraryModel(self.book.isbn, 1, LibraryType.WishList,
                                 State.Pending)
            entry.save_to_db()
            self.assertEqual(entry, LibraryModel.find_by_id_and_isbn(1, 1))

            isbns = [self.book.isbn, book2.isbn]
            prices = [self.book.precio, book2.precio]
            quantities = [1, 1]
            dataTransaction = {
                "isbns": isbns,
                'prices': prices,
                'quantities': quantities,
                "email": self.user.email,
            }
            res = self.client.post(
                "/api/transaction",
                data=dataTransaction,
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(201, res.status_code)

            # check if book is in library
            self.assertEqual(self.book.isbn, self.user.library[0].isbn)
            self.assertEqual(book2.isbn, self.user.library[1].isbn)

            # check if type of book2 has changed: was in wishlist, now bought
            self.assertEqual(LibraryType.Bought,
                             self.user.library[1].library_type)

            self.assertEqual(2, len(self.user.library))

    def test_post_no_stock(self):
        with self.app.app_context():
            self.basic_setup()
            book2 = BooksModel(2, 0, 13.1, "book2")
            book2.save_to_db()

            isbns = [self.book.isbn, book2.isbn]
            prices = [self.book.precio, book2.precio]
            quantities = [1, 100]  # no stock!
            dataTransaction = {
                "isbns": isbns,
                'prices': prices,
                'quantities': quantities,
                "email": self.user.email,
            }
            res = self.client.post(
                "/api/transaction",
                data=dataTransaction,
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(404, res.status_code)

    def test_post_wrong_isbn(self):
        with self.app.app_context():
            self.basic_setup()
            book2 = BooksModel(2, 0, 13.1, "book2")
            book2.save_to_db()

            isbns = [100, book2.isbn]  # wrong isbn
            prices = [self.book.precio, book2.precio]
            quantities = [1, 1]
            dataTransaction = {
                "isbns": isbns,
                'prices': prices,
                'quantities': quantities,
                "email": self.user.email,
            }
            res = self.client.post(
                "/api/transaction",
                data=dataTransaction,
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(404, res.status_code)

    # TEST TASK 3
    # test manual: posar variable config TESTING = False i veure com es reb el mail correctament.
    def test_order_mail(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]
            dataTransaction = {
                "isbns": isbns,
                'prices': prices,
                'quantities': quantities,
                "email": self.user.email,
            }
            res = self.client.post(
                "/api/transaction",
                data=dataTransaction,
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(201, res.status_code)
            # hi ha dues transaccions amb id 1
            transactions = TransactionsModel.find_by_id(1)
            self.assertEqual(len(transactions), 2)

            # les dues transaccions equivalen als llibres que acabem de posar
            for i, isbn in enumerate(isbns):
                self.assertEqual(
                    TransactionsModel.find_by_id_and_isbn(1, isbn).json(),
                    json.loads(res.data)['transactions'][i])

    # TEST TASK 6
    def test_get_transactions_user(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]
            dataTransaction = {
                "isbns": isbns,
                'prices': prices,
                'quantities': quantities,
                "email": self.user.email,
            }

            res = self.client.post(
                "/api/transaction",
                data=dataTransaction,
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(201, res.status_code)

            res = self.client.get(
                f"/api/transactions/{self.user.email}",
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(200, res.status_code)
            # l'usuari te dues transactions
            self.assertEqual(2, len(self.user.transactions))

            # comprovem que son les que hem acabat d'afegir
            expected = [
                transaction.json()
                for transaction in TransactionsModel.find_by_id(1)
            ]
            real_output = [
                transaction.json() for transaction in self.user.transactions
            ]
            self.assertEqual(expected, real_output)

    def test_get_transactions_without_login(self):
        with self.app.app_context():
            user = UsersModel('test', '*****@*****.**')
            user.hash_password('test')
            UsersModel.save_to_db(user)

            book = BooksModel(1, 1, 1.0, "titulo")
            book.save_to_db()

            # no login

            dataTransaction = {}

            res = self.client.post("/api/transaction", data=dataTransaction)
            self.assertEqual(401, res.status_code)

    def test_get_transactions_other_user(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]
            dataTransaction = {
                "isbns": isbns,
                'prices': prices,
                'quantities': quantities,
                "email": self.user.email,
            }

            res = self.client.post(
                "/api/transaction",
                data=dataTransaction,
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(201, res.status_code)
            user2 = UsersModel('test2', '*****@*****.**')
            user2.hash_password('test2')
            UsersModel.save_to_db(user2)

            res = self.client.get(
                '/api/transactions/' + user2.email,
                headers={  # user tries to get user2 transactions
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(401, res.status_code)
コード例 #30
0
class UnitTestOfUS(BaseTest):
    def tearDown(self):
        super().tearDown()
        with self.app.app_context():
            TransactionsModel.it_transaction = 1

    def basic_setup(self):
        self.user = UsersModel("test",
                               "*****@*****.**",
                               role=Roles.Admin)
        self.user.hash_password("test")
        self.user.save_to_db()
        self.user2 = UsersModel("test2", "*****@*****.**", role=Roles.User)
        self.user2.hash_password("test2")
        self.user2.save_to_db()

        self.book = BooksModel(1, 100, 1, "book1")
        self.book.save_to_db()
        self.book2 = BooksModel(2, 50, 13.1, "book2")
        self.book2.save_to_db()

        res = self.client.post("/api/login",
                               data={
                                   "email": self.user.email,
                                   "password": "******"
                               })
        self.token = json.loads(res.data)["token"]
        res = self.client.post("/api/login",
                               data={
                                   "email": self.user2.email,
                                   "password": "******"
                               })
        self.token2 = json.loads(res.data)["token"]

    def add_transactions_user1(self):
        isbns = [self.book.isbn, self.book2.isbn]
        prices = [self.book.precio, self.book2.precio]
        quantities = [1, 1]
        TransactionsModel.save_transaction(self.user.id, isbns, prices,
                                           quantities)

    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)

    # TASK 2
    def test_get_all_transactions_no_filter(self):
        with self.app.app_context():
            self.basic_setup()
            self.add_transactions_user1()
            self.add_transactions_user2()

            res = self.client.get(
                "/api/allTransactions",
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(200, res.status_code)

            transactions = TransactionsModel.query.all()
            expected_transactions = TransactionsModel.group_transactions_by_id(
                transactions)
            for i, transactions in enumerate(expected_transactions):
                for j, transaction in enumerate(transactions):
                    self.assertEqual(
                        transaction,
                        json.loads(res.data)['transactions'][i][j])

    def test_filter_transactions_by_isbn(self):
        with self.app.app_context():
            self.basic_setup()
            self.add_transactions_user1()  # has book with isbn = 1
            self.add_transactions_user2()  # has book with isbn = 1

            res = self.client.get(
                "/api/allTransactions",
                data={'isbn': self.book.isbn},
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(200, res.status_code)

            # the transactions with the books with isbn 1 are transactions 1 and 2
            ids_transactions = [1, 2]
            transactions = TransactionsModel.query.filter(
                TransactionsModel.id_transaction.in_(ids_transactions))
            expected_transactions = TransactionsModel.group_transactions_by_id(
                transactions)
            for i, transactions in enumerate(expected_transactions):
                for j, transaction in enumerate(transactions):
                    self.assertEqual(
                        transaction,
                        json.loads(res.data)['transactions'][i][j])

    def test_filter_transactions_by_isbn_and_user(self):
        with self.app.app_context():
            self.basic_setup()
            self.add_transactions_user1()  # has book with isbn = 1
            self.add_transactions_user2()  # has book with isbn = 1

            res = self.client.get(
                "/api/allTransactions",
                data={
                    'isbn': self.book.isbn,
                    'user_id': self.user2.id
                },
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(200, res.status_code)

            # the transactions with the books with isbn 1 are transactions 1 and 2 but only the second is from user2.
            ids_transactions = [1, 2]
            transactions = TransactionsModel.query.\
                filter(TransactionsModel.id_transaction.in_(ids_transactions)).\
                filter_by(user_id=self.user2.id)
            expected_transactions = TransactionsModel.group_transactions_by_id(
                transactions)
            for i, transactions in enumerate(expected_transactions):
                for j, transaction in enumerate(transactions):
                    self.assertEqual(
                        transaction,
                        json.loads(res.data)['transactions'][i][j])

    def test_filter_transactions_by_user_id(self):
        with self.app.app_context():
            self.basic_setup()
            self.add_transactions_user1()
            self.add_transactions_user2()

            res = self.client.get(
                "/api/allTransactions",
                data={'user_id': self.user2.id},
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(200, res.status_code)

            transactions = TransactionsModel.query.filter_by(
                user_id=self.user2.id).all()
            expected_transactions = [[
                t.json() for t in transactions if t.id_transaction == i
            ] for i in set(t.id_transaction for t in transactions)]
            for i, transactions in enumerate(expected_transactions):
                for j, transaction in enumerate(transactions):
                    self.assertEqual(
                        transaction,
                        json.loads(res.data)['transactions'][i][j])

    def test_filter_transactions_by_user_id_and_date_desc(self):
        with self.app.app_context():
            self.basic_setup()
            self.add_transactions_user1()
            self.add_transactions_user2()

            res = self.client.get(
                "/api/allTransactions",
                data={
                    'user_id': self.user2.id,
                    'date': 'desc'
                },
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(200, res.status_code)

            transactions = TransactionsModel.query.filter_by(
                user_id=self.user2.id).order_by(desc('date')).all()
            expected_transactions = TransactionsModel.group_transactions_by_id(
                transactions)
            for i, transactions in enumerate(expected_transactions):
                for j, transaction in enumerate(transactions):
                    self.assertEqual(
                        transaction,
                        json.loads(res.data)['transactions'][i][j])

    def test_filter_transactions_by_date_desc(self):
        with self.app.app_context():
            self.basic_setup()
            self.add_transactions_user1()
            self.add_transactions_user2()

            res = self.client.get(
                "/api/allTransactions",
                data={'date': 'desc'},
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(200, res.status_code)

            transactions = TransactionsModel.query.order_by(desc('date')).all()
            expected_transactions = TransactionsModel.group_transactions_by_id(
                transactions)
            for i, transactions in enumerate(expected_transactions):
                for j, transaction in enumerate(transactions):
                    self.assertEqual(
                        transaction,
                        json.loads(res.data)['transactions'][i][j])