-
-
Notifications
You must be signed in to change notification settings - Fork 645
Expand file tree
/
Copy pathmock_utils.py
More file actions
69 lines (52 loc) · 1.69 KB
/
Copy pathmock_utils.py
File metadata and controls
69 lines (52 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# This file is a part of IntelOwl https://github.com/intelowlproject/IntelOwl
# See the file 'LICENSE' for copying permission.
"""
mock_utils.py: useful utils for mocking requests and responses for testing
for observable analyzers, if can customize the behavior based on:
MOCK_CONNECTIONS to True -> connections to external analyzers are faked
"""
from dataclasses import dataclass
from unittest import skip, skipIf # noqa: F401
from unittest.mock import MagicMock, patch # noqa: F401
from django.conf import settings
class MockUpRequest:
def __init__(self, user):
self.user = user
# class for mocking responses
class MockUpResponse:
@dataclass()
class Request:
def __init__(self):
self.url = None
def __init__(self, json_data, status_code, text="", content=b"", url="", headers=None):
self.json_data = json_data
self.status_code = status_code
self.text = text
self.content = content
self.url = url
self.headers = headers or {}
self.request = self.Request()
def json(self):
return self.json_data
@staticmethod
def raise_for_status():
pass
# a mock response class that has no operation
class MockResponseNoOp:
def __init__(self, json_data, status_code):
pass
@staticmethod
def search(*args, **kwargs):
return {}
@staticmethod
def query(*args, **kwargs):
return {}
@staticmethod
def lookup(*args, **kwargs):
return {}
def if_mock_connections(*decorators):
def apply_all(f):
for d in reversed(decorators):
f = d(f)
return f
return apply_all if settings.MOCK_CONNECTIONS else lambda x: x