Best Python code snippet using localstack_python
test_mfa.py
Source:test_mfa.py  
1from datetime import datetime, timezone2from unittest import mock3import pytest4import massgov.pfml.mfa as mfa_actions5from tests.conftest import get_mock_logger6@pytest.fixture7def last_enabled_at():8    return datetime(2022, 1, 2, 0, 0, 0, tzinfo=timezone.utc)9class TestEnableMfa:10    mock_logger = get_mock_logger()11    @mock.patch("massgov.pfml.mfa.cognito.enable_user_mfa")12    def test_success(self, mock_enable_user_mfa, auth_token):13        mfa_actions.enable_mfa(auth_token)14        mock_enable_user_mfa.assert_called_once_with(auth_token)15    @mock.patch("massgov.pfml.mfa.logger", mock_logger)16    @mock.patch("massgov.pfml.mfa.cognito.enable_user_mfa")17    def test_logging(self, mock_enable_user_mfa, auth_token):18        mfa_actions.enable_mfa(auth_token)19        self.mock_logger.info.assert_any_call(20            "MFA enabled in Cognito",21        )22class TestDisableMfa:23    mock_logger = get_mock_logger()24    @mock.patch("massgov.pfml.mfa.cognito.disable_user_mfa")25    def test_success(self, mock_disable_user_mfa, auth_token):26        mfa_actions.disable_mfa(auth_token)27        mock_disable_user_mfa.assert_called_once_with(auth_token)28    @mock.patch("massgov.pfml.mfa.logger", mock_logger)29    @mock.patch("massgov.pfml.mfa.cognito.disable_user_mfa")30    def test_logging(self, mock_disable_user_mfa, auth_token):31        mfa_actions.disable_mfa(auth_token)32        self.mock_logger.info.assert_any_call(33            "MFA disabled in Cognito",34        )35class TestHandleMfaDisabled:36    mock_logger = get_mock_logger()37    @pytest.fixture(autouse=True)38    def with_env_vars(self, monkeypatch):39        monkeypatch.setenv("BOUNCE_FORWARDING_EMAIL_ADDRESS_ARN", "bounce_arn")40        monkeypatch.setenv("DISABLE_SENDING_EMAILS", None)41    @pytest.fixture42    def user(self, user_with_mfa):43        user_with_mfa.email_address = "claimant@mock.nava.com"44        return user_with_mfa45    @mock.patch("massgov.pfml.mfa.send_templated_email")46    @mock.patch("massgov.pfml.mfa.logger", mock_logger)47    def test_logging(self, mock_send_email, user, last_enabled_at):48        mfa_actions.handle_mfa_disabled(user, last_enabled_at)49        self.mock_logger.info.assert_any_call(50            "MFA disabled for user",51            extra={52                "last_enabled_at": mock.ANY,53                "time_since_enabled_in_sec": mock.ANY,54                "updated_by": "user",55            },56        )57        assert (58            self.mock_logger.info.call_args.kwargs["extra"]["last_enabled_at"].strftime("%Y-%m-%d")59            == "2022-01-02"60        )61    @mock.patch("massgov.pfml.mfa.cognito.disable_user_mfa")62    @mock.patch("massgov.pfml.mfa.send_templated_email")63    @mock.patch("massgov.pfml.mfa.logger", mock_logger)64    def test_with_no_last_enabled_at(self, mock_send_email, mock_disable_user_mfa, user):65        mfa_actions.handle_mfa_disabled(user, None)66        self.mock_logger.error.assert_any_call(67            "MFA disabled, but no last_enabled_at timestamp was available"68        )69    @mock.patch("massgov.pfml.mfa.send_templated_email")70    def test_email(self, mock_send_email, user, last_enabled_at):71        mfa_actions.handle_mfa_disabled(user, last_enabled_at)72        mock_send_email.assert_called_once_with(73            mock.ANY,74            "MfaHasBeenDisabled",75            "PFML_DoNotReply@eol.mass.gov",76            "PFML_DoNotReply@eol.mass.gov",77            "bounce_arn",78            mock.ANY,79        )80        assert mock_send_email.call_args.args[0].to_addresses == ["claimant@mock.nava.com"]81        assert mock_send_email.call_args.args[5] == {"phone_number_last_four": "3075"}82    @mock.patch("massgov.pfml.mfa.cognito.disable_user_mfa")83    @mock.patch("massgov.pfml.mfa.send_templated_email")84    @mock.patch("massgov.pfml.mfa.logger", mock_logger)85    def test_does_not_send_email_when_aws_integration_is_disabled(86        self, mock_send_email, mock_disable_user_mfa, user, last_enabled_at, monkeypatch87    ):88        monkeypatch.setenv("DISABLE_SENDING_EMAILS", "1")89        mfa_actions.handle_mfa_disabled(user, last_enabled_at)90        mock_send_email.assert_not_called()91        self.mock_logger.info.assert_any_call(92            "Skipping sending an MFA disabled notification email in local environment",93            extra={94                "last_enabled_at": mock.ANY,95                "time_since_enabled_in_sec": mock.ANY,96                "updated_by": "user",97            },98        )99    @mock.patch("massgov.pfml.mfa.cognito.disable_user_mfa")100    @mock.patch("massgov.pfml.mfa.send_templated_email")101    def test_sends_email_when_aws_integration_is_enabled(102        self, mock_send_email, mock_disable_user_mfa, user, last_enabled_at, monkeypatch103    ):104        monkeypatch.setenv("DISABLE_SENDING_EMAILS", "0")105        mfa_actions.handle_mfa_disabled(user, last_enabled_at)106        mock_send_email.assert_called_once_with(107            mock.ANY,108            "MfaHasBeenDisabled",109            "PFML_DoNotReply@eol.mass.gov",110            "PFML_DoNotReply@eol.mass.gov",111            "bounce_arn",112            mock.ANY,113        )114    @mock.patch("massgov.pfml.mfa.cognito.disable_user_mfa")115    @mock.patch("massgov.pfml.mfa.send_templated_email")116    def test_sends_email_when_environment_is_not_local(117        self, mock_send_email, mock_disable_user_mfa, user, last_enabled_at, monkeypatch118    ):119        monkeypatch.setenv("ENVIRONMENT", "prod")120        monkeypatch.setenv("DISABLE_SENDING_EMAILS", "1")121        mfa_actions.handle_mfa_disabled(user, last_enabled_at)122        mock_send_email.assert_called_once_with(123            mock.ANY,124            "MfaHasBeenDisabled",125            "PFML_DoNotReply@eol.mass.gov",126            "PFML_DoNotReply@eol.mass.gov",127            "bounce_arn",128            mock.ANY,129        )130class TestHandleMfaDisabledByAdmin:131    mock_logger = get_mock_logger()132    @pytest.fixture(autouse=True)133    def with_env_vars(self, monkeypatch):134        monkeypatch.setenv("BOUNCE_FORWARDING_EMAIL_ADDRESS_ARN", "bounce_arn")135        monkeypatch.setenv("DISABLE_SENDING_EMAILS", None)136    @pytest.fixture137    def user(self, user_with_mfa):138        user_with_mfa.email_address = "claimant@mock.nava.com"139        return user_with_mfa140    @mock.patch("massgov.pfml.mfa.cognito.admin_disable_user_mfa")141    @mock.patch("massgov.pfml.mfa.send_templated_email")142    def test_success(self, mock_send_email, mock_disable_mfa, user, last_enabled_at, auth_token):143        mfa_actions.handle_mfa_disabled_by_admin(user, last_enabled_at)144        mock_disable_mfa.assert_called_once_with(user.email_address)145    @mock.patch("massgov.pfml.mfa.cognito.admin_disable_user_mfa")146    @mock.patch("massgov.pfml.mfa.send_templated_email")147    @mock.patch("massgov.pfml.mfa.logger", mock_logger)148    def test_logging(self, mock_send_email, mock_disable_mfa, user, last_enabled_at):149        mfa_actions.handle_mfa_disabled_by_admin(user, last_enabled_at)150        self.mock_logger.info.assert_any_call(151            "MFA disabled for user",152            extra={153                "last_enabled_at": mock.ANY,154                "time_since_enabled_in_sec": mock.ANY,155                "updated_by": "admin",156            },157        )158        assert (159            self.mock_logger.info.call_args.kwargs["extra"]["last_enabled_at"].strftime("%Y-%m-%d")160            == "2022-01-02"161        )162    @mock.patch("massgov.pfml.mfa.cognito.admin_disable_user_mfa")163    @mock.patch("massgov.pfml.mfa.send_templated_email")164    @mock.patch("massgov.pfml.mfa.logger", mock_logger)165    def test_with_no_last_enabled_at(self, mock_send_email, mock_disable_mfa, user):166        mfa_actions.handle_mfa_disabled_by_admin(user, None)167        self.mock_logger.error.assert_any_call(168            "MFA disabled, but no last_enabled_at timestamp was available"169        )170        self.mock_logger.info.assert_any_call(171            "MFA disabled for user", extra={"updated_by": "admin"}172        )173    @mock.patch("massgov.pfml.mfa.cognito.admin_disable_user_mfa")174    @mock.patch("massgov.pfml.mfa.send_templated_email")175    @mock.patch("massgov.pfml.mfa.logger", mock_logger)176    def test_does_not_send_email_or_sync_to_cognito_when_aws_integration_is_disabled(177        self, mock_send_email, mock_disable_mfa, user, last_enabled_at, monkeypatch178    ):179        monkeypatch.setenv("DISABLE_SENDING_EMAILS", "1")180        mfa_actions.handle_mfa_disabled_by_admin(user, last_enabled_at)181        mock_send_email.assert_not_called()182        mock_disable_mfa.assert_not_called()183        self.mock_logger.info.assert_any_call(184            "Skipping updating Cognito or sending an MFA disabled notification email in local environment",185            extra={186                "last_enabled_at": mock.ANY,187                "time_since_enabled_in_sec": mock.ANY,188                "updated_by": "admin",189            },190        )191    @mock.patch("massgov.pfml.mfa.cognito.admin_disable_user_mfa")192    @mock.patch("massgov.pfml.mfa.send_templated_email")193    def test_sends_email_and_syncs_to_cognito_when_environment_is_not_local(194        self, mock_send_email, mock_disable_mfa, user, last_enabled_at, monkeypatch195    ):196        monkeypatch.setenv("ENVIRONMENT", "prod")197        monkeypatch.setenv("DISABLE_SENDING_EMAILS", "1")198        mfa_actions.handle_mfa_disabled_by_admin(user, last_enabled_at)199        mock_send_email.assert_called_once_with(200            mock.ANY,201            "MfaHasBeenDisabled",202            "PFML_DoNotReply@eol.mass.gov",203            "PFML_DoNotReply@eol.mass.gov",204            "bounce_arn",205            mock.ANY,206        )...views.py
Source:views.py  
...8		TO_EMAIL = ['contato@sparkit.com.br']9		nome 	= request.POST['nome']10		sender	= request.POST['sender']11		msg 	= request.POST['msg']12		send_templated_email(TO_EMAIL, 'emails/contato', locals())13		ENVIADO = True14	return locals()15@render_to(template='core/camp.html', mobile_template='core/mobile-camp.html')16def spark_camp(request):17	form = SparkCampForm(request.POST or None)18	ENVIADO = False19	20	if form.is_valid():21		TO_EMAIL = ['contato@sparkit.com.br']22		nome 			= form.cleaned_data['nome']23		nome_projeto 	= form.cleaned_data['nome_projeto']24		url_projeto 	= form.cleaned_data['url_projeto']25		desc_projeto 	= form.cleaned_data['desc_projeto']26		time 			= form.cleaned_data['time']27		email_2 		= form.cleaned_data['email']28		celular 		= form.cleaned_data['celular']29		print locals()30		send_templated_email(TO_EMAIL, 'emails/spark-campus', locals())31		ENVIADO = True...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
