diff --git a/src/meshapi/tests/test_install_create_signals.py b/src/meshapi/tests/test_install_create_signals.py index cc2b5c6c..0b0f34bc 100644 --- a/src/meshapi/tests/test_install_create_signals.py +++ b/src/meshapi/tests/test_install_create_signals.py @@ -1,11 +1,7 @@ import json -import uuid from unittest.mock import patch -import pytest import requests_mock -from django.contrib.auth.models import User -from django.core.exceptions import ValidationError from django.test import TestCase from flags.state import disable_flag, enable_flag @@ -64,6 +60,10 @@ def test_constructing_install_triggers_slack_message(self, request_mocker): "meshapi.util.events.osticket_creation.OSTICKET_NEW_TICKET_ENDPOINT", "http://example.com/test-url", ) + @patch( + "meshapi.util.events.osticket_creation.OSTICKET_API_TOKEN", + "mock-token", + ) @requests_mock.Mocker() def test_constructing_install_triggers_osticket(self, request_mocker): request_mocker.post("http://example.com/test-url", text="00123456", status_code=201) @@ -100,8 +100,62 @@ def test_constructing_install_triggers_osticket(self, request_mocker): install.refresh_from_db() self.assertEqual(install.ticket_number, "00123456") - # - # def test_constructing_install_triggers_osticket_call(self): - # enable_flag("INTEGRATION_ENABLED_SEND_JOIN_REQUEST_SLACK_MESSAGES") - # install = Install(**self.sample_install_copy) - # install.save() + @patch( + "meshapi.util.events.join_requests_slack_channel.SLACK_JOIN_REQUESTS_CHANNEL_WEBHOOK_URL", + "", + ) + @patch( + "meshapi.util.events.osticket_creation.OSTICKET_NEW_TICKET_ENDPOINT", + "", + ) + @requests_mock.Mocker() + def test_no_events_when_env_variables_unset(self, request_mocker): + enable_flag("INTEGRATION_ENABLED_SEND_JOIN_REQUEST_SLACK_MESSAGES") + enable_flag("INTEGRATION_ENABLED_CREATE_OSTICKET_TICKETS") + + install = Install(**self.sample_install_copy) + install.save() + + self.assertEqual(len(request_mocker.request_history), 0) + + @patch( + "meshapi.util.events.osticket_creation.OSTICKET_NEW_TICKET_ENDPOINT", + "http://example.com/test-url", + ) + @patch( + "meshapi.util.events.osticket_creation.OSTICKET_API_TOKEN", + "", + ) + @requests_mock.Mocker() + def test_no_osticket_event_when_no_api_token(self, request_mocker): + enable_flag("INTEGRATION_ENABLED_CREATE_OSTICKET_TICKETS") + + install = Install(**self.sample_install_copy) + install.save() + + self.assertEqual(len(request_mocker.request_history), 0) + + @patch( + "meshapi.util.events.join_requests_slack_channel.SLACK_JOIN_REQUESTS_CHANNEL_WEBHOOK_URL", + "http://example.com/test-url", + ) + @patch( + "meshapi.util.events.osticket_creation.OSTICKET_NEW_TICKET_ENDPOINT", + "http://example.com/test-url", + ) + @patch( + "meshapi.util.events.osticket_creation.OSTICKET_API_TOKEN", + "mock-token", + ) + @requests_mock.Mocker() + def test_no_events_for_install_edit(self, request_mocker): + install = Install(**self.sample_install_copy) + install.save() + + enable_flag("INTEGRATION_ENABLED_SEND_JOIN_REQUEST_SLACK_MESSAGES") + enable_flag("INTEGRATION_ENABLED_CREATE_OSTICKET_TICKETS") + + install.notes = "foo" + install.save() + + self.assertEqual(len(request_mocker.request_history), 0) diff --git a/src/meshapi/util/django_flag_decorator.py b/src/meshapi/util/django_flag_decorator.py index e5db035b..894202e5 100644 --- a/src/meshapi/util/django_flag_decorator.py +++ b/src/meshapi/util/django_flag_decorator.py @@ -1,41 +1,17 @@ -import inspect from functools import wraps -from typing import Callable +from typing import Any, Callable from flags.state import flag_state -def create_noop(func): - """Creates a no-op function that conforms to the argument - specification of the given function.""" - - def noop(*args, **kwargs): - pass - - # Get the argument specification of the original function - argspec = inspect.getfullargspec(func) - - # Update the noop function's signature to match the original function - noop.__signature__ = inspect.Signature( - parameters=[inspect.Parameter(name, inspect.Parameter.POSITIONAL_OR_KEYWORD) for name in argspec.args] - + [ - inspect.Parameter(name, inspect.Parameter.KEYWORD_ONLY, default=argspec.defaults[i]) - for i, name in enumerate(argspec.kwonlyargs) - ], - return_annotation=argspec.annotations.get("return", None), - ) - - return noop - - def skip_if_flag_disabled(flag_name: str) -> Callable: """ Decorator that transforms the annotated function into a noop if the given flag name is disabled :param flag_name: the flag to check """ - def decorator(func): - def inner(*args, **kwargs): + def decorator(func: Callable) -> Callable: + def inner(*args: list, **kwargs: dict) -> Any: enabled = flag_state(flag_name) if enabled: