コード例 #1
0
def test_find_instance_by_email(context):
    ("user.find_instance_by_email should return a list of users with no element or a single one"
     )

    # Given I have an existing user
    first_name = 'Guido'
    email = '*****@*****.**'
    password = '******'
    user = User(first_name, email, password)
    user.persist()
    context.objects.append(user)

    # And that I call the find_instance_by_email with the user email
    result = User.find_instance_by_email(user.email)

    # Then the result should be a list
    result.should.be.a(list)

    # And the list should have an unique element
    len(result).should.equal(1)

    # And the user should be in the list
    user.should.be.within(result)

    # However if I call the find_instance_by_email with an invalid email
    result = User.find_instance_by_email("*****@*****.**")

    # Then the result should be a list
    result.should.be.a(list)

    # And the list should be empty
    result.should.be.empty
コード例 #2
0
def test_api_create_user(context):
    ("POST on /api/user should return a json containing the information persisted in database"
     )

    # Given that I perform a POST to /api/user with a valid JSON data
    first_name = 'Guido'
    email = '*****@*****.**'
    password = '******'
    json_data = {
        enums.FIRST_NAME_KEY: first_name,
        enums.EMAIL_KEY: email,
        enums.PASSWORD_KEY: password
    }

    response = context.http.post('/api/user', json=json_data)
    context.objects.append(User.find_instance_by_email(email)[0])

    # When I check the response
    response.headers.should.have.key("Content-Type").being.equal(
        "application/json")

    # And check if the status was success
    response.status_code.should.equal(enums.HTTP_SUCCESS)

    # And when I deserialize the JSON
    data = json.loads(response.data)

    # Then the data should have a specific key and value as below
    data.should.have.key(enums.FIRST_NAME_KEY).being.equal(first_name)
    data.should.have.key(enums.EMAIL_KEY).being.equal(email)
    data.should.have.key(enums.UUID_KEY)
コード例 #3
0
def test_api_delete_user(context):
    ("DELETE on /api/user/<string:email> should remove the user persisted in database"
     )

    # Given that I have already an user in the database
    first_name = 'Guido'
    email = '*****@*****.**'
    password = '******'
    user_created = User(first_name, email, password)
    user_created.persist()

    # Given that I perform a DELETE to /api/user/email
    response = context.http.delete(f'/api/user/{email}')

    # When I check the response
    response.headers.should.have.key("Content-Type").being.equal(
        "application/json")

    # And check if the status was success
    response.status_code.should.equal(enums.HTTP_SUCCESS)

    # And when I deserialize the JSON
    data = json.loads(response.data)

    # Then the data should have a specific key and value as below
    data.should.have.key(enums.FIRST_NAME_KEY).being.equal(first_name)
    data.should.have.key(enums.EMAIL_KEY).being.equal(email)
    data.should.have.key(enums.UUID_KEY).being.equal(user_created.uuid)

    # And the user should not be persisted anymore
    user_list = User.find_instance_by_email(email)
    user_list.should.be.empty
コード例 #4
0
def edit_database_user(**kwargs):
    ''' Interface to edit user database entry
        Should receive UUID and first_name and/or email and/or password
        Will return first_name, email and uuid upon success
    '''
    user_list = User.find_instance_by_uuid(kwargs.get(enums.UUID_KEY))
    if user_list:
        user = user_list[0]
        first_name = kwargs.get(enums.FIRST_NAME_KEY)
        email = kwargs.get(enums.EMAIL_KEY)
        if email and User.find_instance_by_email(email):
            raise Exception(f'Could not edit user: {email} already in use')

        password = kwargs.get(enums.PASSWORD_KEY)
        if any([first_name, email, password]):
            try:
                user.edit_user(first_name, email, password)
                user.persist()
                return user.payload()
            except Exception as err:
                raise Exception(f'Could not edit user: {err}')

        else:
            raise Exception('No new user information was passed in')

    else:
        raise Exception('No user was found for the given UUID')
コード例 #5
0
def test_create_database_user(context):
    ("user_interface.create_database_user should return an user payload (dict)"
     )

    # Given I have a valid data
    first_name = 'Guido'
    email = '*****@*****.**'
    password = '******'
    json_data = {
        enums.FIRST_NAME_KEY: first_name,
        enums.EMAIL_KEY: email,
        enums.PASSWORD_KEY: password
    }

    # And that I call create_database_user
    result = user_interface.create_database_user(**json_data)
    user = User.find_instance_by_email(email)[0]
    context.objects.append(user)

    # When I check the result

    # Then it should be a dictionary
    result.should.be.a(dict)

    # And it should be the user payload
    result.should.equal(user.payload())

    # However if I try to add the same json_data when I call create_database_user it should throw an Exception
    user_interface.create_database_user.when.called_with(
        **json_data).should.have.raised(Exception)

    # And if I try to add a json with missing information it should also throw an Exception
    json_data_invalid = {}
    user_interface.create_database_user.when.called_with(
        **json_data_invalid).should.have.raised(Exception)
