Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check to make sure only one AWX token exists #331

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/aap_eda/api/exceptions.py
Expand Up @@ -29,6 +29,7 @@
"Conflict",
"Unprocessable",
"PermissionDenied",
"TooManyControllerTokens",
)


Expand Down Expand Up @@ -70,7 +71,7 @@ class NoControllerToken(APIException):
class TooManyControllerTokens(APIException):
status_code = 422
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use this status.HTTP_422_UNPROCESSABLE_ENTITY instead of just hard-coded integer? Or we could inherit from Unprocessable class on line 48 and customize detail field since they fall under the same error code. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree

default_detail = (
"More than one controller token found, "
"Found an existing controller token, "
"currently only 1 token is supported"
)

Expand Down
21 changes: 13 additions & 8 deletions src/aap_eda/api/views/user.py
Expand Up @@ -24,7 +24,7 @@
from rest_framework.viewsets import GenericViewSet

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

from .mixins import (
Expand Down Expand Up @@ -156,14 +156,19 @@ def get_queryset(self):

def perform_create(self, serializer):
try:
tokens = models.AwxToken.objects.filter(
user_id=self.request.user.id
).count()

if tokens > 0:
raise TooManyControllerTokens

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

except django.db.utils.IntegrityError as ie:
raise Conflict(str(ie))
except TooManyControllerTokens as e:
raise TooManyControllerTokens(e)


@extend_schema_view(
Expand Down
21 changes: 0 additions & 21 deletions tests/integration/api/test_activation.py
Expand Up @@ -459,24 +459,3 @@ def test_create_activation_no_token(client: APIClient):
response = client.post(f"{api_url_v1}/activations/", data=test_activation)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
assert str(response.data["detail"]) == "No controller token specified"


@pytest.mark.django_db
def test_create_activation_more_tokens(client: APIClient):
fks = create_activation_related_data()
test_activation = TEST_ACTIVATION.copy()
test_activation["is_enabled"] = True
test_activation["decision_environment_id"] = fks["decision_environment_id"]
test_activation["project_id"] = fks["project_id"]
test_activation["rulebook_id"] = fks["rulebook_id"]
test_activation["extra_var_id"] = fks["extra_var_id"]

client.post(f"{api_url_v1}/users/me/awx-tokens/", data=TEST_AWX_TOKEN)
client.post(f"{api_url_v1}/users/me/awx-tokens/", data=TEST_AWX_TOKEN_2)
response = client.post(f"{api_url_v1}/activations/", data=test_activation)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
assert (
str(response.data["detail"])
== "More than one controller token found, "
"currently only 1 token is supported"
)
22 changes: 4 additions & 18 deletions tests/integration/api/test_awx_token.py
Expand Up @@ -67,21 +67,6 @@ def test_create_controller_token(client: APIClient, user: models.User):
row = cursor.fetchone()
assert row[0].startswith("$encrypted$fernet-256$")

response = client.post(
f"{api_url_v1}/users/me/awx-tokens/",
data={
"name": "Test token 2",
"description": "Token description",
"token": "test-token-value",
},
)
assert response.status_code == status.HTTP_201_CREATED
data = response.json()
assert data["name"] == "Test token 2"
assert data["description"] == "Token description"
assert data["user_id"] == user.id
assert "token" not in data


@pytest.mark.django_db
def test_create_token_missing_field(client: APIClient, user: models.User):
Expand Down Expand Up @@ -109,7 +94,7 @@ def test_create_token_missing_field(client: APIClient, user: models.User):


@pytest.mark.django_db(transaction=True)
def test_create_token_duplicate_name(client: APIClient, user: models.User):
def test_create_token_to_many_tokens(client: APIClient, user: models.User):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly a typo: _to_many_token ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

likely :)

models.AwxToken.objects.create(
user=user, name="test-token", token="test-token-value"
)
Expand All @@ -121,9 +106,10 @@ def test_create_token_duplicate_name(client: APIClient, user: models.User):
"token": "test-token-value",
},
)
assert response.status_code == status.HTTP_409_CONFLICT
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
assert response.json() == {
"detail": "Token with this name already exists."
"detail": "Found an existing controller token, "
"currently only 1 token is supported"
}


Expand Down