|
| 1 | +import pytest |
| 2 | + |
| 3 | +from environments.models import Environment |
| 4 | +from integrations.sentry.models import SentryChangeTrackingConfiguration |
| 5 | + |
| 6 | +from rest_framework.test import APIClient |
| 7 | + |
| 8 | + |
| 9 | +def test_sentry__change_tracking__setup__accepts_new_configuration( |
| 10 | + admin_client: APIClient, |
| 11 | + environment: Environment, |
| 12 | +): |
| 13 | + # Given |
| 14 | + pass |
| 15 | + |
| 16 | + # When |
| 17 | + url = f"/api/v1/environments/{environment.api_key}/integrations/sentry/" |
| 18 | + payload = { |
| 19 | + "webhook_url": "https://sentry.example.com/webhook", |
| 20 | + "secret": "hush hush!", |
| 21 | + } |
| 22 | + response = admin_client.post(url, payload, format="json") |
| 23 | + |
| 24 | + # Then |
| 25 | + assert response.status_code == 201 |
| 26 | + assert SentryChangeTrackingConfiguration.objects.filter( |
| 27 | + environment=environment, |
| 28 | + webhook_url="https://sentry.example.com/webhook", |
| 29 | + secret="hush hush!", |
| 30 | + ).count() == 1 |
| 31 | + |
| 32 | +@pytest.mark.parametrize( |
| 33 | + "payload, errors", |
| 34 | + [ |
| 35 | + ( |
| 36 | + { |
| 37 | + "secret": "hush hush!", |
| 38 | + }, |
| 39 | + { |
| 40 | + "webhook_url": ["This field is required."], |
| 41 | + } |
| 42 | + ), |
| 43 | + ( |
| 44 | + { |
| 45 | + "webhook_url": "https://sentry.example.com/webhook", |
| 46 | + }, |
| 47 | + { |
| 48 | + "secret": ["This field is required."], |
| 49 | + } |
| 50 | + ), |
| 51 | + ( |
| 52 | + { |
| 53 | + "webhook_url": "https://sentry.example.com/webhook", |
| 54 | + "secret": "hush!", |
| 55 | + }, |
| 56 | + { |
| 57 | + "secret": ["Ensure this field has at least 10 characters."], |
| 58 | + } |
| 59 | + ), |
| 60 | + ( |
| 61 | + { |
| 62 | + "webhook_url": "https://sentry.example.com/webhook", |
| 63 | + "secret": "Hush, hush, hush, hush; I've already spoken, our love is broken; Baby, hush, hush", |
| 64 | + }, |
| 65 | + { |
| 66 | + "secret": ["Ensure this field has no more than 60 characters."] |
| 67 | + } |
| 68 | + ), |
| 69 | + ( |
| 70 | + { |
| 71 | + "webhook_url": "https://sentry.example.com/webhook", |
| 72 | + "secret": "Hush, hush, hush, hush; I've already spoken, our love is broken; Baby, hush, hush", |
| 73 | + }, |
| 74 | + { |
| 75 | + "secret": ["Ensure this field has no more than 60 characters."] |
| 76 | + } |
| 77 | + ), |
| 78 | + ( |
| 79 | + { |
| 80 | + "webhook_url": "https://sentry.example.com/webhook", |
| 81 | + "secret": "hush hush!", |
| 82 | + }, |
| 83 | + ['This integration already exists for this environment.'] |
| 84 | + ) |
| 85 | + ], |
| 86 | +) |
| 87 | +def test_sentry__change_tracking__setup__rejects_invalid_configuration( |
| 88 | + admin_client: APIClient, |
| 89 | + environment: Environment, |
| 90 | + errors: dict, |
| 91 | + payload: dict, |
| 92 | +): |
| 93 | + # Given |
| 94 | + # Already existing configuration |
| 95 | + SentryChangeTrackingConfiguration.objects.create( |
| 96 | + environment=environment, |
| 97 | + webhook_url="https://sentry.example.com/webhook", |
| 98 | + secret="hush hush!", |
| 99 | + ) |
| 100 | + |
| 101 | + # When |
| 102 | + url = f"/api/v1/environments/{environment.api_key}/integrations/sentry/" |
| 103 | + response = admin_client.post(url, payload, format="json") |
| 104 | + |
| 105 | + # Then |
| 106 | + assert response.status_code == 400 |
| 107 | + assert response.json() == errors |
0 commit comments