def test_job_download_failure(self): user = self.guest_login() entity = Entity.objects.create(name="entity", created_user=user) job = Job.new_create(user, entity, "hoge") # When user send a download request of Job with invalid Job-id, then HTTP 400 is returned resp = self.client.get("/job/download/%d" % (job.id + 1)) self.assertEqual(resp.status_code, 400) self.assertEqual(resp.content.decode(), "Invalid Job-ID is specified") # When user send a download request of non export Job, then HTTP 400 is returned resp = self.client.get("/job/download/%d" % job.id) self.assertEqual(resp.status_code, 400) self.assertEqual(resp.content.decode(), "Target Job has no value to return") # The case user sends a download request for a job which doesn't have a result job = Job.new_export(user, text="fuga") resp = self.client.get("/job/download/%d" % job.id) self.assertEqual(resp.status_code, 400) self.assertEqual(resp.content.decode(), "This result is no longer available") # When user send a download request of export Job by differenct user from creating one, # then HTTP 400 is returned job = Job.new_export(user, text="fuga") user = self.admin_login() resp = self.client.get("/job/download/%d" % job.id) self.assertEqual(resp.status_code, 400) self.assertEqual(resp.content.decode(), "Target Job is executed by other people")
def test_get_exporting_job(self): user = self.guest_login() # create jobs which are related with export Job.new_export(user), Job.new_export_search_result(user), resp = self.client.get("/job/") self.assertEqual(resp.status_code, 200) self.assertEqual(len(resp.context["jobs"]), 2)
def test_get_exporting_job(self): user = self.guest_login() # create jobs which are related with export Job.new_export(user), Job.new_export_search_result(user), resp = self.client.get('/job/api/v2/jobs') self.assertEqual(resp.status_code, 200) self.assertEqual(len(resp.json()), 2)
def test_get_object(self): Entry.objects.create(name='entry', created_user=self.guest, schema=self.entity) params = { 'entities': self.entity.id, 'attrinfo': { 'name': 'foo', 'keyword': '' }, 'export_style': '"yaml"', } # check there is no job self.assertFalse(Job.get_job_with_params(self.guest, params).exists()) # create a new job job = Job.new_export(self.guest, text='hoge', params=params) self.assertEqual(job.target_type, Job.TARGET_UNKNOWN) self.assertEqual(job.operation, Job.OP_EXPORT) self.assertEqual(job.text, 'hoge') # check created job is got by specified params self.assertEqual( Job.get_job_with_params(self.guest, params).count(), 1) self.assertEqual( Job.get_job_with_params(self.guest, params).last(), job) # check the case when different params is specified then it returns None params['attrinfo']['name'] = '' self.assertFalse(Job.get_job_with_params(self.guest, params).exists())
def test_duplicate_export(self): user = self.admin entity = Entity.objects.create(name='Entity', created_user=user) export_params = { 'entities': [entity.id], 'attrinfo': [{'name': 'attr', 'keyword': 'data-5'}], 'export_style': 'csv', } # create a job to export search result job = Job.new_export(user, params=export_params) # A request with same parameter which is under execution will be denied resp = self.client.post(reverse('dashboard:export_search_result'), json.dumps(export_params, sort_keys=True), 'application/json') self.assertEqual(resp.status_code, 400) # A request with another condition will be accepted new_export_params = dict(export_params, **{'export_style': 'yaml'}) resp = self.client.post(reverse('dashboard:export_search_result'), json.dumps(new_export_params, sort_keys=True), 'application/json') self.assertEqual(resp.status_code, 200) # When the job is finished, the processing is passed. job.status = Job.STATUS['DONE'] job.save(update_fields=['status']) resp = self.client.post(reverse('dashboard:export_search_result'), json.dumps(export_params, sort_keys=True), 'application/json') self.assertEqual(resp.status_code, 200)
def test_get_object(self): params = { "entities": self.entity.id, "attrinfo": { "name": "foo", "keyword": "" }, "export_style": '"yaml"', } # check there is no job self.assertFalse(Job.get_job_with_params(self.guest, params).exists()) # create a new job job = Job.new_export(self.guest, text="hoge", params=params) self.assertEqual(job.target_type, Job.TARGET_UNKNOWN) self.assertEqual(job.operation, JobOperation.EXPORT_ENTRY.value) self.assertEqual(job.text, "hoge") # check created job is got by specified params self.assertEqual( Job.get_job_with_params(self.guest, params).count(), 1) self.assertEqual( Job.get_job_with_params(self.guest, params).last(), job) # check the case when different params is specified then it returns None params["attrinfo"]["name"] = "" self.assertFalse(Job.get_job_with_params(self.guest, params).exists())
def export_search_result(request, recv_data): # additional validation if recv_data['export_style'] != 'yaml' and recv_data[ 'export_style'] != 'csv': return HttpResponse('Invalid "export_type" is specified', status=400) user = User.objects.get(id=request.user.id) # check whether same job is sent job_status_not_finished = [ Job.STATUS['PREPARING'], Job.STATUS['PROCESSING'] ] if Job.get_job_with_params( user, recv_data).filter(status__in=job_status_not_finished).exists(): return HttpResponse('Same export processing is under execution', status=400) # create a job to export search result job = Job.new_export( user, **{ 'text': 'search_results.%s' % recv_data['export_style'], 'params': recv_data, }) # register task to make export data and cache it task_export_search_result.delay(job.id) return JsonResponse({ 'result': 'Succeed in registering export processing. ' + 'Please check Job list.' })
def test_job_download_exported_result(self): user = self.guest_login() # initialize an export Job job = Job.new_export(user, text='hoge') job.set_cache('abcd') # check job contents could be downloaded resp = self.client.get('/job/download/%d' % job.id) self.assertEqual(resp.status_code, 200) self.assertEqual(resp['Content-Disposition'], 'attachment; filename="hoge"') self.assertEqual(resp.content.decode('utf8'), 'abcd')
def post(self, request, entity_id: int): if not Entity.objects.filter(id=entity_id).exists(): return Response("Failed to get entity of specified id", status=status.HTTP_400_BAD_REQUEST) serializer = EntryExportSerializer(data=request.data) if not serializer.is_valid(): return Response("Parameters in post body is invalid", status=status.HTTP_400_BAD_REQUEST) job_params = { "export_format": serializer.validated_data["format"], "target_id": entity_id, } # check whether same job is sent job_status_not_finished = [ Job.STATUS["PREPARING"], Job.STATUS["PROCESSING"] ] if (Job.get_job_with_params(request.user, job_params).filter( status__in=job_status_not_finished).exists()): return Response("Same export processing is under execution", status=status.HTTP_400_BAD_REQUEST) entity = Entity.objects.get(id=entity_id) if not request.user.has_permission(entity, ACLType.Readable): return Response('Permission denied to _value "%s"' % entity.name, status=status.HTTP_400_BAD_REQUEST) # create a job to export search result and run it job = Job.new_export( request.user, **{ "text": "entry_%s.%s" % (entity.name, job_params["export_format"]), "target": entity, "params": job_params, }, ) job.run() return Response( { "result": "Succeed in registering export processing. " + "Please check Job list." }, status=status.HTTP_200_OK, )
def test_dependent_job(self): (job1, job2) = [Job.new_edit(self.guest, self.entry) for x in range(2)] self.assertIsNone(job1.dependent_job) self.assertEqual(job2.dependent_job, job1) # When a job don't has target parameter, dependent_job is not set because # there is no problem when these tasks are run simultaneouslly. jobs = [Job.new_export(self.guest) for x in range(2)] self.assertTrue(all([j.dependent_job is None for j in jobs])) # overwrite timeout timeout value for testing settings.AIRONE['JOB_TIMEOUT'] = -1 # Because jobs[1] is created after the expiry of jobs[0] jobs = [Job.new_edit(self.guest, self.entry) for x in range(2)] self.assertTrue(all([j.dependent_job is None for j in jobs]))
def export(request, entity_id): user = User.objects.get(id=request.user.id) job_params = { 'export_format': 'yaml', 'target_id': entity_id, } if not Entity.objects.filter(id=entity_id).exists(): return HttpResponse('Failed to get entity of specified id', status=400) if 'format' in request.GET and request.GET.get('format') == 'CSV': job_params['export_format'] = 'csv' # check whether same job is sent job_status_not_finished = [ Job.STATUS['PREPARING'], Job.STATUS['PROCESSING'] ] if Job.get_job_with_params( user, job_params).filter(status__in=job_status_not_finished).exists(): return HttpResponse('Same export processing is under execution', status=400) entity = Entity.objects.get(id=entity_id) if not user.has_permission(entity, ACLType.Readable): return HttpResponse('Permission denied to export "%s"' % entity.name, status=400) # create a job to export search result job = Job.new_export( user, **{ 'text': 'entry_%s.%s' % (entity.name, job_params['export_format']), 'target': entity, 'params': job_params, }) # create a job to export search result export_entries.delay(job.id) return JsonResponse({ 'result': 'Succeed in registering export processing. ' + 'Please check Job list.' })
def export(request, entity_id, recv_data): job_params = { "export_format": "yaml", "target_id": entity_id, } if not Entity.objects.filter(id=entity_id).exists(): return HttpResponse("Failed to get entity of specified id", status=400) if "format" in recv_data and recv_data["format"] == "CSV": job_params["export_format"] = "csv" # check whether same job is sent job_status_not_finished = [ Job.STATUS["PREPARING"], Job.STATUS["PROCESSING"] ] if (Job.get_job_with_params( request.user, job_params).filter(status__in=job_status_not_finished).exists()): return HttpResponse("Same export processing is under execution", status=400) entity = Entity.objects.get(id=entity_id) if not request.user.has_permission(entity, ACLType.Readable): return HttpResponse('Permission denied to export "%s"' % entity.name, status=400) # create a job to export search result and run it job = Job.new_export( request.user, **{ "text": "entry_%s.%s" % (entity.name, job_params["export_format"]), "target": entity, "params": job_params, }) job.run() return JsonResponse({ "result": "Succeed in registering export processing. " + "Please check Job list." })