def test_admin_merge(self):
        with self.client as client:
            res = client.patch(
                '/d/',
                data=self.patch,
                content_type='application/json',
                headers={'Authorization': 'Bearer '
                         + 'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'})
            patch_id = int(res.headers['Location'].split('/')[-2])

            # Test that there's a link header
            patch_url = urlparse(res.headers['Location']).path
            res = self.client.get(patch_url, headers={
                'Authorization': 'Bearer '
                + 'ZjdjNjQ1ODQtMDc1MC00Y2I2LThjODEtMjkzMmY1ZGFhYmI4'})
            self.assertEqual(res.headers.get('Link'),
                             '<{}>;rel="merge"'.format(patch_url + 'merge'))

            res = client.post(
                patch_url + 'merge',
                headers={'Authorization': 'Bearer '
                         + 'ZjdjNjQ1ODQtMDc1MC00Y2I2LThjODEtMjkzMmY1ZGFhYmI4'})
            self.assertEqual(res.status_code, http.client.NO_CONTENT)
            merger = database.query_db(
                'SELECT merged_by FROM patch_request WHERE id = ?',
                (patch_id,), one=True)['merged_by']
            self.assertEqual(merger, 'http://orcid.org/1211-1098-7654-321X')
Example #2
0
    def test_comment_on_patch(self):
        with open(filepath('test-patch-adds-items.json')) as f:
            patch = f.read()
        with self.client as client:
            res = client.patch(
                '/d/',
                data=patch,
                content_type='application/json',
                headers={'Authorization': 'Bearer '
                         + 'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'})
            patch_id = int(res.headers['Location'].split('/')[-2])
            patch_url = urlparse(res.headers['Location']).path
            res = client.post(
                patch_url + 'messages',
                data=json.dumps({'message': 'This is a comment'}),
                content_type='application/json',
                headers={'Authorization': 'Bearer ' +
                         'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'}
            )

            self.assertEqual(res.status_code, http.client.OK)
            self.assertEqual(patch_url, urlparse(res.headers['Location']).path)

            row = database.query_db(
                'SELECT * FROM patch_request_comment WHERE patch_request_id=?',
                (patch_id,), one=True)
            self.assertEqual('https://orcid.org/1234-5678-9101-112X',
                             row['author'])
            self.assertEqual(patch_id, row['patch_request_id'])
            self.assertEqual('This is a comment', row['message'])

            res = client.get(patch_url)
            comments = json.loads(res.get_data(as_text=True)).get('comments')
            self.assertEqual(1, len(comments))
Example #3
0
 def test_initial_patch(self):
     with self.client as client:
         client.patch(
             '/d/',
             data=self.patch,
             content_type='application/json',
             headers={'Authorization': 'Bearer '
                      + 'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'})
         created_entities = database.query_db(
             'SELECT created_entities FROM patch_request WHERE id = 1',
             one=True)['created_entities']
         self.assertEqual(created_entities, '["p0trgkv", "p0trgkvwbjd"]')
         updated_entities = database.query_db(
             'SELECT updated_entities FROM patch_request WHERE id = 1',
             one=True)['updated_entities']
         self.assertEqual(updated_entities, '[]')
 def test_update_merged_patch(self):
     with self.client as client:
         res = client.patch(
             '/d/',
             data=self.patch,
             content_type='application/json',
             headers={'Authorization': 'Bearer '
                      + 'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'})
         patch_path = urlparse(res.headers['Location']).path
         res = client.post(
             patch_path + 'merge',
             headers={'Authorization': 'Bearer '
                      + 'ZjdjNjQ1ODQtMDc1MC00Y2I2LThjODEtMjkzMmY1ZGFhYmI4'})
         res = client.put(
             patch_path + 'patch.jsonpatch',
             data=self.patch,
             content_type='application/json',
             headers={'Authorization': 'Bearer '
                      + 'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'})
     self.assertEqual(res.status_code, http.client.FORBIDDEN)
     self.assertEqual(
         res.headers['WWW-Authenticate'],
         'Bearer realm="PeriodO", error="insufficient_scope", '
         + 'error_description='
         + '"The access token does not provide sufficient privileges", '
         + 'error_uri="http://tools.ietf.org/html/rfc6750#section-6.2.3"')
    def test_history_turtle(self):
        with self.client as client:
            res = client.patch(
                '/d/',
                data=self.patch,
                content_type='application/json',
                headers={'Authorization': 'Bearer '
                         + 'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'})
            res = client.post(
                urlparse(res.headers['Location']).path + 'merge',
                buffered=True,
                headers={'Authorization': 'Bearer '
                         + 'ZjdjNjQ1ODQtMDc1MC00Y2I2LThjODEtMjkzMmY1ZGFhYmI4'})

        res1 = self.client.get('/history.ttl')
        self.assertEqual(res1.status_code, http.client.OK)
        self.assertEqual(res1.headers['Content-Type'], 'text/turtle')
        self.assertEqual(
            res1.headers['Cache-Control'],
            'public, max-age={}'.format(cache.SHORT_TIME))
        self.assertEqual(
            res1.headers['Content-Disposition'],
            'attachment; filename="periodo-history.ttl"')

        g = Graph()
        g.parse(data=res1.get_data(as_text=True), format='turtle')
        self.assertIn((HOST['h#patch-1'],
                       FOAF.page, HOST['patches/1/patch.jsonpatch']), g)
        self.assertIn((HOST['d'],
                       DCTERMS.provenance, HOST['h#changes']), g)

        res3 = self.client.get('/history.ttl/')
        self.assertEqual(res3.status_code, http.client.NOT_FOUND)
