예제 #1
0
    def test_parent_links(self, client, entities, endpoint, parents):
        """ Test the existance and formatting of _links """
        # Setup inputs
        model_cls = ENDPOINT_ENTITY_MAP.get(endpoint)
        entity = entities.get(model_cls)[0]
        resp = client.get(endpoint + '/' + entity.kf_id,
                          headers={'Content-Type': 'application/json'})

        body = json.loads(resp.data.decode('utf-8'))
        # All links are formatted properly
        assert '_links' in body
        for parent in parents:
            # Links formatted properly
            assert parent in body['_links']
            link = body['_links'][parent]
            if link:
                assert len(link.split('/')[-1].split('=')[-1]) == 11
                # test that link responds ok
                resp = client.get(link,
                                  headers={'Content-Type': 'application/json'})
                assert resp.status_code == 200

        # Test self and collection links
        assert 'collection' in body['_links']
        assert body['_links']['collection'] == endpoint
        assert 'self' in body['_links']
        self_link = body['_links']['self']
        self_kf_id = self_link.split('/')[-1]
        self_endpoint = '/' + self_link.split('/')[1]
        self_model_cls = ENDPOINT_ENTITY_MAP.get(self_endpoint)
        assert self_model_cls.query.get(self_kf_id)
예제 #2
0
    def test_missing_required_params(self, client, entities, endpoint, method,
                                     field):
        """ Tests missing required parameters """
        # Setup inputs
        inputs = ENTITY_PARAMS['fields'][endpoint].copy()
        model_cls = ENDPOINT_ENTITY_MAP.get(endpoint)
        entity = entities.get(model_cls)[0]
        _add_foreign_keys(inputs, entity)
        inputs.pop(field, None)

        # Setup endpoint
        url = endpoint
        action = 'create'
        if method.lower() in {'put'}:
            action = 'update'
            kf_id = entities.get('kf_ids').get(endpoint)
            url = '{}/{}'.format(endpoint, kf_id)
        call_func = getattr(client, method.lower())
        resp = call_func(url,
                         data=json.dumps(inputs),
                         headers={'Content-Type': 'application/json'})

        body = json.loads(resp.data.decode('utf-8'))
        assert body['_status']['code'] == 400
        assert 'could not {} '.format(action) in body['_status']['message']
예제 #3
0
    def test_bad_input(self, client, entities, endpoint, invalid_params,
                       method):
        """ Tests bad inputs """
        # Setup inputs
        inputs = ENTITY_PARAMS['fields'][endpoint].copy()
        model_cls = ENDPOINT_ENTITY_MAP.get(endpoint)
        entity = entities.get(model_cls)[0]
        _add_foreign_keys(inputs, entity)
        inputs.update(invalid_params)

        # Setup endpoint
        url = endpoint
        action = 'create'
        if method.lower() in {'put', 'patch'}:
            action = 'update'
            kf_id = entity.kf_id
            url = '{}/{}'.format(endpoint, kf_id)
        call_func = getattr(client, method.lower())

        # Send request
        resp = call_func(url,
                         data=json.dumps(inputs),
                         headers={'Content-Type': 'application/json'})

        body = json.loads(resp.data.decode('utf-8'))
        assert body['_status']['code'] == 400
        assert 'could not {} '.format(action) in body['_status']['message']
예제 #4
0
    def test_unknown_field(self, client, entities, endpoint, method):
        """ Test that unknown fields are rejected when trying to create  """
        # Setup inputs
        inputs = ENTITY_PARAMS['fields'][endpoint].copy()
        model_cls = ENDPOINT_ENTITY_MAP.get(endpoint)
        entity = entities.get(model_cls)[0]
        _add_foreign_keys(inputs, entity)
        inputs.update({'blah': 'test'})

        # Setup endpoint
        url = endpoint
        action = 'create'
        if method.lower() in {'put', 'patch'}:
            action = 'update'
            kf_id = entity.kf_id
            url = '{}/{}'.format(endpoint, kf_id)
        call_func = getattr(client, method.lower())
        resp = call_func(url,
                         data=json.dumps(inputs),
                         headers={'Content-Type': 'application/json'})

        body = json.loads(resp.data.decode('utf-8'))
        assert body['_status']['code'] == 400
        assert 'could not {} '.format(action) in body['_status']['message']
        assert 'Unknown field' in body['_status']['message']
