Esempio n. 1
0
    def test_valid_account_number_and_org_admin_fake_header_defaults(self):
        """
        Test with valid account_number and org_admin returns defaults.

        Test with an internal account header, valid account_number and True
        org_admin headers, expect a normal response for the default start_date
        of "yesterday".
        """
        yesterday = get_today() - datetime.timedelta(days=1)
        today = get_today()
        api_helper.calculate_concurrent(yesterday, today, self.user1.id)
        fake_header = util_helper.get_identity_auth_header(
            account_number=self.user1.username,
            is_org_admin=True,
        )

        client = APIClient()
        client.credentials(
            HTTP_X_RH_IDENTITY=util_helper.get_internal_identity_auth_header(),
            HTTP_X_RH_INTERNAL_FAKE_IDENTITY=fake_header,
        )
        response = client.get(self.concurrent_api_url, data={}, format="json")
        body = response.json()
        self.assertEqual(body["meta"]["count"], 1)
        self.assertEqual(len(body["data"]), 1)
        self.assertEqual(body["data"][0]["date"], str(yesterday))
Esempio n. 2
0
    def test_future_start_date_returns_400(self):
        """
        Test with far-future start_date, expecting it to return 400.

        When an start_date is given that is tomorrow or later, we return
        a 400 response, because we cannot predict the future.
        """
        today = get_today()
        future = today + datetime.timedelta(days=1)
        data = {"start_date": str(future)}
        api_helper.calculate_concurrent(today, future, self.user1.id)

        client = APIClient()
        client.force_authenticate(user=self.user1)
        response = client.get(self.concurrent_api_url, data=data, format="json")
        self.assertEqual(response.status_code, 400)

        body = response.json()
        self.assertEqual(body["start_date"], [_("start_date cannot be in the future.")])
    def test_start_date_is_inclusive_and_end_date_is_exclusive(self):
        """Test that start_date is inclusive and end_date is exclusive."""
        start_date = datetime.date(2019, 1, 1)
        end_date = datetime.date(2019, 1, 4)
        data = {
            "start_date": start_date.strftime("%Y-%m-%d"),
            "end_date": end_date.strftime("%Y-%m-%d"),
        }
        api_helper.calculate_concurrent(start_date, end_date, self.user1.id)

        client = APIClient()
        client.force_authenticate(user=self.user1)
        response = client.get(self.concurrent_api_url,
                              data=data,
                              format="json")
        body = response.json()
        self.assertEqual(body["meta"]["count"], 3)
        self.assertEqual(len(body["data"]), 3)
        self.assertEqual(body["data"][0]["date"], "2019-01-01")
        self.assertEqual(body["data"][1]["date"], "2019-01-02")
        self.assertEqual(body["data"][2]["date"], "2019-01-03")
    def test_start_date_end_date_defaults(self):
        """
        Test with no start_date and no end_date set.

        Default start_date is "yesterday" and default end_date is "today", and
        since start_date is inclusive and end_date is exclusive, the resulting
        output should be data for one day: yesterday.
        """
        yesterday = get_today() - datetime.timedelta(days=1)
        today = get_today()
        data = {}
        api_helper.calculate_concurrent(yesterday, today, self.user1.id)

        client = APIClient()
        client.force_authenticate(user=self.user1)
        response = client.get(self.concurrent_api_url,
                              data=data,
                              format="json")
        body = response.json()
        self.assertEqual(body["meta"]["count"], 1)
        self.assertEqual(len(body["data"]), 1)
        self.assertEqual(body["data"][0]["date"], str(yesterday))
Esempio n. 5
0
    def test_invalid_account_number(self):
        """
        Test with an invalid account_number fake identity.

        Test with an internal account header, an invalid account_number and True
        org_admin fake identity header, expect an authentication error.
        """
        yesterday = get_today() - datetime.timedelta(days=1)
        today = get_today()
        fake_header = util_helper.get_identity_auth_header(
            account_number=str(_faker.pyint()),
            is_org_admin=True,
        )
        api_helper.calculate_concurrent(yesterday, today, self.user1.id)

        client = APIClient()
        client.credentials(
            HTTP_X_RH_IDENTITY=util_helper.get_internal_identity_auth_header(),
            HTTP_X_RH_INTERNAL_FAKE_IDENTITY=fake_header,
        )
        response = client.get(self.concurrent_api_url, data={}, format="json")
        body = response.json()
        self.assertEqual(response.status_code, 403)
        self.assertEqual(body["detail"], "Incorrect authentication credentials.")
