Beispiel #1
0
class UploadTests(TestCase):
    def setUp(self):
        # The test client is a Python class that acts as a dummy Web browser
        self._client = Client()

        # populate database
        self.populate = Command()
        self.populate.handle()

        # data is a pointer to workflow, users and categories names
        self.data = self.populate.getData()

    def test_0_form_workflow_add_manually(self):
        # create auxiliary json file
        jsonFileName = "/tmp/workflow.json"
        f = open(jsonFileName, "w")
        f.write(self.populate.getJson())
        f.close()
        upload_file = open(jsonFileName, "rb")
        file_dict = {
            NAME_JSON: SimpleUploadedFile(upload_file.name, upload_file.read())
        }

        # get category IDs for the select Category field
        category0 = Category.objects.get(name=self.data[CATEGORY][0])
        category1 = Category.objects.get(name=self.data[CATEGORY][1])
        # new workflow name
        workFlowName = 'workflow999'
        data_dict = {
            NAME_NAME: workFlowName,
            NAME_CATEGORY: [category0.id, category1.id],
            NAME_KEYWORDS: self.populate.getParragraph(1, 14),
            NAME_DESCRIPTION: self.populate.getParragraph(23, 123),
            NAME_VERSION: '3.1415',
        }

        # form with fieldfields has two independent dictionaries
        form = WorkFlowManualForm(data=data_dict, files=file_dict)
        self.assertTrue(form.is_valid(), form.errors)
        if form.is_valid():
            file_data = form.cleaned_data["json"].read().decode("utf-8")
            # modify object json value
            form.instance.json = file_data
        newWorkflow = form.save()
        self.assertEqual(newWorkflow.name, workFlowName)

        # Note that we are ignoring recaptcha
        # there is no simple way to automatize it

    def test_1_form_workflowProgStep1_add(self):
        # create auxiliary json file
        jsonFileName = "/tmp/workflow.json"
        f = open(jsonFileName, "w")
        f.write(self.populate.getJson())
        f.close()
        upload_file = open(jsonFileName, "rb")
        file_dict = {
            NAME_JSON: SimpleUploadedFile(upload_file.name, upload_file.read())
        }

        # new workflow name
        workFlowName = 'workflow999'
        data_dict = {
            NAME_VERSION: '3.141592',
        }
        # form with fieldfields has two independent dictionaries
        # we are testing the form not the views
        form = WorkFlowProgStep1Form(data=data_dict, files=file_dict)
        self.assertTrue(form.is_valid(), form.errors)

    def test_2_form_workflowProgStep2_add(self):
        # get category IDs for the select Category field
        category0 = Category.objects.get(name=self.data[CATEGORY][0])
        category1 = Category.objects.get(name=self.data[CATEGORY][1])
        # new workflow name
        workFlowName = 'workflow999'
        data_dict = {
            NAME_NAME: workFlowName,
            NAME_CATEGORY: [category0.id, category1.id],
            NAME_KEYWORDS: self.populate.getParragraph(1, 14),
            NAME_DESCRIPTION: self.populate.getParragraph(23, 123),
            NAME_VERSION: '3.1415',
        }

        # form with fieldfields has two independent dictionaries
        form = WorkFlowProgStep2Form(data=data_dict, )
        self.assertTrue(form.is_valid(), form.errors)
        if form.is_valid():
            file_data = self.populate.getJson()
            # modify object json 2alue
            form.instance.json = file_data
        newWorkflow = form.save()
        self.assertEqual(newWorkflow.name, workFlowName)

        # Note that we are ignoring recaptcha
        # there is no simple way to automatize it

    # Now test views

    def test_4_view_workflow_add_manually(self):
        # create auxiliary json file
        jsonFileName = "/tmp/workflow.json"
        f = open(jsonFileName, "w")
        f.write(self.populate.getJson())
        f.close()
        upload_file = open(jsonFileName, "rb")
        file = SimpleUploadedFile(upload_file.name, upload_file.read())

        workFlowName = 'workflow999'
        category0 = Category.objects.get(name=self.data[CATEGORY][0])
        category1 = Category.objects.get(name=self.data[CATEGORY][1])

        version = '3.1415'
        response = self._client.post(reverse(WORKFLOW_ADD_MANUALLY),
                                     data={
                                         NAME_NAME:
                                         workFlowName,
                                         NAME_KEYWORDS:
                                         self.populate.getParragraph(1, 14),
                                         NAME_CATEGORY:
                                         [category0.id, category1.id],
                                         NAME_DESCRIPTION:
                                         self.populate.getParragraph(23, 123),
                                         NAME_VERSION:
                                         version,
                                         NAME_JSON:
                                         file
                                     })
        workflow = WorkFlow.objects.get(name=workFlowName)
        self.assertEqual(workflow.versionInit, version)

    def test_5_view_workflow_add_programatically(self):
        # create auxiliary json file
        jsonFileName = "/tmp/workflow.json"
        f = open(jsonFileName, "w")
        f.write(self.populate.getJson())
        f.close()
        upload_file = open(jsonFileName, "rb")
        file = SimpleUploadedFile(upload_file.name, upload_file.read())

        workFlowName = 'workflow999'
        category0 = Category.objects.get(name=self.data[CATEGORY][0])
        category1 = Category.objects.get(name=self.data[CATEGORY][1])

        version = '3.1415'
        response = self._client.post(reverse(WORKFLOW_PROG_STEP1),
                                     data={
                                         NAME_VERSION: version,
                                         NAME_JSON: file
                                     })
        self.assertIn("jsonFileName", response.content.decode("utf-8"))
        _dict = json.loads(response.content.decode("utf-8"))

        # call with GET (send filename)
        fileNameUrl = "?jsonFileName=%s&versionInit=%s" % \
                      (_dict['jsonFileName'], version)
        response = self._client.get(reverse(WORKFLOW_PROG_STEP2) + fileNameUrl)

        # workflow should not be available yet.
        try:
            workflow = WorkFlow.objects.get(name=workFlowName)
            self.assertTrue(False, "This workflow should not exist")
        except ObjectDoesNotExist:
            pass

        # call with POST (send metadata)
        response = self._client.post(
            reverse(WORKFLOW_PROG_STEP2),
            data={
                NAME_NAME: workFlowName,
                NAME_KEYWORDS: self.populate.getParragraph(1, 14),
                NAME_CATEGORY: [category0.id, category1.id],
                NAME_DESCRIPTION: self.populate.getParragraph(23, 123),
                NAME_VERSION: version,  # from json
                # NAME_JSON: file - in session variable
            })
        workflow = WorkFlow.objects.get(name=workFlowName)
        self.assertEqual(workflow.versionInit, version)
