Beispiel #1
0
def test_validation_job_id(dispatcher_live_fixture):
    server = dispatcher_live_fixture
    DispatcherJobState.remove_scratch_folders()

    logger.info("constructed server: %s", server)

    # let's generate a valid token
    token_payload = {
        **default_token_payload,
    }
    encoded_token = jwt.encode(token_payload, secret_key, algorithm='HS256')

    # these parameters define request content
    base_dict_param = dict(
        instrument="empty-async",
        product_type="dummy",
        query_type="real",
    )

    dict_param = dict(query_status="new",
                      token=encoded_token,
                      **base_dict_param)

    # this should return status submitted, so email sent
    c = requests.get(server + "/run_analysis", dict_param)

    print(json.dumps(c.json(), sort_keys=True, indent=4))

    assert c.status_code == 200
    dispatcher_job_state = DispatcherJobState.from_run_analysis_response(c)
    jdata = c.json()
    assert jdata['exit_status']['job_status'] == 'submitted'

    # let's generate another valid token, just for a different user
    token_payload['sub'] = "*****@*****.**"

    # this should return status submitted, so email sent
    dict_param['token'] = jwt.encode(token_payload,
                                     secret_key,
                                     algorithm='HS256')
    dict_param[
        'job_id'] = dispatcher_job_state.job_id  # this is job id from different user
    dict_param['query_status'] = 'submitted'

    c = requests.get(server + "/run_analysis", dict_param)

    wrong_job_id = make_hash({**base_dict_param, "sub": "*****@*****.**"})

    from cdci_data_analysis.flask_app.dispatcher_query import InstrumentQueryBackEnd
    assert InstrumentQueryBackEnd.restricted_par_dic(
        dict_param) == base_dict_param

    assert c.status_code == 403, json.dumps(c.json(), indent=4, sort_keys=True)
    jdata = c.json()

    assert jdata["exit_status"]["debug_message"] == \
           f'The provided job_id={dispatcher_job_state.job_id} does not match with the ' \
           f'job_id={wrong_job_id} derived from the request parameters for your user account email; parameters are derived from recorded job state'
    assert jdata["exit_status"]["error_message"] == ""
    assert jdata["exit_status"]["message"] == "Request not authorized"
def empty_products_user_files_fixture(default_params_dict,
                                      default_token_payload):
    sub = default_token_payload['sub']

    # generate job_id related to a certain user
    job_id = make_hash({
        **InstrumentQueryBackEnd.restricted_par_dic(default_params_dict), "sub":
        sub
    })

    # generate random session_id
    session_id = u''.join(
        random.choice(string.ascii_uppercase + string.digits)
        for _ in range(16))
    scratch_params = dict(job_id=job_id, session_id=session_id)
    DispatcherJobState.remove_scratch_folders(job_id=job_id)
    DispatcherJobState.remove_download_folders()

    scratch_dir_path = f'scratch_sid_{session_id}_jid_{job_id}'
    # set the scratch directory
    os.makedirs(scratch_dir_path)

    with open(scratch_dir_path + '/test.fits.gz', 'wb') as fout:
        scratch_params['content'] = os.urandom(20)
        fout.write(scratch_params['content'])

    with open(scratch_dir_path + '/analysis_parameters.json', 'w') as outfile:
        my_json_str = json.dumps(default_params_dict, indent=4)
        outfile.write(u'%s' % my_json_str)

    yield scratch_params
Beispiel #3
0
def test_scws_list_file(dispatcher_live_fixture):
    server = dispatcher_live_fixture
    logger.info("constructed server: %s", server)

    # let's generate a valid token
    token_payload = {
        **default_token_payload,
        "roles": "unige-hpc-full, general",
    }
    encoded_token = jwt.encode(token_payload, secret_key, algorithm='HS256')

    params = {
        **default_params, 'product_type': 'numerical',
        'query_type': "Dummy",
        'instrument': 'empty',
        'p': 5,
        'use_scws': 'user_file',
        'token': encoded_token
    }

    file_path = DispatcherJobState.create_p_value_file(p_value=5)

    list_file = open(file_path)

    jdata = ask(server,
                params,
                expected_query_status=["done"],
                max_time_s=150,
                method='post',
                files={"user_scw_list_file": list_file.read()})

    list_file.close()
    assert 'p_list' in jdata['products']['analysis_parameters']
    assert 'use_scws' not in jdata['products']['analysis_parameters']
    assert jdata['products']['analysis_parameters']['p_list'] == ['5']
    # test job_id
    job_id = jdata['products']['job_id']
    params.pop('use_scws', None)
    # adapting some values to string
    for k, v in params.items():
        params[k] = str(v)

    restricted_par_dic = InstrumentQueryBackEnd.restricted_par_dic({
        **params, "p_list": ["5"],
        "sub":
        "*****@*****.**"
    })
    calculated_job_id = make_hash(restricted_par_dic)

    assert job_id == calculated_job_id
