Esempio n. 1
0
 def test_get_play_by_uuid(self):
     play = factories.PlayFactory(
         name="play1", uuid="6b838b6f-cfc7-4e11-a264-73df8683ee0e")
     factories.PlayFactory(name="play2")
     request = self.client.get(
         "/api/v1/plays?uuid=6b838b6f-cfc7-4e11-a264-73df8683ee0e")
     self.assertEqual(1, len(request.data["results"]))
     self.assertEqual(play.name, request.data["results"][0]["name"])
Esempio n. 2
0
    def test_get_plays_by_name(self):
        # Create a playbook and two plays
        playbook = factories.PlaybookFactory()
        play = factories.PlayFactory(name="first_play", playbook=playbook)
        factories.PlayFactory(name="second_play", playbook=playbook)

        # Query for the first play name and expect one result
        request = self.client.get("/api/v1/plays?name=%s" % play.name)
        self.assertEqual(1, len(request.data["results"]))
        self.assertEqual(play.name, request.data["results"][0]["name"])
Esempio n. 3
0
 def test_partial_update_play(self):
     play = factories.PlayFactory()
     self.assertNotEqual("update", play.name)
     request = self.client.patch("/api/v1/plays/%s" % play.id, {"name": "update"})
     self.assertEqual(200, request.status_code)
     play_updated = models.Play.objects.get(id=play.id)
     self.assertEqual("update", play_updated.name)
Esempio n. 4
0
 def test_get_play_duration(self):
     started = timezone.now()
     ended = started + datetime.timedelta(hours=1)
     play = factories.PlayFactory(started=started, ended=ended)
     request = self.client.get("/api/v1/plays/%s" % play.id)
     self.assertEqual(parse_duration(request.data["duration"]),
                      ended - started)
Esempio n. 5
0
 def test_update_wrong_play_status(self):
     play = factories.PlayFactory()
     self.assertNotEqual("wrong", play.status)
     request = self.client.patch("/api/v1/plays/%s" % play.id, {"status": "wrong"})
     self.assertEqual(400, request.status_code)
     play_updated = models.Play.objects.get(id=play.id)
     self.assertNotEqual("wrong", play_updated.status)
Esempio n. 6
0
    def test_expired_play(self):
        play = factories.PlayFactory(status="running")
        self.assertEqual("running", play.status)

        request = self.client.patch("/api/v1/plays/%s" % play.id, {"status": "expired"})
        self.assertEqual(200, request.status_code)
        play_updated = models.Play.objects.get(id=play.id)
        self.assertEqual("expired", play_updated.status)
Esempio n. 7
0
    def test_get_play_by_status(self):
        play = factories.PlayFactory(status="running")
        factories.PlayFactory(status="completed")
        factories.PlayFactory(status="unknown")

        # Confirm we have three objects
        request = self.client.get("/api/v1/plays")
        self.assertEqual(3, len(request.data["results"]))

        # Test single status
        request = self.client.get("/api/v1/plays?status=running")
        self.assertEqual(1, len(request.data["results"]))
        self.assertEqual(play.status, request.data["results"][0]["status"])

        # Test multiple status
        request = self.client.get("/api/v1/plays?status=running&status=completed")
        self.assertEqual(2, len(request.data["results"]))
Esempio n. 8
0
    def test_get_tasks_by_play(self):
        playbook = factories.PlaybookFactory()
        # Create two plays
        play = factories.PlayFactory(name="first_play", playbook=playbook)
        second_play = factories.PlayFactory(name="second_play",
                                            playbook=playbook)
        request = self.client.get("/api/v1/plays")
        self.assertEqual(2, len(request.data["results"]))

        # Add tasks for the first play and validate
        task = factories.TaskFactory(name="inside_first", play=play)
        factories.TaskFactory(name="inside_second", play=play)

        request = self.client.get("/api/v1/tasks?play=%s" % play.id)
        self.assertEqual(2, len(request.data["results"]))
        self.assertEqual(task.name, request.data["results"][1]["name"])
        self.assertEqual("inside_second", request.data["results"][0]["name"])

        request = self.client.get("/api/v1/tasks?play=%s" % second_play.id)
        self.assertEqual(0, len(request.data["results"]))