コード例 #6
0
def delete_database_user(**kwargs):
    ''' Interface to remove user database entry
        Should receive email
    '''
    email = kwargs.get(enums.EMAIL_KEY)
    user_list = User.find_instance_by_email(email)
    if user_list:
        user = user_list[0]
        payload = user.payload()
        user.remove()
        return payload

    else:
        raise Exception(f'No user was found for the given email {email}')
コード例 #7
0
def test__delete_user_handler(context):
    ("controller._delete_user_handler should return a tuple with a dictionary and a HTTP code status"
     )

    # Given I have an existing user
    first_name = 'Guido'
    email = '*****@*****.**'
    password = '******'
    user_created = User(first_name, email, password)
    user_created.persist()

    # And that I call _delete_user_handler
    data_json = {enums.EMAIL_KEY: email}
    result = controller._delete_user_handler(data_json)

    # When I check the result

    # Then it should be a tuple containing a dict and an int
    result.should.be.a(tuple)
    payload_dict, http_status_code = result

    # And it should have a list of dicts and a 200 status code
    payload_dict.should.have.key(enums.FIRST_NAME_KEY).being.equal(first_name)
    payload_dict.should.have.key(enums.EMAIL_KEY).being.equal(email)
    payload_dict.should.have.key(enums.UUID_KEY).being.equal(user_created.uuid)
    http_status_code.should.equal(enums.HTTP_SUCCESS)

    # And the user should not be persisted anymore
    user_list = User.find_instance_by_email(email)
    user_list.should.be.empty

    # However if I try to remove an inexisting email
    invalid_email = '*****@*****.**'

    # And that I call _delete_user_handler
    json_data_invalid = {enums.EMAIL_KEY: invalid_email}
    result = controller._delete_user_handler(json_data_invalid)

    # When I check the result

    # Then it should be a tuple containing a dict and an int
    result.should.be.a(tuple)
    payload_dict, http_status_code = result

    # And it should have a dict with an error and a 500 status code
    payload_dict.should.have.key('error').should.contain(invalid_email)
    http_status_code.should.equal(enums.HTTP_ERR_INT)
コード例 #8
0
def create_database_user(**kwargs):
    ''' Interface to create user database entry
        Should receive first_name, email and password
        Will return first_name, email and uuid upon success
    '''
    email = kwargs.get(enums.EMAIL_KEY)
    if User.find_instance_by_email(email):
        raise Exception(f'Could not create user: email {email} already in use')

    first_name = kwargs.get(enums.FIRST_NAME_KEY)
    password = kwargs.get(enums.PASSWORD_KEY)
    try:
        new_user = User(first_name, email, password)
        new_user.persist()
        return new_user.payload()
    except Exception as err:
        raise Exception(f'Could not create user: {err}')
コード例 #9
0
def test__create_user_handler(context):
    ("controller._create_user_handler should return a tuple with a dictionary and a HTTP code status"
     )

    # Given I have a valid json_data
    first_name = 'Guido'
    email = '*****@*****.**'
    password = '******'
    json_data = {
        enums.FIRST_NAME_KEY: first_name,
        enums.EMAIL_KEY: email,
        enums.PASSWORD_KEY: password
    }

    # And that I call _create_user_handler
    result = controller._create_user_handler(json_data)
    user = User.find_instance_by_email(email)[0]
    context.objects.append(user)

    # When I check the result

    # Then it should be a tuple containing a dict and an int
    result.should.be.a(tuple)
    payload_dict, http_status_code = result

    # And it should have a dict and a 200 status code
    payload_dict.should.have.key(enums.FIRST_NAME_KEY).being.equal(first_name)
    payload_dict.should.have.key(enums.EMAIL_KEY).being.equal(email)
    payload_dict.should.have.key(enums.UUID_KEY).being.equal(user.uuid)

    http_status_code.should.equal(enums.HTTP_SUCCESS)

    # However if I try to add the same json_data when I call _create_user_handler
    result = controller._create_user_handler(json_data)

    # When I check the result

    # Then it should be a tuple
    result.should.be.a(tuple)
    payload_dict, http_status_code = result

    # And it should have a dict with an error and a 500 status code
    payload_dict.should.have.key('error').should.contain(email)
    http_status_code.should.equal(500)