Beispiel #4
0
def test_download_products_unauthorized_user(dispatcher_live_fixture,
                                             empty_products_user_files_fixture,
                                             default_params_dict):
    server = dispatcher_live_fixture

    logger.info("constructed server: %s", server)

    # let's generate a valid token with high threshold
    token_payload = {
        **default_token_payload, "sub": "*****@*****.**",
        "mstout": True,
        "mssub": True,
        "intsub": 5
    }

    encoded_token = jwt.encode(token_payload, secret_key, algorithm='HS256')

    session_id = empty_products_user_files_fixture['session_id']
    job_id = empty_products_user_files_fixture['job_id']

    params = {
        # since we are passing a job_id
        'query_status': 'ready',
        'file_list': 'test.fits.gz',
        'download_file_name': 'output_test',
        'session_id': session_id,
        'job_id': job_id,
        'token': encoded_token
    }

    c = requests.get(server + "/download_products", params=params)
    default_param_dict = default_params_dict
    default_param_dict.pop('token', None)
    default_param_dict.pop('session_id', None)
    default_param_dict.pop('job_id', None)
    wrong_job_id = make_hash(
        InstrumentQueryBackEnd.restricted_par_dic({
            **default_param_dict, "sub":
            "*****@*****.**"
        }))

    assert c.status_code == 403

    jdata = c.json()
    assert jdata["exit_status"]["debug_message"] == \
           f'The provided job_id={job_id} does not match with the ' \
           f'job_id={wrong_job_id} derived from the request parameters for your user account email; parameters are derived from recorded job state'
    assert jdata["exit_status"]["error_message"] == ""
    assert jdata["exit_status"]["message"] == "Request not authorized"
Beispiel #5
0
def test_user_catalog(dispatcher_live_fixture):
    server = dispatcher_live_fixture
    logger.info("constructed server: %s", server)

    # let's generate a valid token
    token_payload = {
        **default_token_payload,
        "roles": "unige-hpc-full, general",
    }
    encoded_token = jwt.encode(token_payload, secret_key, algorithm='HS256')

    selected_catalog_dict = dict(cat_lon_name="ra",
                                 cat_lat_name="dec",
                                 cat_frame="fk5",
                                 cat_coord_units="deg",
                                 cat_column_list=[[1], ["Test A"], [6], [5],
                                                  [4], [3], [2], [1], [0]],
                                 cat_column_names=[
                                     "meta_ID", "src_names", "significance",
                                     "ra", "dec", "NEW_SOURCE", "ISGRI_FLAG",
                                     "FLAG", "ERR_RAD"
                                 ],
                                 cat_column_descr=[["meta_ID", "<i8"],
                                                   ["src_names", "<U6"],
                                                   ["significance", "<i8"],
                                                   ["ra", "<f8"],
                                                   ["dec", "<f8"],
                                                   ["NEW_SOURCE", "<i8"],
                                                   ["ISGRI_FLAG", "<i8"],
                                                   ["FLAG", "<i8"],
                                                   ["ERR_RAD", "<i8"]])
    params = {
        **default_params, 'product_type': 'dummy',
        'query_type': "Dummy",
        'instrument': 'empty',
        'selected_catalog': json.dumps(selected_catalog_dict),
        'token': encoded_token
    }

    jdata = ask(server,
                params,
                expected_query_status=["done"],
                max_time_s=150,
                method='post')

    assert 'selected_catalog' in jdata['products']['analysis_parameters']
    assert jdata['products']['analysis_parameters'][
        'selected_catalog'] == json.dumps(selected_catalog_dict)
    # test job_id
    job_id = jdata['products']['job_id']
    # adapting some values to string
    for k, v in params.items():
        params[k] = str(v)

    restricted_par_dic = InstrumentQueryBackEnd.restricted_par_dic({
        **params, "sub":
        "*****@*****.**"
    })
    calculated_job_id = make_hash(restricted_par_dic)

    assert job_id == calculated_job_id