Esempio n. 9
0
    def test_get_play_order(self):
        old_started = timezone.now() - datetime.timedelta(hours=12)
        old_ended = old_started + datetime.timedelta(minutes=30)
        old_play = factories.PlayFactory(started=old_started, ended=old_ended)
        new_started = timezone.now() - datetime.timedelta(hours=6)
        new_ended = new_started + datetime.timedelta(hours=1)
        new_play = factories.PlayFactory(started=new_started, ended=new_ended)

        # Ensure we have two objects
        request = self.client.get("/api/v1/plays")
        self.assertEqual(2, len(request.data["results"]))

        order_fields = ["id", "created", "updated", "started", "ended", "duration"]
        # Ascending order
        for field in order_fields:
            request = self.client.get("/api/v1/plays?order=%s" % field)
            self.assertEqual(request.data["results"][0]["id"], old_play.id)

        # Descending order
        for field in order_fields:
            request = self.client.get("/api/v1/plays?order=-%s" % field)
            self.assertEqual(request.data["results"][0]["id"], new_play.id)
Esempio n. 10
0
    def test_get_play_by_date(self):
        play = factories.PlayFactory()

        past = datetime.datetime.now() - datetime.timedelta(hours=12)
        negative_date_fields = ["created_before", "started_before", "updated_before"]
        positive_date_fields = ["created_after", "started_after", "updated_after"]

        # Expect no play when searching before it was created
        for field in negative_date_fields:
            request = self.client.get("/api/v1/plays?%s=%s" % (field, past.isoformat()))
            self.assertEqual(request.data["count"], 0)

        # Expect a play when searching after it was created
        for field in positive_date_fields:
            request = self.client.get("/api/v1/plays?%s=%s" % (field, past.isoformat()))
            self.assertEqual(request.data["count"], 1)
            self.assertEqual(request.data["results"][0]["id"], play.id)
Esempio n. 11
0
 def test_create_task(self):
     play = factories.PlayFactory()
     file = factories.FileFactory()
     self.assertEqual(0, models.Task.objects.count())
     request = self.client.post(
         "/api/v1/tasks",
         {
             "name": "create",
             "action": "test",
             "lineno": 2,
             "handler": False,
             "status": "running",
             "play": play.id,
             "file": file.id,
             "playbook": play.playbook.id,
         },
     )
     self.assertEqual(201, request.status_code)
     self.assertEqual(1, models.Task.objects.count())
Esempio n. 12
0
 def test_task_serializer_compress_tags(self):
     play = factories.PlayFactory()
     file = factories.FileFactory()
     serializer = serializers.TaskSerializer(
         data={
             "name": "compress",
             "action": "test",
             "lineno": 2,
             "status": "running",
             "handler": False,
             "play": play.id,
             "file": file.id,
             "tags": factories.TASK_TAGS,
             "playbook": play.playbook.id,
         })
     serializer.is_valid()
     task = serializer.save()
     task.refresh_from_db()
     self.assertEqual(task.tags, utils.compressed_obj(factories.TASK_TAGS))
Esempio n. 13
0
 def test_task_serializer(self):
     play = factories.PlayFactory()
     file = factories.FileFactory()
     serializer = serializers.TaskSerializer(
         data={
             "name": "serializer",
             "action": "test",
             "lineno": 2,
             "status": "completed",
             "handler": False,
             "play": play.id,
             "file": file.id,
             "playbook": play.playbook.id,
         })
     serializer.is_valid()
     task = serializer.save()
     task.refresh_from_db()
     self.assertEqual(task.name, "serializer")
     self.assertEqual(task.status, "completed")
Esempio n. 14
0
 def test_get_play_by_playbook(self):
     play = factories.PlayFactory(name="play1")
     factories.PlayFactory(name="play2")
     request = self.client.get("/api/v1/plays?playbook=1")
     self.assertEqual(1, len(request.data["results"]))
     self.assertEqual(play.name, request.data["results"][0]["name"])
Esempio n. 15
0
 def test_get_play(self):
     play = factories.PlayFactory()
     request = self.client.get("/api/v1/plays/%s" % play.id)
     self.assertEqual(play.name, request.data["name"])
Esempio n. 16
0
 def test_delete_play(self):
     play = factories.PlayFactory()
     self.assertEqual(1, models.Play.objects.all().count())
     request = self.client.delete("/api/v1/plays/%s" % play.id)
     self.assertEqual(204, request.status_code)
     self.assertEqual(0, models.Play.objects.all().count())
Esempio n. 17
0
 def test_get_plays(self):
     play = factories.PlayFactory()
     request = self.client.get("/api/v1/plays")
     self.assertEqual(1, len(request.data["results"]))
     self.assertEqual(play.name, request.data["results"][0]["name"])
Esempio n. 18
0
 def test_play_factory(self):
     play = factories.PlayFactory(name="play factory")
     self.assertEqual(play.name, "play factory")
Esempio n. 19
0
 def test_get_playbook_arguments(self):
     play = factories.PlayFactory()
     request = self.client.get("/api/v1/plays/%s" % play.id)
     self.assertIn("inventory", request.data["playbook"]["arguments"])