本文整理汇总了Python中tastypie.test.TestApiClient.delete方法的典型用法代码示例。如果您正苦于以下问题:Python TestApiClient.delete方法的具体用法?Python TestApiClient.delete怎么用?Python TestApiClient.delete使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tastypie.test.TestApiClient
的用法示例。
在下文中一共展示了TestApiClient.delete方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: JobTest
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import delete [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))
示例2: mock_request
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import delete [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
示例3: ResourceTest
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import delete [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"))
示例4: TestCampaignResource
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import delete [as 别名]
class TestCampaignResource(ResourceTestCase):
def setUp(self):
#Tastypie stuff
super(TestCampaignResource, self).setUp()
self.bob_api_client = TestApiClient()
self.bill_api_client = TestApiClient()
self.anon_api_client = TestApiClient()
# Create a user bob.
self.user_bob_username = 'bob'
self.user_bob_password = 'bob'
self.user_bob = User.objects.create_user(self.user_bob_username,
'[email protected]',
self.user_bob_password)
# Create a user bob.
self.user_bill_username = 'bill'
self.user_bill_password = 'bill'
self.user_bill = User.objects.create_user(self.user_bill_username,
'[email protected]',
self.user_bill_password)
self.bob_api_client.client.login(username='bob', password='bob')
self.bill_api_client.client.login(username='bill', password='bill')
#assign users to the Public group
public_group, created = Group.objects.get_or_create(name='Public')
self.user_bob.groups.add(public_group)
self.user_bill.groups.add(public_group)
guardian.utils.get_anonymous_user().groups.add(public_group)
#make a couple of campaigns and save
self.campaign_bobs = mommy.make_one('catamidb.Campaign', id=1)
self.campaign_bills = mommy.make_one('catamidb.Campaign', id=2)
#assign this one to bob
authorization.apply_campaign_permissions(
self.user_bob, self.campaign_bobs)
#assign this one to bill
authorization.apply_campaign_permissions(
self.user_bill, self.campaign_bills)
#the API url for campaigns
self.campaign_url = '/api/dev/campaign/'
#some post data for testing campaign creation
self.post_data = {
'short_name': 'Blah',
'description': 'Blah',
'associated_researchers': 'Blah',
'associated_publications': 'Blah',
'associated_research_grant': 'Blah',
'date_start': '2012-05-01',
'date_end': '2012-05-01',
'contact_person': 'Blah',
}
# can only do GET at this stage
def test_campaign_operations_disabled(self):
# test that we can NOT create
self.assertHttpMethodNotAllowed(
self.anon_api_client.post(self.campaign_url,
format='json',
data=self.post_data))
# test that we can NOT modify
self.assertHttpMethodNotAllowed(
self.anon_api_client.put(
self.campaign_url + self.campaign_bobs.id.__str__() + "/",
format='json',
data={})
)
# test that we can NOT delete
self.assertHttpMethodNotAllowed(
self.anon_api_client.delete(
self.campaign_url + self.campaign_bobs.id.__str__() + "/",
format='json')
)
# test that we can NOT create authenticated
self.assertHttpMethodNotAllowed(
self.bob_api_client.post(self.campaign_url,
format='json',
data=self.post_data)
)
# test that we can NOT modify authenticated
self.assertHttpMethodNotAllowed(
self.bob_api_client.put(
self.campaign_url + self.campaign_bobs.id.__str__() + "/",
format='json',
data={})
)
# test that we can NOT delete authenticated
self.assertHttpMethodNotAllowed(
#.........这里部分代码省略.........
示例5: TestScientificImageMeasurementResource
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import delete [as 别名]
#.........这里部分代码省略.........
#assign this one to bob
authorization.apply_campaign_permissions(
self.user_bob, self.campaign_bobs)
#assign this one to bill
authorization.apply_campaign_permissions(
self.user_bill, self.campaign_bills)
#the API url for campaigns
self.image_measurement_url = '/api/dev/scientificimagemeasurement/'
self.post_data = []
# can only do GET at this stage
def test_image_measurement_operations_disabled(self):
# test that we can NOT create
self.assertHttpMethodNotAllowed(
self.anon_api_client.post(self.image_measurement_url,
format='json',
data=self.post_data))
# test that we can NOT modify
self.assertHttpMethodNotAllowed(
self.anon_api_client.put(
self.image_measurement_url +
self.deployment_bobs.id.__str__() + "/",
format='json',
data={}
)
)
# test that we can NOT delete
self.assertHttpMethodNotAllowed(
self.anon_api_client.delete(
self.image_measurement_url +
self.deployment_bobs.id.__str__() + "/",
format='json'
)
)
# test that we can NOT create authenticated
self.assertHttpMethodNotAllowed(
self.bob_api_client.post(
self.image_measurement_url,
format='json',
data=self.post_data
)
)
# test that we can NOT modify authenticated
self.assertHttpMethodNotAllowed(
self.bob_api_client.put(
self.image_measurement_url +
self.deployment_bobs.id.__str__() + "/",
format='json',
data={}
)
)
# test that we can NOT delete authenticated
self.assertHttpMethodNotAllowed(
self.bob_api_client.delete(
self.image_measurement_url +
self.deployment_bobs.id.__str__() + "/",
format='json'
)
示例6: ProjectPartResourceTest
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import delete [as 别名]
class ProjectPartResourceTest(ResourceTestCase):
fixtures = ['group.json', 'group_permission.json',
'user_group.json', 'user.json',
'user_project.json', 'project_part.json',
'project.json', 'profile.json']
def setUp(self):
self.api_client = TestApiClient()
self.post_data = {'title': 'new part',
'project_id': {'pk': 3},
'project_part_id': {'pk': 1},
'order': 0,
'weight': 20
}
self.project_part_id = 1
self.open_project_part_id = 2
self.detail_base_url = '/api/v1/project_part/{0}/'
self.open_detail_url = self.detail_base_url.format(self.open_project_part_id)
self.detail_url = self.detail_base_url.format(self.project_part_id)
self.list_url = '/api/v1/project_part/'
self.serializer = Serializer()
def get_credentials(self):
self.api_client.client.login(email='[email protected]',
password='django')
def test_get_list_unauthorized(self):
self.assertHttpUnauthorized(self.api_client.get(self.list_url,
format='json'))
def test_get_list(self):
self.get_credentials()
self.assertHttpUnauthorized(self.api_client.get(self.list_url, format='json'))
def test_get_detail_unauthenticated(self):
self.assertHttpUnauthorized(self.api_client.get(self.detail_url, format='json'))
def test_get_detail(self):
self.get_credentials()
resp = self.api_client.get(self.detail_url, format='json')
self.assertValidJSONResponse(resp)
self.assertEqual(json.loads(resp.content)['title'], 'Project Part 1')
# FIXME Idem from above
# def test_post_list_unauthenticated(self):
# self.assertHttpUnauthorized(self.api_client.post(self.list_url, format='json', data=self.post_data))
def test_post_list(self):
self.get_credentials()
self.assertEqual(ProjectPart.objects.count(), 2)
resp = self.api_client.post(self.list_url, format='json',
data=self.post_data)
self.assertHttpCreated(resp)
self.assertEqual(ProjectPart.objects.count(), 3)
def test_put_detail_unauthenticated(self):
self.assertHttpUnauthorized(self.api_client.put(self.detail_url, format='json', data={}))
@mock.patch('wevolve.projects.api.update_drive_folder')
def test_put_detail(self, method):
self.get_credentials()
content = self.api_client.get(self.detail_url, format='json').content
original_data = json.loads(content)
new_data = original_data.copy()
new_data['title'] = 'Updated First Post'
self.assertEqual(ProjectPart.objects.count(), 2)
resp = self.api_client.put(self.detail_url, format='json', data=new_data)
self.assertHttpAccepted(resp)
self.assertEqual(ProjectPart.objects.count(), 2)
self.assertEqual(ProjectPart.objects.get(pk=self.project_part_id).title, 'Updated First Post')
def test_delete_detail_unauthenticated(self):
self.assertHttpUnauthorized(self.api_client.delete(self.detail_url, format='json'))
def test_delete_detail(self):
self.get_credentials()
self.assertEqual(ProjectPart.objects.count(), 2)
self.assertHttpAccepted(self.api_client.delete(self.detail_url, format='json'))
self.assertEqual(ProjectPart.objects.count(), 1)
# Project parts in open projects
def test_get_detail_open(self):
resp = self.api_client.get(self.open_detail_url, format='json')
self.assertValidJSONResponse(resp)
self.assertEqual(json.loads(resp.content)['title'], 'Project Part 2')
def test_put_detail_open(self):
content = self.api_client.get(self.open_detail_url, format='json').content
original_data = json.loads(content)
new_data = original_data.copy()
new_data['title'] = 'Updated First Post'
self.assertEqual(ProjectPart.objects.count(), 2)
resp = self.api_client.put(self.detail_url, format='json', data=new_data)
self.assertHttpUnauthorized(resp)
def test_delete_detail_open(self):
self.assertEqual(ProjectPart.objects.count(), 2)
#.........这里部分代码省略.........
示例7: TaskResourceTest
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import delete [as 别名]
class TaskResourceTest(ResourceTestCase):
fixtures = ['group.json', 'group_permission.json',
'user_group.json', 'user.json',
'user_project.json', 'project_part.json',
'project.json', 'profile.json',
'task.json']
def setUp(self):
self.api_client = TestApiClient()
self.task_id = 1
self.project_part_id = 1
self.task = {'description': 'new Task',
'project_part': {'pk': self.project_part_id},
'flag_finished': 0,
'weight': 20
}
self.detail_url = '/api/v1/task/{0}/'.format(self.task_id)
self.project_part_query = '='.join(['?project_part', str(self.project_part_id)])
self.list_url = '/api/v1/task/'
self.serializer = Serializer()
def get_credentials(self):
result = self.api_client.client.login(email='[email protected]',
password='django')
def test_post_list(self):
self.get_credentials()
self.assertIn('_auth_user_id', self.api_client.client.session)
self.assertEqual(self.api_client.client.session['_auth_user_id'], 1)
self.assertEqual(Task.objects.count(), 2)
resp = self.api_client.post(self.list_url,
format='json',
data=self.task)
self.assertHttpCreated(resp)
self.assertEqual(Task.objects.count(), 3)
def test_get_list_unauthenticated(self):
self.assertHttpUnauthorized(self.api_client.get(''.join([self.list_url, self.project_part_query]),
format='json'))
def test_get_list_json(self):
self.get_credentials()
resp = self.api_client.get(''.join([self.list_url, self.project_part_query]),
format='json')
self.assertValidJSONResponse(resp)
self.assertEqual(len(json.loads(resp.content)['objects']), 2)
def test_get_detail_unauthenticated(self):
resp = self.api_client.get(self.detail_url, format='json')
self.assertHttpUnauthorized(resp)
def test_get_detail_json(self):
self.get_credentials()
resp = self.api_client.get(self.detail_url, format='json')
self.assertValidJSONResponse(resp)
self.assertEqual(json.loads(resp.content)['description'], 'new task')
# def test_post_list_unauthenticated(self):
# self.assertHttpUnauthorized(self.api_client.post(self.list_url, format='json',
# data=self.task))
def test_put_detail_unauthenticated(self):
self.assertHttpUnauthorized(self.api_client.put(self.detail_url, format='json', data={}))
def test_put_detail(self):
self.get_credentials()
original_data = json.loads(self.api_client.get(self.detail_url, format='json').content)
new_data = original_data.copy()
new_data['title'] = 'Updated: First Task'
self.assertEqual(Task.objects.count(), 2)
self.assertHttpAccepted(self.api_client.put(self.detail_url, format='json', data=new_data))
self.assertEqual(Task.objects.count(), 2)
def test_delete_detail_unauthenticated(self):
self.assertHttpUnauthorized(self.api_client.delete(self.detail_url, format='json'))
def test_delete_detail(self):
self.get_credentials()
self.assertEqual(Task.objects.count(), 2)
self.assertHttpAccepted(self.api_client.delete(self.detail_url, format='json'))
self.assertEqual(Task.objects.count(), 1)
示例8: TestMeasurementsResource
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import delete [as 别名]
class TestMeasurementsResource(ResourceTestCase):
def setUp(self):
# Tastypie stuff
super(TestMeasurementsResource, self).setUp()
self.bob_api_client = TestApiClient()
self.bill_api_client = TestApiClient()
self.anon_api_client = TestApiClient()
# Create a user bob.
self.user_bob_username = "bob"
self.user_bob_password = "bob"
self.user_bob = User.objects.create_user(self.user_bob_username, "[email protected]", self.user_bob_password)
# Create a user bob.
self.user_bill_username = "bill"
self.user_bill_password = "bill"
self.user_bill = User.objects.create_user(
self.user_bill_username, "[email protected]", self.user_bill_password
)
self.bob_api_client.client.login(username="bob", password="bob")
self.bill_api_client.client.login(username="bill", password="bill")
# assign users to the Public group
public_group, created = Group.objects.get_or_create(name="Public")
self.user_bob.groups.add(public_group)
self.user_bill.groups.add(public_group)
guardian.utils.get_anonymous_user().groups.add(public_group)
# make a couple of campaigns and save
self.campaign_bobs = mommy.make_one("catamidb.Campaign", id=1)
self.campaign_bills = mommy.make_one("catamidb.Campaign", id=2)
# make a deployments
self.deployment_bobs = mommy.make_recipe("catamidb.Deployment1", id=1, campaign=self.campaign_bobs)
self.deployment_bills = mommy.make_recipe("catamidb.Deployment2", id=2, campaign=self.campaign_bills)
# make images
self.image_bobs = mommy.make_recipe("catamidb.Image1", id=1, deployment=self.deployment_bobs)
self.image_bills = mommy.make_recipe("catamidb.Image2", id=2, deployment=self.deployment_bills)
# make measurements
self.measurements_bobs = mommy.make_one("catamidb.Measurement", id=1, image=self.image_bobs)
self.measurements_bills = mommy.make_one("catamidb.Measurement", id=2, image=self.image_bills)
# assign this one to bob
authorization.apply_campaign_permissions(self.user_bob, self.campaign_bobs)
# assign this one to bill
authorization.apply_campaign_permissions(self.user_bill, self.campaign_bills)
# the API url for Measurements
self.image_measurement_url = "/api/dev/measurements/"
self.post_data = {
"altitude": "13",
"pitch": "1",
"roll": "5",
"salinity": "20",
"temperature": "22",
"yaw": "1",
}
# can only do GET at this stage
def test_measurement_operations_disabled(self):
# test that we can NOT modify
self.assertHttpUnauthorized(
self.anon_api_client.put(
self.image_measurement_url + self.deployment_bobs.id.__str__() + "/", format="json", data={}
)
)
# test that we can NOT delete
self.assertHttpMethodNotAllowed(
self.anon_api_client.delete(
self.image_measurement_url + self.deployment_bobs.id.__str__() + "/", format="json"
)
)
# test that we can NOT modify authenticated
self.assertHttpUnauthorized(
self.bob_api_client.put(
self.image_measurement_url + self.deployment_bobs.id.__str__() + "/", format="json", data={}
)
)
# test that we can NOT delete authenticated
self.assertHttpMethodNotAllowed(
self.bob_api_client.delete(
self.image_measurement_url + self.deployment_bobs.id.__str__() + "/", format="json"
)
)
def test_measurement_operations_as_authorised_users(self):
# create a campaign & deployment that ONLY bill can see
bills_campaign = mommy.make_one("catamidb.Campaign", id=3, short_name="cp__3")
bills_deployment = mommy.make_recipe("catamidb.Deployment1", id=3, campaign=bills_campaign, short_name="dp__3")
bills_image = mommy.make_recipe("catamidb.Image3", id=3, deployment=bills_deployment)
bills_measurements = mommy.make_one("catamidb.Measurement", id=3, image=bills_image)
#.........这里部分代码省略.........
示例9: DocumentResourceTest
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import delete [as 别名]
class DocumentResourceTest(ResourceTestCase):
fixtures = ['group.json', 'group_permission.json',
'user_group.json', 'user.json',
'user_project.json', 'project_part.json',
'project.json', 'profile.json',
'document.json']
def setUp(self):
self.api_client = TestApiClient()
self.project_part_id = 1
self.document_data = {'text': 'My document', 'project_part': {'pk': self.project_part_id}}
self.document_id = 1
self.project_part_query = '='.join(['?project_part', str(self.project_part_id)])
self.detail_url = '/api/v1/document/{0}/'.format(self.document_id)
self.list_url = '/api/v1/document/'
self.serializer = Serializer()
def get_credentials(self):
result = self.api_client.client.login(email='[email protected]',
password='django')
def test_document_list(self):
self.get_credentials()
self.assertIn('_auth_user_id', self.api_client.client.session)
self.assertEqual(self.api_client.client.session['_auth_user_id'], 1)
self.assertEqual(Document.objects.count(), 2)
self.assertHttpCreated(self.api_client.post(self.list_url,
format='json',
data=self.document_data))
self.assertEqual(Document.objects.count(), 3)
def test_get_list_unauthorzied(self):
self.assertHttpUnauthorized(self.api_client.get(self.list_url,
format='json'))
def test_get_list(self):
self.get_credentials()
resp = self.api_client.get(''.join([self.list_url, self.project_part_query]),
format='json')
self.assertValidJSONResponse(resp)
self.assertEqual(len(json.loads(resp.content)['objects']), 1)
def test_get_detail_unauthenticated(self):
self.assertHttpUnauthorized(self.api_client.get(self.detail_url, format='json'))
def test_get_detail(self):
self.get_credentials()
resp = self.api_client.get(self.detail_url, format='json')
self.assertValidJSONResponse(resp)
self.assertEqual(json.loads(resp.content)['text'], '<h1>hola</h1>')
# def test_post_list_unauthenticated(self):
# self.assertHttpUnauthorized(self.api_client.post(self.list_url, format='json',
# data=self.document_data))
def test_put_detail_unauthenticated(self):
self.assertHttpUnauthorized(self.api_client.put(self.detail_url, format='json', data={'text': 'data'}))
def test_put_detail(self):
self.get_credentials()
original_data = json.loads(self.api_client.get(self.detail_url, format='json').content)
new_data = original_data.copy()
new_data['title'] = 'Updated: First Document'
self.assertEqual(Document.objects.count(), 2)
self.assertHttpAccepted(self.api_client.put(self.detail_url, format='json', data=new_data))
# Make sure the count hasn't changed & we did an update.
self.assertEqual(Document.objects.count(), 2)
def test_delete_detail_unauthenticated(self):
self.assertHttpUnauthorized(self.api_client.delete(self.detail_url, format='json'))
def test_delete_detail(self):
self.get_credentials()
self.assertEqual(Document.objects.count(), 2)
resp = self.api_client.delete(self.detail_url, format='json')
self.assertHttpAccepted(resp)
self.assertEqual(Document.objects.count(), 1)
示例10: AuthResourceTest
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import delete [as 别名]
class AuthResourceTest(ResourceTestCase):
fixtures = ['test_auth.json']
def setUp(self):
super(AuthResourceTest, self).setUp()
self.client = TestApiClient()
self.endpoint = '/api/v1/auth/'
self.format = 'json'
# Get user 1 and token 1 from fixture
self.user = User.objects.all()[0]
self.token = self.user.usertoken_set.all()[0]
self.password = '123'
self.detail = '/api/v1/auth/{0}/'.format(self.token.id)
# Get user 2 and token 2 from fixture
self.user2 = User.objects.all()[1]
self.token2 = self.user2.usertoken_set.all()[0]
self.detail2 = '/api/v1/auth/{0}/'.format(self.token2.id)
def get_credentials(self):
return self.create_basic(username=self.user.username,
password=self.password)
# Try to get tokens without credentials
def test_get_keys_unauthorzied(self):
self.assertHttpUnauthorized(self.client.get(self.endpoint,
self.format))
# Try to create a token (POST)
def test_create_token_json(self):
post_data = {}
credentials = self.get_credentials()
self.assertHttpCreated(self.client.post(self.endpoint,
self.format,
data=post_data,
authentication=credentials))
# Try to get all tokens (GET)
def test_get_keys_json(self):
credentials = self.get_credentials()
resp = self.client.get(self.endpoint,
self.format,
authentication=credentials)
self.assertValidJSONResponse(resp)
# Try to delete a token (DELETE)
def test_delete_token_json(self):
credentials = self.get_credentials()
self.assertEqual(UserToken.objects.count(), 2)
resp = self.client.delete(self.detail,
format=self.format,
authentication=credentials)
self.assertHttpAccepted(resp)
self.assertEqual(UserToken.objects.count(), 1)
# User 1 try to delete user 2 token
def test_delete_another_token(self):
credentials = self.get_credentials()
self.assertHttpNotFound(self.client.delete(self.detail2,
format=self.format,
authentication=credentials))
# Try to create a token (POST) with wrong credentials
def test_create_token_with_wrong_credentials_json(self):
post_data = {}
credentials = self.create_basic(username=self.user.username,
password="bla")
self.assertHttpUnauthorized(self.client.post(self.endpoint,
self.format,
data=post_data,
authentication=credentials))
示例11: ResourceTest
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import delete [as 别名]
class ResourceTest(ResourceTestCase):
serializer= Serializer()
def setUp(self):
self.api_client = TestApiClient()
self.username = 'creator'
self.password = 'mybadpassword'
self.user = hydroshare.create_account(
'[email protected]',
username=self.username,
password=self.password,
first_name='User0_FirstName',
last_name='User0_LastName',
)
self.api_client.client.login(username=self.username, password=self.password)
self.url_proto = '/hsapi/resource/{0}/files/{1}/'
self.filename = 'test.txt'
def tearDown(self):
User.objects.all().delete()
GenericResource.objects.all().delete()
def test_resource_file_get(self):
myfile = open(self.filename, 'w')
myfile.write('hello world!\n')
myfile.close()
myfile = open(self.filename, 'r')
res1 = hydroshare.create_resource('GenericResource', self.user, 'res1')
hydroshare.add_resource_files(res1.short_id, myfile)
url = self.url_proto.format(res1.short_id, self.filename)
resp = self.api_client.get(url)
self.assertIn(resp.status_code, [201, 200])
def test_resource_file_put(self):
myfile = open(self.filename, 'w')
myfile.write('hello world!\n')
myfile.close()
myfile = open(self.filename, 'r')
res1 = hydroshare.create_resource('GenericResource', self.user, 'res1')
hydroshare.add_resource_files(res1.short_id, myfile)
mynewfile = open(self.filename, 'w')
mynewfile.write('anyone there?\n')
mynewfile.close()
mynewfile = open(self.filename, 'r')
url = self.url_proto.format(res1.short_id, self.filename)
put_data = { 'resource_file': mynewfile
}
resp = self.api_client.put(url, data=put_data)
self.assertHttpAccepted(resp)
resp = self.api_client.get(url)
self.assertIn(resp.status_code, [201, 200])
def test_resource_file_post(self):
myfile = open(self.filename, 'w')
myfile.write('hello world!\n')
myfile.close()
myfile = open(self.filename, 'r')
res1 = hydroshare.create_resource('GenericResource', self.user, 'res1')
post_data = { 'resource_file': myfile
}
url = self.url_proto.format(res1.short_id, self.filename)
resp = self.api_client.post(url, data=post_data)
self.assertHttpAccepted(resp)
resp = self.api_client.get(url)
self.assertIn(resp.status_code, [201, 200])
def test_resource_file_delete(self):
myfile = open(self.filename, 'w')
myfile.write('hello world!\n')
myfile.close()
myfile = open(self.filename, 'r')
#.........这里部分代码省略.........
示例12: CommentResourceTest
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import delete [as 别名]
class CommentResourceTest(ResourceTestCase):
fixtures = ['group.json', 'group_permission.json',
'user_group.json', 'user.json',
'user_project.json', 'project_part.json',
'project.json', 'profile.json',
'post.json', 'comment.json']
def setUp(self):
self.api_client = TestApiClient()
self.post_id = 1
self.post_data = {'post': {'pk': self.post_id,
'text': 'New comment'}}
self.detail_url = '/api/v1/comment/{0}/'.format(self.post_id)
self.list_url = '/api/v1/comment/'
self.serializer = Serializer()
def get_credentials(self):
result = self.api_client.client.login(email='[email protected]',
password='django')
# def test_post_list(self):
# self.get_credentials()
# self.assertIn('_auth_user_id', self.api_client.client.session)
# self.assertEqual(self.api_client.client.session['_auth_user_id'], 1)
# self.assertEqual(Comment.objects.count(), 1)
# self.assertHttpCreated(self.api_client.post(self.list_url,
# format='json',
# data=self.post_data))
# self.assertEqual(Comment.objects.count(), 2)
def test_get_list_unauthorized(self):
self.assertHttpUnauthorized(self.api_client.get(self.list_url,
format='json'))
def test_get_list(self):
self.get_credentials()
resp = self.api_client.get(self.list_url,
format='json')
self.assertValidJSONResponse(resp)
self.assertEqual(len(json.loads(resp.content)['objects']), 1)
def test_get_detail_unauthenticated(self):
self.assertHttpUnauthorized(self.api_client.get(self.detail_url, format='json'))
# def test_get_detail(self):
# self.get_credentials()
# resp = self.api_client.get(self.detail_url, format='json')
# self.assertValidJSONResponse(resp)
# self.assertEqual(json.loads(resp.content)['comment'], '')
def test_post_list_unauthenticated(self):
self.assertHttpUnauthorized(self.api_client.post(self.list_url, format='json',
data=self.post_data))
def test_put_detail_unauthenticated(self):
self.assertHttpUnauthorized(self.api_client.put(self.detail_url, format='json', data={}))
def test_put_detail(self):
self.get_credentials()
resp = self.api_client.get(self.detail_url, format='json')
original_data = json.loads(resp.content)
new_data = original_data.copy()
new_data['title'] = 'Updated: First Comment'
self.assertEqual(Comment.objects.count(), 1)
self.assertHttpAccepted(self.api_client.put(self.detail_url, format='json', data=new_data))
self.assertEqual(Comment.objects.count(), 1)
def test_delete_detail_unauthenticated(self):
self.assertHttpUnauthorized(self.api_client.delete(self.detail_url, format='json'))
def test_delete_detail(self):
self.get_credentials()
self.assertEqual(Comment.objects.count(), 1)
resp = self.api_client.delete(self.detail_url, format='json')
self.assertHttpAccepted(resp)
self.assertEqual(Comment.objects.count(), 0)
示例13: TestCameraResource
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import delete [as 别名]
class TestCameraResource(ResourceTestCase):
def setUp(self):
# Tastypie stuff
super(TestCameraResource, self).setUp()
self.bob_api_client = TestApiClient()
self.bill_api_client = TestApiClient()
self.anon_api_client = TestApiClient()
# Create a user bob.
self.user_bob_username = "bob"
self.user_bob_password = "bob"
self.user_bob = User.objects.create_user(self.user_bob_username, "[email protected]", self.user_bob_password)
# Create a user bob.
self.user_bill_username = "bill"
self.user_bill_password = "bill"
self.user_bill = User.objects.create_user(
self.user_bill_username, "[email protected]", self.user_bill_password
)
self.bob_api_client.client.login(username="bob", password="bob")
self.bill_api_client.client.login(username="bill", password="bill")
# assign users to the Public group
public_group, created = Group.objects.get_or_create(name="Public")
self.user_bob.groups.add(public_group)
self.user_bill.groups.add(public_group)
guardian.utils.get_anonymous_user().groups.add(public_group)
# make a couple of campaigns and save
self.campaign_bobs = mommy.make_one("catamidb.Campaign", id=1)
self.campaign_bills = mommy.make_one("catamidb.Campaign", id=2)
# make a deployments
self.deployment_bobs = mommy.make_recipe("catamidb.Deployment1", id=1, campaign=self.campaign_bobs)
self.deployment_bills = mommy.make_recipe("catamidb.Deployment2", id=2, campaign=self.campaign_bills)
# make images
self.image_bobs = mommy.make_recipe("catamidb.Image1", id=1, deployment=self.deployment_bobs)
self.image_bills = mommy.make_recipe("catamidb.Image2", id=2, deployment=self.deployment_bills)
# make cameras
self.camera_bobs = mommy.make_one("catamidb.Camera", id=1, image=self.image_bobs)
self.camera_bills = mommy.make_one("catamidb.Camera", id=2, image=self.image_bills)
# assign this one to bob
authorization.apply_campaign_permissions(self.user_bob, self.campaign_bobs)
# assign this one to bill
authorization.apply_campaign_permissions(self.user_bill, self.campaign_bills)
# the API url for deployments
self.camera_url = "/api/dev/camera/"
# some post data for testing camera creation
self.post_data = []
def test_camera_operations_disabled(self):
# test that we can NOT create
self.assertHttpUnauthorized(self.anon_api_client.post(self.camera_url, format="json", data=self.post_data))
# test that we can NOT modify
self.assertHttpUnauthorized(
self.anon_api_client.put(self.camera_url + self.deployment_bobs.id.__str__() + "/", format="json", data={})
)
# test that we can NOT delete
self.assertHttpMethodNotAllowed(
self.anon_api_client.delete(self.camera_url + self.deployment_bobs.id.__str__() + "/", format="json")
)
# test that we can NOT modify authenticated
self.assertHttpUnauthorized(
self.bob_api_client.put(self.camera_url + self.deployment_bobs.id.__str__() + "/", format="json", data={})
)
# test that we can NOT delete authenticated
self.assertHttpMethodNotAllowed(
self.bob_api_client.delete(self.camera_url + self.deployment_bobs.id.__str__() + "/", format="json")
)
def test_camera_operations_as_authorised_users(self):
# create a campaign & deployment that ONLY bill can see
bills_campaign = mommy.make_one("catamidb.Campaign", id=3, short_name="cp_3")
bills_deployment = mommy.make_recipe("catamidb.Deployment3", id=3, campaign=bills_campaign)
assign("view_campaign", self.user_bill, bills_campaign)
# make exclusive image for bill and assign this image to camera to image
self.image_bill_exc = mommy.make_recipe(
"catamidb.Image3", id=3, deployment=bills_deployment
) # IMPORTANT camera checks campaign which has reference to deployment. Image has reference to deployment and camera
# make exclusive camera for bill
self.camera_bill_exc = mommy.make_one("catamidb.Camera", id=3, image=self.image_bill_exc)
# check that bill can see via the API
response = self.bill_api_client.get(self.camera_url, format="json")
#.........这里部分代码省略.........
示例14: PostResourceTest
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import delete [as 别名]
class PostResourceTest(ResourceTestCase):
fixtures = ['group.json', 'group_permission.json',
'user_group.json', 'user.json',
'user_project.json', 'project_part.json',
'project.json', 'profile.json',
'post.json', 'comment.json']
def setUp(self):
# Generic
self.api_client = TestApiClient()
self.serializer = Serializer()
# API List data
self.list_url = '/api/v1/post/'
# API requests data
self.project_part_id = 1
self.post_id = 1
self.detail_url = '/api/v1/post/{0}/'.format(self.post_id)
self.project_part_query = '='.join(['?project_part', str(self.project_part_id)])
self.post_data = {'content': 'My post', 'project_part': {'pk': self.project_part_id}}
# Open API request data
self.open_project_part_id = 2
self.open_post_id = 2
self.open_detail_url = '/api/v1/post/{0}/'.format(self.open_post_id)
self.open_project_part_query = '='.join(['?project_part', str(self.open_project_part_id)])
self.open_post_data = {'content': 'My post', 'project_part': {'pk': self.open_project_part_id}}
def get_credentials(self):
result = self.api_client.client.login(email='[email protected]',
password='django')
def test_post_list(self):
self.get_credentials()
self.assertIn('_auth_user_id', self.api_client.client.session)
self.assertEqual(self.api_client.client.session['_auth_user_id'], 1)
self.assertEqual(Post.objects.count(), 2)
self.assertHttpCreated(self.api_client.post(self.list_url,
format='json',
data=self.post_data))
self.assertEqual(Post.objects.count(), 3)
def test_get_list_unauthorzied(self):
self.assertHttpUnauthorized(self.api_client.get(self.list_url,
format='json'))
def test_get_list(self):
self.get_credentials()
resp = self.api_client.get(''.join([self.list_url, self.project_part_query]),
format='json')
self.assertValidJSONResponse(resp)
self.assertEqual(len(json.loads(resp.content)['objects']), 1)
def test_get_detail_unauthenticated(self):
self.assertHttpUnauthorized(self.api_client.get(self.detail_url, format='json'))
def test_get_detail(self):
self.get_credentials()
resp = self.api_client.get(self.detail_url, format='json')
self.assertValidJSONResponse(resp)
self.assertEqual(json.loads(resp.content)['content'], 'hola')
# def test_post_list_unauthenticated(self):
# self.assertHttpUnauthorized(self.api_client.post(self.list_url, format='json',
# data=self.post_data))
def test_put_detail_unauthenticated(self):
self.assertHttpUnauthorized(self.api_client.put(self.detail_url, format='json', data={}))
def test_put_detail(self):
self.get_credentials()
original_data = json.loads(self.api_client.get(self.detail_url, format='json').content)
new_data = original_data.copy()
new_data['title'] = 'Updated: First Post'
self.assertEqual(Post.objects.count(), 2)
self.assertHttpAccepted(self.api_client.put(self.detail_url, format='json', data=new_data))
# Make sure the count hasn't changed & we did an update.
self.assertEqual(Post.objects.count(), 2)
def test_delete_detail_unauthenticated(self):
self.assertHttpUnauthorized(self.api_client.delete(self.detail_url, format='json'))
def test_delete_detail(self):
self.get_credentials()
self.assertEqual(Post.objects.count(), 2)
resp = self.api_client.delete(self.detail_url, format='json')
self.assertHttpAccepted(resp)
self.assertEqual(Post.objects.count(), 1)
# Open Projects
# FIXME It fails because tastypie is not accessing authorization
# before calling obj_create
# def test_post_list_open(self):
# self.assertEqual(Post.objects.count(), 2)
# resp = self.api_client.post(self.list_url,
# format='json',
# data=self.open_post_data)
#.........这里部分代码省略.........
示例15: TestCampaignResource
# 需要导入模块: from tastypie.test import TestApiClient [as 别名]
# 或者: from tastypie.test.TestApiClient import delete [as 别名]
class TestCampaignResource(ResourceTestCase):
def setUp(self):
# Tastypie stuff
super(TestCampaignResource, self).setUp()
self.bob_api_client = TestApiClient()
self.bill_api_client = TestApiClient()
self.anon_api_client = TestApiClient()
# Create a user bob.
self.user_bob_username = "bob"
self.user_bob_password = "bob"
self.user_bob = User.objects.create_user(self.user_bob_username, "[email protected]", self.user_bob_password)
# Create a user bob.
self.user_bill_username = "bill"
self.user_bill_password = "bill"
self.user_bill = User.objects.create_user(
self.user_bill_username, "[email protected]", self.user_bill_password
)
self.bob_api_client.client.login(username="bob", password="bob")
self.bill_api_client.client.login(username="bill", password="bill")
# assign users to the Public group
public_group, created = Group.objects.get_or_create(name="Public")
self.user_bob.groups.add(public_group)
self.user_bill.groups.add(public_group)
guardian.utils.get_anonymous_user().groups.add(public_group)
# make a couple of campaigns and save
self.campaign_bobs = mommy.make_one("catamidb.Campaign")
self.campaign_bills = mommy.make_one("catamidb.Campaign")
# assign this one to bob
authorization.apply_campaign_permissions(self.user_bob, self.campaign_bobs)
# assign this one to bill
authorization.apply_campaign_permissions(self.user_bill, self.campaign_bills)
# the API url for campaigns
self.campaign_url = "/api/dev/campaign/"
# some post data for testing campaign creation
self.post_data = {
"short_name": "Blah",
"description": "Blah",
"associated_researchers": "Blah",
"associated_publications": "Blah",
"associated_research_grant": "Blah",
"date_start": "2012-05-01",
"date_end": "2012-05-01",
"contact_person": "Blah",
}
# can only do GET at this stage
def test_campaign_operations_disabled(self):
# test that we are unauthorized to create
self.assertHttpUnauthorized(self.anon_api_client.post(self.campaign_url, format="json", data=self.post_data))
# test that we can NOT modify
self.assertHttpMethodNotAllowed(
self.anon_api_client.put(self.campaign_url + self.campaign_bobs.id.__str__() + "/", format="json", data={})
)
# test that we can NOT delete
self.assertHttpMethodNotAllowed(
self.anon_api_client.delete(self.campaign_url + self.campaign_bobs.id.__str__() + "/", format="json")
)
# test that we can NOT modify authenticated
self.assertHttpMethodNotAllowed(
self.bob_api_client.put(self.campaign_url + self.campaign_bobs.id.__str__() + "/", format="json", data={})
)
# test that we can NOT delete authenticated
self.assertHttpMethodNotAllowed(
self.bob_api_client.delete(self.campaign_url + self.campaign_bobs.id.__str__() + "/", format="json")
)
# test can get a list of campaigns authorised
def test_campaigns_operations_as_authorised_users(self):
# create a campaign that only bill can see
bills_campaign = mommy.make_one("catamidb.Campaign")
assign("view_campaign", self.user_bill, bills_campaign)
# check that bill can see via the API
response = self.bill_api_client.get(self.campaign_url, format="json")
self.assertValidJSONResponse(response)
self.assertEqual(len(self.deserialize(response)["objects"]), 3)
# check that bill can get to the object itself
response = self.bill_api_client.get(self.campaign_url + bills_campaign.id.__str__() + "/", format="json")
self.assertValidJSONResponse(response)
# check that bob can not see - now we know tastypie API has correct
# permission validation
response = self.bob_api_client.get(self.campaign_url, format="json")
self.assertValidJSONResponse(response)
#.........这里部分代码省略.........