def test_loan_book(client, user, admin, book): post_book = post(endpoint='books:loan', isbn=book.isbn) test_data = {'user': user.id} response = post_book(client, data=test_data, user=admin) assert response.status_code == 201 assert response.json == {}
def test_loan_only_admin(client, user, admin, book, db): post_book = post(endpoint='books:loan', isbn=book.isbn) test_data = {'user': user.id} response = post_book(client, data=test_data, user=user) assert response.status_code == 403 assert response.json == {'message': {'Authorization': 'Unauthorized'}} assert len(book.loans) == 0
def test_loan_book_new_loan(client, user, admin, book): post_book = post(endpoint='books:loan', isbn=book.isbn) test_data = {'user': user.id} response = post_book(client, data=test_data, user=admin) assert response.status_code == 201 assert len(book.loans) == 1 assert book.loans[0].user == user
def test_return_book(client, user, admin, book, db): post_book = post(endpoint='books:return', isbn=book.isbn) test_data = {'user': user.id} loan = Loan() loan.book = book loan.user = user db.session.add(loan) db.session.commit() response = post_book(client, data=test_data, user=admin) assert response.status_code == 200 assert response.json == {} assert len(book.loans) == 0
def test_loan_no_more_copies(client, user, admin, book, db): post_book = post(endpoint='books:loan', isbn=book.isbn) test_data = {'user': user.id} loan = Loan() loan.book = book loan.user = admin db.session.add(loan) db.session.commit() response = post_book(client, data=test_data, user=admin) assert response.status_code == 400 assert response.json == {'message': {'book': 'No more free copies'}} assert len(book.loans) == 1
def test_return_user_has_not_loan_this_book(client, user, admin, book, db): post_book = post(endpoint='books:return', isbn=book.isbn) test_data = {'user': user.id} loan = Loan() loan.book = book loan.user = admin db.session.add(loan) db.session.commit() response = post_book(client, data=test_data, user=admin) assert response.status_code == 400 assert response.json == { 'message': { 'loan': 'This user has not loan this book' } } assert len(book.loans) == 1
from pytest import mark from project.utils.test import get, post, put from project.db import Loan, Book from ..factories import BookFactory _get_books = get(endpoint='books:list') _post_books = post(endpoint='books:list') @mark.integration def test_list_books_empty_list(client, db): response = _get_books(client) assert response.json == [] @mark.integration def test_list_books(client, book): response = _get_books(client) assert response.status_code == 200 assert len(response.json) == 1 assert response.json[0]['title'] == book.title assert response.json[0]['isbn'] == book.isbn assert response.json[0]['author'] == book.author.name assert response.json[0]['category'] == book.category.name assert response.json[0]['users'] == [] @mark.integration
from pytest import mark from flask import url_for from project.db import User from project.utils.auth import decode_token from project.utils.test import post from ..controllers import db _post_register = post(endpoint='user:register') _post_login = post(endpoint='user:login') def test_register_bad_data(client): response = _post_register(client, { 'password': '******', }) assert response.status_code == 400 assert 'email' in response.json['message'] def test_register_proper_result(client, mocker): mocker.patch.object(db, 'session') response = _post_register(client, { 'email': '*****@*****.**', 'password': '******', })
from pytest import mark from flask import url_for from project.db import User from project.utils.auth import decode_token from project.utils.test import post from ..controllers import db _post_register = post(endpoint='user:register') _post_login = post(endpoint='user:login') def test_register_bad_data(client): response = _post_register(client, { 'password': '******', }) assert response.status_code == 400 assert 'email' in response.json['message'] def test_register_proper_result(client, mocker): mocker.patch.object(db, 'session') response = _post_register(client, { 'email': '*****@*****.**', 'password': '******', }) assert response.status_code == 200
from pytest import mark from project.utils.test import get, post, put from project.db import Loan, Book from ..factories import BookFactory _get_books = get(endpoint='books:list') _post_books = post(endpoint='books:list') @mark.integration def test_list_books_empty_list(client, db): response = _get_books(client) assert response.json == [] @mark.integration def test_list_books(client, book): response = _get_books(client) assert response.status_code == 200 assert len(response.json) == 1 assert response.json[0]['title'] == book.title assert response.json[0]['isbn'] == book.isbn assert response.json[0]['author'] == book.author.name assert response.json[0]['category'] == book.category.name assert response.json[0]['users'] == []