2024-08-23 07:22:54 -06:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
2024-08-28 13:32:24 -03:00
|
|
|
from apps.auth_token.exceptions import InvalidToken
|
2024-08-23 07:22:54 -06:00
|
|
|
from apps.grafana_plugin.helpers.gcom import check_gcom_permission
|
Update org creation to use DB uniqueness constraint (#4926)
Fix issue related to
[logs](https://ops.grafana-ops.net/explore?schemaVersion=1&panes=%7B%2257p%22:%7B%22datasource%22:%22000000193%22,%22queries%22:%5B%7B%22refId%22:%22A%22,%22expr%22:%22%7Bnamespace%3D%5C%22amixr-prod%5C%22,%20cluster%3D%5C%22prod-us-central-0%5C%22,%20job%3D%5C%22amixr-prod%2Famixr-engine%5C%22%7D%20%7C%3D%20%5C%22user_management_organization.user_management_organization_stack_id_org_id_727b4929_uniq%5C%22%22,%22queryType%22:%22range%22,%22datasource%22:%7B%22type%22:%22loki%22,%22uid%22:%22000000193%22%7D,%22editorMode%22:%22code%22%7D%5D,%22range%22:%7B%22from%22:%22now-2d%22,%22to%22:%22now%22%7D%7D%7D&orgId=1)
(check for existing org using the unique DB index)
2024-08-26 12:03:26 -03:00
|
|
|
from apps.user_management.models import Organization
|
2024-08-23 07:22:54 -06:00
|
|
|
|
|
|
|
|
|
2024-08-23 12:21:57 -06:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"api_token, api_token_updated",
|
|
|
|
|
[
|
|
|
|
|
("glsa_abcdefghijklmnopqrztuvwxyz", True),
|
|
|
|
|
("abcdefghijklmnopqrztuvwxyz", True),
|
|
|
|
|
("abc", False),
|
|
|
|
|
("", False),
|
|
|
|
|
("<no_value>", False),
|
|
|
|
|
(None, False),
|
|
|
|
|
(24, False),
|
|
|
|
|
],
|
|
|
|
|
)
|
2024-08-23 07:22:54 -06:00
|
|
|
@pytest.mark.django_db
|
2024-08-23 12:21:57 -06:00
|
|
|
def test_check_gcom_permission_updates_fields(make_organization, api_token, api_token_updated):
|
2024-08-23 07:22:54 -06:00
|
|
|
gcom_token = "gcom:test_token"
|
2024-08-23 12:21:57 -06:00
|
|
|
broken_token = "broken_token"
|
2024-08-23 07:22:54 -06:00
|
|
|
instance_info = {
|
|
|
|
|
"id": 324534,
|
|
|
|
|
"slug": "testinstance",
|
|
|
|
|
"url": "http://example.com",
|
|
|
|
|
"orgId": 5671,
|
|
|
|
|
"orgSlug": "testorg",
|
|
|
|
|
"orgName": "Test Org",
|
|
|
|
|
"regionSlug": "us",
|
|
|
|
|
"clusterSlug": "us-test",
|
|
|
|
|
}
|
|
|
|
|
context = {
|
|
|
|
|
"stack_id": str(instance_info["id"]),
|
|
|
|
|
"org_id": str(instance_info["orgId"]),
|
2024-08-23 12:21:57 -06:00
|
|
|
"grafana_token": api_token,
|
2024-08-23 07:22:54 -06:00
|
|
|
}
|
|
|
|
|
|
2024-08-23 12:21:57 -06:00
|
|
|
org = make_organization(stack_id=instance_info["id"], org_id=instance_info["orgId"], api_token=broken_token)
|
|
|
|
|
last_time_gcom_synced = org.gcom_token_org_last_time_synced
|
2024-08-23 07:22:54 -06:00
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
|
"apps.grafana_plugin.helpers.GcomAPIClient.get_instance_info",
|
|
|
|
|
return_value=instance_info,
|
|
|
|
|
) as mock_instance_info:
|
|
|
|
|
check_gcom_permission(gcom_token, context)
|
|
|
|
|
mock_instance_info.assert_called()
|
|
|
|
|
|
|
|
|
|
org.refresh_from_db()
|
|
|
|
|
assert org.stack_id == instance_info["id"]
|
|
|
|
|
assert org.stack_slug == instance_info["slug"]
|
|
|
|
|
assert org.grafana_url == instance_info["url"]
|
|
|
|
|
assert org.org_id == instance_info["orgId"]
|
|
|
|
|
assert org.org_slug == instance_info["orgSlug"]
|
|
|
|
|
assert org.org_title == instance_info["orgName"]
|
|
|
|
|
assert org.region_slug == instance_info["regionSlug"]
|
|
|
|
|
assert org.cluster_slug == instance_info["clusterSlug"]
|
2024-08-23 12:21:57 -06:00
|
|
|
assert org.api_token == api_token if api_token_updated else broken_token
|
2024-08-23 07:22:54 -06:00
|
|
|
assert org.gcom_token == gcom_token
|
2024-08-23 12:21:57 -06:00
|
|
|
assert org.gcom_token_org_last_time_synced != last_time_gcom_synced
|
Update org creation to use DB uniqueness constraint (#4926)
Fix issue related to
[logs](https://ops.grafana-ops.net/explore?schemaVersion=1&panes=%7B%2257p%22:%7B%22datasource%22:%22000000193%22,%22queries%22:%5B%7B%22refId%22:%22A%22,%22expr%22:%22%7Bnamespace%3D%5C%22amixr-prod%5C%22,%20cluster%3D%5C%22prod-us-central-0%5C%22,%20job%3D%5C%22amixr-prod%2Famixr-engine%5C%22%7D%20%7C%3D%20%5C%22user_management_organization.user_management_organization_stack_id_org_id_727b4929_uniq%5C%22%22,%22queryType%22:%22range%22,%22datasource%22:%7B%22type%22:%22loki%22,%22uid%22:%22000000193%22%7D,%22editorMode%22:%22code%22%7D%5D,%22range%22:%7B%22from%22:%22now-2d%22,%22to%22:%22now%22%7D%7D%7D&orgId=1)
(check for existing org using the unique DB index)
2024-08-26 12:03:26 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_check_gcom_permission_uniqueness_update_fields(make_organization):
|
|
|
|
|
gcom_token = "gcom:test_token"
|
|
|
|
|
fixed_token = "fixed_token"
|
|
|
|
|
instance_info = {
|
|
|
|
|
"id": 324534,
|
|
|
|
|
"slug": "testinstance",
|
|
|
|
|
"url": "http://example.com",
|
|
|
|
|
"orgId": 5671,
|
|
|
|
|
"orgSlug": "testorg",
|
|
|
|
|
"orgName": "Test Org",
|
|
|
|
|
"regionSlug": "us",
|
|
|
|
|
"clusterSlug": "us-test",
|
|
|
|
|
}
|
|
|
|
|
context = {
|
|
|
|
|
"stack_id": str(instance_info["id"]),
|
|
|
|
|
"org_id": str(instance_info["orgId"]),
|
|
|
|
|
"grafana_token": fixed_token,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
org = make_organization(stack_id=instance_info["id"], org_id=instance_info["orgId"], api_token="broken_token")
|
|
|
|
|
|
|
|
|
|
# organization does not exist in the first check but it is created before the second check
|
|
|
|
|
with patch(
|
2024-08-28 13:32:24 -03:00
|
|
|
"apps.grafana_plugin.helpers.gcom.Organization.objects_with_deleted.filter",
|
|
|
|
|
return_value=Organization.objects.none(),
|
Update org creation to use DB uniqueness constraint (#4926)
Fix issue related to
[logs](https://ops.grafana-ops.net/explore?schemaVersion=1&panes=%7B%2257p%22:%7B%22datasource%22:%22000000193%22,%22queries%22:%5B%7B%22refId%22:%22A%22,%22expr%22:%22%7Bnamespace%3D%5C%22amixr-prod%5C%22,%20cluster%3D%5C%22prod-us-central-0%5C%22,%20job%3D%5C%22amixr-prod%2Famixr-engine%5C%22%7D%20%7C%3D%20%5C%22user_management_organization.user_management_organization_stack_id_org_id_727b4929_uniq%5C%22%22,%22queryType%22:%22range%22,%22datasource%22:%7B%22type%22:%22loki%22,%22uid%22:%22000000193%22%7D,%22editorMode%22:%22code%22%7D%5D,%22range%22:%7B%22from%22:%22now-2d%22,%22to%22:%22now%22%7D%7D%7D&orgId=1)
(check for existing org using the unique DB index)
2024-08-26 12:03:26 -03:00
|
|
|
):
|
|
|
|
|
with patch(
|
|
|
|
|
"apps.grafana_plugin.helpers.GcomAPIClient.get_instance_info",
|
|
|
|
|
return_value=instance_info,
|
|
|
|
|
) as mock_instance_info:
|
|
|
|
|
check_gcom_permission(gcom_token, context)
|
|
|
|
|
mock_instance_info.assert_called()
|
|
|
|
|
|
|
|
|
|
org.refresh_from_db()
|
|
|
|
|
assert org.stack_id == instance_info["id"]
|
|
|
|
|
assert org.stack_slug == instance_info["slug"]
|
|
|
|
|
assert org.grafana_url == instance_info["url"]
|
|
|
|
|
assert org.org_id == instance_info["orgId"]
|
|
|
|
|
assert org.org_slug == instance_info["orgSlug"]
|
|
|
|
|
assert org.org_title == instance_info["orgName"]
|
|
|
|
|
assert org.region_slug == instance_info["regionSlug"]
|
|
|
|
|
assert org.cluster_slug == instance_info["clusterSlug"]
|
|
|
|
|
assert org.api_token == fixed_token
|
|
|
|
|
assert org.gcom_token == gcom_token
|
2024-08-28 13:32:24 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_check_gcom_permission_undelete_org(make_organization):
|
|
|
|
|
gcom_token = "gcom:test_token"
|
|
|
|
|
fixed_token = "fixed_token"
|
|
|
|
|
instance_info = {
|
|
|
|
|
"id": 324534,
|
|
|
|
|
"slug": "testinstance",
|
|
|
|
|
"url": "http://example.com",
|
|
|
|
|
"orgId": 5671,
|
|
|
|
|
"orgSlug": "testorg",
|
|
|
|
|
"orgName": "Test Org",
|
|
|
|
|
"regionSlug": "us",
|
|
|
|
|
"clusterSlug": "us-test",
|
|
|
|
|
}
|
|
|
|
|
context = {
|
|
|
|
|
"stack_id": str(instance_info["id"]),
|
|
|
|
|
"org_id": str(instance_info["orgId"]),
|
|
|
|
|
"grafana_token": fixed_token,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
org = make_organization(stack_id=instance_info["id"], org_id=instance_info["orgId"], api_token="broken_token")
|
|
|
|
|
org.delete()
|
|
|
|
|
|
|
|
|
|
with pytest.raises(InvalidToken):
|
|
|
|
|
check_gcom_permission(gcom_token, context)
|
|
|
|
|
|
|
|
|
|
org.refresh_from_db()
|
|
|
|
|
# org is still deleted
|
|
|
|
|
assert org.deleted_at
|