Example #1
0
def invalid_data_test(method, url, token):
    """
    This functions sends http request to a given URL with different
    invalid data and checks for InvalidUsage (400 status code)
    :param http_method method: http method e.g. POST, PUT, DELETE
    :param string url: api url
    :param string token: auth token
    """
    data_set = [None, {}, get_fake_dict(), '', '  ', []]
    for data in data_set:
        response = send_request(method, url, token, data, is_json=False)
        assert response.status_code == codes.BAD_REQUEST
        response = send_request(method, url, token, data, is_json=True)
        assert response.status_code == codes.BAD_REQUEST
Example #2
0
def invalid_value_test(url,
                       data,
                       key,
                       values,
                       token,
                       method='post',
                       expected_status=(codes.BAD_REQUEST, )):
    """
    This function sends a request to given url with required field
    having an invalid value and checks that it returns InvalidUsage 400
    :param dict data: campaign data
    :param string url: api endpoint url
    :param string key: field key
    :param list values: possible invalid values
    :param string token: auth token
    :param http_method method: http request method, post/put
    :param tuple(int)  expected_status: what can be possible expected status for this request

    :Example:

        >>> invalid_values = ['', '  ', {}, [], None, True]
        >>> invalid_value_test(URL, campaign_data, 'body_text', invalid_values, token_first)
    """
    for val in values:
        data[key] = val
        response = send_request(method, url, token, data)
        assert response.status_code in expected_status, 'Invalid field %s with value %s' % (
            key, val)
Example #3
0
def create_candidate(talent_pool_id, token, expected_status=(201, )):
    """
    This method sends a POST request to Candidate API to create  a candidate.
    :type talent_pool_id: int | long
    :type token: string
    :type expected_status: tuple[int]
    :rtype dict
    """
    data = {
        "candidates": [{
            "first_name":
            fake.first_name(),
            "middle_name":
            fake.user_name(),
            "last_name":
            fake.last_name(),
            "talent_pool_ids": {
                "add": [talent_pool_id]
            },
            "emails": [{
                "label": "Primary",
                "address": fake.safe_email(),
                "is_default": True
            }]
        }]
    }
    response = send_request('post',
                            CandidateApiUrl.CANDIDATES,
                            token,
                            data=data)
    print('common_tests : create_candidate: ', response.content)
    assert response.status_code in expected_status
    return response.json()
Example #4
0
def create_talent_pipelines(token,
                            talent_pool_id,
                            count=1,
                            expected_status=(200, )):
    """
    This method sends a POST request to CandidatePool API to create  a talent pipeline.
    :type token: string
    :type talent_pool_id: int | long
    :type count: int
    :type expected_status: tuple[int]
    :rtype dict
    """
    data = {"talent_pipelines": []}
    for index in xrange(count):
        talent_pipeline = {
            "name":
            random_word(30),
            "description":
            fake.paragraph(),
            "talent_pool_id":
            talent_pool_id,
            "date_needed":
            (datetime.now() + timedelta(days=100)).strftime("%Y-%m-%d")
        }
        data["talent_pipelines"].append(talent_pipeline)
    response = send_request('post',
                            CandidatePoolApiUrl.TALENT_PIPELINES,
                            token,
                            data=data)
    print('common_tests : create_talent_pipelines: ', response.content)
    assert response.status_code in expected_status
    return response.json()
Example #5
0
def unauthorize_test(method, url, data=None):
    """
    This method is used to test for unauthorized requests (401).
    :param http_method method: http method
    :param string url: target url
    :param (dict | None) data: dictionary payload
    :return:
    """
    response = send_request(method, url, 'invalid_token', data)
    print('common_tests : unauthorize_test: ', response.content)
    assert response.status_code == codes.UNAUTHORIZED
Example #6
0
def delete_scheduler_task(task_id, token, expected_status=(200, )):
    """
    This method sends a DELETE request to Scheduler API to delete  a scheduled task.
    :type task_id: string
    :type token: string
    :type expected_status: tuple[int]
    :rtype dict
    """
    response = send_request('delete', SchedulerApiUrl.TASK % task_id, token)
    print('common_tests : delete_scheduler: ', response.content)
    assert response.status_code in expected_status
    return response.json()