Example #6
0
    def test_remove_period(self):
        with open(filepath('test-patch-remove-period.json')) as f:
            patch1 = f.read()
        with self.client as client:
            res = client.patch(
                '/d/',
                data=patch1,
                content_type='application/json',
                headers={'Authorization': 'Bearer '
                         + 'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'})
            patch_url = urlparse(res.headers['Location']).path
            res = client.post(
                patch_url + 'merge',
                buffered=True,
                headers={'Authorization': 'Bearer '
                         + 'ZjdjNjQ1ODQtMDc1MC00Y2I2LThjODEtMjkzMmY1ZGFhYmI4'})
            self.assertEqual(res.status_code, http.client.NO_CONTENT)
            removed_entities = database.get_removed_entity_keys()
            self.assertEqual(removed_entities, set(['p0trgkvwbjd']))
            res = client.get('/trgkvwbjd',
                             headers={'Accept': 'application/json'},
                             follow_redirects=True)
            self.assertEqual(res.status_code, http.client.GONE)
            res = client.get('/trgkvwbjd.json',
                             headers={'Accept': 'application/json'},
                             follow_redirects=True)
            self.assertEqual(res.status_code, http.client.GONE)
            res = client.get('/trgkvwbjd?version=0',
                             headers={'Accept': 'application/json'},
                             follow_redirects=True)
            self.assertEqual(res.status_code, http.client.NOT_FOUND)
            res = client.get('/trgkvwbjd.json?version=0',
                             headers={'Accept': 'application/json'},
                             follow_redirects=True)
            self.assertEqual(res.status_code, http.client.NOT_FOUND)
            res = client.get('/trgkvwbjd?version=1',
                             headers={'Accept': 'application/json'},
                             follow_redirects=True)
            self.assertEqual(res.status_code, http.client.OK)
            res = client.get('/trgkvwbjd.json?version=1',
                             headers={'Accept': 'application/json'},
                             follow_redirects=True)
            self.assertEqual(res.status_code, http.client.OK)

            res = client.get('/history.jsonld?inline-context')
            self.assertEqual(
                res.headers['Cache-Control'],
                'public, max-age=0')
            self.assertEqual(
                res.headers['X-Accel-Expires'],
                '{}'.format(cache.MEDIUM_TIME))

            g = ConjunctiveGraph()
            g.parse(format='json-ld', data=res.get_data(as_text=True))

            generated = list(g.objects(subject=HOST['h#change-2'],
                                       predicate=PROV.generated))
            self.assertEqual(len(generated), 1)
            self.assertIn(HOST['d?version=2'], generated)
Example #7
0
    def test_remove_definition(self):
        with open(filepath('test-patch-remove-definition.json')) as f:
            patch1 = f.read()
        with self.client as client:
            res = client.patch(
                '/d/',
                data=patch1,
                content_type='application/json',
                headers={'Authorization': 'Bearer '
                         + 'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'})
            patch_url = urlparse(res.headers['Location']).path
            res = client.post(
                patch_url + 'merge',
                headers={'Authorization': 'Bearer '
                         + 'ZjdjNjQ1ODQtMDc1MC00Y2I2LThjODEtMjkzMmY1ZGFhYmI4'})
            self.assertEqual(res.status_code, http.client.NO_CONTENT)
            removed_entities = database.get_removed_entity_keys()
            self.assertEqual(removed_entities, set(['p0trgkvwbjd']))
            res = client.get('/trgkvwbjd',
                             headers={'Accept': 'application/json'},
                             follow_redirects=True)
            self.assertEqual(res.status_code, http.client.GONE)
            res = client.get('/trgkvwbjd.json',
                             headers={'Accept': 'application/json'},
                             follow_redirects=True)
            self.assertEqual(res.status_code, http.client.GONE)
            res = client.get('/trgkvwbjd?version=0',
                             headers={'Accept': 'application/json'},
                             follow_redirects=True)
            self.assertEqual(res.status_code, http.client.NOT_FOUND)
            res = client.get('/trgkvwbjd.json?version=0',
                             headers={'Accept': 'application/json'},
                             follow_redirects=True)
            self.assertEqual(res.status_code, http.client.NOT_FOUND)
            res = client.get('/trgkvwbjd?version=1',
                             headers={'Accept': 'application/json'},
                             follow_redirects=True)
            self.assertEqual(res.status_code, http.client.OK)
            res = client.get('/trgkvwbjd.json?version=1',
                             headers={'Accept': 'application/json'},
                             follow_redirects=True)
            self.assertEqual(res.status_code, http.client.OK)

            res = client.get('/h')

            g = ConjunctiveGraph()
            g.parse(format='json-ld', data=res.get_data(as_text=True))

            invalidated = g.value(subject=PERIODO['p0h#change-2'],
                                  predicate=PROV.invalidated,
                                  any=False)
            self.assertEqual(invalidated, PERIODO['p0trgkvwbjd'])

            generated = list(g.objects(subject=PERIODO['p0h#change-2'],
                                       predicate=PROV.generated))
            self.assertEqual(len(generated), 2)
            self.assertIn(PERIODO['p0d?version=2'], generated)
            self.assertIn(PERIODO['p0trgkv?version=2'], generated)
 def test_authorized_user(self):
     with self.client as client:
         res = client.patch(
             '/d/',
             data=self.patch,
             content_type='application/json',
             headers={'Authorization': 'Bearer '
                      + 'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'})
         self.assertEqual(res.status_code, http.client.ACCEPTED)
         patch_id = int(res.headers['Location'].split('/')[-2])
         creator = database.query_db(
             'SELECT created_by FROM patch_request WHERE id = ?',
             (patch_id,), one=True)['created_by']
         self.assertEqual(creator, 'http://orcid.org/1234-5678-9101-112X')
 def test_creator_patch_update(self):
     with self.client as client:
         res = client.patch(
             '/d/',
             data=self.patch,
             content_type='application/json',
             headers={'Authorization': 'Bearer '
                      + 'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'})
         res = client.put(
             urlparse(res.headers['Location']).path + 'patch.jsonpatch',
             data=self.patch,
             content_type='application/json',
             headers={'Authorization': 'Bearer '
                      + 'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'})
         self.assertEqual(res.status_code, http.client.OK)
