Example #1
0
    def test_get_jobs_deleted_target(self):
        user = self.guest_login()

        entity = Entity.objects.create(name='entity', created_user=user)
        entry = Entry.objects.create(name='entry',
                                     created_user=user,
                                     schema=entity)
        Job.new_create(user, entry)

        resp = self.client.get('/job/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(len(resp.context['jobs']), 1)

        # check the case show jobs after deleting job target
        entry.delete()

        # Create delete job
        Job.new_delete(user, entry)

        resp = self.client.get('/job/')
        self.assertEqual(resp.status_code, 200)

        # Confirm that the delete job can be obtained
        self.assertEqual(len(resp.context['jobs']), 1)
        self.assertEqual(resp.context['jobs'][0]['operation'],
                         JobOperation.DELETE_ENTRY.value)
Example #2
0
    def test_get_jobs_deleted_target(self):
        user = self.guest_login()

        entity = Entity.objects.create(name='entity', created_user=user)
        entry = Entry.objects.create(name='entry', created_user=user, schema=entity)
        Job.new_create(user, entry)

        resp = self.client.get('/job/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(len(resp.context['jobs']), 1)

        # check the case show jobs after deleting job target
        entry.delete()

        # Create delete job
        Job.new_delete(user, entry)

        resp = self.client.get('/job/')
        self.assertEqual(resp.status_code, 200)

        # Confirm that the delete job can be obtained
        self.assertEqual(len(resp.context['jobs']), 1)
        self.assertEqual(resp.context['jobs'][0]['operation'], JobOperation.DELETE_ENTRY.value)

        # check respond HTML has expected elements which are specified of CSS selectors
        parser = HTML(html=resp.content.decode('utf-8'))
        job_elems = parser.find('#entry_container .job_info')
        self.assertEqual(len(job_elems), 1)
        for job_elem in job_elems:
            for _cls in ['target', 'status', 'execution_time', 'created_at', 'note', 'operation']:
                self.assertIsNotNone(job_elem.find('.%s' % _cls))
Example #3
0
    def delete(self, request, *args, **kwargs):
        # checks mandatory parameters are specified
        if not all([x in request.data for x in ['entity', 'entry']]):
            return Response('Parameter "entity" and "entry" are mandatory',
                            status=status.HTTP_400_BAD_REQUEST)

        entity = Entity.objects.filter(name=request.data['entity']).first()
        if not entity:
            return Response('Failed to find specified Entity (%s)' % request.data['entity'],
                            status=status.HTTP_404_NOT_FOUND)

        entry = Entry.objects.filter(name=request.data['entry'], schema=entity).first()
        if not entry:
            return Response('Failed to find specified Entry (%s)' % request.data['entry'],
                            status=status.HTTP_404_NOT_FOUND)

        # permission check
        user = User.objects.get(id=request.user.id)
        if (not user.has_permission(entry, ACLType.Full) or
                not user.has_permission(entity, ACLType.Readable)):
            return Response('Permission denied to operate', status=status.HTTP_400_BAD_REQUEST)

        # Delete the specified entry then return its id, if is active
        if entry.is_active:
            # create a new Job to delete entry and run it
            job = Job.new_delete(user, entry)
            job.run()

        return Response({'id': entry.id})
Example #4
0
    def test_cancel_job(self):
        user = self.guest_login()

        entity = Entity.objects.create(name='entity', created_user=user)
        entry = Entry.objects.create(name='entry', schema=entity, created_user=user)

        # make a job
        job = Job.new_delete(user, entry)
        self.assertEqual(job.status, Job.STATUS['PREPARING'])

        # send request without any parameters
        resp = self.client.delete('/api/v1/job/', json.dumps({}), 'application/json')
        self.assertEqual(resp.status_code, 400)
        self.assertEqual(resp.content, b'"Parameter job_id is required"')

        # send request with invalid job id
        resp = self.client.delete('/api/v1/job/', json.dumps({'job_id': 99999}), 'application/json')
        self.assertEqual(resp.status_code, 400)
        self.assertEqual(resp.content, b'"Failed to find Job(id=99999)"')

        # send request with proper parameter
        resp = self.client.delete('/api/v1/job/', json.dumps({'job_id': job.id}),
                                  'application/json')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content, b'"Success to cancel job"')

        job.refresh_from_db()
        self.assertEqual(job.status, Job.STATUS['CANCELED'])
Example #5
0
    def test_get_search_job(self):
        user = self.guest_login()

        entity = Entity.objects.create(name="entity", created_user=user)
        entry = Entry.objects.create(name="entry", schema=entity, created_user=user)

        # make a job
        job = Job.new_delete(user, entry)

        # send request without any GET parameters
        resp = self.client.get("/api/v1/job/search")
        self.assertEqual(resp.status_code, 400)

        # send request with a GET parameter that doesn't match any job
        resp = self.client.get("/api/v1/job/search", {"operation": JobOperation.COPY_ENTRY.value})
        self.assertEqual(resp.status_code, 404)

        # send requests with GET parameter that matches the created job
        for param in [
            {"operation": JobOperation.DELETE_ENTRY.value},
            {"target_id": entry.id},
        ]:
            resp = self.client.get("/api/v1/job/search", param)
            self.assertEqual(resp.status_code, 200)
            self.assertEqual(len(resp.json()), 1)
            self.assertEqual(resp.json()["result"][0]["id"], job.id)
            self.assertEqual(resp.json()["result"][0]["target"]["id"], entry.id)
Example #6
0
def do_delete(request, entry_id, recv_data):
    user = User.objects.get(id=request.user.id)
    ret = {}

    if not Entry.objects.filter(id=entry_id).exists():
        return HttpResponse('Failed to get an Entry object of specified id', status=400)

    # update name of Entry object
    entry = Entry.objects.filter(id=entry_id).get()

    if custom_view.is_custom("do_delete_entry", entry.schema.name):
        # do_delete custom view
        resp = custom_view.call_custom("do_delete_entry", entry.schema.name, request, user, entry)

        # If custom_view returns available response this returns it to user,
        # or continues default processing.
        if resp:
            return resp

    # set deleted flag in advance because deleting processing taks long time
    entry.is_active = False
    entry.save(update_fields=['is_active'])

    # save deleting Entry name before do it
    ret['name'] = entry.name

    # register operation History for deleting entry
    user.seth_entry_del(entry)

    # Create a new job to delete entry and run it
    job = Job.new_delete(user, entry)
    job.run()

    return JsonResponse(ret)
Example #7
0
    def test_get_search_job(self):
        user = self.guest_login()

        entity = Entity.objects.create(name='entity', created_user=user)
        entry = Entry.objects.create(name='entry',
                                     schema=entity,
                                     created_user=user)

        # make a job
        job = Job.new_delete(user, entry)

        # send request without any GET parameters
        resp = self.client.get('/api/v1/job/search')
        self.assertEqual(resp.status_code, 400)

        # send request with a GET parameter that doesn't match any job
        resp = self.client.get('/api/v1/job/search',
                               {'operation': JobOperation.COPY_ENTRY.value})
        self.assertEqual(resp.status_code, 404)

        # send requests with GET parameter that matches the created job
        for param in [{
                'operation': JobOperation.DELETE_ENTRY.value
        }, {
                'target_id': entry.id
        }]:
            resp = self.client.get('/api/v1/job/search', param)
            self.assertEqual(resp.status_code, 200)
            self.assertEqual(len(resp.json()), 1)
            self.assertEqual(resp.json()['result'][0]['id'], job.id)
            self.assertEqual(resp.json()['result'][0]['target']['id'],
                             entry.id)
Example #8
0
    def test_get_jobs_deleted_target(self):
        user = self.guest_login()

        entity = Entity.objects.create(name="entity", created_user=user)
        entry = Entry.objects.create(name="entry",
                                     created_user=user,
                                     schema=entity)
        Job.new_create(user, entry)

        resp = self.client.get("/job/")
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(len(resp.context["jobs"]), 1)

        # check the case show jobs after deleting job target
        entry.delete()

        # Create delete job
        Job.new_delete(user, entry)

        resp = self.client.get("/job/")
        self.assertEqual(resp.status_code, 200)

        # Confirm that the delete job can be obtained
        self.assertEqual(len(resp.context["jobs"]), 1)
        self.assertEqual(resp.context["jobs"][0]["operation"],
                         JobOperation.DELETE_ENTRY.value)

        # check respond HTML has expected elements which are specified of CSS selectors
        parser = HTML(html=resp.content.decode("utf-8"))
        job_elems = parser.find("#entry_container .job_info")
        self.assertEqual(len(job_elems), 1)
        for job_elem in job_elems:
            for _cls in [
                    "target",
                    "status",
                    "execution_time",
                    "created_at",
                    "note",
                    "operation",
            ]:
                self.assertIsNotNone(job_elem.find(".%s" % _cls))
Example #9
0
    def test_get_jobs_deleted_target(self):
        user = self.guest_login()

        entity = Entity.objects.create(name="entity", created_user=user)
        entry = Entry.objects.create(name="entry", created_user=user, schema=entity)
        Job.new_create(user, entry)

        resp = self.client.get("/job/api/v2/jobs")
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(len(resp.json()), 1)

        # check the case show jobs after deleting job target
        entry.delete()

        # Create delete job
        Job.new_delete(user, entry)

        resp = self.client.get("/job/api/v2/jobs")
        self.assertEqual(resp.status_code, 200)

        # Confirm that the delete job can be obtained
        body = resp.json()
        self.assertEqual(len(body), 1)
        self.assertEqual(body[0]["operation"], JobOperation.DELETE_ENTRY.value)
Example #10
0
    def delete(self, request, *args, **kwargs):
        # checks mandatory parameters are specified
        if not all([x in request.data for x in ["entity", "entry"]]):
            return Response(
                'Parameter "entity" and "entry" are mandatory',
                status=status.HTTP_400_BAD_REQUEST,
            )

        entity = Entity.objects.filter(name=request.data["entity"]).first()
        if not entity:
            return Response(
                "Failed to find specified Entity (%s)" %
                request.data["entity"],
                status=status.HTTP_404_NOT_FOUND,
            )

        entry = Entry.objects.filter(name=request.data["entry"],
                                     schema=entity).first()
        if not entry:
            return Response(
                "Failed to find specified Entry (%s)" % request.data["entry"],
                status=status.HTTP_404_NOT_FOUND,
            )

        # permission check
        if not request.user.has_permission(
                entry, ACLType.Full) or not request.user.has_permission(
                    entity, ACLType.Readable):
            return Response("Permission denied to operate",
                            status=status.HTTP_400_BAD_REQUEST)

        # Delete the specified entry then return its id, if is active
        if entry.is_active:
            # create a new Job to delete entry and run it
            job = Job.new_delete(request.user, entry)

            # create and run notify delete entry task
            job_notify = Job.new_notify_delete_entry(request.user, entry)
            job_notify.run()

            job.dependent_job = job_notify
            job.save(update_fields=["dependent_job"])

            job.run()

        return Response({"id": entry.id})
Example #11
0
def do_delete(request, entry_id, recv_data):
    entry, error = get_obj_with_check_perm(request.user, Entry, entry_id,
                                           ACLType.Full)
    if error:
        return error

    if custom_view.is_custom("do_delete_entry", entry.schema.name):
        # do_delete custom view
        resp = custom_view.call_custom("do_delete_entry", entry.schema.name,
                                       request, request.user, entry)

        # If custom_view returns available response this returns it to user,
        # or continues default processing.
        if resp:
            return resp

    # set deleted flag in advance because deleting processing taks long time
    entry.is_active = False
    entry.save(update_fields=["is_active"])

    ret = {}
    # save deleting Entry name before do it
    ret["name"] = entry.name

    # register operation History for deleting entry
    request.user.seth_entry_del(entry)

    # Create a new job to delete entry and run it
    job_delete_entry = Job.new_delete(request.user, entry)
    job_notify_event = Job.new_notify_delete_entry(request.user, entry)

    # This prioritizes notifying job rather than deleting entry
    if job_delete_entry.dependent_job:
        job_notify_event.dependent_job = job_delete_entry.dependent_job

    job_notify_event.save(update_fields=["dependent_job"])
    job_notify_event.run()

    # This update dependent job of deleting entry job
    job_delete_entry.dependent_job = job_notify_event
    job_delete_entry.save(update_fields=["dependent_job"])

    job_delete_entry.run()

    return JsonResponse(ret)
Example #12
0
    def test_cancel_job(self):
        user = self.guest_login()

        entity = Entity.objects.create(name="entity", created_user=user)
        entry = Entry.objects.create(name="entry",
                                     schema=entity,
                                     created_user=user)

        # make a job
        job = Job.new_delete(user, entry)
        self.assertEqual(job.status, Job.STATUS["PREPARING"])

        # send request without any parameters
        resp = self.client.delete("/api/v1/job/", json.dumps({}),
                                  "application/json")
        self.assertEqual(resp.status_code, 400)
        self.assertEqual(resp.content, b'"Parameter job_id is required"')

        # send request with invalid job id
        resp = self.client.delete("/api/v1/job/", json.dumps({"job_id":
                                                              99999}),
                                  "application/json")
        self.assertEqual(resp.status_code, 400)
        self.assertEqual(resp.content, b'"Failed to find Job(id=99999)"')

        # target jobs that cannot be canceled
        resp = self.client.delete("/api/v1/job/",
                                  json.dumps({"job_id":
                                              job.id}), "application/json")
        self.assertEqual(resp.status_code, 400)
        self.assertEqual(resp.content, b'"Target job cannot be canceled"')

        # send request with proper parameter
        job = Job.new_create(user, entry)
        self.assertEqual(job.status, Job.STATUS["PREPARING"])

        resp = self.client.delete("/api/v1/job/",
                                  json.dumps({"job_id":
                                              job.id}), "application/json")
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content, b'"Success to cancel job"')

        job.refresh_from_db()
        self.assertEqual(job.status, Job.STATUS["CANCELED"])
