-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Convert dynatrace integration views TestCase to normal test fu…
…nction (#3289)
- Loading branch information
Showing
1 changed file
with
135 additions
and
131 deletions.
There are no files selected for viewing
266 changes: 135 additions & 131 deletions
266
api/tests/unit/integrations/dynatrace/test_unit_dynatrace_views.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 |
---|---|---|
@@ -1,142 +1,146 @@ | ||
import json | ||
from unittest.case import TestCase | ||
|
||
import pytest | ||
from django.urls import reverse | ||
from rest_framework import status | ||
from rest_framework.test import APIClient | ||
|
||
from environments.models import Environment | ||
from integrations.dynatrace.models import DynatraceConfiguration | ||
from organisations.models import Organisation, OrganisationRole | ||
from projects.models import Project | ||
from util.tests import Helper | ||
|
||
|
||
@pytest.mark.django_db | ||
class DynatraceConfigurationTestCase(TestCase): | ||
def setUp(self): | ||
self.client = APIClient() | ||
user = Helper.create_ffadminuser() | ||
self.client.force_authenticate(user=user) | ||
|
||
self.organisation = Organisation.objects.create(name="Test Org") | ||
user.add_organisation( | ||
self.organisation, OrganisationRole.ADMIN | ||
) # admin to bypass perms | ||
|
||
self.project = Project.objects.create( | ||
name="Test project", organisation=self.organisation | ||
) | ||
self.environment = Environment.objects.create( | ||
name="Test Environment", project=self.project | ||
) | ||
self.list_url = reverse( | ||
"api-v1:environments:integrations-dynatrace-list", | ||
args=[self.environment.api_key], | ||
) | ||
|
||
def test_should_create_dynatrace_config_when_post(self): | ||
# Given setup data | ||
data = { | ||
"base_url": "http://test.com", | ||
"api_key": "abc-123", | ||
"entity_selector": "type(APPLICATION),entityName(docs)", | ||
} | ||
|
||
# When | ||
response = self.client.post( | ||
self.list_url, | ||
data=json.dumps(data), | ||
content_type="application/json", | ||
) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_201_CREATED | ||
# and | ||
assert ( | ||
DynatraceConfiguration.objects.filter(environment=self.environment).count() | ||
== 1 | ||
) | ||
|
||
def test_should_return_BadRequest_when_duplicate_dynatrace_config_is_posted(self): | ||
# Given | ||
DynatraceConfiguration.objects.create( | ||
base_url="http://test.com", api_key="api_123", environment=self.environment | ||
) | ||
data = { | ||
"base_url": "http://test.com", | ||
"api_key": "abc-123", | ||
"entity_selector": "type(APPLICATION),entityName(docs)", | ||
} | ||
|
||
# When | ||
response = self.client.post( | ||
self.list_url, | ||
data=json.dumps(data), | ||
content_type="application/json", | ||
) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_400_BAD_REQUEST | ||
assert ( | ||
DynatraceConfiguration.objects.filter(environment=self.environment).count() | ||
== 1 | ||
) | ||
|
||
# | ||
def test_should_update_configuration_when_put(self): | ||
# Given | ||
config = DynatraceConfiguration.objects.create( | ||
base_url="http://test.com", api_key="api_123", environment=self.environment | ||
) | ||
api_key_updated = "new api" | ||
data = { | ||
"base_url": "http://test.com", | ||
"api_key": "new api", | ||
"entity_selector": "type(APPLICATION),entityName(docs)", | ||
def test_should_create_dynatrace_config_when_post( | ||
admin_client: APIClient, | ||
environment: Environment, | ||
) -> None: | ||
# Given setup data | ||
data = { | ||
"base_url": "http://test.com", | ||
"api_key": "abc-123", | ||
"entity_selector": "type(APPLICATION),entityName(docs)", | ||
} | ||
url = reverse( | ||
"api-v1:environments:integrations-dynatrace-list", | ||
args=[environment.api_key], | ||
) | ||
|
||
# When | ||
response = admin_client.post( | ||
url, | ||
data=json.dumps(data), | ||
content_type="application/json", | ||
) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_201_CREATED | ||
# and | ||
assert DynatraceConfiguration.objects.filter(environment=environment).count() == 1 | ||
|
||
|
||
def test_should_return_400_when_duplicate_dynatrace_config_is_posted( | ||
admin_client: APIClient, | ||
environment: Environment, | ||
) -> None: | ||
# Given | ||
DynatraceConfiguration.objects.create( | ||
base_url="http://test.com", api_key="api_123", environment=environment | ||
) | ||
data = { | ||
"base_url": "http://test.com", | ||
"api_key": "abc-123", | ||
"entity_selector": "type(APPLICATION),entityName(docs)", | ||
} | ||
url = reverse( | ||
"api-v1:environments:integrations-dynatrace-list", | ||
args=[environment.api_key], | ||
) | ||
|
||
# When | ||
response = admin_client.post( | ||
url, | ||
data=json.dumps(data), | ||
content_type="application/json", | ||
) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_400_BAD_REQUEST | ||
assert DynatraceConfiguration.objects.filter(environment=environment).count() == 1 | ||
|
||
|
||
def test_should_update_configuration_when_put( | ||
admin_client: APIClient, | ||
environment: Environment, | ||
) -> None: | ||
# Given | ||
config = DynatraceConfiguration.objects.create( | ||
base_url="http://test.com", api_key="api_123", environment=environment | ||
) | ||
api_key_updated = "new api" | ||
data = { | ||
"base_url": "http://test.com", | ||
"api_key": "new api", | ||
"entity_selector": "type(APPLICATION),entityName(docs)", | ||
} | ||
|
||
# When | ||
url = reverse( | ||
"api-v1:environments:integrations-dynatrace-detail", | ||
args=[environment.api_key, config.id], | ||
) | ||
response = admin_client.put( | ||
url, | ||
data=json.dumps(data), | ||
content_type="application/json", | ||
) | ||
config.refresh_from_db() | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_200_OK | ||
assert config.api_key == api_key_updated | ||
|
||
|
||
def test_should_return_dynatrace_config_list_when_requested( | ||
admin_client: APIClient, | ||
environment: Environment, | ||
) -> None: | ||
# Given | ||
config = DynatraceConfiguration.objects.create( | ||
base_url="http://test.com", api_key="api_123", environment=environment | ||
) | ||
url = reverse( | ||
"api-v1:environments:integrations-dynatrace-list", | ||
args=[environment.api_key], | ||
) | ||
|
||
# When | ||
response = admin_client.get(url) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_200_OK | ||
assert response.data == [ | ||
{ | ||
"api_key": config.api_key, | ||
"base_url": config.base_url, | ||
"entity_selector": "", | ||
"id": config.id, | ||
} | ||
|
||
# When | ||
url = reverse( | ||
"api-v1:environments:integrations-dynatrace-detail", | ||
args=[self.environment.api_key, config.id], | ||
) | ||
response = self.client.put( | ||
url, | ||
data=json.dumps(data), | ||
content_type="application/json", | ||
) | ||
config.refresh_from_db() | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_200_OK | ||
assert config.api_key == api_key_updated | ||
|
||
def test_should_return_dynatrace_config_list_when_requested(self): | ||
# Given - set up data | ||
|
||
# When | ||
response = self.client.get(self.list_url) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_200_OK | ||
|
||
def test_should_remove_configuration_when_delete(self): | ||
# Given | ||
config = DynatraceConfiguration.objects.create( | ||
base_url="http://test.com", api_key="api_123", environment=self.environment | ||
) | ||
# When | ||
url = reverse( | ||
"api-v1:environments:integrations-dynatrace-detail", | ||
args=[self.environment.api_key, config.id], | ||
) | ||
res = self.client.delete(url) | ||
|
||
# Then | ||
assert res.status_code == status.HTTP_204_NO_CONTENT | ||
# and | ||
assert not DynatraceConfiguration.objects.filter( | ||
environment=self.environment | ||
).exists() | ||
] | ||
|
||
|
||
def test_should_remove_configuration_when_delete( | ||
admin_client: APIClient, | ||
environment: Environment, | ||
) -> None: | ||
# Given | ||
config = DynatraceConfiguration.objects.create( | ||
base_url="http://test.com", api_key="api_123", environment=environment | ||
) | ||
# When | ||
url = reverse( | ||
"api-v1:environments:integrations-dynatrace-detail", | ||
args=[environment.api_key, config.id], | ||
) | ||
response = admin_client.delete(url) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_204_NO_CONTENT | ||
assert not DynatraceConfiguration.objects.filter(environment=environment).exists() |
f88f334
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docs – ./docs
docs-flagsmith.vercel.app
docs.flagsmith.com
docs-git-main-flagsmith.vercel.app
docs.bullet-train.io
f88f334
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
flagsmith-frontend-preview – ./frontend
flagsmith-frontend-preview-git-main-flagsmith.vercel.app
flagsmith-frontend-production-native.vercel.app
flagsmith-frontend-preview-flagsmith.vercel.app
f88f334
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
flagsmith-frontend-staging – ./frontend
flagsmith-frontend-staging-git-main-flagsmith.vercel.app
staging.flagsmith.com
flagsmith-staging-frontend.vercel.app
flagsmith-frontend-staging-flagsmith.vercel.app