Esempio n. 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)
Esempio n. 2
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
Esempio n. 3
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)
Esempio n. 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()

            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)
Esempio n. 5
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))
Esempio n. 6
0
    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()
Esempio n. 7
0
    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"]
Esempio n. 8
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)
Esempio n. 9
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))
Esempio n. 10
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
Esempio n. 11
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)
Esempio n. 12
0
    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)
Esempio n. 13
0
    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)
Esempio n. 14
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
Esempio n. 15
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)
Esempio n. 16
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)
Esempio n. 17
0
    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())
Esempio n. 18
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))
Esempio n. 19
0
    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"])
Esempio n. 20
0
    def test_model_book_not_exist(self):
        with self.app.app_context():
            entry = LibraryModel(1, 1, LibraryType.WishList, State.Pending)

            with self.assertRaises(Exception):
                entry.save_to_db()
Esempio n. 21
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, 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_put_library(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}/{self.book.isbn}",
                data=new_data,
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })

            self.assertEqual(200, res.status_code)
            self.assertEqual(
                State.Finished,
                LibraryModel.find_by_id_and_isbn(self.user.id,
                                                 self.book.isbn).state)
            self.assertEqual(
                LibraryType.WishList,
                LibraryModel.find_by_id_and_isbn(self.user.id,
                                                 self.book.isbn).library_type)

    def test_library_visibility_invalid_email(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,
                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)

    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)

    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)

    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)
Esempio n. 22
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, 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)