import pytest import flask from pytest_lazyfixture import lazy_fixture class TestAccountsStatesReportView: ENDPOINT = "accounts_validation.accounts_states_report_view" def test_failure_authentication(self, app): with app.test_client() as client: resp = client.get(flask.url_for(self.ENDPOINT)) assert resp.status_code == 302 def test_failure_authorization(self, app, authenticated): with app.test_client() as client: resp = client.get(flask.url_for(self.ENDPOINT)) assert resp.status_code == 401 @pytest.mark.parametrize( "authorized_user, status", [ (lazy_fixture("authorized_developer"), 401), ], ) def test_no_access(self, app, authorized_user, status): with app.test_client() as client: resp = client.get(flask.url_for(self.ENDPOINT)) assert resp.status_code == status @pytest.mark.parametrize( "authorized_user", [ lazy_fixture("authorized_admin"), lazy_fixture("authorized_manager"), ], ) def test_success(self, app, faker, mocker, pgdb_session, authorized_user): headers = [ "Email", "Is Active", "Last Login", ] mocked_report = mocker.patch( "atlas_um.accounts_validation.views.accounts_states_report" ) mocked_report.return_value = self.fake_report([headers]) with app.test_client() as client: resp = client.get(flask.url_for(self.ENDPOINT)) assert resp.status_code == 200 assert mocked_report.called assert resp.text == ",".join(headers) + "\r\n" @staticmethod def fake_report(rows): for row in rows: yield row class TestAccountsValidationTaskView: ENDPOINT = "accounts_validation.accounts_validation_task_view" def test_failure_authentication(self, app): with app.test_client() as client: resp = client.get(flask.url_for(self.ENDPOINT)) assert resp.status_code == 302 def test_failure_authorization(self, app, authenticated): with app.test_client() as client: resp = client.get(flask.url_for(self.ENDPOINT)) assert resp.status_code == 401 @pytest.mark.parametrize( "authorized_user, status", [ (lazy_fixture("authorized_developer"), 401), ], ) def test_no_access(self, app, authorized_user, status): with app.test_client() as client: resp = client.get(flask.url_for(self.ENDPOINT)) assert resp.status_code == status @pytest.mark.parametrize( "authorized_user", [lazy_fixture("authorized_admin")], ) def test_success_get( self, app, faker, mocker, pgdb_session, authorized_user ): mocker.spy(flask, "render_template") with app.test_client() as client: resp = client.get(flask.url_for(self.ENDPOINT)) assert resp.status_code == 200 assert flask.render_template.call_args_list == [ mocker.call("accounts_validation/accounts_validation_task.html") ] @pytest.mark.parametrize( "authorized_user", [lazy_fixture("authorized_admin")], ) def test_success_post( self, app, faker, mocker, pgdb_session, authorized_user ): mocked_task = mocker.patch("atlas_um.accounts_validation.views.tasks") with app.test_client() as client: resp = client.post(flask.url_for(self.ENDPOINT)) assert resp.status_code == 302 assert resp.location == flask.url_for(self.ENDPOINT, _external=False) assert mocked_task.accounts_validation_task.delay.called