-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(db-utils): fix batch_delete function (#6285)
Co-authored-by: Víctor Fernández Poyatos <victor@prowler.com>
- Loading branch information
1 parent
7022b7b
commit c656cf8
Showing
5 changed files
with
149 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
api/src/backend/api/migrations/0005_update_provider_unique_constraint_with_is_deleted.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Generated by Django 5.1.1 on 2024-12-20 13:16 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("api", "0004_rbac_missing_admin_roles"), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveConstraint( | ||
model_name="provider", | ||
name="unique_provider_uids", | ||
), | ||
migrations.AddConstraint( | ||
model_name="provider", | ||
constraint=models.UniqueConstraint( | ||
fields=("tenant_id", "provider", "uid", "is_deleted"), | ||
name="unique_provider_uids", | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
from unittest.mock import Mock, patch | ||
|
||
import pytest | ||
from conftest import get_api_tokens, get_authorization_header | ||
from django.urls import reverse | ||
from rest_framework.test import APIClient | ||
|
||
from api.models import Provider | ||
|
||
|
||
@patch("api.v1.views.Task.objects.get") | ||
@patch("api.v1.views.delete_provider_task.delay") | ||
@pytest.mark.django_db | ||
def test_delete_provider_without_executing_task( | ||
mock_delete_task, mock_task_get, create_test_user, tenants_fixture, tasks_fixture | ||
): | ||
client = APIClient() | ||
|
||
test_user = "test_email@prowler.com" | ||
test_password = "test_password" | ||
|
||
prowler_task = tasks_fixture[0] | ||
task_mock = Mock() | ||
task_mock.id = prowler_task.id | ||
mock_delete_task.return_value = task_mock | ||
mock_task_get.return_value = prowler_task | ||
|
||
user_creation_response = client.post( | ||
reverse("user-list"), | ||
data={ | ||
"data": { | ||
"type": "users", | ||
"attributes": { | ||
"name": "test", | ||
"email": test_user, | ||
"password": test_password, | ||
}, | ||
} | ||
}, | ||
format="vnd.api+json", | ||
) | ||
assert user_creation_response.status_code == 201 | ||
|
||
access_token, _ = get_api_tokens(client, test_user, test_password) | ||
auth_headers = get_authorization_header(access_token) | ||
|
||
create_provider_response = client.post( | ||
reverse("provider-list"), | ||
data={ | ||
"data": { | ||
"type": "providers", | ||
"attributes": { | ||
"provider": Provider.ProviderChoices.AWS, | ||
"uid": "123456789012", | ||
}, | ||
} | ||
}, | ||
format="vnd.api+json", | ||
headers=auth_headers, | ||
) | ||
assert create_provider_response.status_code == 201 | ||
provider_id = create_provider_response.json()["data"]["id"] | ||
provider_uid = create_provider_response.json()["data"]["attributes"]["uid"] | ||
|
||
remove_provider = client.delete( | ||
reverse("provider-detail", kwargs={"pk": provider_id}), | ||
headers=auth_headers, | ||
) | ||
assert remove_provider.status_code == 202 | ||
|
||
recreate_provider_response = client.post( | ||
reverse("provider-list"), | ||
data={ | ||
"data": { | ||
"type": "providers", | ||
"attributes": { | ||
"provider": Provider.ProviderChoices.AWS, | ||
"uid": provider_uid, | ||
}, | ||
} | ||
}, | ||
format="vnd.api+json", | ||
headers=auth_headers, | ||
) | ||
assert recreate_provider_response.status_code == 201 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters