Skip to content
This repository has been archived by the owner on May 26, 2021. It is now read-only.

issue-18: Adds mitm #109

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 38 additions & 32 deletions http_stubs/models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from enum import Enum
from typing import Iterator, KeysView, Tuple
from typing import Tuple

from django.contrib.postgres.fields import HStoreField
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.db.models import Lookup


class HTTPMethod(Enum):
class HTTPMethod(models.TextChoices):
"""Enumeration of the available HTTP methods."""

GET = 'GET'
Expand All @@ -19,25 +18,9 @@ class HTTPMethod(Enum):
OPTIONS = 'OPTIONS'
TRACE = 'TRACE'

@classmethod
def names(cls) -> KeysView[str]:
"""Set of the available HTTP method names.

:returns: all names in the enumeration.
"""
return cls.__members__.keys()

@classmethod
def slugs(cls) -> Iterator[Tuple[str, str]]:
"""Names of the methods for the model choice fields.

:returns: iterator of tuples with http method names
"""
return zip(cls.names(), cls.names())


class HTTPStub(models.Model):
"""HTTP stub."""
class AbstractHTTPStub(models.Model):
"""Abstract HTTP stub."""

is_active = models.BooleanField(
verbose_name='Enabled',
Expand All @@ -57,7 +40,7 @@ class HTTPStub(models.Model):
verbose_name='Request method',
max_length=10,
db_index=True,
choices=HTTPMethod.slugs(),
choices=HTTPMethod.choices,
)
resp_delay = models.PositiveIntegerField(
verbose_name='Response delay',
Expand Down Expand Up @@ -91,25 +74,40 @@ class HTTPStub(models.Model):
blank=True,
)

def __str__(self):
"""Return string representation of the model.

:returns: e.g. `get: /test/`.
"""
return f'{self.method}: {self.path}'

class Meta:
verbose_name = 'http stub'
verbose_name_plural = 'stubs'
abstract = True
constraints = [
models.UniqueConstraint(
fields=('path', 'method'), name='uniq-path-method',
),
]

def __str__(self):
"""Return string representation of the model.

:returns: e.g. `get: /test/`.
"""
return f'{self.method}: {self.path}'
class HTTPStub(AbstractHTTPStub):
"""HTTP stub."""

class Meta:
verbose_name = 'http stub'
verbose_name_plural = 'stubs'

class LogEntry(models.Model):
"""Log entry."""

class ProxyHTTPStub(AbstractHTTPStub):
"""Proxy HTTP stub."""

class Meta:
verbose_name = 'proxy http stub'
verbose_name_plural = 'proxy stubs'


class AbstractLogEntry(models.Model):
"""Abstract log entry."""

path = models.URLField(
verbose_name='Full request path',
Expand All @@ -118,7 +116,7 @@ class LogEntry(models.Model):
method = models.CharField(
verbose_name='Request method',
max_length=10,
choices=HTTPMethod.slugs(),
choices=HTTPMethod.choices,
)
source_ip = models.GenericIPAddressField(
verbose_name='Source IP',
Expand Down Expand Up @@ -159,6 +157,14 @@ def __str__(self) -> str:
return ''


class LogEntry(AbstractLogEntry):
"""Log entry."""


class ProxyLogEntity(AbstractLogEntry):
"""Proxy log entry."""


@models.CharField.register_lookup
class RegExpLookup(Lookup):
"""Regular expression field lookup.
Expand Down
6 changes: 3 additions & 3 deletions http_stubs/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class TestHTTPStubView:
"""Tests representation of the http stubs."""

@pytest.mark.parametrize('method', HTTPMethod.names())
@pytest.mark.parametrize('method', HTTPMethod.values)
def test_nonexistent_stub(self, method: str, client):
"""Tests response when stub is not found.

Expand All @@ -22,7 +22,7 @@ def test_nonexistent_stub(self, method: str, client):

assert response.status_code == HTTPStatus.NOT_FOUND

@pytest.mark.parametrize('method', HTTPMethod.names())
@pytest.mark.parametrize('method', HTTPMethod.values)
def test_exist_not_regexp_stub(
self, method: str, http_stub_factory, client,
):
Expand Down Expand Up @@ -83,7 +83,7 @@ def _datefmt(date) -> str: # noqa:WPS430
assert log.path == f'http://testserver{request_path}'
assert log.result_script == 'Done'

@pytest.mark.parametrize('method', HTTPMethod.names())
@pytest.mark.parametrize('method', HTTPMethod.values)
def test_exist_regexp_stub(self, method: str, http_stub_factory, client):
"""Tests response for the regex stub.

Expand Down
56 changes: 38 additions & 18 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ python = "~3.8"
django = "~3.1"
django-simpleui = "~2021.1"
uvicorn = "~0.13.2"
psycopg2 = "~2.8.6"
django-environ = "^0.4.5"
django-extensions = "^3.1.0"
django-debug-toolbar = "^3.1.1"
celery = {extras = ["redis"], version = "^5.0.4"}
RestrictedPython = "~5.1"
requests = "^2.25.1"
psycopg2-binary = "^2.8.6"

[tool.poetry.dev-dependencies]
pytest = "~6.2"
Expand Down