Ejemplo n.º 1
0
def test_user_search(request_helper, default_data, session):
    user = default_data.users.dr_who
    session.add(user)

    url = UserResource.get_collection_path() + '@search'
    # Search user by email
    response = request_helper.get_json(url, params={'email': user.email})
    assert response.status_int == 200
    assert isinstance(response.json, dict)
    assert 'id' in response.json
    assert response.json['id'] == user.id

    # Search user by token
    response = request_helper.get_json(url, params={'token': user.token})
    assert response.status_int == 200
    assert isinstance(response.json, dict)
    assert 'id' in response.json
    assert response.json['id'] == user.id

    # Search user using invalid an invalid value
    response = request_helper.get_json(url, params={'token': 'INVALID'})
    assert response.status == '400 Bad Request'
    assert isinstance(response.json, dict)
    assert 'error' in response.json
    assert response.json['error']['code'] == 'USER_NOT_FOUND'
Ejemplo n.º 2
0
def test_user_update_multiple(request_helper):
    # Get two random users from database
    url = UserResource.get_collection_path()
    response = request_helper.get_json(url)
    index = len(response.json) / 2
    old_user_1 = response.json[index]
    old_user_2 = response.json[index + 1]

    # Use other users data to update old users
    new_user_1 = dict(USER_DATA[0])
    new_user_1['id'] = old_user_1['id']
    new_user_2 = dict(USER_DATA[1])
    new_user_2['id'] = old_user_2['id']

    data = [new_user_1, new_user_2]
    response = request_helper.put_json(url, data)
    assert response.status == '200 OK'
    # Check that 2 users were updated
    assert response.json['info']['count'] == 2

    url = UserResource.get_member_path(old_user_1['id'])
    user = request_helper.get_json(url).json
    assert user['first_name'] == new_user_1['first_name']
    assert user['last_name'] == new_user_1['last_name']
    assert user['email'] == new_user_1['email']

    url = UserResource.get_member_path(old_user_2['id'])
    user = request_helper.get_json(url).json
    assert user['first_name'] == new_user_2['first_name']
    assert user['last_name'] == new_user_2['last_name']
    assert user['email'] == new_user_2['email']
Ejemplo n.º 3
0
def test_user_data_field(request_helper):
    """
    Test for user data (JSON) field.

    """
    # Get the first user in list
    url = UserResource.get_collection_path()
    response = request_helper.get_json(url)
    assert response.status_int == 200
    assert isinstance(response.json, list)
    user = response.json[0]

    # Update user data field
    user['data'] = {'test_field': 'test_value'}
    url = UserResource.get_member_path(user['id'])
    response = request_helper.put_json(url, user)
    assert response.status_int == 200
    assert isinstance(response.json, dict)

    # Check that user has the new data values
    user = response.json
    assert isinstance(user['data'], dict)
    assert 'test_field' in user['data']
    assert user['data']['test_field'] == 'test_value'
    assert len(user['data']) == 1
Ejemplo n.º 4
0
def test_user_delete_multiple(request_helper):
    # Get number of users
    url = UserResource.get_collection_path()
    response = request_helper.get_json(url)
    assert response.status == '200 OK'
    user_count = len(response.json)

    # Get two random users from database
    index = len(response.json) / 2
    old_user_1 = response.json[index]
    old_user_2 = response.json[index + 1]

    # Delete 2 users
    data = [old_user_1, old_user_2]
    response = request_helper.delete_json(url, data)
    assert response.status == '200 OK'
    assert response.json['info']['count'] == 2

    # Check that both users were deleted
    deleted_ids = [old_user_1['id'], old_user_2['id']]
    response = request_helper.get_json(url, params={'id': deleted_ids})
    assert response.status == '200 OK'
    # Check that no user was returned
    assert not response.json

    # Get number of users again
    url = UserResource.get_collection_path()
    response = request_helper.get_json(url)
    assert response.status == '200 OK'
    # Check that there are 2 users less
    assert (user_count - 2) == len(response.json)

    # Calls to delete without JSON data should fail
    response = request_helper.delete_json(url)
    assert response.status_int == 400
    assert 'error' in response.json
    assert response.json['error'].get('code') == 'COLLECTION_EXPECTED'
Ejemplo n.º 5
0
def test_user_create_multiple(request_helper):
    # Check that only one exist
    url = UserResource.get_collection_path()
    response = request_helper.get_json(url)
    user_count = len(response.json)
    assert user_count > 0

    # Create three users
    response = request_helper.post_json(url, USER_DATA)
    assert response.status == '200 OK'
    assert len(response.json) == len(USER_DATA)

    # Only 4 more users should exist
    response_get = request_helper.get_json(url)
    assert len(response_get.json) == len(USER_DATA) + user_count
Ejemplo n.º 6
0
def test_user_delete_single(request_helper):
    # Get a random user
    url = UserResource.get_collection_path()
    response = request_helper.get_json(url)
    index = len(response.json) / 2
    user = response.json[index + 1]

    # Delete user using primary key
    url = UserResource.get_member_path(user['id'])
    response = request_helper.delete_json(url)
    assert response.status == '200 OK'

    # Check that user does not exist
    with pytest.raises(NotFound):
        request_helper.get_json(url)