Example #10
0
 def test_submit_patch(self):
     with self.client as client:
         res = client.patch(
             '/d/',
             data=self.patch,
             content_type='application/json',
             headers={'Authorization': 'Bearer '
                      + 'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'})
         self.assertEqual(res.status_code, http.client.ACCEPTED)
         patch_id = int(res.headers['Location'].split('/')[-2])
         updated_entities = database.query_db(
             'SELECT updated_entities FROM patch_request WHERE id = ?',
             (patch_id,), one=True)['updated_entities']
         self.assertEqual(updated_entities, '["p0trgkv", "p0trgkvwbjd"]')
         created_entities = database.query_db(
             'SELECT created_entities FROM patch_request WHERE id = ?',
             (patch_id,), one=True)['created_entities']
         self.assertEqual(created_entities, '[]')
Example #11
0
 def test_merge_patch(self):
     with open(filepath('test-patch-adds-items.json')) as f:
         patch = f.read()
     with self.client as client:
         res = client.patch(
             '/d/',
             data=patch,
             content_type='application/json',
             headers={'Authorization': 'Bearer '
                      + 'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'})
         patch_id = int(res.headers['Location'].split('/')[-2])
         updated_entities = database.query_db(
             'SELECT updated_entities FROM patch_request WHERE id = ?',
             (patch_id,), one=True)['updated_entities']
         self.assertEqual(updated_entities, '["p0trgkv"]')
         created_entities = database.query_db(
             'SELECT created_entities FROM patch_request WHERE id = ?',
             (patch_id,), one=True)['created_entities']
         self.assertEqual(created_entities, '[]')
         patch_url = urlparse(res.headers['Location']).path
         res = client.post(
             patch_url + 'merge',
             buffered=True,
             headers={'Authorization': 'Bearer '
                      + 'ZjdjNjQ1ODQtMDc1MC00Y2I2LThjODEtMjkzMmY1ZGFhYmI4'})
         self.assertEqual(res.status_code, http.client.NO_CONTENT)
         row = database.query_db(
             'SELECT applied_to, resulted_in FROM patch_request WHERE id=?',
             (patch_id,), one=True)
         self.assertEqual(1, row['applied_to'])
         self.assertEqual(2, row['resulted_in'])
         updated_entities = database.query_db(
             'SELECT updated_entities FROM patch_request WHERE id = ?',
             (patch_id,), one=True)['updated_entities']
         self.assertEqual(updated_entities, '["p0trgkv"]')
         created_entities = json.loads(database.query_db(
             'SELECT created_entities FROM patch_request WHERE id = ?',
             (patch_id,), one=True)['created_entities'])
         self.assertEqual(4, len(created_entities))
         for entity_id in created_entities:
             self.assertRegex(entity_id, identifier.IDENTIFIER_RE)
 def test_add_contributors_to_dataset_description(self):
     contribution = (URIRef('http://n2t.net/ark:/99152/p0d'),
                     DCTERMS.contributor,
                     URIRef('http://orcid.org/1234-5678-9101-112X'))
     data = self.client.get(
         '/', headers={'Accept': 'text/turtle'}).get_data(as_text=True)
     g = Graph().parse(format='turtle', data=data)
     self.assertNotIn(contribution, g)
     with self.client as client:
         res = client.patch(
             '/d/',
             data=self.patch,
             content_type='application/json',
             headers={'Authorization': 'Bearer '
                      + 'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'})
         res = client.post(
             urlparse(res.headers['Location']).path + 'merge',
             headers={'Authorization': 'Bearer '
                      + 'ZjdjNjQ1ODQtMDc1MC00Y2I2LThjODEtMjkzMmY1ZGFhYmI4'})
     data = self.client.get(
         '/', headers={'Accept': 'text/turtle'}).get_data(as_text=True)
     g = Graph().parse(format='turtle', data=data)
     self.assertIn(contribution, g)
