def test_links_to_endpoints(self): """ Verify that root links to the available endpoints """ b = Builder('http://localhost/').add_curie(name='ep', href='/rel/{rel}')\ .add_link('ep:user', target='/users')\ .add_link('ep:dataset', target='/datasets')\ .add_link('ep:organization', target='/organizations')\ .add_link('ep:schema', target='/schema') expected = b.as_object() response = self.client.get("/") Document.from_object(json.loads(response.data)) self.assertEquals(response.json, expected)
def test_create_dataset(self): """ Verify that a POST to /datasets will create a new dataset """ data = populate_db() org = data['orgs'][0].id creator = data['users'][0].id response = self.client.post('/datasets', data=json.dumps({ 'organization': str(org), 'created_by': str(creator), 'title':'footest' }), content_type='application/json', environ_base={ 'HTTP_USER_AGENT': 'Chrome', 'REMOTE_ADDR': '127.0.0.1' }) self.assertEquals(201, response.status_code) self.assertIsNotNone(response.headers['Location']) #Verify the Location is valid by getting it and comparing to expected response = self.client.get(response.headers['Location']) response_doc = Document.from_object(response.json) self.assertEquals('footest', response_doc.properties['title'])
def test_get_poll_view(self): response = self.client.get('/poll/%s' % self.poll.id) self.assertEqual(response.status_code, 200) content = simplejson.loads(response.content) doc = Document.from_object(content) self.assertEqual(doc.links['self'].url(), 'http://testserver/poll/%s' % self.poll.id)
def is_link(context, resource): expected_url = get_url(resource) doc = Document.from_object(json.loads(context.page.data)) link = doc.links.get('ep:'+resource, None) assert link, 'Resource link not found: %s'% resource actual_url = link.url assert actual_url, "Resource 'ep:%s' not found in links %s" % (resource, doc.links.keys()) assert expected_url == actual_url, "actual url [%s] does not match expected url [%s]" % (actual_url, expected_url)
def test_get_with_additional_embedded_view(self): response = self.client.get('/poll_with_additional_embedded/%s' % self.poll.id) self.assertEqual(response.status_code, 200) content = simplejson.loads(response.content) doc = Document.from_object(content) self.assertEqual(doc.links['self'].url(), 'http://testserver/poll/%s' % self.poll.id) self.assertEqual(doc.embedded['additional_field'].properties['value'], 'added on %s' % self.poll.id)
def count_links(context, num_links, rel_uri): doc = Document.from_object(json.loads(context.page.data)) link = doc.links.get(rel_uri, None) assert link, 'Resource link not found: %s'% rel_uri assert num_links == link. assert actual_url, "Resource 'ep:%s' not found in links %s" % (resource, doc.links.keys()) assert expected_url == actual_url, "actual url [%s] does not match expected url [%s]" % (actual_url, expected_url)
def test_list_datasets_paginates(self): """ Verify that datasets can be retrieved via pagination """ data = populate_db(num_datasets=100) # Create canned data expected = ['/datasets/%s' % dataset.id for dataset in data['datasets']] assert len(expected) is 100 response = self.client.get("/datasets") response_doc = Document.from_object(response.json) hrefs = [link.url() for link in response_doc.links['/rel/dataset']] while 'next' in response_doc.links.keys(): response = self.client.get(response_doc.links['next'].url()) response_doc = Document.from_object(response.json) hrefs.extend([link.url() for link in response_doc.links['/rel/dataset']]) self.assertEquals(set(expected)-set(hrefs), set())
def test_list_datasets_references_self(self): """ Verify that when we list datasets and paginate over them, that we by default store a url reference to self - the EXACT url used to access the dataset """ data = populate_db(num_datasets=50) # Create canned data # Start at /datasets response = self.client.get("/datasets") response_doc = Document.from_object(response.json) # Select the next list of results next_url = response_doc.links['next'].url() response = self.client.get(next_url) response_doc = Document.from_object(response.json) # We expect that 'next' link in the first results should equal 'self' link in next list of results self.assertEquals(response_doc.links['self'].url(), 'http://localhost%s' % next_url)
def test_get_choice_exclude_votes(self): response = self.client.get('/choice/%s?exclude=votes' % self.choice.id) self.assertEqual(response.status_code, 200) content = simplejson.loads(response.content) doc = Document.from_object(content) self.assertEqual(doc.links['self'].url(), 'http://testserver/choice/%s' % self.choice.id) self.assertEqual(doc.links['poll'].url(), 'http://testserver/poll/%s' % self.poll.id) self.assertEqual(doc.properties['choice_text'], self.choice.choice_text) self.assertNotIn('votes', doc.properties)
def test_get_choice_embed_poll(self): response = self.client.get('/choice/%s?embed=poll' % self.choice.id) self.assertEqual(response.status_code, 200) content = simplejson.loads(response.content) doc = Document.from_object(content) self.assertEqual(doc.links['self'].url(), 'http://testserver/choice/%s' % self.choice.id) self.assertNotIn('poll', doc.links) self.assertEqual(doc.embedded['poll'].properties['question'], self.poll.question) self.assertEqual(doc.embedded['poll'].url(), 'http://testserver/poll/%s' % self.poll.id) self.assertEqual(doc.properties['choice_text'], self.choice.choice_text) self.assertEqual(doc.properties['votes'], self.choice.votes)
def hal(self): """ Return the response as a JSON response. You must have `simplejson <http://goo.gl/B9g6s>`_ installed to use this, or be using a Python version with the json module. The content type must be one of json type to use this. """ if not self.content_type.endswith(('+json', '/json')): raise AttributeError( "Not a JSON response body (content-type: %s)" % self.content_type) return Document.from_string(self.testbody)
def test_get_user(self): """ Verify ability to GET an individual user """ data = populate_db(num_users=100) test_data = data['users'][0] response = self.client.get("/users/%s" % test_data.id) response_doc = Document.from_object(response.json) self.assertEquals(test_data.email, response_doc.properties['email']) self.assertEquals(200, response.status_code)
def test_create_poll_choice_successfully(self): data = { 'choice_text': 'Oishi' } response = self.client.post('/poll/%s/choice' % self.poll.id, data=simplejson.dumps(data), content_type='application/json') self.assertEqual(response.status_code, 201) choice = Choice.objects.get(poll=self.poll, choice_text=data['choice_text']) content = simplejson.loads(response.content) doc = Document.from_object(content) self.assertEqual(doc.links['self'].url(), 'http://testserver/poll/%s/choice/%s' % (self.poll.id, choice.id)) self.assertEqual(doc.links['poll'].url(), 'http://testserver/poll/%s' % self.poll.id) self.assertEqual(doc.properties['choice_text'], choice.choice_text) self.assertEqual(doc.properties['votes'], 0) self.assertNotIn('poll', doc.properties)
def test_list_datasets(self): """ Verify that all datasets can be retrieved """ data = populate_db(num_datasets=2) # Create canned data, assign to 'data' property expected = ['/datasets/%s' % dataset.id for dataset in data['datasets']] response = self.client.get("/datasets") response_doc = Document.from_object(response.json) hrefs = [link.url() for link in response_doc.links['/rel/dataset']] self.assertEquals(set(expected)-set(hrefs), set())
def test_get_organization(self): """ Verify ability to GET an individual organization """ data = populate_db(num_orgs=100) test_data = data['orgs'][0] response = self.client.get("/organizations/%s" % test_data.id) response_doc = Document.from_object(response.json) self.assertEquals(test_data.title, response_doc.properties['title']) self.assertEquals(200, response.status_code)
def test_list_organizations(self): """ Verify that organizations can be retrieved """ data = populate_db(num_orgs=5, num_datasets=0) # Create canned data, assign to 'data' property expected = ['/organizations/%s' % org.id for org in data['orgs']] response = self.client.get("/organizations") response_doc = Document.from_object(response.json) hrefs = [link.url() for link in response_doc.links['/rel/organization']] print hrefs self.assertEquals(set(expected)-set(hrefs), set())
def test_create_organization(self): """ Verify that a POST to /organizations will create a new organization """ response = self.client.post('/organizations', data=json.dumps({ 'title': 'testorg' }), content_type='application/json', environ_base={ 'HTTP_USER_AGENT': 'Chrome', 'REMOTE_ADDR': '127.0.0.1' }) self.assertEquals(201, response.status_code) self.assertIsNotNone(response.headers['Location']) #Verify the Location is valid by getting it and comparing to expected response = self.client.get(response.headers['Location']) response_doc = Document.from_object(response.json) self.assertEquals('testorg', response_doc.properties['title'])
def test_create_user(self): """ Verify that a POST to /users will create a new user """ response = self.client.post('/users', data=json.dumps({ 'display_name': 'jim', 'email': '*****@*****.**', 'password':'******' }), content_type='application/json', environ_base={ 'HTTP_USER_AGENT': 'Chrome', 'REMOTE_ADDR': '127.0.0.1' }) self.assertEquals(201, response.status_code) self.assertIsNotNone(response.headers['Location']) #Verify the Location is valid by getting it and comparing to expected response = self.client.get(response.headers['Location']) response_doc = Document.from_object(response.json) self.assertEquals('*****@*****.**', response_doc.properties['email'])
def hal_loads(resp_str): """Helper function that converts a string into a HAL object""" return Document.from_object(json.loads(resp_str))