|
| 1 | +from unittest.mock import patch |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from apps.grafana_plugin.helpers.gcom import check_gcom_permission |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.parametrize( |
| 9 | + "api_token, api_token_updated", |
| 10 | + [ |
| 11 | + ("glsa_abcdefghijklmnopqrztuvwxyz", True), |
| 12 | + ("abcdefghijklmnopqrztuvwxyz", True), |
| 13 | + ("abc", False), |
| 14 | + ("", False), |
| 15 | + ("<no_value>", False), |
| 16 | + (None, False), |
| 17 | + (24, False), |
| 18 | + ], |
| 19 | +) |
| 20 | +@pytest.mark.django_db |
| 21 | +def test_check_gcom_permission_updates_fields(make_organization, api_token, api_token_updated): |
| 22 | + gcom_token = "gcom:test_token" |
| 23 | + broken_token = "broken_token" |
| 24 | + instance_info = { |
| 25 | + "id": 324534, |
| 26 | + "slug": "testinstance", |
| 27 | + "url": "http://example.com", |
| 28 | + "orgId": 5671, |
| 29 | + "orgSlug": "testorg", |
| 30 | + "orgName": "Test Org", |
| 31 | + "regionSlug": "us", |
| 32 | + "clusterSlug": "us-test", |
| 33 | + } |
| 34 | + context = { |
| 35 | + "stack_id": str(instance_info["id"]), |
| 36 | + "org_id": str(instance_info["orgId"]), |
| 37 | + "grafana_token": api_token, |
| 38 | + } |
| 39 | + |
| 40 | + org = make_organization(stack_id=instance_info["id"], org_id=instance_info["orgId"], api_token=broken_token) |
| 41 | + last_time_gcom_synced = org.gcom_token_org_last_time_synced |
| 42 | + |
| 43 | + with patch( |
| 44 | + "apps.grafana_plugin.helpers.GcomAPIClient.get_instance_info", |
| 45 | + return_value=instance_info, |
| 46 | + ) as mock_instance_info: |
| 47 | + check_gcom_permission(gcom_token, context) |
| 48 | + mock_instance_info.assert_called() |
| 49 | + |
| 50 | + org.refresh_from_db() |
| 51 | + assert org.stack_id == instance_info["id"] |
| 52 | + assert org.stack_slug == instance_info["slug"] |
| 53 | + assert org.grafana_url == instance_info["url"] |
| 54 | + assert org.org_id == instance_info["orgId"] |
| 55 | + assert org.org_slug == instance_info["orgSlug"] |
| 56 | + assert org.org_title == instance_info["orgName"] |
| 57 | + assert org.region_slug == instance_info["regionSlug"] |
| 58 | + assert org.cluster_slug == instance_info["clusterSlug"] |
| 59 | + assert org.api_token == api_token if api_token_updated else broken_token |
| 60 | + assert org.gcom_token == gcom_token |
| 61 | + assert org.gcom_token_org_last_time_synced != last_time_gcom_synced |
0 commit comments