Ejemplo n.º 7
0
def test_permissive_rest_collection_mode(request_helper):
    headers = {'X-REST-Collection-Mode': 'permissive'}
    url = UserResource.get_collection_path()

    # Mode permissive should not fail when no collection is sent
    # and it should return an object when an object is sent
    user = USER_LIST[0]
    response = request_helper.post_json(url, user, headers=headers)
    assert response.status_int == 200
    assert isinstance(response.json_body, dict)

    # Mode permissive should return a list when a list is sent
    user = USER_LIST[1]
    response = request_helper.post_json(url, [user], headers=headers)
    assert response.status_int == 200
    assert isinstance(response.json_body, list)
Ejemplo n.º 8
0
def test_user_get(request_helper):
    # Get a random user
    url = UserResource.get_collection_path()
    response = request_helper.get_json(url)
    index = len(response.json) / 2
    user = response.json[index - 1]

    # get user using primary key
    url = UserResource.get_member_path(user['id'])
    response = request_helper.get_json(url)
    assert response.status == '200 OK'

    # Check that user is the same for both cases
    same_user = response.json
    assert user['first_name'] == same_user['first_name']
    assert user['last_name'] == same_user['last_name']
    assert user['email'] == same_user['email']
Ejemplo n.º 9
0
def test_strict_rest_collection_mode(request_helper):
    headers = {'X-REST-Collection-Mode': 'strict'}
    user = USER_LIST[0]
    url = UserResource.get_collection_path()

    # Mode strict should fail when no collection is sent
    response = request_helper.post_json(url, user, headers=headers)
    assert response.status_int == 400
    response_data = response.json_body
    assert 'error' in response_data
    assert 'code' in response_data['error']
    assert response_data['error']['code'] == 'COLLECTION_EXPECTED'

    # When a collection is sent is should work and return a collection
    response = request_helper.post_json(url, [user], headers=headers)
    assert response.status_int == 200
    assert isinstance(response.json_body, list)
Ejemplo n.º 10
0
def test_user_signup(request_helper):
    request_helper.require_authorization = False

    # Signup a user
    url = UserResource.get_collection_path() + "@signup"
    response = request_helper.post_json(url, USER_DATA[2])
    assert response.status == '200 OK'
    assert isinstance(response.json, dict)
    assert 'token' in response.json
    assert 'key' in response.json
    assert response.json['token'] is not None
    assert response.json['key'] is not None

    # Signin an existing user
    response = request_helper.post_json(url, USER_DATA[2])
    assert response.status == '400 Bad Request'
    assert isinstance(response.json, dict)
    assert 'error' in response.json
    assert response.json['error']['code'] == 'USER_EMAIL_EXISTS'
Ejemplo n.º 11
0
def test_user_create_single(request_helper):
    # Create a user
    url = UserResource.get_collection_path()
    data = USER_DATA[0]
    response = request_helper.post_json(url, [data])
    # All post to collection should returns a collection
    assert isinstance(response.json, list) is True
    # User updated information is returned a single item in a list
    user_data = response.json[0]
    created_id = user_data['id']

    # Get newly created user based on ID
    url = UserResource.get_member_path(created_id)
    response = request_helper.get_json(url)
    assert response.status == '200 OK'

    # Check that user data is right
    new_user = response.json
    assert new_user['first_name'] == data['first_name']
    assert new_user['last_name'] == data['last_name']
    assert new_user['email'] == data['email']
Ejemplo n.º 12
0
def test_user_update_single(request_helper):
    # Get user data
    data = dict(USER_DATA[2])

    # Get a random user
    url = UserResource.get_collection_path()
    response = request_helper.get_json(url)
    index = len(response.json) / 2
    user = response.json[index]
    assert user['first_name'] != data['first_name']
    assert user['last_name'] != data['last_name']
    assert user['email'] != data['email']

    # Update user data
    url = UserResource.get_member_path(user['id'])
    data['id'] = user['id']
    response = request_helper.put_json(url, data)
    assert response.status == '200 OK'

    # Check that data was updated
    updated_user = response.json
    assert updated_user['first_name'] == data['first_name']
    assert updated_user['last_name'] == data['last_name']
    assert updated_user['email'] == data['email']
Ejemplo n.º 13
0
def test_user_signin(request_helper, default_data, session):
    request_helper.require_authorization = False

    user = default_data.users.james_william_elliot
    session.add(user)

    signin_data = {'email': user.email, 'password': '******'}

    # Signin using current user data
    url = UserResource.get_collection_path() + "@signin"
    response = request_helper.post_json(url, signin_data, expect_errors=True)
    assert response.status == '200 OK'
    assert 'token' in response.json
    assert 'key' in response.json
    assert response.json['token'] is not None
    assert response.json['key'] is not None

    # Try to signin using an invalid password
    signin_data['password'] = '******'
    response = request_helper.post_json(url, signin_data, expect_errors=True)
    assert response.status == '400 Bad Request'
    assert isinstance(response.json, dict)
    assert 'error' in response.json
    assert response.json['error']['code'] == 'INVALID_SIGNIN'