Exemple #1
0
    def test_library_visibility_invalid_isbn(self):
        with self.app.app_context():
            self.basic_setup()

            new_data = {
                "state": State.Finished.name,
                "library_type": LibraryType.WishList.name
            }

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

            self.assertEqual(404, res.status_code)
            self.assertEqual(
                self.entry.state,
                LibraryModel.find_by_id_and_isbn(self.user.id,
                                                 self.book.isbn).state)
            self.assertEqual(
                self.entry.library_type,
                LibraryModel.find_by_id_and_isbn(self.user.id,
                                                 self.book.isbn).library_type)
Exemple #2
0
    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()

            new_data = {
                "state": State.Finished.name,
                "library_type": LibraryType.WishList.name
            }

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

            self.assertEqual(401, res.status_code)
            self.assertEqual(
                self.entry.state,
                LibraryModel.find_by_id_and_isbn(self.user.id,
                                                 self.book.isbn).state)
            self.assertEqual(
                self.entry.library_type,
                LibraryModel.find_by_id_and_isbn(self.user.id,
                                                 self.book.isbn).library_type)
Exemple #3
0
    def save_transaction(cls, user_id, isbns, prices, quantities):
        transactions = []
        email_trans = []
        for isbn, price, quantity in zip(isbns, prices, quantities):
            book = BooksModel.find_by_isbn(isbn)
            cls.check_stock(book, quantity)
            transaction = TransactionsModel(user_id, isbn, price, quantity)
            transactions.append(transaction.json())
            email_trans.append(transaction.email_text())
            db.session.add(transaction)
            user = UsersModel.find_by_id(user_id)

            book_library = LibraryModel.find_by_id_and_isbn(
                user.id, transaction.isbn)
            if book_library:  # if the book was already in library
                if book_library.library_type == LibraryType.WishList:  # if it was in the wish list
                    book_library.library_type = LibraryType.Bought  # change it to bought
                    book_library.state = State.Pending
            else:  # if it wasnt in the library, enter it
                entry = LibraryModel(book.isbn, user.id, LibraryType.Bought,
                                     State.Pending)
                db.session.add(entry)

        cls.it_transaction += 1
        db.session.commit()
        cls.send_email(user_id, email_trans)
        return transactions
Exemple #4
0
    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)
Exemple #5
0
    def post(self, email):
        data = parse_entry()
        with lock:
            user = check_user(email)

            if not BooksModel.find_by_isbn(data['isbn']):
                return {
                    "message": f"Book with ['isbn': {data['isbn']}] Not Found"
                }, 404

            if LibraryModel.find_by_id_and_isbn(user.id,
                                                data['isbn']) is not None:
                return {
                    "message":
                    f"Entry with ['email': {email}, 'isbn': {data['isbn']}] already exists"
                }, 409

            data['user_id'] = user.id
            try:
                entry = LibraryModel(**data)
                entry.save_to_db()
            except Exception as e:
                return {"message": str(e)}, 500

        return entry.json(), 201
Exemple #6
0
    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))
Exemple #7
0
    def test_library_visibility_not_logged(self):
        with self.app.app_context():
            self.basic_setup()

            new_data = {
                "state": State.Finished.name,
                "library_type": LibraryType.WishList.name
            }

            res = self.client.put(f"/api/library/invalid/{self.book.isbn}",
                                  data=new_data)

            self.assertEqual(401, res.status_code)
            self.assertEqual(
                self.entry.state,
                LibraryModel.find_by_id_and_isbn(self.user.id,
                                                 self.book.isbn).state)
            self.assertEqual(
                self.entry.library_type,
                LibraryModel.find_by_id_and_isbn(self.user.id,
                                                 self.book.isbn).library_type)
Exemple #8
0
    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)
Exemple #9
0
def check_keys(email, isbn):
    user = check_user(email)

    book = BooksModel.find_by_isbn(isbn)
    if book is None:
        abort(404,
              message={"message": f"Book with ['isbn': {isbn}] Not Found"})

    library = LibraryModel.find_by_id_and_isbn(user.id, isbn)
    if library is None:
        abort(404,
              message={
                  "message":
                  f"Entry with ['email': {email}, 'isbn': {isbn}] Not Found"
              })
    return library
Exemple #10
0
    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)
Exemple #11
0
    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)
Exemple #12
0
    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))
Exemple #13
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))