Skip to content

Commit

Permalink
Refactor & fix integration tests (#766)
Browse files Browse the repository at this point in the history
* refactor existing integration tests to use general fixtures
* fix tests for RBAC
  • Loading branch information
Dostonbek1 authored Mar 27, 2024
1 parent fbbbd57 commit c624517
Show file tree
Hide file tree
Showing 20 changed files with 1,247 additions and 1,498 deletions.
13 changes: 11 additions & 2 deletions src/aap_eda/api/serializers/decision_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from rest_framework import serializers

from aap_eda.api.serializers.credential import CredentialRefSerializer
from aap_eda.api.serializers.organization import OrganizationRefSerializer
from aap_eda.core import models


Expand Down Expand Up @@ -57,6 +58,7 @@ class DecisionEnvironmentReadSerializer(serializers.ModelSerializer):
"""Serializer for reading the DecisionEnvironment with embedded objects."""

credential = CredentialRefSerializer(required=False, allow_null=True)
organization = OrganizationRefSerializer()

class Meta:
model = models.DecisionEnvironment
Expand All @@ -66,7 +68,7 @@ class Meta:
"description",
"image_url",
"credential",
"organization_id",
"organization",
"created_at",
"modified_at",
]
Expand All @@ -78,13 +80,20 @@ def to_representation(self, decision_environment):
if decision_environment["credential"]
else None
)
organization = (
OrganizationRefSerializer(
decision_environment["organization"]
).data
if decision_environment["organization"]
else None
)
return {
"id": decision_environment["id"],
"name": decision_environment["name"],
"description": decision_environment["description"],
"image_url": decision_environment["image_url"],
"credential": credential,
"organization_id": decision_environment["organization_id"],
"organization": organization,
"created_at": decision_environment["created_at"],
"modified_at": decision_environment["modified_at"],
}
Expand Down
2 changes: 1 addition & 1 deletion src/aap_eda/api/serializers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ class ExtraVarSerializer(serializers.ModelSerializer):
extra_var = serializers.CharField(
required=True,
help_text="Content of the extra_var",
validators=[validators.is_extra_var_dict],
)

class Meta:
Expand All @@ -178,6 +177,7 @@ class ExtraVarCreateSerializer(serializers.ModelSerializer):
extra_var = serializers.CharField(
required=True,
help_text="Content of the extra_var",
validators=[validators.is_extra_var_dict],
)
organization_id = serializers.IntegerField(required=False, allow_null=True)

Expand Down
17 changes: 17 additions & 0 deletions src/aap_eda/api/serializers/user.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib.auth.hashers import make_password
from rest_framework import serializers

from aap_eda.api.exceptions import Conflict
from aap_eda.core import models

from .auth import RoleRefSerializer
Expand Down Expand Up @@ -131,3 +132,19 @@ class Meta:
"description",
"token",
]

def validate(self, data):
"""Validate the uniqueness of a combination of user and name fields."""
user = self.context["request"].user
name = data.get("name")

existing_token = models.AwxToken.objects.filter(user=user, name=name)

# If updating, exclude the current instance from the queryset
if self.instance:
existing_token = existing_token.exclude(pk=self.instance.pk)

if existing_token.exists():
raise Conflict("Token with this name already exists.")

return data
7 changes: 7 additions & 0 deletions src/aap_eda/api/views/decision_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ def retrieve(self, request, pk):
if decision_environment.data["credential_id"]
else None
)
decision_environment.data["organization"] = (
models.Organization.objects.get(
pk=decision_environment.data["organization_id"]
)
if decision_environment.data["organization_id"]
else None
)

return Response(
serializers.DecisionEnvironmentReadSerializer(
Expand Down
12 changes: 1 addition & 11 deletions src/aap_eda/api/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import django.db.utils
from django_filters.rest_framework import DjangoFilterBackend
from drf_spectacular.utils import (
OpenApiParameter,
Expand All @@ -25,7 +24,6 @@
from rest_framework.viewsets import GenericViewSet

from aap_eda.api import filters, serializers
from aap_eda.api.exceptions import Conflict
from aap_eda.core import models

from .mixins import (
Expand Down Expand Up @@ -156,15 +154,7 @@ def get_queryset(self):
)

def perform_create(self, serializer):
try:
serializer.save(user=self.request.user)
except django.db.utils.IntegrityError:
name_exists = models.AwxToken.objects.filter(
user=self.request.user, name=serializer.validated_data["name"]
).exists()
if name_exists:
raise Conflict("Token with this name already exists.")
raise Conflict
serializer.save(user=self.request.user)


@extend_schema_view(
Expand Down
Loading

0 comments on commit c624517

Please sign in to comment.