コード例 #1
0
def test_that_getting_existing_book_works(client):
    """
    GIVEN a Flask application
    WHEN the `/api/books/:id` route is called (GET) with valid credentials
    BUT with an id that exists in the database
    THEN check that the response meets the set conditions
    """
    user = User.query.first()
    access_token = create_access_token(identity=user)

    # create an books
    client.set_cookie('localhost', 'access_token', access_token)
    response = client.post(base_route, json=book_data())

    response_data = convert_to_dict(response.data)

    # query the created book
    response = client.get('{}/{}'.format(base_route, response_data.get('id')))

    assert_that(response.status_code).is_equal_to(200)
    assert_that(response.headers['Content-Type']).contains('application/json')

    response_data = convert_to_dict(response.data)
    keys = (
        'id',
        'isbn',
        'title',
        'num_of_pages',
        'publisher',
        'publication_date',
        'authors',
        'categories',
    )

    assert_that(response_data).contains_key(*keys)
コード例 #2
0
def test_that_categories_are_updated_correctly(client):
    """
    GIVEN a Flask application
    WHEN the `/api/categories/:id` route is called (PUT) with valid credentials
    WITH valid data
    THEN check that the response meets the set conditions
    """
    user = User.query.first()
    access_token = create_access_token(identity=user)

    client.set_cookie('localhost', 'access_token', access_token)
    # create new category
    data = category_data()
    response = client.post(base_route, json=data)

    # update created category
    response_data = convert_to_dict(response.data)
    data['name'] = 'Geography'
    response = client.put('{}/{}'.format(base_route, response_data.get('id')),
                          json=data)

    assert_that(response.status_code).is_equal_to(200)
    assert_that(response.headers['Content-Type']).contains('application/json')

    response_data = convert_to_dict(response.data)
    assert_that(response_data).contains_key('id', 'name')
コード例 #3
0
def test_that_books_are_updated_correctly(client):
    """
    GIVEN a Flask application
    WHEN the `/api/books/:id` route is called (PUT) with valid credentials
    WITH valid data
    THEN check that the response meets the set conditions
    """
    user = User.query.first()
    access_token = create_access_token(identity=user)

    client.set_cookie('localhost', 'access_token', access_token)
    # create new book
    data = book_data()
    response = client.post(base_route, json=data)

    # update created book
    response_data = convert_to_dict(response.data)
    data['title'] = 'The Art of Warring'
    response = client.put('{}/{}'.format(base_route, response_data.get('id')),
                          json=data)

    assert_that(response.status_code).is_equal_to(200)
    assert_that(response.headers['Content-Type']).contains('application/json')

    response_data = convert_to_dict(response.data)
    keys = (
        'id',
        'isbn',
        'title',
        'num_of_pages',
        'publisher',
        'publication_date',
    )

    assert_that(response_data).contains_key(*keys)
コード例 #4
0
def test_that_books_are_created_correctly(client):
    """
    GIVEN a Flask application
    WHEN the `/api/books` route is posted (POST) to with valid credentials
    THEN check that the response meets the set conditions
    """
    user = User.query.first()
    access_token = create_access_token(identity=user)

    client.set_cookie('localhost', 'access_token', access_token)
    response = client.post(base_route, json=book_data())

    assert_that(response.status_code).is_equal_to(201)
    assert_that(response.headers['Content-Type']).contains('application/json')

    response_data = convert_to_dict(response.data)
    keys = (
        'id',
        'isbn',
        'title',
        'num_of_pages',
        'publisher',
        'publication_date',
    )

    assert_that(response_data).contains_key(*keys)
コード例 #5
0
def test_that_me_returns_current_user_data(client):
    """
    GIVEN a Flask application
    WHEN the `/api/auth/me` route is called (GET) with valid credentials
    THEN check that the response data contains current user details
    """
    route = '{}/me'.format(base_route)
    user = User.query.first()
    access_token = create_access_token(identity=user)

    client.set_cookie('localhost', 'access_token', access_token)
    response = client.get(route)

    response_data = convert_to_dict(response.data)

    assert_that(response_data).contains_key('id', 'username')
コード例 #6
0
def test_that_users_are_created_correctly(client):
    """
    GIVEN a Flask application
    WHEN the `/api/users` route is posted (POST) to with valid credentials
    THEN check that the response meets the set conditions
    """
    user = User.query.filter(User.is_admin == True).first()
    access_token = create_access_token(identity=user)

    client.set_cookie('localhost', 'access_token', access_token)
    response = client.post(base_route, json=user_data())

    assert_that(response.status_code).is_equal_to(201)
    assert_that(response.headers['Content-Type']).contains('application/json')

    response_data = convert_to_dict(response.data)

    assert_that(response_data).contains_key('id', 'username', 'is_admin')
コード例 #7
0
def test_that_getting_existing_user_works(client):
    """
    GIVEN a Flask application
    WHEN the `/api/users/:id` route is called (GET) with valid credentials
    BUT with an id that exists in the database
    THEN check that the response meets the set conditions
    """
    user = User.query.filter(User.is_admin == True).first()
    access_token = create_access_token(identity=user)

    client.set_cookie('localhost', 'access_token', access_token)
    response = client.get('{}/2'.format(base_route))

    assert_that(response.status_code).is_equal_to(200)
    assert_that(response.headers['Content-Type']).contains('application/json')

    response_data = convert_to_dict(response.data)

    assert_that(response_data).contains_key('id', 'username', 'is_admin')
コード例 #8
0
def test_that_users_deleted_correctly(client):
    """
    GIVEN a Flask application
    WHEN the `/api/users/:id` route is called (DELETE) with valid credentials
    THEN check that the response meets the set conditions
    """
    user = User.query.filter(User.is_admin == True).first()
    access_token = create_access_token(identity=user)

    client.set_cookie('localhost', 'access_token', access_token)
    # create new user
    response = client.post(base_route, json=user_data())

    # delete created user
    response_data = convert_to_dict(response.data)
    response = client.delete('{}/{}'.format(base_route,
                                            response_data.get('id')))

    assert_that(response.status_code).is_equal_to(204)
    assert_that(response.headers['Content-Type']).contains('application/json')
コード例 #9
0
def test_that_categories_are_listed_correctly(client):
    """
    GIVEN a Flask application
    WHEN the `/api/categories` route is accessed (GET) with valid credentials
    THEN check that the response meets the set conditions
    """
    user = User.query.first()
    access_token = create_access_token(identity=user)

    client.set_cookie('localhost', 'access_token', access_token)
    response = client.get(base_route)

    assert_that(response.status_code).is_equal_to(200)
    assert_that(response.headers['Content-Type']).contains('application/json')

    response_data = convert_to_dict(response.data)

    assert_that(response_data).contains_key('data', 'links', 'meta')

    links_keys = ('first', 'last', 'next', 'previous')
    assert_that(response_data.get('links')).contains_key(*links_keys)

    meta_keys = ('current_page', 'last_page', 'per_page', 'total')
    assert_that(response_data.get('meta')).contains_key(*meta_keys)