Skip to content

Commit

Permalink
fix imports for unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
minimike86 committed Dec 3, 2024
1 parent e948f79 commit d157222
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 32 deletions.
File renamed without changes.
File renamed without changes.
31 changes: 19 additions & 12 deletions aws/tests/test_refresh_twitch_access_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import responses
from botocore.exceptions import ClientError
from moto import mock_aws
from refresh_twitch_access_token import get_parameter, get_secret
from refresh_twitch_access_token import (

from aws.lambdas.refresh_twitch_access_token import get_parameter, get_secret
from aws.lambdas.refresh_twitch_access_token import (
lambda_handler as refresh_twitch_access_token_handler,
)
from refresh_twitch_access_token import refresh_token, store_in_dynamodb
from aws.lambdas.refresh_twitch_access_token import refresh_token, store_in_dynamodb


@pytest.fixture
Expand Down Expand Up @@ -135,7 +136,9 @@ def test_get_parameter_not_found():

@pytest.fixture
def mock_get_parameter(mocker):
return mocker.patch("refresh_twitch_access_token.get_parameter", autospec=True)
return mocker.patch(
"aws.lambdas.refresh_twitch_access_token.get_parameter", autospec=True
)


def test_get_secret_success(mock_get_parameter):
Expand Down Expand Up @@ -177,7 +180,7 @@ def test_refresh_token_success_path(mocker):
https://dev.twitch.tv/docs/authentication/refresh-tokens/
"""
mock_get_secret = mocker.patch("refresh_twitch_access_token.get_secret")
mock_get_secret = mocker.patch("aws.lambdas.refresh_twitch_access_token.get_secret")
mock_get_secret.side_effect = ["client_id", "client_secret"]

expected_response = json.dumps(
Expand Down Expand Up @@ -209,15 +212,15 @@ def test_refresh_token_success_path(mocker):


def test_refresh_token_get_secret_raises_exception(mocker):
mock_get_secret = mocker.patch("refresh_twitch_access_token.get_secret")
mock_get_secret = mocker.patch("aws.lambdas.refresh_twitch_access_token.get_secret")
mock_get_secret.side_effect = Exception("some exception")

with pytest.raises(Exception):
refresh_token("_refresh_token")


def test_refresh_token_requests_post_raises_exception(mocker):
mock_get_secret = mocker.patch("refresh_twitch_access_token.get_secret")
mock_get_secret = mocker.patch("aws.lambdas.refresh_twitch_access_token.get_secret")
mock_get_secret.side_effect = ["client_id", "client_secret"]

mock_requests_post = mocker.patch("requests.post")
Expand All @@ -240,7 +243,7 @@ def test_refresh_token_invalid_token(mocker):
https://dev.twitch.tv/docs/authentication/refresh-tokens/
"""
mock_get_secret = mocker.patch("refresh_twitch_access_token.get_secret")
mock_get_secret = mocker.patch("aws.lambdas.refresh_twitch_access_token.get_secret")
mock_get_secret.side_effect = ["client_id", "client_secret"]

expected_response = json.dumps(
Expand Down Expand Up @@ -268,7 +271,7 @@ def test_refresh_token_invalid_token(mocker):
@responses.activate
@mock_aws
def test_refresh_twitch_access_token_handler_success(mocker, set_environment_variables):
mock_get_secret = mocker.patch("refresh_twitch_access_token.get_secret")
mock_get_secret = mocker.patch("aws.lambdas.refresh_twitch_access_token.get_secret")
mock_get_secret.side_effect = ["client_id", "client_secret"]

expected_token_refresh_response = json.dumps(
Expand Down Expand Up @@ -313,7 +316,9 @@ def test_refresh_twitch_access_token_handler_missing_refresh_token():


def test_refresh_twitch_access_token_handler_refresh_token_raises_exception(mocker):
mock_refresh_token = mocker.patch("refresh_twitch_access_token.refresh_token")
mock_refresh_token = mocker.patch(
"aws.lambdas.refresh_twitch_access_token.refresh_token"
)
mock_refresh_token.side_effect = Exception("some exception")

event_in = {"queryStringParameters": {"refresh_token": "refresh_token"}}
Expand All @@ -329,7 +334,9 @@ def test_refresh_twitch_access_token_handler_refresh_token_raises_exception(mock
def test_refresh_twitch_access_token_handler_refresh_token_fails_to_refresh_token(
mocker,
):
mock_refresh_token = mocker.patch("refresh_twitch_access_token.refresh_token")
mock_refresh_token = mocker.patch(
"aws.lambdas.refresh_twitch_access_token.refresh_token"
)
mock_refresh_token.return_value = False, json.dumps(
{
"error": "Bad Request",
Expand All @@ -350,7 +357,7 @@ def test_refresh_twitch_access_token_handler_refresh_token_fails_to_refresh_toke
def test_refresh_twitch_access_token_handler_store_in_dynamodb_put_and_update(
mocker, set_environment_variables, db_item
):
mock_get_secret = mocker.patch("refresh_twitch_access_token.get_secret")
mock_get_secret = mocker.patch("aws.lambdas.refresh_twitch_access_token.get_secret")
mock_get_secret.side_effect = ["client_id", "client_secret"]

expected_token_refresh_response = json.dumps(
Expand Down
55 changes: 36 additions & 19 deletions aws/tests/test_store_oauth_authorize_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
from botocore.exceptions import ClientError
from moto import mock_aws
from moto.ssm.exceptions import ParameterNotFound
from store_oauth_authorize_code import get_parameter, get_secret
from store_oauth_authorize_code import (

from aws.lambdas.store_oauth_authorize_code import get_parameter, get_secret
from aws.lambdas.store_oauth_authorize_code import (
lambda_handler as store_oauth_authorize_code_handler,
)
from store_oauth_authorize_code import store_in_dynamodb, validate_token
from aws.lambdas.store_oauth_authorize_code import store_in_dynamodb, validate_token


@pytest.fixture
Expand Down Expand Up @@ -343,7 +344,9 @@ def test_store_in_dynamodb_client_error(mocker, set_environment_variables):

@pytest.fixture
def mock_get_parameter(mocker):
return mocker.patch("store_oauth_authorize_code.get_parameter", autospec=True)
return mocker.patch(
"aws.lambdas.store_oauth_authorize_code.get_parameter", autospec=True
)


def test_get_secret_success(mock_get_parameter):
Expand Down Expand Up @@ -378,10 +381,12 @@ def test_get_secret_failure(mock_get_parameter):

@mock_aws
def test_lambda_handler_success(mocker):
mock_get_secret = mocker.patch("store_oauth_authorize_code.get_secret")
mock_get_secret = mocker.patch("aws.lambdas.store_oauth_authorize_code.get_secret")
mock_get_secret.side_effect = ["client_id", "client_secret"]

mock_get_parameter = mocker.patch("store_oauth_authorize_code.get_parameter")
mock_get_parameter = mocker.patch(
"aws.lambdas.store_oauth_authorize_code.get_parameter"
)
mock_get_parameter.return_value = "redirect_uri"

mock_token_response = mocker.Mock()
Expand All @@ -396,7 +401,9 @@ def test_lambda_handler_success(mocker):
"token_type": "bearer",
}

mock_validate_token = mocker.patch("store_oauth_authorize_code.validate_token")
mock_validate_token = mocker.patch(
"aws.lambdas.store_oauth_authorize_code.validate_token"
)
mock_validate_token.return_value = True, json.dumps(
{
"client_id": "wbmytr93xzw8zbg0p1izqyzzc5mbiz",
Expand All @@ -408,7 +415,7 @@ def test_lambda_handler_success(mocker):
)

mock_store_in_dynamodb = mocker.patch(
"store_oauth_authorize_code.store_in_dynamodb"
"aws.lambdas.store_oauth_authorize_code.store_in_dynamodb"
)
mock_store_in_dynamodb.return_value = None

Expand Down Expand Up @@ -533,7 +540,7 @@ def test_lambda_handler_code_is_none():


def test_lambda_handler_get_secret_parameter_not_found(mocker):
mock_get_secret = mocker.patch("store_oauth_authorize_code.get_secret")
mock_get_secret = mocker.patch("aws.lambdas.store_oauth_authorize_code.get_secret")
mock_get_secret.side_effect = ParameterNotFound(message="message")

event_in = {"queryStringParameters": {"code": "code"}}
Expand All @@ -546,7 +553,7 @@ def test_lambda_handler_get_secret_parameter_not_found(mocker):


def test_lambda_handler_get_secret_exception(mocker):
mock_get_secret = mocker.patch("store_oauth_authorize_code.get_secret")
mock_get_secret = mocker.patch("aws.lambdas.store_oauth_authorize_code.get_secret")
mock_get_secret.side_effect = Exception

event_in = {"queryStringParameters": {"code": "code"}}
Expand All @@ -558,10 +565,12 @@ def test_lambda_handler_get_secret_exception(mocker):


def test_lambda_handler_get_parameter_exception(mocker):
mock_get_secret = mocker.patch("store_oauth_authorize_code.get_secret")
mock_get_secret = mocker.patch("aws.lambdas.store_oauth_authorize_code.get_secret")
mock_get_secret.side_effect = ["client_id", "client_secret"]

mock_get_parameter = mocker.patch("store_oauth_authorize_code.get_parameter")
mock_get_parameter = mocker.patch(
"aws.lambdas.store_oauth_authorize_code.get_parameter"
)
mock_get_parameter.side_effect = Exception

event_in = {"queryStringParameters": {"code": "code"}}
Expand All @@ -573,10 +582,12 @@ def test_lambda_handler_get_parameter_exception(mocker):


def test_lambda_handler_invalid_token_response(mocker):
mock_get_secret = mocker.patch("store_oauth_authorize_code.get_secret")
mock_get_secret = mocker.patch("aws.lambdas.store_oauth_authorize_code.get_secret")
mock_get_secret.side_effect = ["client_id", "client_secret"]

mock_get_parameter = mocker.patch("store_oauth_authorize_code.get_parameter")
mock_get_parameter = mocker.patch(
"aws.lambdas.store_oauth_authorize_code.get_parameter"
)
mock_get_parameter.return_value = "redirect_uri"

mock_token_response = mocker.Mock()
Expand All @@ -593,7 +604,9 @@ def test_lambda_handler_invalid_token_response(mocker):
mock_response = mocker.patch("requests.post")
mock_response.return_value = mock_token_response

mock_validate_token = mocker.patch("store_oauth_authorize_code.validate_token")
mock_validate_token = mocker.patch(
"aws.lambdas.store_oauth_authorize_code.validate_token"
)
mock_validate_token.return_value = False, json.dumps(
{"status": 401, "message": "invalid access token"}
)
Expand All @@ -606,10 +619,12 @@ def test_lambda_handler_invalid_token_response(mocker):


def test_lambda_handler_failed_token_response(mocker):
mock_get_secret = mocker.patch("store_oauth_authorize_code.get_secret")
mock_get_secret = mocker.patch("aws.lambdas.store_oauth_authorize_code.get_secret")
mock_get_secret.side_effect = ["client_id", "client_secret"]

mock_get_parameter = mocker.patch("store_oauth_authorize_code.get_parameter")
mock_get_parameter = mocker.patch(
"aws.lambdas.store_oauth_authorize_code.get_parameter"
)
mock_get_parameter.return_value = "redirect_uri"

mock_token_response = mocker.Mock()
Expand All @@ -629,10 +644,12 @@ def test_lambda_handler_failed_token_response(mocker):


def test_lambda_handler_http_error_response(mocker):
mock_get_secret = mocker.patch("store_oauth_authorize_code.get_secret")
mock_get_secret = mocker.patch("aws.lambdas.store_oauth_authorize_code.get_secret")
mock_get_secret.side_effect = ["client_id", "client_secret"]

mock_get_parameter = mocker.patch("store_oauth_authorize_code.get_parameter")
mock_get_parameter = mocker.patch(
"aws.lambdas.store_oauth_authorize_code.get_parameter"
)
mock_get_parameter.return_value = "redirect_uri"

mocker.patch("requests.post", side_effect=Exception("Mocked exception"))
Expand Down
2 changes: 1 addition & 1 deletion twitchrce/custom_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
StreamOfflineData,
StreamOnlineData,
)
from utils.utils import Utils

from twitchrce.api.virustotal.virus_total_api import VirusTotalApiClient
from twitchrce.config.bot_config import BotConfig
from twitchrce.esclient import CustomEventSubClient
from twitchrce.psclient import CustomPubSubClient
from twitchrce.utils.utils import Utils

logging.basicConfig(
level=logging.INFO,
Expand Down

0 comments on commit d157222

Please sign in to comment.