def test_signup_creates_new_user_with_none_names(dummy_request, db_session): """Test that signup sets first and last name to None when not given.""" data = {'email': FAKE.email(), 'password': FAKE.password()} dummy_request.POST = data res = signup_view(dummy_request) assert res['first_name'] is None assert res['last_name'] is None
def test_incomplete_user_no_email_not_added_to_database(db_session): """Test that a User cannot be added without required fields.""" user = User(first_name=FAKE.first_name(), last_name=FAKE.last_name(), password='******') db_session.add(user) with pytest.raises(IntegrityError): db_session.flush()
def test_signup_post_incomplete_data_gets_400_status_code(testapp): """Test that POST to signup route gets 400 status code for bad data.""" data = { 'first_name': FAKE.first_name(), 'last_name': FAKE.last_name(), 'password': FAKE.password() } res = testapp.post('/signup', data, status=400) assert res.status_code == 400
def test_book_list_post_missing_auth_gets_400_status_code(testapp): """Test that POST to book-list route gets 400 status code for missing auth.""" data = { 'title': FAKE.sentence(nb_words=3), 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%m/%d/%Y') } res = testapp.post('/books', data, status=400) assert res.status_code == 400
def test_signup_raises_error_for_incomplete_post_data(dummy_request): """Test that signup raises HTTPBadRequest for missing email.""" data = { 'first_name': FAKE.first_name(), 'last_name': FAKE.last_name(), 'password': FAKE.password() } dummy_request.POST = data with pytest.raises(HTTPBadRequest): signup_view(dummy_request)
def test_user_duplicate_email_not_added_to_database(db_session, one_user): """Test that a User can be added to the database.""" db_session.add(one_user) db_session.flush() dup_user = User(first_name=FAKE.first_name(), last_name=FAKE.last_name(), email=one_user.email, password='******') db_session.add(dup_user) with pytest.raises(IntegrityError): db_session.flush()
def test_signup_adds_new_user_to_the_database(dummy_request, db_session): """Test that signup adds a new User to the database.""" data = { 'first_name': FAKE.first_name(), 'last_name': FAKE.last_name(), 'email': FAKE.email(), 'password': FAKE.password() } dummy_request.POST = data signup_view(dummy_request) assert len(db_session.query(User).all()) == 1
def test_incomplete_book_no_title_not_added_to_database(db_session, one_user): """Test that a Book cannot be added without required fields.""" book = Book( user=one_user, author=FAKE.name(), isbn=FAKE.isbn13(separator="-"), pub_date=FAKE.date_object() ) db_session.add(book) with pytest.raises(IntegrityError): db_session.flush()
def test_book_list_post_incomplete_data_gets_400_status_code( testapp, one_user): """Test that POST to book-list route gets 400 status code for missing data.""" data = { 'email': one_user.email, 'password': '******', 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%m/%d/%Y') } res = testapp.post('/books', data, status=400) assert res.status_code == 400
def test_book_list_post_incorrect_date_gets_400_status_code(testapp, one_user): """Test that POST to book-list route gets 400 status code for bad data.""" data = { 'email': one_user.email, 'password': '******', 'title': FAKE.sentence(nb_words=3), 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%Y-%m-%d') } res = testapp.post('/books', data, status=400) assert res.status_code == 400
def test_book_list_post_complete_data_gets_201_status_code(testapp, one_user): """Test that POST to book-list route gets 201 status code.""" data = { 'email': one_user.email, 'password': '******', 'title': FAKE.sentence(nb_words=3), 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%m/%d/%Y') } res = testapp.post('/books', data) assert res.status_code == 201
def test_signup_raises_error_for_duplicate_email(dummy_request, db_session, one_user): """Test that signup raises HTTPBadRequest for duplicate email.""" db_session.add(one_user) data = { 'first_name': FAKE.first_name(), 'last_name': FAKE.last_name(), 'email': one_user.email, 'password': FAKE.password() } dummy_request.POST = data with pytest.raises(HTTPBadRequest): signup_view(dummy_request)
def test_signup_returns_dict_with_new_user_data(dummy_request): """Test that signup returns dict with the new User's data.""" data = { 'first_name': FAKE.first_name(), 'last_name': FAKE.last_name(), 'email': FAKE.email(), 'password': FAKE.password() } dummy_request.POST = data res = signup_view(dummy_request) assert isinstance(res, dict) assert all(prop in res for prop in ['id', 'first_name', 'last_name', 'email'])
def test_signup_creates_new_user_using_post_data(dummy_request, db_session): """Test that signup uses POST data to create the new User.""" data = { 'first_name': FAKE.first_name(), 'last_name': FAKE.last_name(), 'email': FAKE.email(), 'password': FAKE.password() } dummy_request.POST = data res = signup_view(dummy_request) new_user = db_session.query(User).get(res['id']) for prop in ['first_name', 'last_name', 'email']: assert getattr(new_user, prop) == data[prop] assert new_user.verify(data['password'])
def test_book_list_post_sets_email_user_as_book_owner(testapp, testapp_session, one_user): """Test that POST to book-list route sets user with email as book owner.""" data = { 'email': one_user.email, 'password': '******', 'title': FAKE.sentence(nb_words=3), 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%m/%d/%Y') } res = testapp.post('/books', data) new_book = testapp_session.query(Book).get(res.json['id']) assert new_book.user.email == one_user.email
def test_book_list_post_complete_data_adds_book_to_database( testapp, testapp_session, one_user): """Test that POST to book-list route creates a new Book.""" num_books = len(testapp_session.query(Book).all()) data = { 'email': one_user.email, 'password': '******', 'title': FAKE.sentence(nb_words=3), 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%m/%d/%Y') } testapp.post('/books', data) assert len(testapp_session.query(Book).all()) == num_books + 1
def test_create_raises_error_for_incomplete_post_data(dummy_request, db_session, one_user): """Test that create raises HTTPBadRequest for missing title.""" db_session.add(one_user) data = { 'email': one_user.email, 'password': '******', 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%m/%d/%Y') } dummy_request.POST = data with pytest.raises(HTTPBadRequest): _create_book(dummy_request, one_user)
def test_create_raises_error_for_bad_date_format(dummy_request, db_session, one_user): """Test that create raises HTTPBadRequest for incorrect date format.""" db_session.add(one_user) data = { 'email': one_user.email, 'password': '******', 'title': FAKE.sentence(nb_words=3), 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%Y-%m-%d') } dummy_request.POST = data with pytest.raises(HTTPBadRequest): _create_book(dummy_request, one_user)
def test_create_sets_email_user_as_owner_of_new_book(dummy_request, db_session, one_user): """Test that create uses email from POST data to set Book owner.""" db_session.add(one_user) data = { 'email': one_user.email, 'password': '******', 'title': FAKE.sentence(nb_words=3), 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%m/%d/%Y') } dummy_request.POST = data res = _create_book(dummy_request, one_user) new_book = db_session.query(Book).get(res['id']) assert one_user is new_book.user
def test_create_adds_new_book_to_the_database(dummy_request, db_session, one_user): """Test that create adds a new Book to the database.""" db_session.add(one_user) assert len(db_session.query(Book).all()) == 0 data = { 'email': one_user.email, 'password': '******', 'title': FAKE.sentence(nb_words=3), 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%m/%d/%Y') } dummy_request.POST = data _create_book(dummy_request, one_user) assert len(db_session.query(Book).all()) == 1
def test_create_returns_dict_with_new_book_data(dummy_request, db_session, one_user): """Test that create returns dict with the new Book's data.""" db_session.add(one_user) data = { 'email': one_user.email, 'password': '******', 'title': FAKE.sentence(nb_words=3), 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%m/%d/%Y') } dummy_request.POST = data res = _create_book(dummy_request, one_user) assert isinstance(res, dict) assert all(prop in res for prop in ['id', 'title', 'author', 'isbn', 'pub_date'])
def test_create_creates_new_book_using_post_data(dummy_request, db_session, one_user): """Test that create uses POST data to create the new Book.""" db_session.add(one_user) data = { 'email': one_user.email, 'password': '******', 'title': FAKE.sentence(nb_words=3), 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%m/%d/%Y') } dummy_request.POST = data res = _create_book(dummy_request, one_user) new_book = db_session.query(Book).get(res['id']) for prop in ['title', 'author', 'isbn']: assert getattr(new_book, prop) == data[prop] assert new_book.pub_date.strftime('%m/%d/%Y') == data['pub_date']