Example #13
0
    def test_reject_patch(self):
        with open(filepath('test-patch-adds-items.json')) as f:
            patch = f.read()
        with self.client as client:
            res = client.patch(
                '/d/',
                data=patch,
                content_type='application/json',
                headers={'Authorization': 'Bearer '
                         + 'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'})
            patch_id = int(res.headers['Location'].split('/')[-2])
            patch_url = urlparse(res.headers['Location']).path
            res = client.post(
                patch_url + 'reject',
                headers={'Authorization': 'Bearer '
                         + 'ZjdjNjQ1ODQtMDc1MC00Y2I2LThjODEtMjkzMmY1ZGFhYmI4'})
            self.assertEqual(res.status_code, http.client.NO_CONTENT)
            row = database.query_db(
                'SELECT open, merged FROM patch_request WHERE id=?',
                (patch_id,), one=True)

            self.assertEqual(0, row['open'])
            self.assertEqual(0, row['merged'])
Example #14
0
    def test_versioning(self):
        with open(filepath('test-patch-adds-items.json')) as f:
            patch1 = f.read()
        with open(filepath('test-patch-add-definition.json')) as f:
            patch2 = f.read()
        with self.client as client:
            res = client.patch(
                '/d/',
                data=patch1,
                content_type='application/json',
                headers={'Authorization': 'Bearer '
                         + 'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'})
            patch_url = urlparse(res.headers['Location']).path
            res = client.post(
                patch_url + 'merge',
                headers={'Authorization': 'Bearer '
                         + 'ZjdjNjQ1ODQtMDc1MC00Y2I2LThjODEtMjkzMmY1ZGFhYmI4'})
            self.assertEqual(res.status_code, http.client.NO_CONTENT)
            res = client.patch(
                '/d/',
                data=patch2,
                content_type='application/json',
                headers={'Authorization': 'Bearer '
                         + 'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'})
            patch_url = urlparse(res.headers['Location']).path
            res = client.post(
                patch_url + 'merge',
                headers={'Authorization': 'Bearer '
                         + 'ZjdjNjQ1ODQtMDc1MC00Y2I2LThjODEtMjkzMmY1ZGFhYmI4'})
            self.assertEqual(res.status_code, http.client.NO_CONTENT)
            res = client.get('/trgkv?version=0',
                             headers={'Accept': 'application/json'},
                             follow_redirects=True)
            self.assertEqual(res.status_code, http.client.NOT_FOUND)
            for version in range(1, 4):
                res = client.get(
                    '/trgkv?version={}'.format(version),
                    headers={'Accept': 'application/json'})
                self.assertEqual(
                    res.status_code, http.client.SEE_OTHER)
                self.assertEqual(
                    '/' + res.headers['Location'].split('/')[-1],
                    '/trgkv.json?version={}'.format(version))
                res = client.get(
                    '/trgkv.json?version={}'.format(version))
                self.assertEqual(
                    res.status_code, http.client.OK)
                self.assertEqual(
                    res.headers['Content-Type'], 'application/json')
            res = client.get('/h')

            g = ConjunctiveGraph()
            g.parse(format='json-ld', data=res.get_data(as_text=True))

            for o in g.objects(subject=PERIODO['p0h#change-3'],
                               predicate=PROV.generated):
                path = '/' + urlparse(o).path.split('/')[-1][2:]
                if path == '/trgkv' or path == '/d':
                    continue
                for version in range(0, 3):
                    res = client.get(
                        '{}?version={}'.format(path, version),
                        headers={'Accept': 'application/json'},
                        follow_redirects=True)
                    self.assertEqual(res.status_code, http.client.NOT_FOUND)
                res = client.get('{}?version=3'.format(path),
                                 headers={'Accept': 'application/json'})
                self.assertEqual(res.status_code, http.client.SEE_OTHER)
                self.assertEqual(
                    '/' + res.headers['Location'].split('/')[-1],
                    '{}.json?version=3'.format(path))
                res = client.get(
                    '{}.json?version=3'.format(path))
                self.assertEqual(
                    res.status_code, http.client.OK)
                self.assertEqual(
                    res.headers['Content-Type'], 'application/json')

            for o in g.objects(subject=PERIODO['p0h#change-2'],
                               predicate=PROV.generated):
                path = '/' + urlparse(o).path.split('/')[-1][2:]
                if path == '/trgkv' or path == '/d':
                    continue
                for version in range(0, 2):
                    res = client.get(
                        '{}?version={}'.format(path, version),
                        headers={'Accept': 'application/json'},
                        follow_redirects=True)
                    self.assertEqual(res.status_code, http.client.NOT_FOUND)
                res = client.get('{}?version=3'.format(path),
                                 headers={'Accept': 'application/json'})
                self.assertEqual(res.status_code, http.client.SEE_OTHER)
                self.assertEqual(
                    '/' + res.headers['Location'].split('/')[-1],
                    '{}.json?version=3'.format(path))
                res = client.get('{}.json?version=3'.format(path))
                self.assertEqual(
                    res.status_code, http.client.MOVED_PERMANENTLY)
                self.assertEqual(
                    '/' + res.headers['Location'].split('/')[-1],
                    '{}.json?version=2'.format(path))
                res = client.get(
                    '{}.json?version=2'.format(path))
                self.assertEqual(
                    res.status_code, http.client.OK)
                self.assertEqual(
                    res.headers['Content-Type'], 'application/json')