Beispiel #2
0
class FindTests(TestCase):
    def setUp(self):
        # The test client is a Python class that acts as a dummy Web browser
        self._client = Client()

        # populate database
        self.populate = Command()
        self.populate.handle()

        #data is a pointer to workflow, users and categories names
        self.data = self.populate.getData()

    def test_0_workflow_list(self):
        #firts check all workflows
        print("test_workflow_list:")
        # WORKFLOW_LIST returns all  workflows
        # If you set follow = True the client will follow any redirects
        response = self._client.get(reverse(WORKFLOW_LIST), follow=True)

        # Check that all categories and workflow are in the returned page
        baseName = CATEGORY
        for itemName in self.data[baseName]:
            self.assertIn(itemName, str(response.content))
            print("    assert: %s" % itemName)
        baseName = WORKFLOW
        for itemName in self.data[baseName]:
            self.assertIn(itemName, str(response.content))
            print("    assert: %s" % itemName)

    def test_1_workflow_category(self):
        # firts check all workflows related with a category
        print("test_workflow_category")
        baseName = CATEGORY
        category = Category.objects.get(name=self.data[baseName][0])
        response = self._client.get(
            reverse(WORKFLOW_LIST_BY_CARTEGORY,
                    kwargs={'category_slug': category.slug}))

        workflows = WorkFlow.objects.filter(category=category)
        for itemName in workflows:
            self.assertIn("%s</a>" % itemName.name, str(response.content))
            print("    assert: %s in %s" % (itemName.name, category.name))

        # django Q objects have being designed to make complex queries
        # in this case search for objects NOT equal to a given category
        workflows = WorkFlow.objects.filter(~Q(category=category))
        for itemName in workflows:
            self.assertNotIn("%s</a>" % itemName.name, str(response.content))
            print("    assert: %s NOT in %s" % (itemName.name, category.name))

    def test_2_workflow_detail(self):
        print("test_workflow_detail.")
        # getworkflow detail
        baseName = WORKFLOW
        workflow = WorkFlow.objects.get(name=self.data[baseName][0])
        print("    workflow %s has been seem %d times" %
              (workflow.name, workflow.views))
        response = self._client.get(
            reverse(WORKFLOW_DETAIL,
                    kwargs={
                        'id': workflow.id,
                        'slug': workflow.slug
                    }))
        self.assertIn(workflow.name, str(response.content))
        self.assertIn(workflow.description, response.content.decode("utf-8"))
        # get workflow detail again, viewsshould be 1
        workflow = WorkFlow.objects.get(name=self.data[baseName][0])
        self.assertEqual(workflow.views, 1)
        print("    workflow %s has been seem %d times" %
              (workflow.name, workflow.views))
        response = self._client.get(
            reverse(WORKFLOW_DETAIL,
                    kwargs={
                        'id': workflow.id,
                        'slug': workflow.slug
                    }))
        workflow = WorkFlow.objects.get(name=self.data[baseName][0])
        # get workflow detail again, views should be 2
        self.assertEqual(workflow.views, 2)
        print("    workflow %s has been seem %d times" %
              (workflow.name, workflow.views))

    def test_3_workflow_download(self):
        print("test_3_workflow_download.")
        baseName = WORKFLOW
        workflow = WorkFlow.objects.get(name=self.data[baseName][0])

        # download workflow & get workflow again
        # downloads should be 1 after download
        print("    workflow %s has been seem %d times" %
              (workflow.name, workflow.downloads))
        self.assertEqual(workflow.downloads, 0)
        response = self._client.get(
            reverse(WORKFLOW_DOWNLOAD,
                    kwargs={
                        'id': workflow.id,
                        'slug': workflow.slug
                    }))
        # reload workflow
        workflow = WorkFlow.objects.get(name=self.data[baseName][0])
        print("    workflow %s has been seem %d times" %
              (workflow.name, workflow.downloads))
        self.assertEqual(workflow.downloads, 1)
        self.assertIn(self.populate.getJson(),
                      response.content.decode("utf-8"))

        # try the non-count version of download
        # download should not be incremented
        response = self._client.get(
            reverse(WORKFLOW_DOWNLOAD_NO_COUNT,
                    kwargs={
                        'id': workflow.id,
                        'slug': workflow.slug
                    }))
        # reload workflow
        workflow = WorkFlow.objects.get(name=self.data[baseName][0])
        print("    workflow %s has been seem %d times (No count)" %
              (workflow.name, workflow.downloads))
        self.assertEqual(workflow.downloads, 1)
        self.assertIn(self.populate.getJson(),
                      response.content.decode("utf-8"))

    def test_4_workflow_search(self):
        #search for a workflow workflow_search/
        print("test_4_workflow_search.")
        baseName = WORKFLOW
        workflow = WorkFlow.objects.get(name=self.data[baseName][0])
        response = self._client.post(reverse(WORKFLOW_SEARCH), {
            'byName': 'byName',
            'key': workflow.slug
        })
        print("   ByName search for workflow %s" % workflow.slug)
        self.assertIn(workflow.name, str(response.content))
        self.assertIn(workflow.description, response.content.decode("utf-8"))

        valueToSearch = workflow.keywords[:5]
        response = self._client.post(reverse(WORKFLOW_SEARCH), {
            'byKeyWord': 'byKeyWord',
            'key': workflow.slug
        })
        print("   ByKeyWord search for %s" % valueToSearch)
        self.assertIn(workflow.name, str(response.content))
 def populateDataBase(self):
     populate = Command()
     populate.handle()
     data = populate.getData()
     self.CATEGORYNAME = data[CATEGORY][0]