|
| 1 | +import pytest |
| 2 | +from django.utils import timezone |
| 3 | +from rest_framework.exceptions import AuthenticationFailed |
| 4 | +from rest_framework.test import APIRequestFactory |
| 5 | + |
| 6 | +from apps.auth_token.auth import PluginAuthentication |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.django_db |
| 10 | +def test_plugin_authentication_self_hosted_success(make_organization, make_user, make_token_for_organization): |
| 11 | + organization = make_organization(stack_id=42, org_id=24) |
| 12 | + user = make_user(organization=organization, user_id=12) |
| 13 | + token, token_string = make_token_for_organization(organization) |
| 14 | + |
| 15 | + headers = { |
| 16 | + "HTTP_AUTHORIZATION": token_string, |
| 17 | + "HTTP_X-Instance-Context": '{"stack_id": 42, "org_id": 24}', |
| 18 | + "HTTP_X-Grafana-Context": '{"UserId": 12}', |
| 19 | + } |
| 20 | + request = APIRequestFactory().get("/", **headers) |
| 21 | + |
| 22 | + assert PluginAuthentication().authenticate(request) == (user, token) |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.django_db |
| 26 | +def test_plugin_authentication_gcom_success(make_organization, make_user, make_token_for_organization): |
| 27 | + # Setting gcom_token_org_last_time_synced to now, so it doesn't try to sync with gcom |
| 28 | + organization = make_organization( |
| 29 | + stack_id=42, org_id=24, gcom_token="123", gcom_token_org_last_time_synced=timezone.now() |
| 30 | + ) |
| 31 | + user = make_user(organization=organization, user_id=12) |
| 32 | + |
| 33 | + headers = { |
| 34 | + "HTTP_AUTHORIZATION": "gcom:123", |
| 35 | + "HTTP_X-Instance-Context": '{"stack_id": 42, "org_id": 24}', |
| 36 | + "HTTP_X-Grafana-Context": '{"UserId": 12}', |
| 37 | + } |
| 38 | + request = APIRequestFactory().get("/", **headers) |
| 39 | + |
| 40 | + ret_user, ret_token = PluginAuthentication().authenticate(request) |
| 41 | + assert ret_user == user |
| 42 | + assert ret_token.organization == organization |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.django_db |
| 46 | +@pytest.mark.parametrize("grafana_context", [None, "", "non-json", '"string"', "{}", '{"UserId": 1}']) |
| 47 | +def test_plugin_authentication_fail_grafana_context( |
| 48 | + make_organization, make_user, make_token_for_organization, grafana_context |
| 49 | +): |
| 50 | + organization = make_organization(stack_id=42, org_id=24) |
| 51 | + token, token_string = make_token_for_organization(organization) |
| 52 | + |
| 53 | + headers = {"HTTP_AUTHORIZATION": token_string, "HTTP_X-Instance-Context": '{"stack_id": 42, "org_id": 24}'} |
| 54 | + if grafana_context is not None: |
| 55 | + headers["HTTP_X-Grafana-Context"] = grafana_context |
| 56 | + |
| 57 | + request = APIRequestFactory().get("/", **headers) |
| 58 | + with pytest.raises(AuthenticationFailed): |
| 59 | + PluginAuthentication().authenticate(request) |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.django_db |
| 63 | +@pytest.mark.parametrize("authorization", [None, "", "123", "gcom:123"]) |
| 64 | +@pytest.mark.parametrize("instance_context", [None, "", "non-json", '"string"', "{}", '{"stack_id": 1, "org_id": 1}']) |
| 65 | +def test_plugin_authentication_fail(authorization, instance_context): |
| 66 | + headers = {} |
| 67 | + |
| 68 | + if authorization is not None: |
| 69 | + headers["HTTP_AUTHORIZATION"] = authorization |
| 70 | + |
| 71 | + if instance_context is not None: |
| 72 | + headers["HTTP_X-Instance-Context"] = instance_context |
| 73 | + |
| 74 | + request = APIRequestFactory().get("/", **headers) |
| 75 | + |
| 76 | + with pytest.raises(AuthenticationFailed): |
| 77 | + PluginAuthentication().authenticate(request) |
0 commit comments