Esempio n. 6
0
    def test_with_false_is_org_admin_parameter(self):
        """
        Test with a False is_org_admin parameter.

        Test with an internal account header, a valid account_number and False
        org_admin headers, expect an authentication error.
        """
        yesterday = get_today() - datetime.timedelta(days=1)
        today = get_today()
        api_helper.calculate_concurrent(yesterday, today, self.user1.id)
        fake_header = util_helper.get_identity_auth_header(
            account_number=self.user1.username,
            is_org_admin=False,
        )

        client = APIClient()
        client.credentials(
            HTTP_X_RH_IDENTITY=util_helper.get_internal_identity_auth_header(),
            HTTP_X_RH_INTERNAL_FAKE_IDENTITY=fake_header,
        )
        response = client.get(self.concurrent_api_url, data={}, format="json")
        body = response.json()
        self.assertEqual(response.status_code, 403)
        self.assertEqual(body["detail"], "User must be an org admin.")
    def test_daily_pagination(self):
        """
        Test proper pagination handling of days from the custom queryset.

        This test asserts that the pagination envelope is correctly populated
        and that the included list is populated with the expected dates with
        calculated concurrency values.

        We ask for 31 days worth of concurrency here, but default pagination
        should limit the response to the first 10 days.

        One instance run exist in the first day of this period. All other days
        have no activity. Therefore, only that first day should have non-zero
        values for instances; all other days should have 0s.
        """
        api_helper.generate_single_run(
            self.instance1,
            (
                util_helper.utc_dt(2019, 3, 15, 1, 0, 0),
                util_helper.utc_dt(2019, 3, 15, 2, 0, 0),
            ),
            image=self.instance1.machine_image,
            instance_type=self.instance_type1,
        )
        api_helper.generate_single_run(
            self.instance1,
            (
                util_helper.utc_dt(2019, 3, 16, 1, 0, 0),
                util_helper.utc_dt(2019, 3, 16, 2, 0, 0),
            ),
            image=self.instance1.machine_image,
            instance_type=self.instance_type1,
        )
        api_helper.generate_single_run(
            self.instance2,
            (
                util_helper.utc_dt(2019, 3, 16, 1, 0, 0),
                util_helper.utc_dt(2019, 3, 17, 2, 0, 0),
            ),
            image=self.instance2.machine_image,
            instance_type=self.instance_type1,
        )

        start_date = datetime.date(2019, 3, 15)
        end_date = datetime.date(2019, 4, 15)

        api_helper.calculate_concurrent(start_date, end_date, self.user1.id)
        data = {
            "start_date": start_date.strftime("%Y-%m-%d"),
            "end_date": end_date.strftime("%Y-%m-%d"),
        }
        client = APIClient()
        client.force_authenticate(user=self.user1)
        response = client.get(self.concurrent_api_url,
                              data=data,
                              format="json")
        body = response.json()

        self.assertEquals(body["meta"]["count"], 31)
        self.assertEquals(len(body["data"]), 10)

        link_first = body["links"]["first"]
        self.assertIn("offset=0", link_first)
        self.assertIn("start_date=2019-03-15", link_first)
        self.assertIn("end_date=2019-04-15", link_first)

        link_next = body["links"]["next"]
        self.assertIn("offset=10", link_next)
        self.assertIn("start_date=2019-03-15", link_next)
        self.assertIn("end_date=2019-04-15", link_next)

        self.assertIsNone(body["links"]["previous"])

        link_last = body["links"]["last"]
        self.assertIn("offset=21", link_last)
        self.assertIn("start_date=2019-03-15", link_last)
        self.assertIn("end_date=2019-04-15", link_last)

        first_date = datetime.date(2019, 3, 15)
        first_result = body["data"][0]

        self.assertEqual(first_result["maximum_counts"][0]["instances_count"],
                         1)
        self.assertEqual(first_result["date"], str(first_date))

        second_date = datetime.date(2019, 3, 16)
        second_result = body["data"][1]

        self.assertEqual(second_result["maximum_counts"][0]["instances_count"],
                         2)
        self.assertEqual(second_result["date"], str(second_date))

        third_date = datetime.date(2019, 3, 17)
        third_result = body["data"][2]

        self.assertEqual(third_result["maximum_counts"][0]["instances_count"],
                         1)
        self.assertEqual(third_result["date"], str(third_date))

        # assert that every other day exists with zero reported concurrency.
        for offset, result in enumerate(body["data"][3:]):
            this_date = third_date + datetime.timedelta(days=offset + 1)
            self.assertEqual(result["maximum_counts"], [])
            self.assertEqual(result["date"], str(this_date))