Skip to content

Commit

Permalink
refactor(tests): use pytest factoryboy to create objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Moreno authored and photonbit committed Aug 27, 2021
1 parent c2c3762 commit 4df51b1
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 39 deletions.
1 change: 1 addition & 0 deletions dev_requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ lxml
pre-commit
pytest
pytest-cov
pytest-factoryboy
mypy
semgrep
27 changes: 20 additions & 7 deletions dev_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ backports.entry-points-selectable==1.1.0
# via virtualenv
black==21.7b0
# via -r dev_requirements.in
boto3==1.18.28
boto3==1.18.30
# via -r dev_requirements.in
botocore==1.21.28
botocore==1.21.30
# via
# boto3
# s3transfer
bracex==2.1.1
# via wcmatch
certifi==2021.5.30
# via requests
cfgv==3.3.0
cfgv==3.3.1
# via pre-commit
charset-normalizer==2.0.4
# via requests
Expand All @@ -37,6 +37,10 @@ coverage==5.5
# via pytest-cov
distlib==0.3.2
# via virtualenv
factory-boy==3.2.0
# via pytest-factoryboy
faker==8.12.1
# via factory-boy
filelock==3.0.12
# via virtualenv
flake8==3.9.2
Expand All @@ -45,6 +49,8 @@ identify==2.2.13
# via pre-commit
idna==3.2
# via requests
inflection==0.5.1
# via pytest-factoryboy
iniconfig==1.1.1
# via pytest
jmespath==0.10.0
Expand Down Expand Up @@ -93,29 +99,36 @@ pytest==6.2.4
# via
# -r dev_requirements.in
# pytest-cov
# pytest-factoryboy
pytest-cov==2.12.1
# via -r dev_requirements.in
pytest-factoryboy==2.1.0
# via -r dev_requirements.in
python-dateutil==2.8.2
# via botocore
# via
# botocore
# faker
pyyaml==5.4.1
# via pre-commit
regex==2021.8.21
regex==2021.8.27
# via black
requests==2.26.0
# via semgrep
ruamel.yaml==0.17.13
ruamel.yaml==0.17.14
# via semgrep
ruamel.yaml.clib==0.2.6
# via ruamel.yaml
s3transfer==0.5.0
# via boto3
semgrep==0.62.0
semgrep==0.63.0
# via -r dev_requirements.in
six==1.16.0
# via
# jsonschema
# python-dateutil
# virtualenv
text-unidecode==1.3
# via faker
toml==0.10.2
# via
# mypy
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ attrs==21.2.0
# via jsonschema
blinker==1.4
# via sentry-sdk
boto3==1.18.28
boto3==1.18.30
# via
# kappa
# zappa
botocore==1.21.28
botocore==1.21.30
# via
# boto3
# s3transfer
Expand Down
5 changes: 5 additions & 0 deletions test/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import flask_migrate
import pytest
from pytest_factoryboy import register
from sqlalchemy import inspect

from giges.app import create_connexion_app
from giges.db import db
from .factories import EventFactory, ProjectFactory, WebhookFactory

for factory in (EventFactory, ProjectFactory, WebhookFactory):
register(factory)


@pytest.fixture(scope="session")
Expand Down
43 changes: 43 additions & 0 deletions test/integration/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from factory import Faker, SubFactory
from factory.alchemy import SQLAlchemyModelFactory
from factory.fuzzy import FuzzyChoice, FuzzyText
from faker.providers import date_time

from giges.db import db
from giges.models.asana import Event, Project, ResourceTypeEnum, Webhook

Faker.add_provider(date_time)


class WebhookFactory(SQLAlchemyModelFactory):
external_id = FuzzyText(chars="0123456789")
path = "/asana/projects"
resource_type = FuzzyChoice(choices=ResourceTypeEnum)
secret = FuzzyText()

class Meta:
model = Webhook
sqlalchemy_session = db.session
sqlalchemy_session_persistence = "commit"


