def test_update_cloudtype_fails(self): """Test updating cloud_type returns validation error.""" class MockCloudAccountSerializer(serializers.CloudAccountSerializer): cloud_type = serializers.ChoiceField(choices=["aws", "bad_cloud"], required=True) with patch("internal.views.InternalAccountViewSet.get_serializer_class" ) as mock_viewset_serializer: mock_viewset_serializer.return_value = MockCloudAccountSerializer data = { "cloud_type": "bad_cloud", "name": self.faker.bs()[:256], } account_id = self.account4.id request = self.factory.patch("/accounts/", data=data) force_authenticate(request, user=self.user2) view = InternalAccountViewSet.as_view( actions={"patch": "partial_update"}) response = view(request, pk=account_id) expected_error = "You cannot update field cloud_type." self.assertEqual(response.status_code, 400) self.assertEqual(expected_error, response.data["cloud_type"][0])
def test_create_account_without_name_fail(self): """Test create account without a name fails.""" data = util_helper.generate_dummy_aws_cloud_account_post_data() del data["name"] request = self.factory.post("/accounts/", data=data) force_authenticate(request, user=self.user2) view = InternalAccountViewSet.as_view(actions={"post": "create"}) response = view(request) self.assertEqual(response.status_code, 400) self.assertIn("name", response.data)
def test_create_with_malformed_arn_fails(self): """Test create account with malformed arn returns validation error.""" data = util_helper.generate_dummy_aws_cloud_account_post_data() data["account_arn"] = self.faker.bs() request = self.factory.post("/accounts/", data=data) force_authenticate(request, user=self.user2) view = InternalAccountViewSet.as_view(actions={"post": "create"}) response = view(request) self.assertEqual(response.status_code, 400) self.assertIn("account_arn", response.data)
def test_update_account_patch_arn_fails(self): """Test that updating to change the arn fails.""" data = { "cloud_type": "aws", "account_arn": util_helper.generate_dummy_arn(), } account_id = self.account4.id request = self.factory.patch("/accounts/", data=data) force_authenticate(request, user=self.user2) view = InternalAccountViewSet.as_view( actions={"patch": "partial_update"}) response = view(request, pk=account_id) self.assertEqual(response.status_code, 400)
def test_update_account_patch_duplicate_name_fail(self): """Test updating an account with a duplicate name fails.""" data = { "cloud_type": "aws", "name": "unique", } account_id = self.account3.id request = self.factory.patch("/accounts/", data=data) force_authenticate(request, user=self.user2) view = InternalAccountViewSet.as_view( actions={"patch": "partial_update"}) response = view(request, pk=account_id) self.assertEqual(response.status_code, 400) self.assertIn("name", response.data)
def test_update_account_patch_name_success(self): """Test updating an account with a name succeeds.""" data = { "cloud_type": "aws", "name": self.faker.bs()[:256], } account_id = self.account4.id request = self.factory.patch("/accounts/", data=data) force_authenticate(request, user=self.user2) view = InternalAccountViewSet.as_view( actions={"patch": "partial_update"}) response = view(request, pk=account_id) self.assertEqual(response.status_code, 200) self.assertEqual(data["name"], response.data["name"])
def test_create_account_with_duplicate_name_fail(self, mock_aws, mock_task): """Test create account with a duplicate name fails.""" mock_aws.verify_account_access.return_value = True, [] data = util_helper.generate_dummy_aws_cloud_account_post_data() data["name"] = "unique" # should collide with self.account5 request = self.factory.post("/accounts/", data=data) force_authenticate(request, user=self.user2) view = InternalAccountViewSet.as_view(actions={"post": "create"}) response = view(request) self.assertEqual(response.status_code, 400) self.assertIn("name", response.data) mock_task.delay.assert_not_called()
def test_create_azure_account_with_name_success(self, mock_enable): """Test creating an azure account succeeds.""" data = util_helper.generate_dummy_azure_cloud_account_post_data() request = self.factory.post("/accounts/", data=data) force_authenticate(request, user=self.user2) view = InternalAccountViewSet.as_view(actions={"post": "create"}) response = view(request) self.assertEqual(response.status_code, 201) self.assertEqual(response.data["content_object"]["tenant_id"], str(data["tenant_id"])) self.assertEqual(response.data["name"], data["name"]) self.assertIsNotNone(response.data["name"]) self.assertEqual(response.data["is_enabled"], True) # True by default mock_enable.assert_called()
def get_account_delete_response(self, user, account_id): """ Generate a response for a delete-destroy on the InternalAccountViewSet. Args: user (User): Django auth user performing the request account_id (int): the id of the account to retrieve Returns: Response: the generated response for this request """ request = self.factory.delete(self.path) force_authenticate(request, user=user) view = InternalAccountViewSet.as_view(actions={"delete": "destroy"}) response = view(request, pk=account_id) return response
def get_account_list_response(self, user, data=None): """ Generate a response for a get-list on the AccountViewSet. Args: user (User): Django auth user performing the request data (dict): optional data to use as query params Returns: Response: the generated response for this request """ request = self.factory.get(self.path, data) force_authenticate(request, user=user) view = InternalAccountViewSet.as_view(actions={"get": "list"}) response = view(request) return response
def get_account_get_response(self, user, account_id): """ Generate a response for a get-retrieve on the InternalAccountViewSet. Args: user (User): Django auth user performing the request account_id (int): the id of the account to retrieve data (dict): optional data to use as query params Returns: Response: the generated response for this request """ request = self.factory.get(self.path) force_authenticate(request, user=user) view = InternalAccountViewSet.as_view(actions={"get": "retrieve"}) response = view(request, pk=account_id) return response