Esempio n. 1
0
def test_fsm_rest_get(app, json_headers, record, users, test_blueprint):
    """TEST FSM REST API logged in as certain users."""
    recpid = recid_fetcher_v2(record.id, record)
    print(app.url_map)
    test_cases = [(users['user'], {
        'transitions': {
            'delete': build_url_transition_for_pid(recpid, 'delete'),
            'open': build_url_transition_for_pid(recpid, 'open'),
        },
        'self':
        url_for('invenio_records_rest.recid_item',
                _external=True,
                pid_value=recid_fetcher_v2(record.id, record).pid_value)
    }),
                  (users['editor'], {
                      'transitions': {
                          'delete':
                          build_url_transition_for_pid(recpid, 'delete'),
                          'open': build_url_transition_for_pid(recpid, 'open'),
                      },
                      'self':
                      url_for('invenio_records_rest.recid_item',
                              _external=True,
                              pid_value=recid_fetcher_v2(record.id,
                                                         record).pid_value)
                  }),
                  (users['admin'], {
                      'transitions': {
                          'delete':
                          build_url_transition_for_pid(recpid, 'delete'),
                          'open':
                          build_url_transition_for_pid(recpid, 'open'),
                          'archive':
                          build_url_transition_for_pid(recpid, 'archive'),
                      },
                      'self':
                      url_for('invenio_records_rest.recid_item',
                              _external=True,
                              pid_value=recid_fetcher_v2(record.id,
                                                         record).pid_value)
                  })]

    url = url_for('invenio_records_rest.recid_item',
                  pid_value=recid_fetcher_v2(record.id, record).pid_value) \
        .replace('/api', '')

    for user, expected_links in test_cases:
        with app.test_client() as client:
            client.get(
                url_for('_tests.test_login_{}'.format(user.id)).replace(
                    '/api', ''))
            res = client.get(url, headers=json_headers)

        assert res.status_code == 200
        res_dict = json.loads(res.data.decode('utf-8'))
        assert res_dict['metadata']['oarepo:recordStatus'] == record[
            'oarepo:recordStatus']
        assert res_dict['links'] == expected_links
Esempio n. 2
0
def test_links_factory(app, record, json_headers):
    pid = recid_fetcher_v2(record.id, record).pid_value
    url = url_for('invenio_records_rest.recid_item',
                  pid_value=pid).replace('/api', '')

    with app.test_client() as client:
        res = client.get(url, headers=json_headers)
        assert res.status_code == 200
        res_dict = json.loads(res.data.decode('utf-8'))
        assert set(res_dict['links'].keys()) == {'self', 'transitions'}
        assert set(
            res_dict['links']['transitions'].keys()) == {'open', 'delete'}
Esempio n. 3
0
def test_recid_fetcher_v2(app, db):
    """Test recommended recid fetcher."""
    with app.app_context():
        rec_uuid = uuid.uuid4()
        data = {}
        minted_pid = recid_minter_v2(rec_uuid, data)

        fetched_pid = recid_fetcher_v2(rec_uuid, data)

        assert minted_pid.pid_value == fetched_pid.pid_value
        assert minted_pid.pid_type == fetched_pid.pid_type
        assert fetched_pid.pid_type == 'recid'
        assert fetched_pid.pid_value == minted_pid.pid_value
Esempio n. 4
0
def test_rest_state_change_prevented(app, record, users, json_patch_headers,
                                     json_headers, test_blueprint):
    url = url_for('invenio_records_rest.recid_item',
                  pid_value=recid_fetcher_v2(record.id,
                                             record).pid_value).replace(
                                                 '/api', '')
    print(url, app)
    print(url, app)
    orig_state = record['oarepo:recordStatus']

    with app.test_client() as client:
        client.get(url_for('_tests.test_login_{}'.format(users['admin'].id)))
        res = client.get(url, headers=json_headers)
        res = client.patch(url,
                           data=json.dumps([{
                               "op": "replace",
                               "path": "/oarepo:recordStatus",
                               "value": "boo"
                           }]),
                           headers=json_patch_headers)

        assert res.status_code == 403
        rec = ExampleRecord.get_record(record.id)
        assert rec['oarepo:recordStatus'] == record['oarepo:recordStatus']
Esempio n. 5
0
def test_fsm_rest_post(app, json_headers, record, users, test_blueprint):
    """TEST FSM REST API logged in as certain users."""
    test_cases = [(users['user'], [('open', {
        'id': 1
    }), ('close', {
        'id': 1
    }), ('publish', {})], [
        (202, {
            'metadata': {
                'pid': record['pid'],
                'oarepo:recordStatus': 'open',
                'title': 'example'
            }
        }),
        (202, {
            'metadata': {
                'pid': record['pid'],
                'oarepo:recordStatus': 'closed',
                'title': 'example'
            }
        }),
        (400, {
            'message': 'Transition from closed to published is not allowed'
        })
    ]),
                  (users['editor'], [('open', {
                      'id': 2
                  }), ('close', {
                      'id': 2
                  }), ('publish', {})],
                   [(202, {
                       'metadata': {
                           'pid': record['pid'],
                           'oarepo:recordStatus': 'open',
                           'title': 'example'
                       }
                   }),
                    (202, {
                        'metadata': {
                            'pid': record['pid'],
                            'oarepo:recordStatus': 'closed',
                            'title': 'example'
                        }
                    }),
                    (400, {
                        'message':
                        'Transition from closed to published is not allowed'
                    })]),
                  (users['admin'], [('open', {
                      'id': 3
                  }), ('close', {
                      'id': 3
                  }), ('archive', {}), ('publish', {}), ('delete', {})], [
                      (202, {
                          'metadata': {
                              'pid': record['pid'],
                              'oarepo:recordStatus': 'open',
                              'title': 'example'
                          }
                      }),
                      (202, {
                          'metadata': {
                              'pid': record['pid'],
                              'oarepo:recordStatus': 'closed',
                              'title': 'example'
                          }
                      }),
                      (202, {
                          'metadata': {
                              'pid': record['pid'],
                              'oarepo:recordStatus': 'archived',
                              'title': 'example'
                          }
                      }),
                      (202, {
                          'metadata': {
                              'pid': record['pid'],
                              'oarepo:recordStatus': 'published',
                              'title': 'example'
                          }
                      }), (202, {
                          'status': 'deleted'
                      })
                  ])]

    for user, transitions, expected_results in test_cases:
        with app.test_client() as client:
            client.get(
                url_for('_tests.test_login_{}'.format(user.id)).replace(
                    '/api', ''))
            for idx, transition in enumerate(transitions):
                expected_status, expected_body = expected_results[idx]
                actname, kwargs = transition

                url = url_for('oarepo_fsm.recid_transitions',
                              transition=actname,
                              pid_value=recid_fetcher_v2(record.id, record).pid_value) \
                    .replace('/api', '')
                print(user, url, transition)
                res = client.post(url, json={**kwargs}, headers=json_headers)
                print(res.status_code, res.data)
                res_dict = json.loads(res.data.decode('utf-8'))
                assert res.status_code == expected_status
                for k, v in expected_body.items():
                    assert res_dict[k] == v