Example #7
0
def delete_candidate(candidate_id, token, expected_status=(200, )):
    """
    This method sends a DELETE request to Candidate API to delete a candidate given by candidate_id.
    :type candidate_id: int | long
    :type token: string
    :type expected_status: tuple[int]
    :rtype dict
    """
    response = send_request('delete', CandidateApiUrl.CANDIDATE % candidate_id,
                            token)
    print('common_tests : delete_candidate: ', response.content)
    assert response.status_code in expected_status
Example #8
0
def unexpected_field_test(method, url, data, token):
    """
    This function send a request to given URL with an unexpected field in request body.
    API should raise InvalidUsage 400
    :param http_method method: request method, POST/PUT
    :param string url: API resource url
    :param dict data: request data
    :param string token: access token
    """
    fake_word = fake.word()
    data[fake_word] = fake_word
    response = send_request(method, url, token, data)
    assert response.status_code == codes.BAD_REQUEST, 'Unexpected field name: %s' % fake_word
Example #9
0
def invalid_data_test(method, url, token):
    """
    This functions sends http request to a given url with different
    invalid data and checks for InvalidUsage
    :param http_method method: http method e.g. POST, PUT
    :param string url: api url
    :param string token: auth token
    :return:
    """
    data = None
    response = send_request(method, url, token, data, is_json=True)
    assert response.status_code == codes.BAD_REQUEST
    response = send_request(method, url, token, data, is_json=False)
    assert response.status_code == codes.BAD_REQUEST
    data = {}
    response = send_request(method, url, token, data, is_json=True)
    assert response.status_code == codes.BAD_REQUEST
    response = send_request(method, url, token, data, is_json=False)
    assert response.status_code == codes.BAD_REQUEST
    data = get_fake_dict()
    response = send_request(method, url, token, data, is_json=False)
    assert response.status_code == codes.BAD_REQUEST
Example #10
0
def get_talent_pipeline(talent_pipeline_id, token, expected_status=(200, )):
    """
    This method sends a GET request to CandidatePool API to get talent pipeline.
    :type talent_pipeline_id: int | long
    :type token: string
    :type expected_status: tuple[int]
    :rtype dict
    """
    response = send_request(
        'get', CandidatePoolApiUrl.TALENT_PIPELINE % talent_pipeline_id, token)
    print('common_tests : get_talent_pipeline: ', response.content)
    assert response.status_code in expected_status
    return response.json()
Example #11
0
def get_and_assert_zero(url, key, token, sleep_time=SLEEP_TIME):
    """
    This function gets list of objects from given url and asserts that length of objects under a given key is zero.
    It keeps on retrying this process until it founds some records or sleep_time is over
    :param string url: URL of requested resource
    :param string key: key in response that has resource list
    :param string token: user access token
    :param int sleep_time: maximum time to wait
    """
    attempts = sleep_time / SLEEP_INTERVAL
    for _ in retrier(attempts=attempts, sleeptime=SLEEP_INTERVAL,
                     sleepscale=1):
        assert len(send_request('get', url, token).json()[key]) == 0
Example #12
0
def get_candidate(candidate_id, token, expected_status=(200, )):
    """
    This method sends a GET request to Candidate API to get a candidate info.
    :type candidate_id: int | long
    :type token: string
    :type expected_status: tuple[int]
    :rtype dict
    """
    response = send_request('get', CandidateApiUrl.CANDIDATE % candidate_id,
                            token)
    print('common_tests : get_candidate: ', response.content)
    assert response.status_code in expected_status
    return response.json()
Example #13
0
def remove_roles(user_id, roles, token):
    """
    This method sends a DELETE request to UserService to remove given roles to given user
    :param (int | long) user_id: id of user
    :param list[>0] roles: permissions list
    :param string token: auth token
    """
    data = {"roles": roles}
    response = send_request('delete',
                            UserServiceApiUrl.USER_ROLES_API % user_id,
                            token,
                            data=data)
    print('common_tests : remove_roles: ', response.content)
    assert response.status_code in [codes.OK, codes.BAD_REQUEST]
Example #14
0
def delete_talent_pool(talent_pool_id, token, expected_status=(200, )):
    """
    This method sends a DELETE request to CandidatePool API to delete a talent pool.
    :type talent_pool_id: int | long
    :type token: string
    :type expected_status: tuple[int]
    :rtype dict
    """
    response = send_request('delete',
                            CandidatePoolApiUrl.TALENT_POOL % talent_pool_id,
                            token)
    print('common_tests : delete_talent_pool: ', response.content)
    assert response.status_code in expected_status
    return response.json()
