Пример #1
0
class UpdateScenarioViewTest(TestCase):
    def setUp(self):
        self.scenario = ScenarioFactory(xml=get_xml("default.xml"))
        self.client.login(username=self.scenario.user.username, password="******")
        self.url = reverse("ts_om.scenario.update")
        # self.data = {"xml": get_xml("default.xml")}
        self.data = {
            "scenario_id": self.scenario.id,
            "xml": "",
            "name": "Updated scenario name",
            "description": "New Description"
        }

    def test(self):
        response = self.client.post(self.url, data=self.data)
        self.assertEqual(response.status_code, 200)
        self.scenario.refresh_from_db()
        self.assertEqual(self.scenario.name, "Updated scenario name")
        self.assertEqual(self.scenario.description, "New Description")
        self.assertEqual(self.scenario.xml, "")

    def test_no_permission(self):
        user = UserFactory()
        client = Client()
        client.login(username=user.username, password="******")
        response = client.post(self.url, data=self.data)
        self.assertEqual(response.status_code, 403)
        self.scenario.refresh_from_db()
        self.assertNotEqual(self.scenario.name, "Updated scenario name")
        self.assertNotEqual(self.scenario.description, "New Description")
        self.assertNotEqual(self.scenario.xml, "")
Пример #2
0
class DeleteScenarioViewTest(TestCase):
    def setUp(self):
        self.user = UserFactory()
        self.scenario = ScenarioFactory(user=self.user)
        self.client.login(username=self.user.username, password="******")
        self.url = reverse("ts_om.delete",
                           kwargs={"scenario_id": self.scenario.id})

    def test_as_anonymous(self):
        client = Client()
        response = client.get(self.url)
        self.assertEqual(response.status_code, 302)
        self.scenario.refresh_from_db()
        self.assertEqual(self.scenario.deleted, False)

    def test_no_permission(self):
        user = UserFactory()
        client = Client()
        client.login(username=user.username, password="******")
        response = client.get(self.url)
        self.assertEqual(response.status_code, 403)
        self.scenario.refresh_from_db()
        self.assertEqual(self.scenario.deleted, False)

    def test_success(self):
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(self.client.session["notifications"][0]["type"],
                         SUCCESS)
        self.assertEqual(response.url, reverse("ts_om.list"))
        self.scenario.refresh_from_db()
        self.assertEqual(self.scenario.deleted, True)

    def test_success_undelete(self):
        self.scenario.deleted = True
        self.scenario.save()
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(self.client.session["notifications"][0]["type"],
                         SUCCESS)
        self.assertEqual(response.url, reverse("ts_om.list"))
        self.scenario.refresh_from_db()
        self.assertEqual(self.scenario.deleted, False)
Пример #3
0
class SubmitScenariosViewTest(TestCase):
    def setUp(self):
        self.scenario = ScenarioFactory()
        self.user = self.scenario.user
        self.user.set_password("1")
        self.user.save()

    @patch("website.apps.ts_om.views.submit_scenarios_view.submit")
    def test_anonymous(self, submit_func):
        client = Client()
        data = {"scenario_ids": json.dumps([self.scenario.id])}
        response = client.post(reverse("ts_om.submit"), data=data)
        self.assertEqual(response.status_code, 200)
        response_json = json.loads(response.content)
        self.assertEqual(response_json["ok"], False)
        self.scenario.refresh_from_db()
        self.assertEqual(submit_func.called, False)

    @patch("website.apps.ts_om.views.submit_scenarios_view.submit")
    @patch("website.apps.ts_om.views.submit_scenarios_view.rest_validate")
    def test_success(self, rest_validate_func, submit_func):
        rest_validate_func.return_value = json.dumps({'result': 0})
        client = Client()
        client.login(username=self.user.username, password="******")
        data = {"scenario_ids": json.dumps([self.scenario.id])}
        response = client.post(reverse("ts_om.submit"), data=data)
        self.assertEqual(response.status_code, 200)
        response_json = json.loads(response.content)
        self.assertEqual(response_json["ok"], True)
        self.scenario.refresh_from_db()
        self.assertEqual(rest_validate_func.called, True)
        self.assertEqual(submit_func.called, True)

    @patch("website.apps.ts_om.views.submit_scenarios_view.submit")
    @patch("website.apps.ts_om.views.submit_scenarios_view.rest_validate")
    def test_validation_fails(self, rest_validate_func, submit_func):
        rest_validate_func.return_value = json.dumps({'result': 66})
        client = Client()
        client.login(username=self.user.username, password="******")
        data = {"scenario_ids": json.dumps([self.scenario.id])}
        response = client.post(reverse("ts_om.submit"), data=data)
        self.assertEqual(response.status_code, 200)
        response_json = json.loads(response.content)
        # self.assertEqual(response_json["ok"], False)
        self.scenario.refresh_from_db()
        self.assertEqual(rest_validate_func.called, True)
        self.assertEqual(submit_func.called, False)