class ProjectFactory(SQLAlchemyModelFactory):
external_id = FuzzyText(chars="0123456789")
name = FuzzyText()
created_at = Faker("date_between", start_date="-60d", end_date="-1d")
updated_at = Faker("date_between", start_date="-50d", end_date="now")

class Meta:
model = Project
sqlalchemy_session = db.session
sqlalchemy_session_persistence = "commit"


class EventFactory(SQLAlchemyModelFactory):
webhook = SubFactory(WebhookFactory)
content = {FuzzyText(): FuzzyText()}

class Meta:
model = Event
sqlalchemy_session = db.session
sqlalchemy_session_persistence = "commit"
47 changes: 17 additions & 30 deletions test/integration/test_asana.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from giges.models.asana import Event, Webhook
from giges.models.asana import Event


def test_asana_projects_handshake(client, transactional_db):
def test_asana_projects_handshake(client, webhook):
secret = "wow_such_very_secret"
webhook = Webhook(path="/asana/projects")
transactional_db.add(webhook)
transactional_db.commit()
webhook.path = "/asana/projects"
webhook.secret = None

response = client.post(
"/asana/projects",
Expand All @@ -26,42 +25,32 @@ def test_asana_projects_handshake_no_webhook(client):
assert response.status_code == 404


def test_asana_projects_handshake_hack_attempt(client, transactional_db):
secret = "wow_such_very_secret"
webhook = Webhook(path="/asana/projects", secret=secret)
transactional_db.add(webhook)
transactional_db.commit()
def test_asana_projects_handshake_hack_attempt(client, webhook):
secret = webhook.secret

response = client.post(
"/asana/projects",
webhook.path,
headers={"X-Hook-Secret": "evil hacker"},
json={},
)
assert response.status_code == 400
assert webhook.secret == secret


def test_asana_project_event_no_secret(client, transactional_db):
webhook = Webhook(path="/asana/projects")
transactional_db.add(webhook)
transactional_db.commit()

def test_asana_project_event_no_secret(client, webhook):
webhook.secret = None
response = client.post(
"/asana/projects",
webhook.path,
headers={"X-Hook-Signature": "wow_such_very_secret_signature"},
json={"events": [{"glitch": "in matrix"}]},
)
assert response.status_code == 500
assert Event.query.count() == 0


def test_asana_project_event_bad_signature(client, transactional_db):
webhook = Webhook(path="/asana/projects", secret="pork_fillet")
transactional_db.add(webhook)
transactional_db.commit()

def test_asana_project_event_bad_signature(client, webhook):
response = client.post(
"/asana/projects",
webhook.path,
headers={
"X-Hook-Signature": "I am a bad cracker wanting to crack the pipe"
},
Expand All @@ -71,17 +60,15 @@ def test_asana_project_event_bad_signature(client, transactional_db):
assert Event.query.count() == 0


def test_asana_project_event_ok(client, transactional_db):
def test_asana_project_event_ok(client, webhook):
signature = (
"56302af223b5cfad770ff07469189e5a2ac961bf77ac6fcf000ae75c619af3d7"
)
assert Event.query.count() == 0
webhook = Webhook(path="/asana/projects", secret="pork_fillet")
transactional_db.add(webhook)
transactional_db.commit()
webhook.secret = "pork_fillet"

response = client.post(
"/asana/projects",
webhook.path,
headers={"X-Hook-Signature": signature},
json={"events": [{"glitch": "in matrix"}]},
)
Expand All @@ -97,6 +84,6 @@ def test_asana_project_event_no_events(client):
assert response.status_code == 400


def test_asana_project_event_no_signature(client):
response = client.post("/asana/projects", json={"events": []})
def test_asana_project_event_no_signature(client, webhook):
response = client.post(webhook.path, json={"events": []})
assert response.status_code == 400

0 comments on commit 4df51b1

Please sign in to comment.