Exemple #1
0
 def setUp(self):
     """Set up data for tests."""
     self.account_number_no_user = str(_faker.pyint())
     self.rh_header_as_admin_no_user = util_helper.get_identity_auth_header(
         account_number=self.account_number_no_user)
     self.rh_header_no_account_number = util_helper.get_identity_auth_header(
         account_number=None)
     self.auth_class = IdentityHeaderAuthenticationUserNotRequired()
Exemple #2
0
    def setUp(self):
        """Set up data for tests."""
        self.account_number = str(_faker.pyint())
        self.account_number_not_found = str(_faker.pyint())

        self.user = util_helper.generate_test_user(self.account_number)
        self.rh_header_as_admin = util_helper.get_identity_auth_header(
            account_number=self.account_number)
        self.rh_header_not_admin = util_helper.get_identity_auth_header(
            account_number=self.account_number, is_org_admin=False)
        self.rh_header_as_admin_not_found = util_helper.get_identity_auth_header(
            account_number=self.account_number_not_found)
        self.auth_class = IdentityHeaderAuthenticationInternal()
Exemple #3
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))
Exemple #4
0
 def test_authenticate_with_invalid_account_number_and_not_org_admin_fake_header(
     self, ):
     """Test authentication with invalid account_number and org admin fails."""
     request = Mock()
     fake_header = util_helper.get_identity_auth_header(
         account_number=str(_faker.pyint()),
         is_org_admin=False,
     )
     request.META = {
         settings.INSIGHTS_IDENTITY_HEADER: self.internal_rh_header,
         settings.INSIGHTS_INTERNAL_FAKE_IDENTITY_HEADER: fake_header,
     }
     with self.assertRaises(exceptions.PermissionDenied) as e:
         self.auth_class.authenticate(request)
     self.assertIn("User must be an org admin.", str(e.exception))
Exemple #5
0
    def test_authenticate_with_valid_account_number_and_org_admin_headers(
            self):
        """Test authentication with valid account_number and org admin succeeds."""
        request = Mock()
        fake_header = util_helper.get_identity_auth_header(
            account_number=self.account_number,
            is_org_admin=True,
        )
        request.META = {
            settings.INSIGHTS_IDENTITY_HEADER: self.internal_rh_header,
            settings.INSIGHTS_INTERNAL_FAKE_IDENTITY_HEADER: fake_header,
        }

        user, auth = self.auth_class.authenticate(request)

        self.assertTrue(auth)
        self.assertEqual(self.account_number, user.username)
Exemple #6
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.")
Exemple #7
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.")
Exemple #8
0
    def gather_api_responses(self):
        """
        Call the API and collect all the responses to be output.

        Returns:
            dict: All of the API responses.

        """
        responses = dict()

        ########################
        # V2 endpoints
        responses["v2_rh_identity"] = util_helper.RH_IDENTITY_ORG_ADMIN
        # convert from binary string to string.
        responses["v2_header"] = util_helper.get_identity_auth_header().decode(
            "utf-8")

        #######################
        # Report Commands
        # IMPORTANT NOTE!
        # These requests must come FIRST since they rely on calculated concurrent usage
        # data that might be clobbered by other subsequent operations.

        # Daily Max Concurrency
        response = self.customer_client.list_concurrent(
            data={"start_date": self.last_week.date()})
        assert_status(response, 200)
        responses["v2_list_concurrent"] = response

        response = self.customer_client.list_concurrent(
            data={
                "start_date": self.this_morning.date(),
                "end_date": self.next_week.date(),
            })
        assert_status(response, 400)
        responses["v2_list_concurrent_partial_future"] = response

        response = self.customer_client.list_concurrent(
            data={"start_date": self.last_month.date()})
        assert_status(response, 425)
        responses["v2_list_concurrent_too_early"] = response
        # IMPORTANT NOTE FOR THIS SCRIPT!
        # Because that request raised a 425 and would *normally* trigger a rollback,
        # we need to squash that rollback here and let the transaction continue.
        transaction.set_rollback(False)

        response = self.customer_client.list_concurrent(
            data={
                "start_date": self.tomorrow.date(),
                "end_date": self.next_week.date(),
            })
        assert_status(response, 400)
        responses["v2_list_concurrent_all_future"] = response

        response = self.customer_client.list_concurrent(
            data={
                "start_date": self.customer_user.date_joined -
                timedelta(days=14),
                "end_date": self.customer_user.date_joined - timedelta(days=7),
            })
        assert_status(response, 400)
        responses["v2_list_concurrent_past_end_date"] = response

        ##########################
        # v2 Customer Account Info

        # List all accounts
        response = self.customer_client.list_accounts()
        assert_status(response, 200)
        responses["v2_account_list"] = response

        # Retrieve a specific account
        response = self.customer_client.get_accounts(
            self.aws_customer_account.id)
        assert_status(response, 200)
        responses["v2_account_get"] = response

        ##################
        # V2 Instance Info

        # List all instances
        response = self.customer_client.list_instances()
        assert_status(response, 200)
        responses["v2_instance_list"] = response

        # Retrieve a specific instance
        response = self.customer_client.get_instances(
            self.customer_instances[0].id)
        assert_status(response, 200)
        responses["v2_instance_get"] = response

        # Filtering instances on running
        response = self.customer_client.list_instances(
            data={"running_since": self.now})
        assert_status(response, 200)
        responses["v2_instance_filter_running"] = response

        #######################
        # V2 Machine Image Info

        # List all images
        response = self.customer_client.list_images()
        assert_status(response, 200)
        responses["v2_list_images"] = response

        # Retrieve a specific image
        response = self.customer_client.get_images(self.images[0].id)
        assert_status(response, 200)
        responses["v2_get_image"] = response

        ########################
        # V2 Miscellaneous Commands
        # Retrieve current cloud account ids used by the application
        cloudigrade_version = ("489-cloudigrade-version - "
                               "d2b30c637ce3788e22990b21434bac2edcfb7ede")

        with override_settings(CLOUDIGRADE_VERSION=cloudigrade_version):
            response = self.customer_client.get_sysconfig()
        assert_status(response, 200)
        responses["v2_get_sysconfig"] = response

        return responses
Exemple #9
0
 def setUp(self):
     """Set up data for tests."""
     self.account_number_not_found = str(_faker.pyint())
     self.rh_header_as_admin_not_found = util_helper.get_identity_auth_header(
         account_number=self.account_number_not_found)
     self.auth_class = IdentityHeaderAuthenticationInternalCreateUser()
Exemple #10
0
 def _force_authenticate(self, user):
     """Force client authentication as the given user."""
     self.authenticated_user = user
     self.client.credentials(
         HTTP_X_RH_IDENTITY=helper.get_identity_auth_header(user.username))