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
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))
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)
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)
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()
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())
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"])
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_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()
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)
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)