Example #15
0
def get_user(user_id, token):
    """
    This utility is used to get user info from UserService
    :param user_id: user unique identifier
    :type user_id: int | long
    :param token: authentication token for user
    :type token: string
    :return: user dictionary
    :rtype: dict
    """
    response = send_request('get', UserServiceApiUrl.USER % user_id, token)
    print('common_tests : get_user: '******'user']
Example #16
0
def delete_smartlist(smartlist_id, token, expected_status=(200, )):
    """
    This method sends a DELETE request to CandidatePool API to delete a smartlist.
    :type smartlist_id: int | long
    :type token: string
    :type expected_status: tuple[int]
    :rtype dict
    """
    response = send_request('delete',
                            CandidatePoolApiUrl.SMARTLIST % smartlist_id,
                            token)
    print('common_tests : delete_smartlist: ', response.content)
    assert response.status_code in expected_status
    return response.json()
Example #17
0
def get_candidate_devices(candidate_id, token, expected_status=(200, )):
    """
    This method sends a GET request to Candidate API to get all associated OneSignal Device Ids to a candidate.

    :type candidate_id: int | long
    :type token: string
    :type expected_status: tuple[int]
    :rtype dict
    """
    response = send_request('get', CandidateApiUrl.DEVICES % candidate_id,
                            token)
    print('tests : get_candidate_devices: %s', response.content)
    assert response.status_code in expected_status
    return response.json()
Example #18
0
def missing_keys_test(url, data, keys, token, method='post'):
    """
    This function sends a request to given url after removing required field from given (valid) data.
    We are expecting that API should raise InvalidUsage error (400 status code)
    :param string url: api endpoint url
    :param dict data: request body data
    :param list | tuple keys: required fields
    :param string token: auth token
    :param http_method method: http request method, post/put
    """
    for key in keys:
        new_data = data.copy()
        new_data.pop(key)
        response = send_request(method, url, token, new_data)
        assert response.status_code == codes.BAD_REQUEST, 'Test failed for key: %s' % key
Example #19
0
def search_candidates(candidate_ids, token, expected_status=(200, )):
    """
    This method sends a GET request to Candidate Search API to get candidates from CloudSearch.
    :type candidate_ids: list|tuple
    :type token: string
    :type expected_status: tuple[int]
    :rtype dict
    """
    params = {'candidate_ids': candidate_ids}
    response = send_request('get',
                            CandidateApiUrl.CANDIDATE_SEARCH_URI,
                            token,
                            data=params)
    print('common_tests : get_candidate: ', response.content)
    assert response.status_code in expected_status
    return response.json()
Example #20
0
def get_smartlist_candidates(smartlist_id,
                             token,
                             expected_status=(200, ),
                             count=None):
    """
    This method sends a GET request to CandidatePool API to get list of candidates associated to  a smartlist.
    :type smartlist_id: int | long
    :type token: string
    :type expected_status: tuple[int]
    :rtype dict
    """
    response = send_request(
        'get', CandidatePoolApiUrl.SMARTLIST_CANDIDATES % smartlist_id, token)
    print('common_tests : get_smartlist_candidates: ', response.content)
    assert response.status_code in expected_status
    response = response.json()
    if count:
        assert len(response['candidates']) == count,\
            'Expecting {} candidates, Found:{}'.format(count, len(response['candidates']))
    return response
Example #21
0
def add_roles(user_id, roles, token):
    """
    This method sends a POST request to UserService to add given roles to given user
    :param ((int | long), >0) user_id: user unique id in gt database
    :param list | tuple roles: permissions list
    :param string token: auth token
    """
    assert roles, 'roles should be a non-empty list or tuple, given: %s' % roles
    assert token, 'token should be a non-empty string, given: %s' % token
    for role in roles:
        data = {"roles": [role]}
        response = send_request('post',
                                UserServiceApiUrl.USER_ROLES_API % user_id,
                                token,
                                data=data)
        if response.status_code == codes.BAD_REQUEST:
            assert response.json(
            )['error']['code'] == ErrorCodes.ROLE_ALREADY_EXISTS
        else:
            assert response.status_code == codes.OK