Example #13
0
    def test_rerun_jobs(self):
        user = self.guest_login()

        entity = Entity.objects.create(name='entity', created_user=user)
        attr = EntityAttr.objects.create(name='attr',
                                         created_user=user,
                                         type=AttrTypeValue['string'],
                                         parent_entity=entity)
        entity.attrs.add(attr)

        # make a job to create an entry
        entry = Entry.objects.create(name='entry', schema=entity, created_user=user)
        job = Job.new_create(user, entry, params={
            'attrs': [
                {'id': str(attr.id), 'value': [{'data': 'hoge', 'index': 0}], 'referral_key': []}
            ]
        })

        # send request to run job
        resp = self.client.post('/api/v1/job/run/%d' % job.id)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content, b'"Success to run command"')

        job = Job.objects.get(id=job.id)
        self.assertEqual(job.status, Job.STATUS['DONE'])
        self.assertEqual(entry.attrs.count(), 1)

        attrv = entry.attrs.first().get_latest_value()
        self.assertEqual(attrv.value, 'hoge')

        # send request to run job with finished job-id
        resp = self.client.post('/api/v1/job/run/%d' % job.id)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content, b'"Target job has already been done"')

        # send request to run job with invalid job-id
        resp = self.client.post('/api/v1/job/run/%d' % 9999)
        self.assertEqual(resp.status_code, 400)

        # make and send a job to update entry
        job = Job.new_edit(user, entry, params={
            'attrs': [
                {'id': str(entry.attrs.first().id), 'value': [{'data': 'fuga', 'index': 0}],
                 'referral_key': []}
            ]
        })
        resp = self.client.post('/api/v1/job/run/%d' % job.id)

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content, b'"Success to run command"')
        self.assertEqual(Job.objects.get(id=job.id).status, Job.STATUS['DONE'])
        self.assertEqual(entry.attrs.first().get_latest_value().value, 'fuga')

        # make and send a job to copy entry
        job = Job.new_copy(user, entry, params={'new_name': 'new_entry'})
        resp = self.client.post('/api/v1/job/run/%d' % job.id)

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content, b'"Success to run command"')
        self.assertEqual(Job.objects.get(id=job.id).status, Job.STATUS['DONE'])

        # checks it's success to clone entry
        new_entry = Entry.objects.get(name='new_entry', schema=entity)
        self.assertEqual(new_entry.attrs.first().get_latest_value().value, 'fuga')

        # make and send a job to delete entry
        job = Job.new_delete(user, entry)
        resp = self.client.post('/api/v1/job/run/%d' % job.id)

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content, b'"Success to run command"')
        self.assertFalse(Entry.objects.get(id=entry.id).is_active)
