import flask import pytest from pytest_lazyfixture import lazy_fixture from werkzeug.datastructures import ImmutableMultiDict from atlas_um.applications.forms import ApplicationForm from atlas_um.auth.claims import ResourceGroups from atlas_um.helpers import either from atlas_um.helpers.ordering import QueryOrdering from atlas_um.helpers.pagination import QueryPagination from atlas_um.tokens.bearer_tokens import M2MBearerToken from tests.atlas_um import factories from tests.atlas_um.pytest_helpers import ( get_default_applications_list_ordering, ) class TestList: def test_list_failure_authentication(self, app): with app.test_client() as client: resp = client.get(flask.url_for("applications.list")) assert resp.status_code == 302 def test_list_failure_authorization(self, app, authenticated): with app.test_client() as client: resp = client.get(flask.url_for("applications.list")) assert resp.status_code == 401 @pytest.mark.parametrize( "authorized_user, status", [ (lazy_fixture("authorized_manager"), 401), ], ) def test_list_apps_no_access(self, app, authorized_user, status): with app.test_client() as client: resp = client.get(flask.url_for("applications.list")) assert resp.status_code == status def test_list_admin_success( self, app, mocker, faker, pgdb_session, authorized_admin ): applications_count = faker.pyint(min_value=2, max_value=10) applications = factories.ApplicationFactory.build_batch( applications_count ) for application in applications: pgdb_session.add(application) pgdb_session.commit() mocker.spy(flask, "render_template") with app.test_client() as client: resp = client.get(flask.url_for("applications.list")) assert resp.status_code == 200 mocker.patch.object( QueryOrdering, "query", new_callable=mocker.PropertyMock ).return_value = applications mocker.patch.object( QueryPagination, "items", new_callable=mocker.PropertyMock ).return_value = applications with app.test_request_context(flask.url_for("applications.list")): render_expects = mocker.call( "applications/list_applications.html", applications=applications, ordering=get_default_applications_list_ordering(applications), pagination=QueryPagination(), ) assert flask.render_template.call_args_list == [render_expects] def test_list_developer_success( self, app, mocker, faker, pgdb_session, authorized_developer ): dna_account = factories.DNAAccountFactory.build() applications_count = faker.pyint(min_value=2, max_value=10) applications = factories.ApplicationFactory.build_batch( applications_count, dna_account=dna_account ) for application in applications: pgdb_session.add(application) pgdb_session.commit() mocker.patch( "atlas_um.auth.models.User.dna_account", new_callable=mocker.PropertyMock, ).return_value = dna_account authorized_developer.claims.append( ResourceGroups( ResourceGroups.Values( a.resource_group.namespace_url for a in applications ) ) ) mocker.spy(flask, "render_template") with app.test_client() as client: resp = client.get(flask.url_for("applications.list")) assert resp.status_code == 200 mocker.patch.object( QueryOrdering, "query", new_callable=mocker.PropertyMock ).return_value = applications mocker.patch.object( QueryPagination, "items", new_callable=mocker.PropertyMock ).return_value = applications with app.test_request_context(flask.url_for("applications.list")): render_expects = mocker.call( "applications/list_applications.html", applications=applications, ordering=get_default_applications_list_ordering(applications), pagination=QueryPagination(), ) assert flask.render_template.call_args_list == [render_expects] def test_list_pagination_success( self, app, mocker, faker, pgdb_session, authorized_admin ): per_page = 10 page = 2 applications_count = faker.pyint(min_value=20, max_value=50) applications = factories.ApplicationFactory.build_batch( applications_count ) for application in applications: pgdb_session.add(application) pgdb_session.commit() mocker.spy(flask, "render_template") with app.test_client() as client: resp = client.get( flask.url_for( "applications.list", per_page=per_page, page=page ) ) assert resp.status_code == 200 mocker.patch.object( QueryOrdering, "query", new_callable=mocker.PropertyMock ).return_value = applications mocker.patch.object( QueryPagination, "items", new_callable=mocker.PropertyMock ).return_value = applications with app.test_request_context( flask.url_for("applications.list", per_page=per_page, page=page) ): render_expects = mocker.call( "applications/list_applications.html", applications=applications[ per_page * (page - 1) : per_page * page # noqa ], ordering=get_default_applications_list_ordering( applications[ per_page * (page - 1) : per_page * page # noqa ], ), pagination=QueryPagination(), ) assert flask.render_template.call_args_list == [render_expects] class TestCreate: def test_failure_authentication(self, app): with app.test_client() as client: resp = client.get(flask.url_for("applications.create")) assert resp.status_code == 302 with app.test_client() as client: resp = client.post(flask.url_for("applications.create")) assert resp.status_code == 302 def test_failure_authorization(self, app, authenticated): with app.test_client() as client: resp = client.get(flask.url_for("applications.create")) assert resp.status_code == 401 with app.test_client() as client: resp = client.post(flask.url_for("applications.create")) assert resp.status_code == 401 @pytest.mark.parametrize( "authorized_user, status", [ (lazy_fixture("authorized_manager"), 401), ], ) def test_get_app_no_access(self, app, authorized_user, status): with app.test_client() as client: resp = client.get(flask.url_for("applications.create")) assert resp.status_code == status def test_get_admin_success( self, app, mocker, authorized_admin, pgdb_session ): mocker.spy(flask, "render_template") with app.test_client() as client: resp = client.get(flask.url_for("applications.create")) assert resp.status_code == 200 form = ApplicationForm() with app.test_request_context(flask.url_for("applications.create")): render_expects = mocker.call( "applications/create_application.html", form=form ) assert flask.render_template.call_args_list == [render_expects] def test_get_developer_success( self, app, mocker, authorized_developer, pgdb_session ): mocker.spy(flask, "render_template") with app.test_client() as client: resp = client.get(flask.url_for("applications.create")) assert resp.status_code == 200 form = ApplicationForm() with app.test_request_context(flask.url_for("applications.create")): render_expects = mocker.call( "applications/create_application.html", form=form ) assert flask.render_template.call_args_list == [render_expects] @pytest.mark.parametrize( "authorized_user, status", [ (lazy_fixture("authorized_manager"), 401), ], ) def test_create_app_no_access(self, app, authorized_user, status): with app.test_client() as client: resp = client.post(flask.url_for("applications.create"), data={}) assert resp.status_code == status def test_post_admin_success( self, app, mocker, faker, pgdb_session, authorized_admin ): resource_group = factories.ResourceGroupFactory.build() pgdb_session.add(resource_group) pgdb_session.commit() expected_data = { "name": faker.pystr(), "audience": faker.pystr(), "resource_group": resource_group, "ttl": faker.pyint(), "scope": faker.pystr(), } form = ApplicationForm(data=expected_data) assert form.validate(), form.errors mocker.spy(flask, "redirect") mocked_dna_account = mocker.patch( "atlas_um.auth.models.User.dna_account" ) mocked_service = mocker.patch( "atlas_um.applications.services.CreateApplicationService.execute" ) application = factories.ApplicationFactory() pgdb_session.add(application) pgdb_session.commit() mocked_service.return_value = either.Right(application) with app.test_client() as client: resp = client.post( flask.url_for("applications.create"), data=form.dump() ) assert resp.status_code == 302 assert mocked_service.call_args_list == [ mocker.call(dna_account=mocked_dna_account, **expected_data) ] assert flask.redirect.call_args_list == [ mocker.call( flask.url_for( "applications.update", application_id=application.id, _external=False, ) ) ] def test_post_developer_success( self, app, mocker, pgdb_session, faker, authorized_developer ): resource_group = factories.ResourceGroupFactory.build() pgdb_session.add(resource_group) pgdb_session.commit() expected_data = { "name": faker.pystr(), "audience": faker.pystr(), "resource_group": resource_group, "ttl": faker.pyint(), "scope": faker.pystr(), } application = factories.ApplicationFactory.build( resource_group=resource_group ) pgdb_session.add(application) pgdb_session.commit() authorized_developer.claims.append( ResourceGroups( ResourceGroups.Values( [application.resource_group.namespace_url] ) ) ) form = ApplicationForm(data=expected_data) assert form.validate(), form.errors mocker.spy(flask, "redirect") mocked_dna_account = mocker.patch( "atlas_um.auth.models.User.dna_account" ) mocked_service = mocker.patch( "atlas_um.applications.services.CreateApplicationService.execute" ) mocked_service.return_value = either.Right(application) with app.test_client() as client: resp = client.post( flask.url_for("applications.create"), data=form.dump() ) assert resp.status_code == 302 assert mocked_service.call_args_list == [ mocker.call(dna_account=mocked_dna_account, **expected_data) ] assert flask.redirect.call_args_list == [ mocker.call( flask.url_for( "applications.update", application_id=application.id, _external=False, ) ) ] def test_post_developer_failure( self, app, mocker, pgdb_session, faker, authorized_developer ): resource_group = factories.ResourceGroupFactory.build() pgdb_session.add(resource_group) pgdb_session.commit() expected_data = { "name": faker.pystr(), "audience": faker.pystr(), "resource_group": resource_group, "ttl": faker.pyint(), "scope": faker.pystr(), } application = factories.ApplicationFactory.build() form = ApplicationForm(data=expected_data) assert not form.validate(), form.errors mocker.spy(flask, "render_template") mocked_service = mocker.patch( "atlas_um.applications.services.CreateApplicationService.execute" ) mocked_service.return_value = either.Right(application) with app.test_client() as client: resp = client.post( flask.url_for("applications.create"), data=form.dump() ) assert resp.status_code == 200 assert not mocked_service.called with app.test_request_context(flask.url_for("applications.create")): render_expects = mocker.call( "applications/create_application.html", form=ApplicationForm( data={**expected_data, "resource_group": None} ), ) assert flask.render_template.call_args_list == [render_expects] class TestUpdate: def test_failure_authentication(self, app, pgdb_session): application = factories.ApplicationFactory.build() pgdb_session.add(application) pgdb_session.commit() with app.test_client() as client: resp = client.get( flask.url_for( "applications.update", application_id=application.id ) ) assert resp.status_code == 302 with app.test_client() as client: resp = client.post( flask.url_for( "applications.update", application_id=application.id ) ) assert resp.status_code == 302 def test_failure_authorization(self, app, pgdb_session, authenticated): application = factories.ApplicationFactory.build() pgdb_session.add(application) pgdb_session.commit() with app.test_client() as client: resp = client.get( flask.url_for( "applications.update", application_id=application.id ) ) assert resp.status_code == 401 with app.test_client() as client: resp = client.post( flask.url_for( "applications.update", application_id=application.id ) ) assert resp.status_code == 401 @pytest.mark.parametrize( "authorized_user, status", [ (lazy_fixture("authorized_manager"), 401), ], ) def test_update_app_no_access( self, app, pgdb_session, authorized_user, status ): application = factories.ApplicationFactory.build() pgdb_session.add(application) pgdb_session.commit() with app.test_client() as client: resp = client.post( flask.url_for( "applications.update", application_id=application.id ), ) assert resp.status_code == status def test_failure_not_owner( self, app, pgdb_session, mocker, authorized_developer ): dna_account = factories.DNAAccountFactory.build() pgdb_session.add(dna_account) pgdb_session.commit() mocker.patch( "atlas_um.auth.models.User.dna_account", new_callable=mocker.PropertyMock, ).return_value = dna_account application = factories.ApplicationFactory.build() pgdb_session.add(application) pgdb_session.commit() with app.test_client() as client: resp = client.get( flask.url_for( "applications.update", application_id=application.id ) ) assert resp.status_code == 404 with app.test_client() as client: resp = client.post( flask.url_for( "applications.update", application_id=application.id ) ) assert resp.status_code == 404 def test_get_admin_success( self, app, mocker, authorized_admin, pgdb_session ): application = factories.ApplicationFactory.build() pgdb_session.add(application) pgdb_session.commit() mocker.spy(flask, "render_template") with app.test_client() as client: resp = client.get( flask.url_for( "applications.update", application_id=application.id ) ) assert resp.status_code == 200 form = ApplicationForm(obj=application) with app.test_request_context( flask.url_for("applications.update", application_id=application.id) ): render_expects = mocker.call( "applications/update_application.html", application=application, form=form, ) assert flask.render_template.call_args_list == [render_expects] def test_get_developer_success( self, app, mocker, authorized_developer, pgdb_session ): dna_account = factories.DNAAccountFactory.build() pgdb_session.add(dna_account) pgdb_session.commit() mocker.patch( "atlas_um.auth.models.User.dna_account", new_callable=mocker.PropertyMock, ).return_value = dna_account application = factories.ApplicationFactory.build( dna_account=dna_account ) pgdb_session.add(application) pgdb_session.commit() mocker.spy(flask, "render_template") with app.test_client() as client: resp = client.get( flask.url_for( "applications.update", application_id=application.id ) ) assert resp.status_code == 200 form = ApplicationForm(obj=application) with app.test_request_context( flask.url_for("applications.update", application_id=application.id) ): render_expects = mocker.call( "applications/update_application.html", application=application, form=form, ) assert flask.render_template.call_args_list == [render_expects] def test_post_admin_success( self, app, mocker, faker, pgdb_session, authorized_admin ): application = factories.ApplicationFactory.build() pgdb_session.add(application) pgdb_session.commit() expected_data = { "name": faker.pystr(), "audience": faker.pystr(), "resource_group": application.resource_group, "ttl": faker.pyint(), "scope": faker.pystr(), } form = ApplicationForm(data=expected_data) assert form.validate(), form.errors mocker.spy(flask, "redirect") mocked_service = mocker.patch( "atlas_um.applications.services.UpdateApplicationService.execute" ) application = factories.ApplicationFactory() pgdb_session.add(application) pgdb_session.commit() mocked_service.return_value = either.Right(application) with app.test_client() as client: resp = client.post( flask.url_for( "applications.update", application_id=application.id ), data=form.dump(), ) assert resp.status_code == 302 assert mocked_service.call_args_list == [ mocker.call(application=application, **expected_data) ] assert flask.redirect.call_args_list == [ mocker.call( flask.url_for( "applications.list", _external=False, ) ) ] def test_post_developer_success( self, app, mocker, pgdb_session, faker, authorized_developer ): dna_account = factories.DNAAccountFactory.build() pgdb_session.add(dna_account) pgdb_session.commit() mocker.patch( "atlas_um.auth.models.User.dna_account", new_callable=mocker.PropertyMock, ).return_value = dna_account resource_group = factories.ResourceGroupFactory.build() pgdb_session.add(resource_group) pgdb_session.commit() expected_data = { "name": faker.pystr(), "audience": faker.pystr(), "resource_group": resource_group, "ttl": faker.pyint(), "scope": faker.pystr(), } application = factories.ApplicationFactory.build( resource_group=resource_group, dna_account=dna_account ) pgdb_session.add(application) pgdb_session.commit() authorized_developer.claims.append( ResourceGroups( ResourceGroups.Values( [application.resource_group.namespace_url] ) ) ) form = ApplicationForm(data=expected_data) assert form.validate(), form.errors mocker.spy(flask, "redirect") mocked_service = mocker.patch( "atlas_um.applications.services.UpdateApplicationService.execute" ) mocked_service.return_value = either.Right(application) with app.test_client() as client: resp = client.post( flask.url_for( "applications.update", application_id=application.id ), data=form.dump(), ) assert resp.status_code == 302 assert mocked_service.call_args_list == [ mocker.call(application=application, **expected_data) ] assert flask.redirect.call_args_list == [ mocker.call( flask.url_for( "applications.list", _external=False, ) ) ] def test_post_developer_failure_resource_group( self, app, mocker, pgdb_session, faker, authorized_developer ): dna_account = factories.DNAAccountFactory.build() pgdb_session.add(dna_account) pgdb_session.commit() mocker.patch( "atlas_um.auth.models.User.dna_account", new_callable=mocker.PropertyMock, ).return_value = dna_account resource_group = factories.ResourceGroupFactory.build() pgdb_session.add(resource_group) pgdb_session.commit() expected_data = { "name": faker.pystr(), "audience": faker.pystr(), "resource_group": resource_group, "ttl": faker.pyint(), "scope": faker.pystr(), } application = factories.ApplicationFactory.build( dna_account=dna_account ) pgdb_session.add(application) pgdb_session.commit() form = ApplicationForm(data=expected_data) assert ( not form.validate() and "resource_group" in form.errors ), form.errors mocker.spy(flask, "render_template") mocked_service = mocker.patch( "atlas_um.applications.services.UpdateApplicationService.execute" ) mocked_service.return_value = either.Right(application) with app.test_client() as client: resp = client.post( flask.url_for( "applications.update", application_id=application.id ), data=form.dump(), ) assert resp.status_code == 200 assert not mocked_service.called with app.test_request_context( flask.url_for("applications.update", application_id=application.id) ): render_expects = mocker.call( "applications/update_application.html", application=application, form=ApplicationForm( formdata=ImmutableMultiDict( {**expected_data, "resource_group": None} ), obj=application, ), ) assert flask.render_template.call_args_list == [render_expects] class TestDelete: FAKE_HOST = "atlas_um.test" def test_failure_authentication(self, app, pgdb_session): application = factories.ApplicationFactory.build() pgdb_session.add(application) pgdb_session.commit() with app.test_client() as client: resp = client.post( flask.url_for( "applications.delete", application_id=application.id ) ) assert resp.status_code == 302 def test_failure_authorization(self, app, pgdb_session, authenticated): application = factories.ApplicationFactory.build() pgdb_session.add(application) pgdb_session.commit() with app.test_client() as client: resp = client.post( flask.url_for( "applications.delete", application_id=application.id ) ) assert resp.status_code == 401 @pytest.mark.parametrize( "authorized_user, status", [ (lazy_fixture("authorized_manager"), 401), ], ) def test_delete_app_no_access( self, app, pgdb_session, authorized_user, status ): application = factories.ApplicationFactory.build() pgdb_session.add(application) pgdb_session.commit() with app.test_client() as client: resp = client.post( flask.url_for( "applications.delete", application_id=application.id ), ) assert resp.status_code == status def test_failure_not_owner( self, app, pgdb_session, mocker, authorized_developer ): dna_account = factories.DNAAccountFactory.build() pgdb_session.add(dna_account) pgdb_session.commit() mocker.patch( "atlas_um.auth.models.User.dna_account", new_callable=mocker.PropertyMock, ).return_value = dna_account application = factories.ApplicationFactory.build() pgdb_session.add(application) pgdb_session.commit() with app.test_client() as client: resp = client.post( flask.url_for( "applications.delete", application_id=application.id ) ) assert resp.status_code == 404 def test_post_admin_success( self, app, mocker, faker, pgdb_session, authorized_admin ): application = factories.ApplicationFactory.build() pgdb_session.add(application) pgdb_session.commit() mocker.spy(flask, "redirect") mocked_service = mocker.patch( "atlas_um.applications.services.DeleteApplicationService.execute" ) application = factories.ApplicationFactory() pgdb_session.add(application) pgdb_session.commit() mocked_service.return_value = either.Right(application) with app.test_client() as client: resp = client.post( flask.url_for( "applications.delete", application_id=application.id ) ) assert resp.status_code == 302 assert mocked_service.call_args_list == [ mocker.call(application=application) ] assert flask.redirect.call_args_list == [ mocker.call( flask.url_for( "applications.list", _external=False, ) ) ] def test_post_developer_success( self, app, mocker, pgdb_session, faker, authorized_developer ): dna_account = factories.DNAAccountFactory.build() pgdb_session.add(dna_account) pgdb_session.commit() mocker.patch( "atlas_um.auth.models.User.dna_account", new_callable=mocker.PropertyMock, ).return_value = dna_account application = factories.ApplicationFactory.build( dna_account=dna_account ) pgdb_session.add(application) pgdb_session.commit() mocker.spy(flask, "redirect") mocked_service = mocker.patch( "atlas_um.applications.services.DeleteApplicationService.execute" ) mocked_service.return_value = either.Right(application) with app.test_client() as client: resp = client.post( flask.url_for( "applications.delete", application_id=application.id ), ) assert resp.status_code == 302 assert mocked_service.call_args_list == [ mocker.call(application=application) ] assert flask.redirect.call_args_list == [ mocker.call( flask.url_for( "applications.list", _external=False, ) ) ] class TestResetSecret: def test_failure_authentication(self, app, pgdb_session): application = factories.ApplicationFactory.build() pgdb_session.add(application) pgdb_session.commit() with app.test_client() as client: resp = client.post( flask.url_for( "applications.reset_secret", application_id=application.id ) ) assert resp.status_code == 302 def test_failure_authorization(self, app, pgdb_session, authenticated): application = factories.ApplicationFactory.build() pgdb_session.add(application) pgdb_session.commit() with app.test_client() as client: resp = client.post( flask.url_for( "applications.reset_secret", application_id=application.id ) ) assert resp.status_code == 401 @pytest.mark.parametrize( "authorized_user, status", [ (lazy_fixture("authorized_manager"), 401), ], ) def test_reset_secret_no_access( self, app, pgdb_session, authorized_user, status ): application = factories.ApplicationFactory.build() pgdb_session.add(application) pgdb_session.commit() with app.test_client() as client: resp = client.post( flask.url_for( "applications.reset_secret", application_id=application.id ), ) assert resp.status_code == status def test_failure_not_owner( self, app, pgdb_session, mocker, authorized_developer ): dna_account = factories.DNAAccountFactory.build() pgdb_session.add(dna_account) pgdb_session.commit() mocker.patch( "atlas_um.auth.models.User.dna_account", new_callable=mocker.PropertyMock, ).return_value = dna_account application = factories.ApplicationFactory.build() pgdb_session.add(application) pgdb_session.commit() with app.test_client() as client: resp = client.post( flask.url_for( "applications.reset_secret", application_id=application.id ) ) assert resp.status_code == 404 def test_post_admin_success( self, app, mocker, faker, pgdb_session, authorized_admin ): application = factories.ApplicationFactory.build() pgdb_session.add(application) pgdb_session.commit() mocker.spy(flask, "redirect") mocked_service = mocker.patch( "atlas_um.applications.services.ResetClientSecretService.execute" ) application = factories.ApplicationFactory() pgdb_session.add(application) pgdb_session.commit() mocked_service.return_value = either.Right(application) with app.test_client() as client: resp = client.post( flask.url_for( "applications.reset_secret", application_id=application.id ) ) assert resp.status_code == 302 assert mocked_service.call_args_list == [ mocker.call(application=application) ] assert flask.redirect.call_args_list == [ mocker.call( flask.url_for( "applications.update", application_id=application.id, _external=False, ) ) ] def test_post_developer_success( self, app, mocker, pgdb_session, faker, authorized_developer ): dna_account = factories.DNAAccountFactory.build() pgdb_session.add(dna_account) pgdb_session.commit() mocker.patch( "atlas_um.auth.models.User.dna_account", new_callable=mocker.PropertyMock, ).return_value = dna_account application = factories.ApplicationFactory.build( dna_account=dna_account ) pgdb_session.add(application) pgdb_session.commit() mocker.spy(flask, "redirect") mocked_service = mocker.patch( "atlas_um.applications.services.ResetClientSecretService.execute" ) mocked_service.return_value = either.Right(application) with app.test_client() as client: resp = client.post( flask.url_for( "applications.reset_secret", application_id=application.id ), ) assert resp.status_code == 302 assert mocked_service.call_args_list == [ mocker.call(application=application) ] assert flask.redirect.call_args_list == [ mocker.call( flask.url_for( "applications.update", application_id=application.id, _external=False, ) ) ] class TestGetToken: def test_failure_authentication(self, app, pgdb_session): application = factories.ApplicationFactory.build() pgdb_session.add(application) pgdb_session.commit() with app.test_client() as client: resp = client.get( flask.url_for( "applications.get_token", application_id=application.id ) ) assert resp.status_code == 302 def test_failure_authorization(self, app, pgdb_session, authenticated): application = factories.ApplicationFactory.build() pgdb_session.add(application) pgdb_session.commit() with app.test_client() as client: resp = client.get( flask.url_for( "applications.get_token", application_id=application.id ) ) assert resp.status_code == 401 @pytest.mark.parametrize( "authorized_user, status", [ (lazy_fixture("authorized_manager"), 401), ], ) def test_no_access(self, app, pgdb_session, authorized_user, status): application = factories.ApplicationFactory.build() pgdb_session.add(application) pgdb_session.commit() with app.test_client() as client: resp = client.get( flask.url_for( "applications.get_token", application_id=application.id ), ) assert resp.status_code == status def test_failure_not_owner( self, app, pgdb_session, mocker, authorized_developer ): dna_account = factories.DNAAccountFactory.build() pgdb_session.add(dna_account) pgdb_session.commit() mocker.patch( "atlas_um.auth.models.User.dna_account", new_callable=mocker.PropertyMock, ).return_value = dna_account application = factories.ApplicationFactory.build() pgdb_session.add(application) pgdb_session.commit() with app.test_client() as client: resp = client.get( flask.url_for( "applications.get_token", application_id=application.id ) ) assert resp.status_code == 404 def test_get_admin_success( self, app, mocker, authorized_admin, pgdb_session ): application = factories.ApplicationFactory.build() pgdb_session.add(application) pgdb_session.commit() mocker.spy(flask, "render_template") mocker.spy(M2MBearerToken, "encode") with app.test_client() as client: resp = client.get( flask.url_for( "applications.get_token", application_id=application.id ) ) assert resp.status_code == 200 assert flask.render_template.call_args_list == [ mocker.call( "applications/components/token_modal_content.html", token=M2MBearerToken.encode.spy_return, ) ] def test_get_developer_success( self, app, mocker, authorized_developer, pgdb_session ): dna_account = factories.DNAAccountFactory.build() pgdb_session.add(dna_account) pgdb_session.commit() mocker.patch( "atlas_um.auth.models.User.dna_account", new_callable=mocker.PropertyMock, ).return_value = dna_account application = factories.ApplicationFactory.build( dna_account=dna_account ) pgdb_session.add(application) pgdb_session.commit() mocker.spy(flask, "render_template") mocker.spy(M2MBearerToken, "encode") with app.test_client() as client: resp = client.get( flask.url_for( "applications.get_token", application_id=application.id, ) ) assert resp.status_code == 200 assert flask.render_template.call_args_list == [ mocker.call( "applications/components/token_modal_content.html", token=M2MBearerToken.encode.spy_return, ) ]