Skip to content

Commit

Permalink
chore: Convert dynatrace integration views TestCase to normal test fu…
Browse files Browse the repository at this point in the history
…nction (#3289)
  • Loading branch information
zachaysan authored Jan 15, 2024
1 parent dd8226b commit f88f334
Showing 1 changed file with 135 additions and 131 deletions.
266 changes: 135 additions & 131 deletions api/tests/unit/integrations/dynatrace/test_unit_dynatrace_views.py
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()

3 comments on commit f88f334

@vercel
Copy link

@vercel vercel bot commented on f88f334 Jan 15, 2024

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

@vercel
Copy link

@vercel vercel bot commented on f88f334 Jan 15, 2024

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on f88f334 Jan 15, 2024

Choose a reason for hiding this comment

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

Please sign in to comment.