예제 #5
0
    def test_read_only(self, client, entities, endpoint, method, fields):
        """ Test that given fields can not be written or modified """
        # Setup inputs
        inputs = ENTITY_PARAMS['fields'][endpoint].copy()
        model_cls = ENDPOINT_ENTITY_MAP.get(endpoint)
        entity = entities.get(model_cls)[0]
        _add_foreign_keys(inputs, entity)
        [inputs.update({field: 'test'}) for field in fields]

        # Setup enpdoint
        url = endpoint
        method_name = method.lower()
        call_func = getattr(client, method_name)
        kwargs = {
            'data': json.dumps(inputs),
            'headers': {
                'Content-Type': 'application/json'
            }
        }
        if method_name in {'put', 'patch'}:
            kf_id = entity.kf_id
            url = '{}/{}'.format(endpoint, kf_id)

        # Send request
        resp = call_func(url, **kwargs)
        body = json.loads(resp.data.decode('utf-8'))

        if 'results' not in body:
            assert ('error saving' in body['_status']['message']
                    or 'already exists' in body['_status']['message'])
            return
        for field in fields:
            assert (field not in body['results']
                    or body['results'][field] != 'test')
    def test_uniqueness_constraints(self, client, entities, endpoint):
        """ Test integrity error from uniqueness violations """
        # _add_foreign_keys is destructive to ENTITY_PARAMS['fields'][endpoint]
        # state used by other tests, so deepcopy it here to prevent side
        # effects.
        inputs = ENTITY_PARAMS['fields'][endpoint].copy()
        model_cls = ENDPOINT_ENTITY_MAP.get(endpoint)
        entity = entities.get(model_cls)[0]
        _add_foreign_keys(inputs, entity)
        response = client.post(
            endpoint, data=json.dumps(inputs),
            headers={'Content-Type': 'application/json'}
        )

        assert response.status_code == 400
        response = json.loads(response.data.decode("utf-8"))
        assert CONSTRAINT_ERR_RE.match(response['_status']['message'])
예제 #7
0
    def test_child_links(self, client, entities, endpoint, child_relations):
        """ Checks that references to other resources have correct ID """
        # Setup inputs
        model_cls = ENDPOINT_ENTITY_MAP.get(endpoint)
        entity = entities.get(model_cls)[0]

        resp = client.get(endpoint + '/' + entity.kf_id)
        links = json.loads(resp.data.decode('utf-8'))['_links']
        for child in child_relations:
            # Child entity exists in links
            assert child in links
            # Format of link
            link = links[child]
            link_endpoint = link.split('?')[0]
            assert ('/' + child.replace('_', '-')) == link_endpoint
            assert type(link) is str
            kf_id = link.split('=')[-1]
            assert len(kf_id) == 11
            # Foreign key exists
            foreign_key = link.split('?')[-1].split('=')[0]
            related_entity_cls = ENDPOINT_ENTITY_MAP[link_endpoint]
            assert getattr(related_entity_cls, foreign_key)
예제 #8
0
    def test_bad_foreign_key(self, client, entities, endpoint, method, field):
        """
        Test bad foreign key
        Foregin key is a valid kf_id but refers an entity that doesn't exist
        """
        # Setup inputs
        inputs = ENTITY_PARAMS['fields'][endpoint].copy()
        model_cls = ENDPOINT_ENTITY_MAP.get(endpoint)
        entity = entities.get(model_cls)[0]
        _add_foreign_keys(inputs, entity)
        inputs.update({field: id_service.kf_id_generator('ZZ')()})

        # Setup endpoint
        url = endpoint
        if method.lower() in {'put', 'patch'}:
            url = '{}/{}'.format(endpoint, entity.kf_id)
        call_func = getattr(client, method.lower())
        resp = call_func(url,
                         data=json.dumps(inputs),
                         headers={'Content-Type': 'application/json'})

        body = json.loads(resp.data.decode('utf-8'))
        assert body['_status']['code'] == 400
        assert 'does not exist' in body['_status']['message']