Example #15
0
    def test_context_versioning(self):
        with open(filepath('test-patch-modify-context.json')) as f:
            patch = f.read()
        with self.client as client:
            res = client.patch(
                '/d/',
                data=patch,
                content_type='application/json',
                headers={'Authorization': 'Bearer '
                         + 'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'})
            patch_url = urlparse(res.headers['Location']).path
            res = client.post(
                patch_url + 'merge',
                buffered=True,
                headers={'Authorization': 'Bearer '
                         + 'ZjdjNjQ1ODQtMDc1MC00Y2I2LThjODEtMjkzMmY1ZGFhYmI4'})
            self.assertEqual(res.status_code, http.client.NO_CONTENT)

            res = client.get('/d.json?version=0',
                             headers={'Accept': 'application/json'},
                             follow_redirects=True)
            self.assertEqual(res.status_code, http.client.OK)
            ctx = json.loads(res.get_data(as_text=True)).get('@context', None)
            self.assertIsNone(ctx)

            res = client.get('/c?version=0',
                             headers={'Accept': 'application/json'},
                             follow_redirects=True)
            self.assertEqual(res.status_code, http.client.NOT_FOUND)

            res = client.get('/d.json?version=1',
                             headers={'Accept': 'application/json'},
                             follow_redirects=True)
            self.assertEqual(res.status_code, http.client.OK)
            self.assertEqual(
                res.headers['Cache-Control'],
                'public, max-age={}'.format(cache.LONG_TIME))
            ctx = json.loads(res.get_data(as_text=True))['@context']
            self.assertEqual(ctx[0], 'http://localhost.localdomain:5000/c?version=1')

            res = client.get('/c?version=1',
                             headers={'Accept': 'application/json'},
                             follow_redirects=True)
            self.assertEqual(res.status_code, http.client.OK)
            self.assertEqual(
                res.headers['Cache-Control'],
                'public, max-age={}'.format(cache.LONG_TIME))
            ctx = json.loads(res.get_data(as_text=True))['@context']
            self.assertNotIn('foobar', ctx)

            res = client.get('/d.json?version=2',
                             headers={'Accept': 'application/json'},
                             follow_redirects=True)
            self.assertEqual(res.status_code, http.client.OK)
            ctx = json.loads(res.get_data(as_text=True))['@context']
            self.assertEqual(ctx[0], 'http://localhost.localdomain:5000/c?version=2')

            res = client.get('/c?version=2',
                             headers={'Accept': 'application/json'},
                             follow_redirects=True)
            self.assertEqual(res.status_code, http.client.OK)
            ctx = json.loads(res.get_data(as_text=True))['@context']
            self.assertIn('foobar', ctx)
