Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-Dickinson committed Oct 6, 2024
1 parent 0c31cd0 commit b0f8251
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 36 deletions.
72 changes: 63 additions & 9 deletions src/meshapi/tests/test_install_create_signals.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
30 changes: 3 additions & 27 deletions src/meshapi/util/django_flag_decorator.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit b0f8251

Please sign in to comment.