コード例 #1
0
def test_update_wishlist_with_second_book(client, sample_user, sample_book):
    """Assert that updating a wishlist with a second book that is not there adds a second to the wishlist, and the user is returned with both books shown in wishlist."""
    second_book_data = {
        'title': 'Theft of Swords',
        'author': 'Micheal J Sullivan',
        'isbn': '0316187747',
        'date_of_publication': '2011-11-23 00:00:00'
    }
    second_book_res = client.post(f"/books/", data=second_book_data)
    second_book_loaded_data = json.loads(second_book_res.data)
    data = {
        'book_id': second_book_loaded_data['data']['id'],
        'password': '******'
    }
    res = client.put(f"/users/wishlist/{sample_user['data']['id']}", data=data)
    loaded_data = json.loads(res.data)
    clone = deepcopy(sample_user)
    del clone['data']['id']
    book_clone = deepcopy(sample_book)
    del book_clone['data']['id']
    clone['data']['wishlist'].append(book_clone['data'])
    clone['data']['wishlist'].append(second_book_data)
    verify_model(loaded_data['data'], clone['data'], 'user')
    assert res.status_code == 200
    assert len(loaded_data['data']['wishlist']) == 2
    assert loaded_data['status'] == 'success'
コード例 #2
0
def test_book_can_be_added_to_multiple_wishlists(client, sample_book):
    """
    Create a new user, and add sample book to wishlist. Assert that this book can exist in that wishlist as well. 

    Then delete user as cleanup, else book will not be able to be deleted later. As this function is not for testing delete, no asserts are used there.
    """
    new_user_data = {
        'first_name': 'Persony',
        'last_name': 'Mcpersonface',
        'email': '*****@*****.**',
        'password': '******'
    }
    new_user_res = client.post('/users/', data=new_user_data)
    new_user_loaded_data = json.loads(new_user_res.data)
    data = {
        'book_id': sample_book['data']['id'],
        'password': '******'
    }
    res = client.put(f"/users/wishlist/{new_user_loaded_data['data']['id']}",
                     data=data)
    loaded_data = json.loads(res.data)
    book_clone = deepcopy(sample_book)
    del book_clone['data']['id']
    new_user_data['wishlist'] = [book_clone['data']]
    verify_model(loaded_data['data'], new_user_data, 'user')
    assert len(loaded_data['data']['wishlist']) == 1
    assert res.status_code == 200
    assert loaded_data['status'] == 'success'
    delete_data = {'password': '******'}
    client.delete(f"/users/{new_user_loaded_data['data']['id']}",
                  data={'password': '******'})
コード例 #3
0
def test_get_book(client, sample_book):
    """User sample_book fixture to assert that the results of the get are the same as the book that was posted."""
    res = client.get(f"/books/{sample_book['data']['id']}")
    loaded_data = json.loads(res.data)
    clone = deepcopy(sample_book)
    del clone['data']['id']
    verify_model(loaded_data['data'], clone['data'], 'book')
    assert res.status_code == 200
    assert loaded_data['status'] == 'success'
コード例 #4
0
def test_update_wishlist_book_already_there(client, sample_user, sample_book):
    """Assert that updating a wishlist with a book that is already there does not add to the wishlist."""
    data = {'book_id': sample_book['data']['id'], 'password': '******'}
    res = client.put(f"/users/wishlist/{sample_user['data']['id']}", data=data)
    loaded_data = json.loads(res.data)
    clone = deepcopy(sample_user)
    del clone['data']['id']
    book_clone = deepcopy(sample_book)
    del book_clone['data']['id']
    clone['data']['wishlist'].append(book_clone['data'])
    verify_model(loaded_data['data'], clone['data'], 'user')
    assert len(loaded_data['data']['wishlist']) == 1
    assert res.status_code == 200
    assert loaded_data['status'] == 'success'
