本文整理汇总了Python中tastypie.test.TestApiClient.get方法的典型用法代码示例。如果您正苦于以下问题:Python TestApiClient.get方法的具体用法?Python TestApiClient.get怎么用?Python TestApiClient.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tastypie.test.TestApiClient
的用法示例。
在下文中一共展示了TestApiClient.get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ApplicationTest
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import get [as 别名]
class ApplicationTest(ResourceTestCase):
def setUp(self):
super(ApplicationTest, self).setUp()
self.client = TestApiClient()
self.endpoint = '/api/v1/apps/'
self.format = 'json'
# Create one user
self.user = User(username="testuser")
self.user.save()
# Create on token
self.token = UserToken(user=self.user)
self.token.save()
# list apps
def test_get_apps(self):
url = "%s?token=%s" % (self.endpoint, self.token.token)
request = self.client.get(url, self.format)
self.assertValidJSONResponse(request)
# list apps details
def test_get_apps_details(self):
app_id = Application.objects.all()[0].id
url = "%s%d/?token=%s" % (self.endpoint, app_id, self.token.token)
request = self.client.get(url, self.format)
self.assertValidJSONResponse(request)
示例2: CheckTokenTest
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import get [as 别名]
class CheckTokenTest(ResourceTestCase):
def setUp(self):
super(CheckTokenTest, self).setUp()
self.client = TestApiClient()
self.endpoint = '/api/v1/token/'
self.format = 'json'
# Create one user
self.user = User(username="testuser")
self.user.save()
# Create on token
self.token = UserToken(user=self.user)
self.token.save()
# check for the token ttl
def test_check_token(self):
url = "%s?token=%s" % (self.endpoint, self.token.token)
request = self.client.get(url, self.format)
self.assertValidJSONResponse(request)
# check for the WRONG token ttl
def test_check_wrong_token(self):
url = "%s?token=%s" % (self.endpoint, "not-a-valid-token")
self.assertHttpUnauthorized(self.client.get(url, self.format))
# check for the NO token ttl
def test_check_no_token(self):
self.assertHttpUnauthorized(self.client.get(self.endpoint, self.format))
示例3: PersonResourceTestCase
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import get [as 别名]
class PersonResourceTestCase(ResourceTestCase):
def setUp(self):
super(PersonResourceTestCase, self).setUp()
call_command('loaddata', 'example_data', verbosity=0)
self.user = User.objects.get(id=1)
self.writeitinstance = WriteItInstance.objects.create(name=u"a test", slug=u"a-test", owner=self.user)
self.api_client = TestApiClient()
self.data = {'format': 'json', 'username': self.user.username, 'api_key': self.user.api_key.key}
def get_credentials(self):
credentials = self.create_apikey(username=self.user.username, api_key=self.user.api_key.key)
return credentials
def test_get_list_of_messages(self):
url = '/api/v1/person/'
response = self.api_client.get(url, authentication=self.get_credentials())
self.assertValidJSONResponse(response)
persons = self.deserialize(response)['objects']
self.assertEqual(len(persons), Person.objects.count()) # All the instances
def test_unauthorized_list_of_persons(self):
url = '/api/v1/person/'
response = self.api_client.get(url)
self.assertHttpUnauthorized(response)
def test_the_remote_url_of_the_person_points_to_its_popit_instance(self):
url = '/api/v1/person/'
response = self.api_client.get(url, authentication=self.get_credentials())
persons = self.deserialize(response)['objects']
self.assertEquals(persons[0]['popit_url'], persons[0]['resource_uri'])
示例4: RefTSGetSites
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import get [as 别名]
class RefTSGetSites(ResourceTestCase):
def setUp(self):
self.serializer = Serializer()
self.logger = logging.getLogger(__name__)
self.api_client = TestApiClient()
self.client = Client()
self.username = 'creator'
self.password = 'mybadpassword'
self.group, _ = Group.objects.get_or_create(name='Hydroshare Author')
# create a user to be used for creating the resource
self.user_creator = hydroshare.create_account(
'[email protected]',
username=self.username,
first_name='Creator_FirstName',
last_name='Creator_LastName',
superuser=False,
password=self.password,
groups=[self.group]
)
self.wsdl_url_swe = "http://hydroportal.cuahsi.org/SwedishMonitoringData/webapp/cuahsi_1_1.asmx?WSDL"
self.wsdl_url_wwo = "http://worldwater.byu.edu/app/index.php/default/services/cuahsi_1_1.asmx?WSDL"
self.rest_url = "http://worldwater.byu.edu/interactive/sandbox/services/index.php/cuahsi_1_1.asmx/GetValues?location=WWO:S-PRHD&variable=WWO:JSWL&startDate=&endDate="
self.site_code_swe = "wq2371"
self.site_code_wwo = "S-PRHD"
self.post_data = {
'title': 'My REST API-created resource',
'resource_type': 'GenericResource'
}
def tearDown(self):
User.objects.all().delete()
GenericResource.objects.all().delete()
#TODO: This throws an encoding error...
# def test_get_sites_wwo(self):
# resp = self.api_client.get("/hsapi/_internal/search-sites/?wsdl_url="+self.wsdl_url_wwo)
# self.assertEqual(resp.status_code, 200)
def test_get_sites_swedish(self):
resp = self.api_client.get("/hsapi/_internal/search-sites/?url="+self.wsdl_url_swe)
self.assertEqual(resp.status_code, 200)
def test_get_variables_wwo(self):
resp = self.api_client.get("/hsapi/_internal/search-variables/?url="+self.wsdl_url_wwo+"&site="+self.site_code_wwo)
self.assertEqual(resp.status_code, 200)
self.assertTrue('Water level' in resp.content)
示例5: RefTSSnotel
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import get [as 别名]
class RefTSSnotel(ResourceTestCase):
def setUp(self):
self.serializer = Serializer()
self.logger = logging.getLogger(__name__)
self.api_client = TestApiClient()
self.client = Client()
self.username = 'creator'
self.password = 'mybadpassword'
self.group, _ = Group.objects.get_or_create(name='Hydroshare Author')
# create a user to be used for creating the resource
self.user_creator = hydroshare.create_account(
'[email protected]',
username=self.username,
first_name='Creator_FirstName',
last_name='Creator_LastName',
superuser=False,
password=self.password,
groups=[self.group]
)
self.wsdl_url = "http://worldwater.byu.edu/interactive/snotel/services/index.php/cuahsi_1_1.asmx?WSDL"
self.rest_url = "http://worldwater.byu.edu/interactive/snotel/services/index.php/cuahsi_1_1.asmx/GetValuesObject?location=SNOTEL:1039&variable=SNOTEL:WTEQ&startDate=2014-10-01&endDate=2015-03-19"
self.time_series_base_rest = "/hsapi/_internal/time-series-from-service/?ref_type=rest&service_url="
self.time_series_base_soap = "/hsapi/_internal/time-series-from-service/?ref_type=soap&service_url="
self.site_code_swe = "wq2371"
self.site_code_wwo = "S-PRHD"
self.post_data = {
'title': 'My REST API-created resource',
'resource_type': 'GenericResource'
}
def tearDown(self):
User.objects.all().delete()
GenericResource.objects.all().delete()
def test_time_series_from_service_rest(self):
url = urllib.quote(self.rest_url)
resp = self.api_client.get(self.time_series_base_rest+url)
self.assertEqual(resp.status_code, 200)
self.assertTrue('visualization' in resp.content)
def test_time_series_from_service_soap(self):
url = urllib.quote(self.wsdl_url)
resp = self.api_client.get(self.time_series_base_soap+url+"&site=823&variable=WTEQ")
self.assertEqual(resp.status_code, 200)
self.assertTrue('visualization' in resp.content)
示例6: JobTest
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import get [as 别名]
class JobTest(ResourceTestCase):
def setUp(self):
super(JobTest, self).setUp()
self.client = TestApiClient()
self.endpoint = '/api/v1/jobs/'
self.format = 'json'
# Create one user
self.user = User(username="testuser")
self.user.save()
# Create on token
self.token = UserToken(user=self.user)
self.token.save()
# create a job
self.job = Job(user=self.user, application=Application.objects.all()[0])
self.job.save()
def test_get_job_list(self):
url = "%s?token=%s" % (self.endpoint, self.token.token)
request = self.client.get(url, self.format)
self.assertValidJSONResponse(request)
def test_get_job_detail(self):
url = "%s%d/?token=%s" % (self.endpoint, self.job.id, self.token.token)
request = self.client.get(url, self.format)
self.assertValidJSONResponse(request)
def test_post_job(self):
data = {"application" : "/api/v1/apps/1/"}
url = "%s?token=%s" % (self.endpoint, self.token.token)
self.assertHttpCreated(self.client.post(url, self.format, data=data))
def test_patch_job(self):
job = Job(user=self.user, application=Application.objects.all()[0])
job.save()
data = {"progress":"50"}
url = "%s%d/?token=%s" % (self.endpoint, job.id, self.token.token)
resp = self.client.patch(url, self.format, data=data)
self.assertHttpAccepted(resp)
def test_delete_job(self):
job = Job(user=self.user, application=Application.objects.all()[0])
job.save()
url = "%s%d/?token=%s" % (self.endpoint, job.id, self.token.token)
self.assertHttpAccepted(self.client.delete(url, self.format))
示例7: CreateOrListGroupsTest
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import get [as 别名]
class CreateOrListGroupsTest(ResourceTestCase):
serializer = Serializer()
def setUp(self):
self.api_client = TestApiClient()
self.user = hydroshare.create_account(
'[email protected]',
username='user0',
first_name='User0_FirstName',
last_name='User0_LastName',
superuser=True,
password='foobar'
)
g0=hydroshare.create_group(name="group0")
g1=hydroshare.create_group(name="group1")
g2=hydroshare.create_group(name="group2")
self.user.groups.add(g0,g1,g2)
self.g_ids=[g0.id,g1.id,g2.id]
self.groups_url_base = '/hsapi/groups/'
self.api_client.client.login(username=self.user.username, password=self.user.password)
def tearDown(self):
Group.objects.all().delete()
User.objects.all().delete()
def test_create_group(self):
post_data = {'name': 'newgroup'}
resp = self.api_client.post(self.groups_url_base, data=post_data)
self.assertHttpCreated(resp)
grouplist = Group.objects.all()
num_of_groups=len(grouplist)
self.assertTrue(any(Group.objects.filter(name='newgroup')))
self.assertEqual(num_of_groups, 4)
def test_list_groups(self):
query = self.serialize({'user': self.user.id})
get_data = {'query': query}
resp = self.api_client.get(self.groups_url_base, data=get_data)
print resp
self.assertEqual(resp.status_code, 200)
groups = self.deserialize(resp)
new_ids = []
for num in range(len(groups)):
new_ids.append(groups[num]['id'])
self.assertTrue(Group.objects.filter(user='user{0}'.format(num)).exists())
self.assertEqual(str(groups[num]['name']), 'group{0}'.format(num))c
self.assertEqual(sorted(self.g_ids), sorted(new_ids))
示例8: GetCapabilities
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import get [as 别名]
class GetCapabilities(ResourceTestCase):
serializer = Serializer()
def setUp(self):
self.api_client = TestApiClient()
self.user = hydroshare.create_account(
"[email protected]", username="user", first_name="User_FirstName", last_name="User_LastName"
)
self.url_base = "/hsapi/capabilities/"
def tearDown(self):
User.objects.all().delete()
def test_generic(self):
res = hydroshare.create_resource("GenericResource", self.user, "res1")
url = "{0}{1}/".format(self.url_base, res.short_id)
resp = self.api_client.get(url)
self.assertValidJSONResponse(resp)
capabilities = self.deserialize(resp)
self.assertEqual(capabilities, None)
def test_other_types(self):
pass
示例9: PublicMessagesInAPI
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import get [as 别名]
class PublicMessagesInAPI(ResourceTestCase):
def setUp(self):
super(PublicMessagesInAPI,self).setUp()
call_command('loaddata', 'example_data', verbosity=0)
self.user = User.objects.all()[0]
self.writeitinstance = WriteItInstance.objects.create(name="a test", slug="a-test", owner=self.user)
self.writeitinstance.moderation_needed_in_all_messages = True
self.writeitinstance.save()
self.person1 = Person.objects.all()[0]
self.api_client = TestApiClient()
self.data = {'format': 'json', 'username': self.user.username, 'api_key':self.user.api_key.key}
def test_non_confirmated_message_not_showing_in_api(self):
"""Non confirmated message is not in the API"""
message = Message.objects.create(content = 'Content 1',
author_name='Felipe',
author_email="[email protected]",
subject='public non confirmated message',
writeitinstance= self.writeitinstance,
persons = [self.person1])
#OK this is just to show that this message is not confirmed
self.assertFalse(message.confirmated)
self.assertNotIn(message, Message.public_objects.all())
#I've tested this in messages_test.py
url = '/api/v1/instance/{0}/messages/'.format(self.writeitinstance.id)
response = self.api_client.get(url,data = self.data)
self.assertValidJSONResponse(response)
messages = self.deserialize(response)['objects']
self.assertFalse(messages)
示例10: PagePaginationTestCase
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import get [as 别名]
class PagePaginationTestCase(ResourceTestCase):
def setUp(self):
super(PagePaginationTestCase, self).setUp()
call_command('loaddata', 'example_data', verbosity=0)
self.user = User.objects.get(id=1)
# self.writeitinstance = WriteItInstance.objects.create(name="a test", slug="a-test", owner=self.user)
self.api_client = TestApiClient()
self.data = {'format': 'json', 'username': self.user.username, 'api_key': self.user.api_key.key}
def test_setting_all_variables(self):
request_data = {
'limit': None,
'offset': 0,
}
objects = Message.objects.all()
paginator = PagePaginator(request_data, objects)
self.assertIsInstance(paginator, Paginator)
def test_get_offset(self):
request_data = {
'limit': None,
'offset': 5,
}
objects = Message.objects.all()
paginator = PagePaginator(request_data, objects)
self.assertEquals(paginator.get_offset(), request_data['offset'])
def assertOffsetEquals(self, page, limit, offset, objects=Message.objects.all()):
request_data = {
'limit': limit,
'page': page,
}
objects = objects
paginator = PagePaginator(request_data, objects)
self.assertEquals(paginator.get_offset(), offset)
def test_get_page(self):
self.assertOffsetEquals(1, 1, 0)
self.assertOffsetEquals(1, 2, 0)
self.assertOffsetEquals(2, 1, 1)
self.assertOffsetEquals(2, 2, 2)
def test_get_paginated(self):
url = '/api/v1/message/'
data = self.data
data['page'] = 2
data['limit'] = 1
for message in Message.objects.all():
Confirmation.objects.create(message=message)
message.recently_confirmated()
response = self.api_client.get(url, data=data)
# All messages must be confirmed
# in order to be in the api
messages = self.deserialize(response)['objects']
self.assertEquals(
messages[0]['id'],
Message.public_objects.order_by('-created')[1].id)
示例11: TestResolveDOIView
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import get [as 别名]
class TestResolveDOIView(ResourceTestCase):
serializer = Serializer()
def setUp(self):
self.api_client = TestApiClient()
user = hydroshare.create_account(
'[email protected]',
username='user0',
first_name='User0_FirstName',
last_name='User0_LastName',
)
self.res = hydroshare.create_resource('GenericResource', user, 'myres')
hydroshare.publish_resource(self.res.short_id)
def tearDown(self):
User.objects.all().delete()
hydroshare.delete_resource(self.res.short_id)
def test_get(self):
url = 'hsapi/resolveDOI/{0}/'.format(self.res.doi)
resp = self.api_client.get(url)
self.assertValidJSONResponse(resp)
resp_pk = self.deserialize(resp)
self.assertEqual(self.res.short_id, resp_pk)
def test_bad_doi(self):
url = 'hsapi/resolveDOI/{0}/'.format('bad_doi')
resp = self.api_client.get(url)
self.assertEqual(resp.status_code, 404)
示例12: TestGetResourceList
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import get [as 别名]
class TestGetResourceList(ResourceTestCase):
serializer = Serializer()
def setUp(self):
self.api_client = TestApiClient()
self.username = 'jamy'
self.password = '00001'
# create a user
self.user = hydroshare.create_account(
'[email protected]',
username=self.username,
first_name='Tian',
last_name='Gan',
superuser=False,
password=self.password,
groups=[]
)
# create a resource
self.res = hydroshare.create_resource(
'GenericResource',
self.user,
'My resource'
)
# create a group
self.group = hydroshare.create_group(
'Test group',
members=[],
owners=[self.user1]
)
def tearDown(self):
User.objects.all().delete()
GenericResource.objects.all().delete()
def test_get_resource_list(self):
query = self.serialize({'user': self.username, 'keywords': ['a', 'b']})
resp = self.api_client.get('/hsapi/resources/', data=query)
self.assertEqual(resp.status_code, 200)
示例13: EncounterParticipantResourceTest
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import get [as 别名]
class EncounterParticipantResourceTest(ResourceTestCase):
fixtures = ['testing_users.json', 'testing_data.json']
def setUp(self):
super(EncounterParticipantResourceTest, self).setUp()
self.api_client = TestApiClient()
self.username = 'SuperUser'
self.password = 'FakePass'
self.user = User.objects.create_superuser(self.username, '[email protected]', self.password)
self.encounter_participant = EncounterParticipant.objects.get(id=1)
def get_credentials(self):
return self.api_client.client.login(username=self.username,
password=self.password)
def test_pc_initiative_save_unauthenticated(self):
self.assertHttpUnauthorized(self.api_client.patch('/dm/api/v1/encounter_participant/1/',
format='json', data={'initiative': 5}))
def test_pc_initiative_save(self):
"""
Should save initiative for PC
"""
resp = self.api_client.patch('/dm/api/v1/encounter_participant/1/',
authorization=self.get_credentials(),
format='json', data={'initiative': 5})
self.assertHttpAccepted(resp)
resp = self.api_client.get('/dm/api/v1/encounter_participant/1/',
format='json',
authentication=self.get_credentials())
self.assertEqual(self.deserialize(resp)['initiative'], 5)
示例14: mock_request
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import get [as 别名]
def mock_request(obj, method, url, **kwargs):
client = TestApiClient()
authentication = 'Basic %s' % base64.b64encode(':'.join([
get_setting('SUPERUSER_USERNAME', None),
get_setting('SUPERUSER_PASSWORD', None),
]))
if method == 'GET':
data = kwargs.get('params', {})
djresponse = client.get(url, data=data, authentication=authentication)
elif method == 'POST':
data = json.loads(kwargs.get('data', '{}'))
djresponse = client.post(url, data=data, authentication=authentication)
elif method == 'PUT':
data = json.loads(kwargs.get('data', '{}'))
djresponse = client.put(url, data=data, authentication=authentication)
elif method == 'PATCH':
data = json.loads(kwargs.get('data', '{}'))
djresponse = client.patch(url, data=data, authentication=authentication)
elif method == 'DELETE':
data = kwargs.get('params', {})
djresponse = client.delete(url, data=data, authentication=authentication)
# convert django.http.HttpResponse to requests.models.Response
response = requests.models.Response()
response.status_code = djresponse.status_code
response.headers = {}
try:
response.headers['content-type'] = djresponse['content-type']
response.headers['location'] = djresponse['location']
except:
pass
response.encoding = requests.utils.get_encoding_from_headers(response.headers)
response._content = djresponse.content
return response
示例15: ResourceTest
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import get [as 别名]
class ResourceTest(ResourceTestCase):
def setUp(self):
self.logger = logging.getLogger(__name__)
self.api_client = TestApiClient()
self.username = "creator"
self.password = "mybadpassword"
# create a user to be used for creating the resource
self.user_creator = hydroshare.create_account(
"[email protected]",
username=self.username,
first_name="Creator_FirstName",
last_name="Creator_LastName",
superuser=False,
password=self.password,
groups=[],
)
self.user_url = "/hsapi/accounts/{0}/".format(self.user_creator.username)
self.api_client.client.login(username=self.username, password=self.password)
# create a resource
self.resource = hydroshare.create_resource(
resource_type="GenericResource",
title="My resource",
owner=self.user_creator,
last_changed_by=self.user_creator,
)
self.resource_url_base = "/hsapi/resource/"
self.resource_url = "{0}{1}/".format(self.resource_url_base, self.resource.short_id)
self.post_data = {"title": "My REST API-created resource", "resource_type": "GenericResource"}
def tearDown(self):
User.objects.all().delete()
GenericResource.objects.all().delete()
def get_credentials(self):
k = self.create_basic(username=self.username, password=self.password)
print k
return k
def test_resource_get(self):
resp = self.api_client.get(self.resource_url)
self.assertTrue(resp["Location"].endswith(".zip"))
def test_resource_post(self):
resp = self.api_client.post(self.resource_url_base, data=self.post_data)
self.assertIn(resp.status_code, [201, 200])
# PID comes back as body of response, but API client doesn't seem to be
# parsing the response for us
pid = str(resp).split("\n")[-1]
new_resource_url = "{0}{1}/".format(self.resource_url_base, pid)
# Fetch the newly created resource
resp = self.api_client.get(new_resource_url)
self.assertTrue(resp["Location"].endswith(".zip"))
def test_resource_put(self):
new_data = {}
new_data["title"] = "My UPDATED REST API-created resource"
resp = self.api_client.put(self.resource_url, data=new_data)
self.assertIn(resp.status_code, ["202", "204"])
def test_resource_delete(self):
x = self.api_client.delete(self.resource_url, format="json")
self.assertIn(x.status_code, [202, 204, 301])
self.assertHttpNotFound(self.api_client.get(self.resource_url, format="json"))