Example #14
0
    def test_rerun_jobs(self):
        user = self.guest_login()

        entity = Entity.objects.create(name="entity", created_user=user)
        attr = EntityAttr.objects.create(
            name="attr",
            created_user=user,
            type=AttrTypeValue["string"],
            parent_entity=entity,
        )
        entity.attrs.add(attr)

        # make a job to create an entry
        entry = Entry.objects.create(name="entry", schema=entity, created_user=user)
        job = Job.new_create(
            user,
            entry,
            params={
                "attrs": [
                    {
                        "id": str(attr.id),
                        "value": [{"data": "hoge", "index": 0}],
                        "referral_key": [],
                    }
                ]
            },
        )

        # send request to run job
        resp = self.client.post("/api/v1/job/run/%d" % job.id)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content, b'"Success to run command"')

        job = Job.objects.get(id=job.id)
        self.assertEqual(job.status, Job.STATUS["DONE"])
        self.assertEqual(entry.attrs.count(), 1)

        attrv = entry.attrs.first().get_latest_value()
        self.assertEqual(attrv.value, "hoge")

        # send request to run job with finished job-id
        resp = self.client.post("/api/v1/job/run/%d" % job.id)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content, b'"Target job has already been done"')

        # send request to run job with invalid job-id
        resp = self.client.post("/api/v1/job/run/%d" % 9999)
        self.assertEqual(resp.status_code, 400)

        # make and send a job to update entry
        job = Job.new_edit(
            user,
            entry,
            params={
                "attrs": [
                    {
                        "id": str(entry.attrs.first().id),
                        "value": [{"data": "fuga", "index": 0}],
                        "referral_key": [],
                    }
                ]
            },
        )
        resp = self.client.post("/api/v1/job/run/%d" % job.id)

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content, b'"Success to run command"')
        self.assertEqual(Job.objects.get(id=job.id).status, Job.STATUS["DONE"])
        self.assertEqual(entry.attrs.first().get_latest_value().value, "fuga")

        # make and send a job to copy entry
        job = Job.new_do_copy(user, entry, params={"new_name": "new_entry"})
        resp = self.client.post("/api/v1/job/run/%d" % job.id)

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content, b'"Success to run command"')
        self.assertEqual(Job.objects.get(id=job.id).status, Job.STATUS["DONE"])

        # checks it's success to clone entry
        new_entry = Entry.objects.get(name="new_entry", schema=entity)
        self.assertEqual(new_entry.attrs.first().get_latest_value().value, "fuga")

        # make and send a job to delete entry
        job = Job.new_delete(user, entry)
        resp = self.client.post("/api/v1/job/run/%d" % job.id)

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content, b'"Success to run command"')
        self.assertFalse(Entry.objects.get(id=entry.id).is_active)