Example #1
0
 def test_race_condition_customer(self):
     """Test case where another request may create the customer in a race condition."""
     customer = self._create_customer_data()
     account_id = customer["account_id"]
     orig_cust = IdentityHeaderMiddleware.create_customer(account_id)
     dup_cust = IdentityHeaderMiddleware.create_customer(account_id)
     self.assertEqual(orig_cust, dup_cust)
Example #2
0
    def _create_context(self):
        """Create request context object."""
        user = None
        customer = None
        encoded_auth_header = self._identity_header.get("x-rh-identity")
        if encoded_auth_header:
            identity = json.loads(b64decode(encoded_auth_header))
            account = identity.get("identity", {}).get("account_number")
            username = identity.get("identity", {}).get("user",
                                                        {}).get("username")
            email = identity.get("identity", {}).get("user", {}).get("email")
            identity_type = identity.get("identity", {}).get("type", "User")
            auth_type = identity.get("identity", {}).get("auth_type")

            if identity_type == "System" and auth_type == "uhc-auth":
                username = identity.get("identity",
                                        {}).get("system", {}).get("cluster_id")
                email = ""

            try:
                customer = Customer.objects.filter(account_id=account).get()
            except Customer.DoesNotExist:
                customer = IdentityHeaderMiddleware.create_customer(account)
            try:
                user = User.objects.get(username=username)
            except User.DoesNotExist:
                user = IdentityHeaderMiddleware.create_user(
                    username, email, customer, None)

        context = {"user": user, "customer": customer}
        return context, customer, user
Example #3
0
    def test_source_list_other_header(self):
        """Test the LIST endpoint with other auth header not matching test data."""
        user_data = self._create_user_data()
        other_account = "10002"
        customer = self._create_customer_data(account=other_account)
        IdentityHeaderMiddleware.create_customer(other_account)
        request_context = self._create_request_context(customer, user_data, create_customer=True, is_admin=True)
        with requests_mock.mock() as m:
            m.get("http://www.sourcesclient.com/api/v1/sources/", status_code=200)

            url = reverse("sources-list")

            response = self.client.get(url, content_type="application/json", **request_context["request"].META)
            body = response.json()

            self.assertEqual(response.status_code, 200)
            self.assertEqual(body.get("meta").get("count"), 0)
Example #4
0
    def test_source_get_other_header(self):
        """Test the GET endpoint other header not matching test data."""
        user_data = self._create_user_data()
        other_account = "10002"
        customer = self._create_customer_data(account=other_account)
        IdentityHeaderMiddleware.create_customer(other_account)

        request_context = self._create_request_context(customer,
                                                       user_data,
                                                       create_customer=True,
                                                       is_admin=True)
        with requests_mock.mock() as m:
            m.get(
                f"http://www.sourcesclient.com/api/v1/sources/{self.test_source_id}/",
                status_code=200,
                headers={"Content-Type": "application/json"},
            )

            url = reverse("sources-detail", kwargs={"pk": self.test_source_id})

            response = self.client.get(url,
                                       content_type="application/json",
                                       **request_context["request"].META)
            self.assertEqual(response.status_code, 404)
Example #5
0
 def setUpClass(cls):
     """Set up the test class."""
     super().setUpClass()
     post_save.disconnect(storage_callback, sender=Sources)
     account = "12345"
     IdentityHeaderMiddleware.create_customer(account)
 def setUpClass(cls):
     """Set up the test class."""
     super().setUpClass()
     account = "12345"
     IdentityHeaderMiddleware.create_customer(account)