-
Notifications
You must be signed in to change notification settings - Fork 696
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #436 from EduardSchwarzkopf/improve-tests
Improve tests
- Loading branch information
Showing
12 changed files
with
402 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import os | ||
import pytest | ||
from pacu import settings, Main | ||
from pacu import core | ||
from pacu.core.models import PacuSession | ||
from sqlalchemy import orm, Column, create_engine | ||
from sqlalchemy.orm import sessionmaker, Session | ||
from sqlalchemy_utils import JSONType | ||
from sqlalchemy.engine import Engine | ||
|
||
from pacu.modules.cognito__attack.tests.dataclasses import AWSCredentials | ||
|
||
settings.DATABASE_CONNECTION_PATH = "sqlite:///:memory:" | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def db() -> core.base: | ||
core.base.engine: Engine = create_engine(settings.DATABASE_CONNECTION_PATH) | ||
core.base.Session: sessionmaker = sessionmaker(bind=core.base.engine) | ||
core.base.Base.metadata.create_all(core.base.engine) | ||
yield core.base.Session() | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def pacu(db, aws_credentials: AWSCredentials, active_session: PacuSession): | ||
pacu = Main() | ||
pacu.database = db | ||
|
||
pacu.set_keys( | ||
key_alias="pytest", | ||
access_key_id=aws_credentials.access_key_id, | ||
secret_access_key=aws_credentials.secret_access_key, | ||
session_token=aws_credentials.session_token, | ||
) | ||
|
||
yield pacu | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def active_session(db, pacu_session: PacuSession): | ||
pacu_session.activate(db) | ||
yield pacu_session | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def pacu_session(db: orm.session.Session): | ||
query: orm.Query = db.query(PacuSession) | ||
assert query.count() == 0 | ||
|
||
pacu_session = PacuSession(name="test") | ||
db.add(pacu_session) | ||
yield pacu_session | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def db_new_column(db: Session): | ||
PacuSession.TestSvc = Column(JSONType, nullable=False, default=dict) | ||
PacuSession.aws_data_field_names = PacuSession.aws_data_field_names + ("TestSvc",) | ||
core.base.Session: sessionmaker = sessionmaker(bind=core.base.engine) | ||
yield core.base.Session() | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def pacu_with_data(pacu: Main, active_session: PacuSession): | ||
active_session.update(pacu.database, CloudWatch={"test_key": "test_value"}) | ||
yield pacu | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def aws_credentials(): | ||
"""Mocked AWS Credentials for moto.""" | ||
|
||
value = "testing" | ||
creds = AWSCredentials( | ||
access_key_id=value, | ||
secret_access_key=value, | ||
session_token=value, | ||
security_token=value, | ||
) | ||
|
||
os.environ["AWS_ACCESS_KEY_ID"] = creds.access_key_id | ||
os.environ["AWS_SECRET_ACCESS_KEY"] = creds.secret_access_key | ||
os.environ["AWS_SECURITY_TOKEN"] = creds.session_token | ||
os.environ["AWS_SESSION_TOKEN"] = creds.session_token | ||
|
||
yield creds |
15 changes: 15 additions & 0 deletions
15
pacu/modules/cfn__resource_injection/cfn__resource_injection_lambda/tests/conftest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import pytest | ||
import moto | ||
import boto3 | ||
import os | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def s3(): | ||
with moto.mock_s3(): | ||
yield boto3.client("s3", region_name="us-east-1") | ||
|
||
|
||
@pytest.fixture | ||
def environ(): | ||
os.environ["PRINCIPAL"] = "asdf" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import boto3 | ||
import moto | ||
import pytest | ||
|
||
from pacu.modules.cognito__attack.tests.dataclasses import CognitoServiceConfig | ||
from pacu.settings import REGION | ||
|
||
|
||
@pytest.fixture | ||
def mock_cognito_user_pool(): | ||
|
||
with moto.mock_cognitoidp(): | ||
|
||
client = boto3.client( | ||
"cognito-idp", | ||
region_name=REGION, | ||
) | ||
|
||
response = client.create_user_pool( | ||
PoolName="TestUserPool", UsernameAttributes=["email"] | ||
) | ||
|
||
user_pool_id = response["UserPool"]["Id"] | ||
|
||
client_response = client.create_user_pool_client( | ||
ClientName="AppClient", | ||
GenerateSecret=False, | ||
UserPoolId=user_pool_id, | ||
) | ||
|
||
with moto.mock_cognitoidentity(): | ||
c = boto3.client( | ||
"cognito-identity", | ||
region_name=REGION, | ||
) | ||
|
||
c_resposnse = c.create_identity_pool( | ||
IdentityPoolName="TestIdentityPool", | ||
AllowUnauthenticatedIdentities=False, | ||
) | ||
|
||
yield CognitoServiceConfig( | ||
client=client, | ||
user_pool_id=user_pool_id, | ||
client_id=client_response["UserPoolClient"]["ClientId"], | ||
client_name=client_response["UserPoolClient"]["ClientName"], | ||
identity_pool_id=c_resposnse["IdentityPoolId"], | ||
) |
Oops, something went wrong.