Example #22
0
def get_activity(type_id,
                 source_id,
                 source_table,
                 token,
                 expected_status=(200, )):
    """
    This method sends a GET request to Activity Service API to get specific activity.
    :param int | long type_id: activity type id, like 4 for campaign creation
    :param int | long source_id: id of source object like push campaign id
    :param string source_table: source table name, like push_campaign
    :param string token: access token
    :type expected_status: tuple[int]
    :rtype dict
    """
    url = "{}?type={}&source_id={}&source_table={}".format(
        ActivityApiUrl.ACTIVITIES, type_id, source_id, source_table)
    response = send_request('get', url, token)
    print('common_tests : get_activity: ', response.content)
    assert response.status_code in expected_status
    return response.json()
Example #23
0
def delete_candidate_device(candidate_id,
                            device_id,
                            token,
                            expected_status=(200, )):
    """
    This method sends a DELETE request to Candidate API to delete  OneSignal Device that is associated to a candidate.

    :type candidate_id: int | long
    :type device_id: string
    :type token: string
    :type expected_status: tuple[int]
    :rtype dict
    """
    data = {'one_signal_device_id': device_id}
    response = send_request('delete',
                            CandidateApiUrl.DEVICES % candidate_id,
                            token,
                            data=data)
    print('tests : delete_candidate_devices: %s', response.content)
    assert response.status_code in expected_status
    return response.json()
Example #24
0
def associate_device_to_candidate(candidate_id,
                                  device_id,
                                  token,
                                  expected_status=(201, )):
    """
    This method sends a POST request to Candidate API to associate a OneSignal Device Id to a candidate.

    :type candidate_id: int | long
    :type device_id: string
    :type token: string
    :type expected_status: tuple[int]
    :rtype dict
    """
    data = {'one_signal_device_id': device_id}
    response = send_request('post',
                            CandidateApiUrl.DEVICES % candidate_id,
                            token,
                            data=data)
    print('tests : associate_device_to_candidate: %s', response.content)
    assert response.status_code in expected_status
    return response.json()
Example #25
0
def add_test_venue(token, user, social_network):
    social_network_id = social_network['id']
    venue = {
        "social_network_id": social_network_id,
        "user_id": user['id'],
        "zip_code": "54600",
        "address_line_2": "H# 163, Block A",
        "address_line_1": "New Muslim Town",
        "latitude": 0,
        "longitude": 0,
        "state": "Punjab",
        "city": "Lahore",
        "country": "Pakistan"
    }

    response_post = send_request('POST',
                                 SocialNetworkApiUrl.VENUES,
                                 access_token=token,
                                 data=venue)
    assert response_post.status_code == codes.created, response_post.text
    venue_id = response_post.json()['id']
    return {'id': venue_id}
Example #26
0
def create_talent_pools(token, count=1, expected_status=(200, )):
    """
    This method sends a POST request to CandidatePool API to create a talent pool.
    :type token: string
    :type count: int | long
    :type expected_status: tuple[int]
    :rtype dict
    """
    data = {"talent_pools": []}
    for index in xrange(count):
        talent_pool = {
            "name": str(uuid.uuid4())[:20],
            "description": fake.paragraph()
        }
        data["talent_pools"].append(talent_pool)
    response = send_request('post',
                            CandidatePoolApiUrl.TALENT_POOLS,
                            token,
                            data=data)
    print('common_tests : create_talent_pools: ', response.content)
    assert response.status_code in expected_status
    return response.json()
Example #27
0
def create_smartlist(candidate_ids,
                     talent_pipeline_id,
                     token,
                     expected_status=(201, )):
    """
    This method sends a POST request to CandidatePool API to create a smartlist.
    :type candidate_ids: list|tuple
    :type talent_pipeline_id: int | long
    :type token: string
    :type expected_status: tuple[int]
    :rtype dict
    """
    data = {
        'candidate_ids': candidate_ids,
        'name': fake.word(),
        "talent_pipeline_id": talent_pipeline_id
    }
    response = send_request('post',
                            CandidatePoolApiUrl.SMARTLISTS,
                            token,
                            data=data)
    print('common_tests : create_smartlist: ', response.content)
    assert response.status_code in expected_status
    return response.json()