コード例 #5
0
def test_update_book(client, sample_book):
    """Assert the sample book can be updated."""
    data = {
        'new_title': 'An updated book name',
        'new_author': 'V. E. Schwab',
        'new_isbn': '0765376466',
        'new_date_of_publication': '2016-01-19 00:00:00'
    }
    res = client.put(f"/books/{sample_book['data']['id']}", data=data)
    loaded_data = json.loads(res.data)
    sample_book['data']['title'] = 'An updated book name'
    clone = deepcopy(sample_book)
    del clone['data']['id']
    verify_model(loaded_data['data'], clone['data'], 'book')
    assert res.status_code == 200
    assert loaded_data['status'] == 'success'
コード例 #6
0
def test_create_book(client):
    """
    Create a book in the database and ensure it presents the proper data.

    Note: This is called before the sample_book fixture, ensuring that the post and return is valid before that fixture is used to assist with other tests.
    """
    data = {
        'title': 'Neverwhere',
        'author': 'Neil Gaiman',
        'isbn': '9780380973637',
        'date_of_publication': '1996-09-16 00:00:00'
    }
    res = client.post('/books/', data=data)
    loaded_data = json.loads(res.data)
    verify_model(loaded_data['data'], data, 'book')
    assert res.status_code == 200
    assert loaded_data['status'] == 'success'
コード例 #7
0
def test_remove_book_from_wishlist_proper_auth(client, sample_user,
                                               sample_book):
    """
    User proper authentication to remove a book from sample user's wishlist.

    Then assert that success message is given, and that the returned user has the proper amount of books in wishlist (just one now). 
    """
    data = {'book_id': sample_book['data']['id'], 'password': '******'}
    res = client.delete(f"/users/wishlist/{sample_user['data']['id']}",
                        data=data)
    loaded_data = json.loads(res.data)
    clone = deepcopy(sample_user)
    del clone['data']['id']
    assert len(loaded_data['data']['wishlist']) == 1
    verify_model(loaded_data['data'], clone['data'], 'user')
    assert res.status_code == 200
    assert loaded_data['status'] == 'success'
コード例 #8
0
def test_add_to_wishlist_proper_auth(client, sample_user, sample_book):
    """
    Add the sample_book to the sample_user's wishlist with proper authentication.

    Assert that it is indeed shown as there.
    """
    data = {'book_id': sample_book['data']['id'], 'password': '******'}
    res = client.put(f"/users/wishlist/{sample_user['data']['id']}", data=data)
    loaded_data = json.loads(res.data)
    clone = deepcopy(sample_user)
    del clone['data']['id']
    book_clone = deepcopy(sample_book)
    book_clone['data']['id']
    clone['data']['wishlist'].append(book_clone['data'])
    verify_model(loaded_data['data'], clone['data'], 'user')
    assert res.status_code == 200
    assert loaded_data['status'] == 'success'
コード例 #9
0
def test_update_user_proper_auth(client, sample_user):
    """Assert that the sample_user can be updated, provided correct password."""
    data = {
        'new_first_name': 'Steve',
        'new_last_name': 'Williams',
        'new_email': '*****@*****.**',
        'new_password': '******',
        'password': '******'
    }
    res = client.put(f"/users/{sample_user['data']['id']}", data=data)
    loaded_data = json.loads(res.data)
    sample_user['data']['email'] = '*****@*****.**'
    clone = deepcopy(sample_user)
    del clone['data']['id']
    verify_model(loaded_data['data'], clone['data'], 'user')
    assert res.status_code == 200
    assert loaded_data['status'] == 'success'
コード例 #10
0
def test_create_user(client):
    """
    Create a user in the database and ensure it presents the proper data.

    Note: This is called before the sample_user fixture, ensuring that the post and return is valid before that fixture is used to assist with other tests.
    """
    data = {
        'first_name': 'John',
        'last_name': 'Smith',
        'email': '*****@*****.**',
        'password': '******'
    }
    res = client.post('/users/', data=data)
    loaded_data = json.loads(res.data)
    data['wishlist'] = []
    verify_model(loaded_data['data'], data, 'user')
    assert res.status_code == 200
    assert loaded_data['status'] == 'success'