Example #16
0
    def test_versioning(self):
        with open(filepath('test-patch-adds-items.json')) as f:
            patch1 = f.read()
        with open(filepath('test-patch-add-period.json')) as f:
            patch2 = f.read()
        with self.client as client:
            res = client.patch(
                '/d/',
                data=patch1,
                content_type='application/json',
                headers={'Authorization': 'Bearer '
                         + 'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'})
            patch_url = urlparse(res.headers['Location']).path
            res = client.post(
                patch_url + 'merge',
                buffered=True,
                headers={'Authorization': 'Bearer '
                         + 'ZjdjNjQ1ODQtMDc1MC00Y2I2LThjODEtMjkzMmY1ZGFhYmI4'})
            self.assertEqual(res.status_code, http.client.NO_CONTENT)
            res = client.patch(
                '/d/',
                data=patch2,
                content_type='application/json',
                headers={'Authorization': 'Bearer '
                         + 'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'})
            patch_url = urlparse(res.headers['Location']).path
            res = client.post(
                patch_url + 'merge',
                buffered=True,
                headers={'Authorization': 'Bearer '
                         + 'ZjdjNjQ1ODQtMDc1MC00Y2I2LThjODEtMjkzMmY1ZGFhYmI4'})
            self.assertEqual(res.status_code, http.client.NO_CONTENT)
            res = client.get('/trgkv?version=0',
                             headers={'Accept': 'application/json'},
                             follow_redirects=True)
            self.assertEqual(res.status_code, http.client.NOT_FOUND)
            for version in range(1, 4):
                res = client.get(
                    '/trgkv?version={}'.format(version),
                    headers={'Accept': 'application/json'})
                self.assertEqual(
                    res.status_code, http.client.SEE_OTHER)
                self.assertEqual(
                    '/' + res.headers['Location'].split('/')[-1],
                    '/trgkv.json?version={}'.format(version))
                res = client.get(
                    '/trgkv.json?version={}'.format(version))
                self.assertEqual(
                    res.status_code, http.client.OK)
                self.assertEqual(
                    res.headers['Content-Type'], 'application/json')
                ctx = json.loads(res.get_data(as_text=True))['@context']
                self.assertEqual(
                    ctx[0],
                    'http://localhost.localdomain:5000/c?version={}'.format(version)
                )

            res = client.get('/history.jsonld?inline-context')
            self.assertEqual(
                res.headers['Cache-Control'],
                'public, max-age=0')
            self.assertEqual(
                res.headers['X-Accel-Expires'],
                '{}'.format(cache.MEDIUM_TIME))
    def test_get_history(self):
        with open(filepath('test-patch-adds-items.json')) as f:
            patch = f.read()

        with self.client as client:
            res1 = client.patch(
                '/d/',
                data=patch,
                content_type='application/json',
                headers={'Authorization': 'Bearer '
                         + 'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'})
            patch_url = urlparse(res1.headers['Location']).path
            client.post(
                patch_url + 'merge',
                headers={'Authorization': 'Bearer '
                         + 'ZjdjNjQ1ODQtMDc1MC00Y2I2LThjODEtMjkzMmY1ZGFhYmI4'})
            res2 = client.get('/h')
            self.assertEqual(res2.status_code, http.client.OK)
            self.assertEqual(
                res2.headers['Content-Type'], 'application/ld+json')
            jsonld = res2.get_data(as_text=True)

        g = ConjunctiveGraph()
        g.parse(format='json-ld', data=jsonld)

        # Initial data load
        self.assertIn(  # None means any
            (PERIODO['p0h#change-1'], PROV.endedAtTime, None), g)
        self.assertIn(
            (PERIODO['p0h#change-1'], PROV.used, PERIODO['p0d?version=0']), g)
        self.assertIn(
            (PERIODO['p0d?version=0'],
             PROV.specializationOf, PERIODO['p0d']), g)
        self.assertIn(
            (PERIODO['p0h#change-1'], PROV.used, PERIODO['p0h#patch-1']), g)
        self.assertIn(
            (PERIODO['p0h#patch-1'],
             FOAF.page, PERIODO['p0patches/1/patch.jsonpatch']), g)
        self.assertIn(
            (PERIODO['p0h#change-1'],
             PROV.generated, PERIODO['p0d?version=1']), g)
        self.assertIn(
            (PERIODO['p0d?version=1'],
             PROV.specializationOf, PERIODO['p0d']), g)
        self.assertIn(
            (PERIODO['p0h#change-1'],
             PROV.generated, PERIODO['p0trgkv?version=1']), g)
        self.assertIn(
            (PERIODO['p0trgkv?version=1'],
             PROV.specializationOf, PERIODO['p0trgkv']), g)
        self.assertIn(
            (PERIODO['p0h#change-1'],
             PROV.generated, PERIODO['p0trgkvwbjd?version=1']), g)
        self.assertIn(
            (PERIODO['p0trgkvwbjd?version=1'],
             PROV.specializationOf, PERIODO['p0trgkvwbjd']), g)

        # Change from first submitted patch
        self.assertIn(  # None means any
            (PERIODO['p0h#change-2'], PROV.startedAtTime, None), g)
        self.assertIn(  # None means any
            (PERIODO['p0h#change-2'], PROV.endedAtTime, None), g)
        start = g.value(
            subject=PERIODO['p0h#change-2'],
            predicate=PROV.startedAtTime)
        self.assertEqual(start.datatype, XSD.dateTime)
        self.assertRegex(start.value.isoformat(), W3CDTF)
        end = g.value(
            subject=PERIODO['p0h#change-2'],
            predicate=PROV.endedAtTime)
        self.assertEqual(end.datatype, XSD.dateTime)
        self.assertRegex(end.value.isoformat(), W3CDTF)
        self.assertIn(
            (PERIODO['p0h#change-2'], PROV.wasAssociatedWith,
             URIRef('http://orcid.org/1234-5678-9101-112X')), g)
        self.assertIn(
            (PERIODO['p0h#change-2'], PROV.wasAssociatedWith,
             URIRef('http://orcid.org/1211-1098-7654-321X')), g)
        for association in g.subjects(
                predicate=PROV.agent,
                object=URIRef('http://orcid.org/1234-5678-9101-112X')):
            role = g.value(subject=association, predicate=PROV.hadRole)
            self.assertIn(role, (PERIODO['p0v#submitted'],
                                 PERIODO['p0v#updated']))
        merger = g.value(
            predicate=PROV.agent,
            object=URIRef('http://orcid.org/1211-1098-7654-321X'))
        self.assertIn(
            (PERIODO['p0h#change-2'], PROV.qualifiedAssociation, merger), g)
        self.assertIn(
            (merger, PROV.hadRole, PERIODO['p0v#merged']), g)
        self.assertIn(
            (PERIODO['p0h#change-2'], PROV.used, PERIODO['p0d?version=1']), g)
        self.assertIn(
            (PERIODO['p0d?version=1'],
             PROV.specializationOf, PERIODO['p0d']), g)
        self.assertIn(
            (PERIODO['p0h#change-2'], PROV.used, PERIODO['p0h#patch-2']), g)
        self.assertIn(
            (PERIODO['p0h#patch-2'],
             FOAF.page, PERIODO['p0patches/2/patch.jsonpatch']), g)
        self.assertIn(
            (PERIODO['p0h#change-2'],
             PROV.generated, PERIODO['p0d?version=2']), g)
        self.assertIn(
            (PERIODO['p0d?version=2'],
             PROV.specializationOf, PERIODO['p0d']), g)
        self.assertIn(
            (PERIODO['p0h#change-2'],
             PROV.generated, PERIODO['p0trgkv?version=2']), g)
        self.assertIn(
            (PERIODO['p0trgkv?version=2'],
             PROV.specializationOf, PERIODO['p0trgkv']), g)
        self.assertIn(
            (PERIODO['p0trgkv?version=2'],
             PROV.wasRevisionOf, PERIODO['p0trgkv?version=1']), g)

        entities = 0
        for _, _, version in g.triples(
                (PERIODO['p0h#change-2'], PROV.generated, None)):
            entity = g.value(subject=version, predicate=PROV.specializationOf)
            self.assertEqual(str(entity) + '?version=2', str(version))
            entities += 1
        self.assertEqual(entities, 5)
Example #18
0
    def test_get_history(self):
        with open(filepath('test-patch-adds-items.json')) as f:
            patch = f.read()

        with self.client as client:
            res1 = client.patch(
                '/d/',
                data=patch,
                content_type='application/json',
                headers={'Authorization': 'Bearer '
                         + 'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'})

            patch_url = urlparse(res1.headers['Location']).path

            client.post(
                patch_url + 'messages',
                data='{"message": "Here is my patch"}',
                content_type='application/json',
                headers={'Authorization': 'Bearer '
                         + 'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4'})

            client.post(
                patch_url + 'messages',
                data='{"message": "Looks good to me"}',
                content_type='application/json',
                headers={'Authorization': 'Bearer '
                         + 'ZjdjNjQ1ODQtMDc1MC00Y2I2LThjODEtMjkzMmY1ZGFhYmI4'})

            client.post(
                patch_url + 'merge',
                buffered=True,
                headers={'Authorization': 'Bearer '
                         + 'ZjdjNjQ1ODQtMDc1MC00Y2I2LThjODEtMjkzMmY1ZGFhYmI4'})

            res3 = client.get('/h', headers={'Accept': 'application/ld+json'})
            self.assertEqual(res3.status_code, http.client.SEE_OTHER)
            self.assertEqual(
                urlparse(res3.headers['Location']).path, '/h.jsonld')

            res4 = client.get('/history.jsonld?inline-context')
            self.assertEqual(res4.status_code, http.client.OK)
            self.assertEqual(
                res4.headers['Content-Type'], 'application/ld+json')
            jsonld = res4.get_data(as_text=True)

        g = ConjunctiveGraph()
        g.parse(format='json-ld', data=jsonld)

        # Initial data load
        self.assertIn(  # None means any
            (HOST['h#change-1'], PROV.endedAtTime, None), g)
        self.assertIn(
            (HOST['h#change-1'], PROV.used, HOST['d?version=0']), g)
        self.assertIn(
            (HOST['d?version=0'],
             PROV.specializationOf, HOST['d']), g)
        self.assertIn(
            (HOST['h#change-1'], RDFS.seeAlso, HOST['h#patch-request-1']), g)
        self.assertIn(
            (HOST['h#patch-request-1'], FOAF.page, HOST['patches/1/']), g)
        self.assertNotIn(
            (HOST['h#patch-request-1'],
             AS.replies, HOST['h#patch-request-1-comments']), g)
        self.assertIn(
            (HOST['h#change-1'], PROV.used, HOST['h#patch-1']), g)
        self.assertIn(
            (HOST['h#patch-1'],
             FOAF.page, HOST['patches/1/patch.jsonpatch']), g)
        self.assertIn(
            (HOST['h#change-1'],
             PROV.generated, HOST['d?version=1']), g)
        self.assertIn(
            (HOST['d?version=1'],
             PROV.specializationOf, HOST['d']), g)

        # Change from first submitted patch
        self.assertIn(  # None means any
            (HOST['h#change-2'], PROV.startedAtTime, None), g)
        self.assertIn(  # None means any
            (HOST['h#change-2'], PROV.endedAtTime, None), g)
        start = g.value(
            subject=HOST['h#change-2'],
            predicate=PROV.startedAtTime)
        self.assertEqual(start.datatype, XSD.dateTime)
        self.assertRegex(start.value.isoformat(), W3CDTF)
        end = g.value(
            subject=HOST['h#change-2'],
            predicate=PROV.endedAtTime)
        self.assertEqual(end.datatype, XSD.dateTime)
        self.assertRegex(end.value.isoformat(), W3CDTF)
        self.assertIn(
            (HOST['h#change-2'], PROV.wasAssociatedWith,
             URIRef('https://orcid.org/1234-5678-9101-112X')), g)
        self.assertIn(
            (HOST['h#change-2'], PROV.wasAssociatedWith,
             URIRef('https://orcid.org/1211-1098-7654-321X')), g)
        for association in g.subjects(
                predicate=PROV.agent,
                object=URIRef('https://orcid.org/1234-5678-9101-112X')):
            role = g.value(subject=association, predicate=PROV.hadRole)
            self.assertIn(role, (HOST['v#submitted'],
                                 HOST['v#updated']))
        merger = g.value(
            predicate=PROV.agent,
            object=URIRef('https://orcid.org/1211-1098-7654-321X'))
        self.assertIn(
            (HOST['h#change-2'], PROV.qualifiedAssociation, merger), g)
        self.assertIn(
            (merger, PROV.hadRole, HOST['v#merged']), g)
        self.assertIn(
            (HOST['h#change-2'], PROV.used, HOST['d?version=1']), g)
        self.assertIn(
            (HOST['d?version=1'],
             PROV.specializationOf, HOST['d']), g)
        self.assertIn(
            (HOST['h#change-2'], RDFS.seeAlso, HOST['h#patch-request-2']), g)
        self.assertIn(
            (HOST['h#patch-request-2'], FOAF.page, HOST['patches/2/']), g)
        self.assertIn(
            (HOST['h#patch-request-2'],
             AS.replies, HOST['h#patch-request-2-comments']), g)
        commentCount = g.value(
            subject=HOST['h#patch-request-2-comments'],
            predicate=AS.totalItems)
        self.assertEqual(commentCount.value, 2)
        self.assertIn(
            (HOST['h#patch-request-2-comments'],
             AS.first, HOST['h#patch-request-2-comment-1']), g)
        self.assertIn(
            (HOST['h#patch-request-2-comments'],
             AS.last, HOST['h#patch-request-2-comment-2']), g)
        self.assertIn(
            (HOST['h#patch-request-2-comments'],
             AS.items, HOST['h#patch-request-2-comment-1']), g)
        self.assertIn(
            (HOST['h#patch-request-2-comments'],
             AS.items, HOST['h#patch-request-2-comment-2']), g)
        self.assertIn(
            (HOST['h#patch-request-2-comment-1'], RDF.type, AS.Note), g)
        self.assertIn(
            (HOST['h#patch-request-2-comment-1'],
             AS.attributedTo,
             URIRef('https://orcid.org/1234-5678-9101-112X')), g)
        self.assertIn(  # None means any
            (HOST['h#patch-request-2-comment-1'], AS.published, None), g)
        comment1_media_type = g.value(
            subject=HOST['h#patch-request-2-comment-1'],
            predicate=AS.mediaType)
        self.assertEqual(comment1_media_type.value, 'text/plain')
        comment1_content = g.value(
            subject=HOST['h#patch-request-2-comment-1'],
            predicate=AS.content)
        self.assertEqual(comment1_content.value, 'Here is my patch')
        self.assertIn(
            (HOST['h#patch-request-2-comment-2'], RDF.type, AS.Note), g)
        self.assertIn(
            (HOST['h#patch-request-2-comment-2'],
             AS.attributedTo,
             URIRef('https://orcid.org/1211-1098-7654-321X')), g)
        self.assertIn(  # None means any
            (HOST['h#patch-request-2-comment-2'], AS.published, None), g)
        comment2_media_type = g.value(
            subject=HOST['h#patch-request-2-comment-2'],
            predicate=AS.mediaType)
        self.assertEqual(comment2_media_type.value, 'text/plain')
        comment2_content = g.value(
            subject=HOST['h#patch-request-2-comment-2'],
            predicate=AS.content)
        self.assertEqual(comment2_content.value, 'Looks good to me')
        self.assertIn(
            (HOST['h#change-2'], PROV.used, HOST['h#patch-2']), g)
        self.assertIn(
            (HOST['h#patch-2'],
             FOAF.page, HOST['patches/2/patch.jsonpatch']), g)
        self.assertIn(
            (HOST['h#change-2'],
             PROV.generated, HOST['d?version=2']), g)
        self.assertIn(
            (HOST['d?version=2'],
             PROV.specializationOf, HOST['d']), g)
Example #19
0
def test_update_artist_unauthorized(client):
    user_id = 123456
    new_artist = get_artist_data(user_id)
    response = client.patch('/api/artist/', data=new_artist)
    assert http.client.UNAUTHORIZED == response.status_code