from atlas_um.api.facades import APIQueryFacade from atlas_um.api.v2 import schemas from atlas_um.helpers.ordering import QueryOrdering from atlas_um.helpers.pagination import APIQueryPagination from atlas_um.pgdb import ( ClaimValue, DNAAccount, ClaimName, ResourceGroup, ) from tests.atlas_um import factories from tests.atlas_um.pytest_helpers import UrlForThisMixin class TestResourceGroups(UrlForThisMixin): ENDPOINT = "api_v2.resource_groups" def test_call_failure_authentication(self, app): with app.test_client() as client: resp = client.get(self.url_for_this(app)) assert resp.status_code == 401 def test_call_failure_authorization(self, app, authenticated): with app.test_client() as client: resp = client.get(self.url_for_this(app)) assert resp.status_code == 401 def test_call_no_cache( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): resource_groups = [ factories.ResourceGroupFactory.build() for _ in range(faker.pyint(1, 5)) ] resource_groups.append( factories.ResourceGroupFactory.build(is_deleted=True) ) for resource_group in resource_groups: pgdb_session.add(resource_group) pgdb_session.commit() url = self.url_for_this(app) mocked_dump = mocker.patch.object( schemas.ResourceGroupsResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: query = ResourceGroup.query.active() resp = client.get(url) with app.test_request_context(url): dump_expects = mocker.call( APIQueryFacade( query, allowed_fields=[ ResourceGroup.name, ResourceGroup.external_id, ], fields_mapping={"id": "external_id"}, ) ) assert resp.status_code == 200 assert mocked_dump.call_args_list == [dump_expects] assert not bypassed_cache.get.called assert not bypassed_cache.set.called def test_call_cache_hit( self, app, faker, mocker, pgdb_session, authorized_api_client, redis_session, ): resource_groups = [ factories.ResourceGroupFactory.build() for _ in range(faker.pyint(1, 5)) ] for resource_group in resource_groups: pgdb_session.add(resource_group) pgdb_session.commit() url = self.url_for_this(app) mocked_dump = mocker.patch.object( schemas.ResourceGroupsResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: client.get(url) resp = client.get(url) assert resp.status_code == 200 assert mocked_dump.call_count == 1 class TestClaimNames(UrlForThisMixin): ENDPOINT = "api_v2.claim_names" def test_call_failure_authentication(self, app): resource_group = factories.ResourceGroupFactory.build() with app.test_client() as client: resp = client.get( self.url_for_this( app, resource_group_id=resource_group.external_id ) ) assert resp.status_code == 401 def test_call_failure_authorization(self, app, authenticated): resource_group = factories.ResourceGroupFactory.build() with app.test_client() as client: resp = client.get( self.url_for_this( app, resource_group_id=resource_group.external_id ) ) assert resp.status_code == 401 def test_call_no_resource_group( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): resource_group = factories.ResourceGroupFactory.build() claim_names = [ factories.ClaimNameFactory.build(resource_group=resource_group) for _ in range(faker.pyint(1, 5)) ] claim_names.append( factories.ClaimNameFactory.build( resource_group=resource_group, is_deleted=True ) ) for claim_name in claim_names: pgdb_session.add(claim_name) pgdb_session.commit() url = self.url_for_this(app, resource_group_id="wrong_id") mocked_dump = mocker.patch.object( schemas.ClaimNamesResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not mocked_dump.called def test_call_no_cache( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): resource_group = factories.ResourceGroupFactory.build() claim_names = [ factories.ClaimNameFactory.build(resource_group=resource_group) for _ in range(faker.pyint(1, 5)) ] claim_names.append( factories.ClaimNameFactory.build( resource_group=resource_group, is_deleted=True ) ) for claim_name in claim_names: pgdb_session.add(claim_name) pgdb_session.commit() url = self.url_for_this( app, resource_group_id=resource_group.external_id ) mocked_dump = mocker.patch.object( schemas.ClaimNamesResponseSchema, "dump" ) mocked_dump.return_value = {} query = ClaimName.query.active().by_resource_group(resource_group) with app.test_client() as client: resp = client.get(url) with app.test_request_context(url): dump_expects = mocker.call( APIQueryFacade( query, allowed_fields=[ ClaimName.friendly, ClaimName.external_id, ], fields_mapping={"id": "external_id"}, ) ) assert resp.status_code == 200 assert mocked_dump.call_args_list == [dump_expects] def test_call_cache_hit( self, app, faker, mocker, pgdb_session, authorized_api_client, redis_session, ): resource_group = factories.ResourceGroupFactory.build() claim_names = [ factories.ClaimNameFactory.build(resource_group=resource_group) for _ in range(faker.pyint(1, 5)) ] for claim_name in claim_names: pgdb_session.add(claim_name) pgdb_session.commit() url = self.url_for_this( app, resource_group_id=resource_group.external_id ) mocked_dump = mocker.patch.object( schemas.ClaimNamesResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: client.get(url) resp = client.get(url) assert resp.status_code == 200 assert mocked_dump.call_count == 1 class TestGlobalClaimNames(UrlForThisMixin): ENDPOINT = "api_v2.global_claim_names" def test_call_failure_authentication(self, app): with app.test_client() as client: resp = client.get(self.url_for_this(app)) assert resp.status_code == 401 def test_call_failure_authorization(self, app, authenticated): with app.test_client() as client: resp = client.get(self.url_for_this(app)) assert resp.status_code == 401 def test_call_no_cache( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): claim_names = [ factories.ClaimNameFactory.build(resource_group=None) for _ in range(faker.pyint(1, 5)) ] for claim_name in claim_names: pgdb_session.add(claim_name) query = ClaimName.query.active(True).filter_global() url = self.url_for_this(app) mocked_dump = mocker.patch.object( schemas.ClaimNamesResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: resp = client.get(url) with app.test_request_context(url): dump_expects = mocker.call( APIQueryFacade( query, allowed_fields=[ ClaimName.friendly, ClaimName.external_id, ], fields_mapping={"id": "external_id"}, ) ) assert resp.status_code == 200 assert mocked_dump.call_args_list == [dump_expects] def test_call_cache_hit( self, app, faker, mocker, pgdb_session, authorized_api_client, redis_session, ): claim_names = [ factories.ClaimNameFactory.build(resource_group=None) for _ in range(faker.pyint(1, 5)) ] for claim_name in claim_names: pgdb_session.add(claim_name) url = self.url_for_this(app) mocked_dump = mocker.patch.object( schemas.ClaimNamesResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: client.get(url) resp = client.get(url) assert resp.status_code == 200 assert mocked_dump.call_count == 1 class TestClaimValues(UrlForThisMixin): ENDPOINT = "api_v2.claim_values" def test_call_failure_authentication(self, app): claim_name = factories.ClaimNameFactory.build() with app.test_client() as client: resp = client.get( self.url_for_this( app, resource_group_id=claim_name.resource_group.external_id, claim_name_id=claim_name.external_id, ) ) assert resp.status_code == 401 def test_call_failure_authorization(self, app, authenticated): claim_name = factories.ClaimNameFactory.build() with app.test_client() as client: resp = client.get( self.url_for_this( app, resource_group_id=claim_name.resource_group.external_id, claim_name_id=claim_name.external_id, ) ) assert resp.status_code == 401 def test_call_no_resource_group( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): claim_name = factories.ClaimNameFactory.build() claim_values = [ factories.ClaimValueFactory.build(claim_name=claim_name) for _ in range(faker.pyint(1, 5)) ] claim_values.append( factories.ClaimValueFactory.build( claim_name=claim_name, is_deleted=True ) ) for claim_value in claim_values: pgdb_session.add(claim_value) pgdb_session.commit() url = self.url_for_this( app, resource_group_id="wrong_id", claim_name_id=claim_name.external_id, ) mocked_dump = mocker.patch.object( schemas.ClaimValuesResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not mocked_dump.called def test_call_no_claim_name( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): claim_name = factories.ClaimNameFactory.build() claim_values = [ factories.ClaimValueFactory.build(claim_name=claim_name) for _ in range(faker.pyint(1, 5)) ] claim_values.append( factories.ClaimValueFactory.build( claim_name=claim_name, is_deleted=True ) ) for claim_value in claim_values: pgdb_session.add(claim_value) pgdb_session.commit() url = self.url_for_this( app, resource_group_id=claim_name.resource_group.external_id, claim_name_id="wrong_id", ) mocked_dump = mocker.patch.object( schemas.ClaimValuesResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not mocked_dump.called def test_call_no_cache( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): claim_name = factories.ClaimNameFactory.build() claim_values = [ factories.ClaimValueFactory.build(claim_name=claim_name) for _ in range(faker.pyint(1, 5)) ] claim_values.append( factories.ClaimValueFactory.build( claim_name=claim_name, is_deleted=True ) ) for claim_value in claim_values: pgdb_session.add(claim_value) pgdb_session.commit() url = self.url_for_this( app, resource_group_id=claim_name.resource_group.external_id, claim_name_id=claim_name.external_id, ) mocked_dump = mocker.patch.object( schemas.ClaimValuesResponseSchema, "dump" ) mocked_dump.return_value = {} query = ClaimValue.query.active().by_claim_name(claim_name) with app.test_client() as client: resp = client.get(url) with app.test_request_context(url): dump_expects = mocker.call( APIQueryFacade( query, allowed_fields=[ ClaimValue.friendly, ClaimValue.external_id, ], fields_mapping={"id": "external_id"}, ) ) assert resp.status_code == 200 assert mocked_dump.call_args_list == [dump_expects] def test_call_cache_hit( self, app, faker, mocker, pgdb_session, authorized_api_client, redis_session, ): claim_name = factories.ClaimNameFactory.build() claim_values = [ factories.ClaimValueFactory.build(claim_name=claim_name) for _ in range(faker.pyint(1, 5)) ] for claim_value in claim_values: pgdb_session.add(claim_value) pgdb_session.commit() url = self.url_for_this( app, resource_group_id=claim_name.resource_group.external_id, claim_name_id=claim_name.external_id, ) mocked_dump = mocker.patch.object( schemas.ClaimValuesResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: client.get(url) resp = client.get(url) assert resp.status_code == 200 assert mocked_dump.call_count == 1 class TestGlobalClaimValues(UrlForThisMixin): ENDPOINT = "api_v2.global_claim_values" def test_call_failure_authentication(self, app): claim_name = factories.ClaimNameFactory.build(resource_group=None) with app.test_client() as client: resp = client.get( self.url_for_this( app, claim_name_id=claim_name.external_id, ) ) assert resp.status_code == 401 def test_call_failure_authorization(self, app, authenticated): claim_name = factories.ClaimNameFactory.build(resource_group=None) with app.test_client() as client: resp = client.get( self.url_for_this(app, claim_name_id=claim_name.external_id) ) assert resp.status_code == 401 def test_call_no_claim_name( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): claim_name = factories.ClaimNameFactory.build(resource_group=None) claim_values = [ factories.ClaimValueFactory.build(claim_name=claim_name) for _ in range(faker.pyint(1, 5)) ] for claim_value in claim_values: pgdb_session.add(claim_value) pgdb_session.commit() url = self.url_for_this(app, claim_name_id="wrong_id") mocked_dump = mocker.patch.object( schemas.ClaimValuesResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not mocked_dump.called def test_call_no_cache( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): claim_name = factories.ClaimNameFactory.build(resource_group=None) claim_values = [ factories.ClaimValueFactory.build(claim_name=claim_name) for _ in range(faker.pyint(1, 5)) ] for claim_value in claim_values: pgdb_session.add(claim_value) pgdb_session.commit() url = self.url_for_this(app, claim_name_id=claim_name.external_id) mocked_dump = mocker.patch.object( schemas.ClaimValuesResponseSchema, "dump" ) mocked_dump.return_value = {} query = ClaimValue.query.active(True).global_by_claim_name(claim_name) with app.test_client() as client: resp = client.get(url) with app.test_request_context(url): dump_expects = mocker.call( APIQueryFacade( query, allowed_fields=[ ClaimValue.friendly, ClaimValue.external_id, ], fields_mapping={"id": "external_id", "name": "friendly"}, ) ) assert resp.status_code == 200 assert mocked_dump.call_args_list == [dump_expects] def test_call_cache_hit( self, app, faker, mocker, pgdb_session, authorized_api_client, redis_session, ): claim_name = factories.ClaimNameFactory.build(resource_group=None) claim_values = [ factories.ClaimValueFactory.build(claim_name=claim_name) for _ in range(faker.pyint(1, 5)) ] for claim_value in claim_values: pgdb_session.add(claim_value) pgdb_session.commit() url = self.url_for_this( app, claim_name_id=claim_name.external_id, ) mocked_dump = mocker.patch.object( schemas.ClaimValuesResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: client.get(url) resp = client.get(url) assert resp.status_code == 200 assert mocked_dump.call_count == 1 class TestClaimValueUsers(UrlForThisMixin): ENDPOINT = "api_v2.claim_value_users" def test_call_failure_authentication(self, app): claim_name = factories.ClaimNameFactory.build() claim_value = factories.ClaimValueFactory.build(claim_name=claim_name) with app.test_client() as client: resp = client.get( self.url_for_this( app, resource_group_id=claim_name.resource_group.external_id, claim_name_id=claim_name.external_id, claim_value_id=claim_value.external_id, ) ) assert resp.status_code == 401 def test_call_failure_authorization(self, app, authenticated): claim_name = factories.ClaimNameFactory.build() claim_value = factories.ClaimValueFactory.build(claim_name=claim_name) with app.test_client() as client: resp = client.get( self.url_for_this( app, resource_group_id=claim_name.resource_group.external_id, claim_name_id=claim_name.external_id, claim_value_id=claim_value.external_id, ) ) assert resp.status_code == 401 def test_call_no_resource_group( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): claim_name = factories.ClaimNameFactory.build() claim_value = factories.ClaimValueFactory.build(claim_name=claim_name) accounts = [] for _ in range(faker.pyint(1, 5)): account = factories.DNAAccountFactory.build( claim_values=[claim_value] ) account.claim_values = [claim_value] accounts.append(account) pgdb_session.add(account) account = factories.DNAAccountFactory.build( claim_values=[claim_value], expiration_date=faker.past_date() ) account.claim_values = [claim_value] accounts.append(account) pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, resource_group_id="wrong_id", claim_name_id=claim_name.external_id, claim_value_id=claim_value.external_id, ) mocked_dump = mocker.patch.object(schemas.UsersResponseSchema, "dump") mocked_dump.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not mocked_dump.called def test_call_no_claim_name( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): claim_name = factories.ClaimNameFactory.build() claim_value = factories.ClaimValueFactory.build(claim_name=claim_name) accounts = [] for _ in range(faker.pyint(1, 5)): account = factories.DNAAccountFactory.build( claim_values=[claim_value] ) account.claim_values = [claim_value] accounts.append(account) pgdb_session.add(account) account = factories.DNAAccountFactory.build( claim_values=[claim_value], expiration_date=faker.past_date() ) account.claim_values = [claim_value] accounts.append(account) pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, resource_group_id=claim_name.resource_group.external_id, claim_name_id="wrong_id", claim_value_id=claim_value.external_id, ) mocked_dump = mocker.patch.object(schemas.UsersResponseSchema, "dump") mocked_dump.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not mocked_dump.called def test_call_no_claim_value( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): claim_name = factories.ClaimNameFactory.build() claim_value = factories.ClaimValueFactory.build(claim_name=claim_name) accounts = [] for _ in range(faker.pyint(1, 5)): account = factories.DNAAccountFactory.build( claim_values=[claim_value] ) account.claim_values = [claim_value] accounts.append(account) pgdb_session.add(account) account = factories.DNAAccountFactory.build( claim_values=[claim_value], expiration_date=faker.past_date() ) account.claim_values = [claim_value] accounts.append(account) pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, resource_group_id=claim_name.resource_group.external_id, claim_name_id=claim_name.external_id, claim_value_id="wrong_id", ) mocked_dump = mocker.patch.object(schemas.UsersResponseSchema, "dump") mocked_dump.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not mocked_dump.called def test_call_no_cache( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): claim_name = factories.ClaimNameFactory.build() claim_value = factories.ClaimValueFactory.build(claim_name=claim_name) accounts = [] for _ in range(faker.pyint(1, 5)): account = factories.DNAAccountFactory.build( claim_values=[claim_value] ) account.claim_values = [claim_value] accounts.append(account) pgdb_session.add(account) account = factories.DNAAccountFactory.build( claim_values=[claim_value], expiration_date=faker.past_date() ) account.claim_values = [claim_value] accounts.append(account) pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, resource_group_id=claim_name.resource_group.external_id, claim_name_id=claim_name.external_id, claim_value_id=claim_value.external_id, ) mocked_dump = mocker.patch.object(schemas.UsersResponseSchema, "dump") mocked_dump.return_value = {} query = DNAAccount.query.activated().by_claim_name_claim_value( claim_name, claim_value ) with app.test_client() as client: resp = client.get(url) with app.test_request_context(url): dump_expects = mocker.call( APIQueryFacade( query, allowed_fields=[ DNAAccount.sub, DNAAccount.email, DNAAccount.given_name, DNAAccount.family_name, ], fields_mapping={"id": "sub"}, ) ) assert resp.status_code == 200 assert mocked_dump.call_args_list == [dump_expects] def test_call_cache_hit( self, app, faker, mocker, pgdb_session, authorized_api_client, redis_session, ): claim_name = factories.ClaimNameFactory.build() claim_value = factories.ClaimValueFactory.build(claim_name=claim_name) accounts = [] for _ in range(faker.pyint(1, 5)): account = factories.DNAAccountFactory.build( claim_values=[claim_value] ) account.claim_values = [claim_value] accounts.append(account) pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, resource_group_id=claim_name.resource_group.external_id, claim_name_id=claim_name.external_id, claim_value_id=claim_value.external_id, ) mocked_dump = mocker.patch.object(schemas.UsersResponseSchema, "dump") mocked_dump.return_value = {} with app.test_client() as client: client.get(url) resp = client.get(url) assert resp.status_code == 200 assert mocked_dump.call_count == 1 class TestUsers(UrlForThisMixin): ENDPOINT = "api_v2.users" def test_call_failure_authentication(self, app): with app.test_client() as client: resp = client.get(self.url_for_this(app)) assert resp.status_code == 401 def test_call_failure_authorization(self, app, authenticated): with app.test_client() as client: resp = client.get(self.url_for_this(app)) assert resp.status_code == 401 def test_call_no_cache( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): for _ in range(faker.pyint(1, 5)): account = factories.DNAAccountFactory.build() pgdb_session.add(account) account = factories.DNAAccountFactory.build( expiration_date=faker.past_date() ) pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this(app) mocked_dump = mocker.patch.object(schemas.UsersResponseSchema, "dump") mocked_dump.return_value = {} with app.test_client() as client: query = DNAAccount.query.with_relations().activated() resp = client.get(url) with app.test_request_context(url): dump_expects = mocker.call( APIQueryFacade( query, allowed_fields=[ DNAAccount.sub, DNAAccount.email, DNAAccount.given_name, DNAAccount.family_name, ], fields_mapping={"id": "sub"}, ) ) assert resp.status_code == 200 assert mocked_dump.call_args_list == [dump_expects] def test_call_cache_hit( self, app, faker, mocker, pgdb_session, authorized_api_client, redis_session, ): for _ in range(faker.pyint(1, 5)): account = factories.DNAAccountFactory.build() pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this(app) mocked_dump = mocker.patch.object(schemas.UsersResponseSchema, "dump") mocked_dump.return_value = {} with app.test_client() as client: client.get(url) resp = client.get(url) assert resp.status_code == 200 assert mocked_dump.call_count == 1 class TestUserDetails(UrlForThisMixin): ENDPOINT = "api_v2.user_details" def test_call_failure_authentication(self, app): account = factories.DNAAccountFactory.build() with app.test_client() as client: resp = client.get(self.url_for_this(app, user_id=account.sub)) assert resp.status_code == 401 def test_call_failure_authorization(self, app, authenticated): account = factories.DNAAccountFactory.build() with app.test_client() as client: resp = client.get(self.url_for_this(app, user_id=account.sub)) assert resp.status_code == 401 def test_call_no_user( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): account = factories.DNAAccountFactory.build() pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this(app, user_id="wrong_id") mocked_dump = mocker.patch.object(schemas.UserSchema, "dump") mocked_dump.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not mocked_dump.called def test_call_no_cache( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): account = factories.DNAAccountFactory.build() pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this(app, user_id=account.sub) mocked_dump = mocker.patch.object(schemas.UserSchema, "dump") mocked_dump.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 200 assert mocked_dump.call_args[0][0].id == account.id def test_call_expired( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): account = factories.DNAAccountFactory.build( expiration_date=faker.past_date() ) pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this(app, user_id=account.sub) mocked_dump = mocker.patch.object(schemas.UserSchema, "dump") mocked_dump.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 def test_call_cache_hit( self, app, faker, mocker, pgdb_session, authorized_api_client, redis_session, ): account = factories.DNAAccountFactory.build() pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this(app, user_id=account.sub) mocked_dump = mocker.patch.object(schemas.UserSchema, "dump") mocked_dump.return_value = {} with app.test_client() as client: client.get(url) resp = client.get(url) assert resp.status_code == 200 assert mocked_dump.call_count == 1 class TestUsersStatusLookup(UrlForThisMixin): ENDPOINT = "api_v2.users_status_lookup" def test_call_failure_authentication(self, app, faker): with app.test_client() as client: resp = client.get( self.url_for_this(app), query_string={ "email": [faker.email()], "id": [faker.pystr()], }, ) assert resp.status_code == 401 def test_call_failure_authorization(self, app, authenticated, faker): with app.test_client() as client: resp = client.get( self.url_for_this(app), query_string={ "email": [faker.email()], "id": [faker.pystr()], }, ) assert resp.status_code == 401 def test_call_failure_validation( self, app, authenticated, faker, authorized_api_client ): with app.test_client() as client: resp = client.get( self.url_for_this(app), query_string={ "email": [faker.email()], "id": [faker.pystr()], }, ) assert resp.status_code == 422 assert resp.json["extra"]["query"]["_schema"] == [ "Either emails or IDs are required, but not both." ] def test_call_email_success( self, app, authenticated, faker, mocker, authorized_api_client ): fake_product = faker.pystr() fake_product2 = faker.email() fake_email = faker.email() fake_email2 = faker.email() fake_response = [ {"email": fake_email, "is_active": False, "last_login": None}, { "email": fake_email2, "is_active": True, "last_login": faker.past_datetime(), }, ] accounts_validation = mocker.patch( "atlas_um.api.v2.views.accounts_validation" ) accounts_validation.get_accounts_with_statuses_by_emails.return_value.all.return_value = ( # noqa fake_response ) with app.test_client() as client: resp = client.get( self.url_for_this(app), query_string={ "email": [fake_email, fake_email2], "product": [fake_product, fake_product2], }, ) assert resp.status_code == 200 assert ( accounts_validation.get_accounts_with_statuses_by_emails.call_args_list # noqa == [ mocker.call( [fake_email, fake_email2], [fake_product, fake_product2] ) ] ) assert resp.json == schemas.UserStatusEmailSchema().dump( fake_response, many=True ) def test_call_id_success( self, app, authenticated, faker, mocker, authorized_api_client ): fake_product = faker.pystr() fake_product2 = faker.email() fake_id = faker.pystr() fake_id2 = faker.pystr() fake_response = [ {"id": fake_id, "is_active": False, "last_login": None}, { "id": fake_id2, "is_active": True, "last_login": faker.past_datetime(), }, ] accounts_validation = mocker.patch( "atlas_um.api.v2.views.accounts_validation" ) accounts_validation.get_accounts_with_statuses_by_ids.return_value.all.return_value = ( # noqa fake_response ) with app.test_client() as client: resp = client.get( self.url_for_this(app), query_string={ "id": [fake_id, fake_id2], "product": [fake_product, fake_product2], }, ) assert resp.status_code == 200 assert ( accounts_validation.get_accounts_with_statuses_by_ids.call_args_list # noqa == [ mocker.call([fake_id, fake_id2], [fake_product, fake_product2]) ] ) assert resp.json == schemas.UserStatusIdSchema().dump( fake_response, many=True ) class TestUserClaims(UrlForThisMixin): ENDPOINT = "api_v2.user_claims" def test_call_failure_authentication(self, app): account = factories.DNAAccountFactory.build() with app.test_client() as client: resp = client.get(self.url_for_this(app, user_id=account.sub)) assert resp.status_code == 401 def test_call_failure_authorization(self, app, authenticated): account = factories.DNAAccountFactory.build() with app.test_client() as client: resp = client.get(self.url_for_this(app, user_id=account.sub)) assert resp.status_code == 401 def test_call_no_user( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): account = factories.DNAAccountFactory.build() pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this(app, user_id="wrong_id") serialize_claims = mocker.patch.object(DNAAccount, "serialize_claims") serialize_claims.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not serialize_claims.called def test_call_no_cache( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): account = factories.DNAAccountFactory.build() pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this(app, user_id=account.sub) serialize_claims = mocker.patch.object(DNAAccount, "serialize_claims") serialize_claims.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 200 assert serialize_claims.called def test_call_expired( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): account = factories.DNAAccountFactory.build( expiration_date=faker.past_date() ) pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this(app, user_id=account.sub) serialize_claims = mocker.patch.object(DNAAccount, "serialize_claims") serialize_claims.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not serialize_claims.called def test_call_cache_hit( self, app, faker, mocker, pgdb_session, authorized_api_client, redis_session, ): account = factories.DNAAccountFactory.build() pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this(app, user_id=account.sub) serialize_claims = mocker.patch.object(DNAAccount, "serialize_claims") serialize_claims.return_value = {} with app.test_client() as client: client.get(url) resp = client.get(url) assert resp.status_code == 200 assert serialize_claims.call_count == 1 class TestUserResourceGroupClaims(UrlForThisMixin): ENDPOINT = "api_v2.user_resource_group_claims" def test_call_failure_authentication(self, app): account = factories.DNAAccountFactory.build() resource_group = factories.ResourceGroupFactory.build() with app.test_client() as client: resp = client.get( self.url_for_this( app, user_id=account.sub, resource_group_id=resource_group.external_id, ) ) assert resp.status_code == 401 def test_call_failure_authorization(self, app, authenticated): account = factories.DNAAccountFactory.build() resource_group = factories.ResourceGroupFactory.build() with app.test_client() as client: resp = client.get( self.url_for_this( app, user_id=account.sub, resource_group_id=resource_group.external_id, ) ) assert resp.status_code == 401 def test_call_no_user( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): resource_group = factories.ResourceGroupFactory.build() account = factories.DNAAccountFactory.build() pgdb_session.add(resource_group) pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, user_id="wrong_id", resource_group_id=resource_group.external_id, ) serialize_claims = mocker.patch.object(DNAAccount, "serialize_claims") serialize_claims.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not serialize_claims.called def test_call_no_resource_group( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): resource_group = factories.ResourceGroupFactory.build() account = factories.DNAAccountFactory.build() pgdb_session.add(resource_group) pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, user_id=account.sub, resource_group_id="wrong_id", ) serialize_claims = mocker.patch.object(DNAAccount, "serialize_claims") serialize_claims.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not serialize_claims.called def test_call_no_cache( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): resource_group = factories.ResourceGroupFactory.build() account = factories.DNAAccountFactory.build() pgdb_session.add(resource_group) pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, user_id=account.sub, resource_group_id=resource_group.external_id, ) serialize_claims = mocker.patch.object(DNAAccount, "serialize_claims") serialize_claims.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 200 assert ( serialize_claims.call_args.kwargs.get("resource_group").id == resource_group.id ) def test_call_expired( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): resource_group = factories.ResourceGroupFactory.build() account = factories.DNAAccountFactory.build( expiration_date=faker.past_date() ) pgdb_session.add(resource_group) pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, user_id=account.sub, resource_group_id=resource_group.external_id, ) serialize_claims = mocker.patch.object(DNAAccount, "serialize_claims") serialize_claims.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 def test_call_cache_hit( self, app, faker, mocker, pgdb_session, authorized_api_client, redis_session, ): resource_group = factories.ResourceGroupFactory.build() account = factories.DNAAccountFactory.build() pgdb_session.add(resource_group) pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, user_id=account.sub, resource_group_id=resource_group.external_id, ) serialize_claims = mocker.patch.object(DNAAccount, "serialize_claims") serialize_claims.return_value = {} with app.test_client() as client: client.get(url) resp = client.get(url) assert resp.status_code == 200 assert ( serialize_claims.call_args.kwargs.get("resource_group").id == resource_group.id ) assert serialize_claims.call_count == 1 class TestUserResourceGroups(UrlForThisMixin): ENDPOINT = "api_v2.user_resource_groups" def test_call_failure_authentication(self, app): account = factories.DNAAccountFactory.build() with app.test_client() as client: resp = client.get(self.url_for_this(app, user_id=account.sub)) assert resp.status_code == 401 def test_call_failure_authorization(self, app, authenticated): account = factories.DNAAccountFactory.build() with app.test_client() as client: resp = client.get(self.url_for_this(app, user_id=account.sub)) assert resp.status_code == 401 def test_call_no_user( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): claim_values = [] for _ in range(faker.pyint(1, 5)): resource_group = factories.ResourceGroupFactory.build() claim_name = factories.ClaimNameFactory.build( resource_group=resource_group ) claim_value = factories.ClaimValueFactory.build( claim_name=claim_name ) pgdb_session.add(claim_value) claim_values.append(claim_value) resource_group = factories.ResourceGroupFactory.build(is_deleted=True) claim_name = factories.ClaimNameFactory.build( resource_group=resource_group ) claim_value = factories.ClaimValueFactory.build(claim_name=claim_name) pgdb_session.add(claim_value) account = factories.DNAAccountFactory.build() account.claim_values = claim_values pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this(app, user_id="wrong_id") mocked_dump = mocker.patch.object( schemas.ResourceGroupsResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not mocked_dump.called def test_call_no_cache( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): claim_values = [] for _ in range(faker.pyint(1, 5)): resource_group = factories.ResourceGroupFactory.build() claim_name = factories.ClaimNameFactory.build( resource_group=resource_group ) claim_value = factories.ClaimValueFactory.build( claim_name=claim_name ) pgdb_session.add(claim_value) claim_values.append(claim_value) resource_group = factories.ResourceGroupFactory.build(is_deleted=True) claim_name = factories.ClaimNameFactory.build( resource_group=resource_group ) claim_value = factories.ClaimValueFactory.build(claim_name=claim_name) pgdb_session.add(claim_value) account = factories.DNAAccountFactory.build() account.claim_values = claim_values pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this(app, user_id=account.sub) mocked_dump = mocker.patch.object( schemas.ResourceGroupsResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: query = ResourceGroup.query.active().by_dna_account_sub( account.sub ) resp = client.get(url) with app.test_request_context(url): dump_expects = mocker.call( APIQueryFacade( query, allowed_fields=[ ResourceGroup.name, ResourceGroup.external_id, ], fields_mapping={"id": "external_id"}, ) ) assert resp.status_code == 200 assert mocked_dump.call_args_list == [dump_expects] def test_call_expired( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): claim_values = [] for _ in range(faker.pyint(1, 5)): resource_group = factories.ResourceGroupFactory.build() claim_name = factories.ClaimNameFactory.build( resource_group=resource_group ) claim_value = factories.ClaimValueFactory.build( claim_name=claim_name ) pgdb_session.add(claim_value) claim_values.append(claim_value) resource_group = factories.ResourceGroupFactory.build(is_deleted=True) claim_name = factories.ClaimNameFactory.build( resource_group=resource_group ) claim_value = factories.ClaimValueFactory.build(claim_name=claim_name) pgdb_session.add(claim_value) account = factories.DNAAccountFactory.build( expiration_date=faker.past_date() ) account.claim_values = claim_values pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this(app, user_id=account.sub) mocked_dump = mocker.patch.object( schemas.ResourceGroupsResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: query = ResourceGroup.query.active().by_dna_account_sub( account.sub ) resp = client.get(url) with app.test_request_context(url): dump_expects = mocker.call( APIQueryPagination( query=QueryOrdering( query, allowed_fields=[ ResourceGroup.name, ResourceGroup.external_id, ], fields_mapping={"id": "external_id"}, ).query ) ) assert resp.status_code == 200 assert mocked_dump.call_args_list == [dump_expects] def test_call_cache_hit( self, app, faker, mocker, pgdb_session, authorized_api_client, redis_session, ): claim_values = [] for _ in range(faker.pyint(1, 5)): resource_group = factories.ResourceGroupFactory.build() claim_name = factories.ClaimNameFactory.build( resource_group=resource_group ) claim_value = factories.ClaimValueFactory.build( claim_name=claim_name ) pgdb_session.add(claim_value) claim_values.append(claim_value) account = factories.DNAAccountFactory.build() account.claim_values = claim_values pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this(app, user_id=account.sub) mocked_dump = mocker.patch.object( schemas.ResourceGroupsResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: client.get(url) client.get(url) assert mocked_dump.call_count == 1 class TestUserClaimNames(UrlForThisMixin): ENDPOINT = "api_v2.user_claim_names" def test_call_failure_authentication(self, app): account = factories.DNAAccountFactory.build() resource_group = factories.ResourceGroupFactory.build() with app.test_client() as client: resp = client.get( self.url_for_this( app, user_id=account.sub, resource_group_id=resource_group.external_id, ) ) assert resp.status_code == 401 def test_call_failure_authorization(self, app, authenticated): resource_group = factories.ResourceGroupFactory.build() account = factories.DNAAccountFactory.build() with app.test_client() as client: resp = client.get( self.url_for_this( app, user_id=account.sub, resource_group_id=resource_group.external_id, ) ) assert resp.status_code == 401 def test_call_no_user( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): resource_group = factories.ResourceGroupFactory.build() claim_values = [] for _ in range(faker.pyint(1, 5)): claim_name = factories.ClaimNameFactory.build( resource_group=resource_group ) claim_value = factories.ClaimValueFactory.build( claim_name=claim_name ) pgdb_session.add(claim_value) claim_values.append(claim_value) claim_name = factories.ClaimNameFactory.build( resource_group=resource_group, is_deleted=True ) claim_value = factories.ClaimValueFactory.build(claim_name=claim_name) pgdb_session.add(claim_value) claim_values.append(claim_value) account = factories.DNAAccountFactory.build() account.claim_values = claim_values pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, user_id="wrong_id", resource_group_id=resource_group.external_id, ) mocked_dump = mocker.patch.object( schemas.ClaimNamesResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not mocked_dump.called def test_call_no_resource_group( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): resource_group = factories.ResourceGroupFactory.build() claim_values = [] for _ in range(faker.pyint(1, 5)): claim_name = factories.ClaimNameFactory.build( resource_group=resource_group ) claim_value = factories.ClaimValueFactory.build( claim_name=claim_name ) pgdb_session.add(claim_value) claim_values.append(claim_value) claim_name = factories.ClaimNameFactory.build( resource_group=resource_group, is_deleted=True ) claim_value = factories.ClaimValueFactory.build(claim_name=claim_name) pgdb_session.add(claim_value) claim_values.append(claim_value) account = factories.DNAAccountFactory.build() account.claim_values = claim_values pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, user_id=account.sub, resource_group_id="wrong_id", ) mocked_dump = mocker.patch.object( schemas.ClaimNamesResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not mocked_dump.called def test_call_no_cache( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): resource_group = factories.ResourceGroupFactory.build() claim_values = [] for _ in range(faker.pyint(1, 5)): claim_name = factories.ClaimNameFactory.build( resource_group=resource_group ) claim_value = factories.ClaimValueFactory.build( claim_name=claim_name ) pgdb_session.add(claim_value) claim_values.append(claim_value) claim_name = factories.ClaimNameFactory.build( resource_group=resource_group, is_deleted=True ) claim_value = factories.ClaimValueFactory.build(claim_name=claim_name) pgdb_session.add(claim_value) claim_values.append(claim_value) account = factories.DNAAccountFactory.build() account.claim_values = claim_values pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, user_id=account.sub, resource_group_id=resource_group.external_id, ) mocked_dump = mocker.patch.object( schemas.ClaimNamesResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: query = ClaimName.query.active().by_dna_account_resource_group( account, resource_group ) resp = client.get(url) with app.test_request_context(url): dump_expects = mocker.call( APIQueryFacade( query, allowed_fields=[ ClaimName.friendly, ClaimName.external_id, ], fields_mapping={"id": "external_id"}, ) ) assert resp.status_code == 200 assert mocked_dump.call_args_list == [dump_expects] def test_call_expired( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): resource_group = factories.ResourceGroupFactory.build() claim_values = [] for _ in range(faker.pyint(1, 5)): claim_name = factories.ClaimNameFactory.build( resource_group=resource_group ) claim_value = factories.ClaimValueFactory.build( claim_name=claim_name ) pgdb_session.add(claim_value) claim_values.append(claim_value) claim_name = factories.ClaimNameFactory.build( resource_group=resource_group, is_deleted=True ) claim_value = factories.ClaimValueFactory.build(claim_name=claim_name) pgdb_session.add(claim_value) claim_values.append(claim_value) account = factories.DNAAccountFactory.build( expiration_date=faker.past_date() ) account.claim_values = claim_values pgdb_session.add(account) url = self.url_for_this( app, user_id=account.sub, resource_group_id=resource_group.external_id, ) mocked_dump = mocker.patch.object( schemas.ClaimNamesResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not mocked_dump.called def test_call_cache_hit( self, app, faker, mocker, pgdb_session, authorized_api_client, redis_session, ): resource_group = factories.ResourceGroupFactory.build() claim_values = [] for _ in range(faker.pyint(1, 5)): claim_name = factories.ClaimNameFactory.build( resource_group=resource_group ) claim_value = factories.ClaimValueFactory.build( claim_name=claim_name ) pgdb_session.add(claim_value) claim_values.append(claim_value) account = factories.DNAAccountFactory.build() account.claim_values = claim_values pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, user_id=account.sub, resource_group_id=resource_group.external_id, ) mocked_dump = mocker.patch.object( schemas.ClaimNamesResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: client.get(url) client.get(url) assert mocked_dump.call_count == 1 class TestUserClaimValues(UrlForThisMixin): ENDPOINT = "api_v2.user_claim_values" def test_call_failure_authentication(self, app): account = factories.DNAAccountFactory.build() claim_name = factories.ClaimNameFactory.build() with app.test_client() as client: resp = client.get( self.url_for_this( app, user_id=account.sub, resource_group_id=claim_name.resource_group.external_id, claim_name_id=claim_name.external_id, ) ) assert resp.status_code == 401 def test_call_failure_authorization(self, app, authenticated): account = factories.DNAAccountFactory.build() claim_name = factories.ClaimNameFactory.build() with app.test_client() as client: resp = client.get( self.url_for_this( app, user_id=account.sub, resource_group_id=claim_name.resource_group.external_id, claim_name_id=claim_name.external_id, ) ) assert resp.status_code == 401 def test_call_no_user( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): claim_name = factories.ClaimNameFactory.build() claim_values = [ factories.ClaimValueFactory.build(claim_name=claim_name) for _ in range(faker.pyint(1, 5)) ] claim_values.append( factories.ClaimValueFactory.build( claim_name=claim_name, is_deleted=True ) ) account = factories.DNAAccountFactory.build() account.claim_values = claim_values pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, user_id="wrong_id", resource_group_id=claim_name.resource_group.external_id, claim_name_id=claim_name.external_id, ) mocked_dump = mocker.patch.object( schemas.ClaimValuesResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not mocked_dump.called def test_call_no_resource_group( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): claim_name = factories.ClaimNameFactory.build() claim_values = [ factories.ClaimValueFactory.build(claim_name=claim_name) for _ in range(faker.pyint(1, 5)) ] claim_values.append( factories.ClaimValueFactory.build( claim_name=claim_name, is_deleted=True ) ) account = factories.DNAAccountFactory.build() account.claim_values = claim_values pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, user_id=account.sub, resource_group_id="wrong_id", claim_name_id=claim_name.external_id, ) mocked_dump = mocker.patch.object( schemas.ClaimValuesResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not mocked_dump.called def test_call_no_claim_name( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): claim_name = factories.ClaimNameFactory.build() claim_values = [ factories.ClaimValueFactory.build(claim_name=claim_name) for _ in range(faker.pyint(1, 5)) ] claim_values.append( factories.ClaimValueFactory.build( claim_name=claim_name, is_deleted=True ) ) account = factories.DNAAccountFactory.build() account.claim_values = claim_values pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, user_id=account.sub, resource_group_id=claim_name.resource_group.external_id, claim_name_id="wrong_id", ) mocked_dump = mocker.patch.object( schemas.ClaimValuesResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not mocked_dump.called def test_call_no_cache( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): claim_name = factories.ClaimNameFactory.build() claim_values = [ factories.ClaimValueFactory.build(claim_name=claim_name) for _ in range(faker.pyint(1, 5)) ] claim_values.append( factories.ClaimValueFactory.build( claim_name=claim_name, is_deleted=True ) ) account = factories.DNAAccountFactory.build() account.claim_values = claim_values pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, user_id=account.sub, resource_group_id=claim_name.resource_group.external_id, claim_name_id=claim_name.external_id, ) mocked_dump = mocker.patch.object( schemas.ClaimValuesResponseSchema, "dump" ) mocked_dump.return_value = {} query = ClaimValue.query.by_dna_account_claim_name(account, claim_name) with app.test_client() as client: resp = client.get(url) with app.test_request_context(url): dump_expects = mocker.call( APIQueryFacade( query, allowed_fields=[ ClaimValue.friendly, ClaimValue.external_id, ], fields_mapping={"id": "external_id"}, ) ) assert resp.status_code == 200 assert mocked_dump.call_args_list == [dump_expects] def test_call_expired( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): claim_name = factories.ClaimNameFactory.build() claim_values = [ factories.ClaimValueFactory.build(claim_name=claim_name) for _ in range(faker.pyint(1, 5)) ] claim_values.append( factories.ClaimValueFactory.build( claim_name=claim_name, is_deleted=True ) ) account = factories.DNAAccountFactory.build( expiration_date=faker.past_date() ) account.claim_values = claim_values pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, user_id=account.sub, resource_group_id=claim_name.resource_group.external_id, claim_name_id=claim_name.external_id, ) mocked_dump = mocker.patch.object( schemas.ClaimValuesResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not mocked_dump.called def test_call_cache_hit( self, app, faker, mocker, pgdb_session, authorized_api_client, redis_session, ): claim_name = factories.ClaimNameFactory.build() claim_values = [ factories.ClaimValueFactory.build(claim_name=claim_name) for _ in range(faker.pyint(1, 5)) ] account = factories.DNAAccountFactory.build() account.claim_values = claim_values pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, user_id=account.sub, resource_group_id=claim_name.resource_group.external_id, claim_name_id=claim_name.external_id, ) mocked_dump = mocker.patch.object( schemas.ClaimValuesResponseSchema, "dump" ) mocked_dump.return_value = {} with app.test_client() as client: client.get(url) client.get(url) assert mocked_dump.call_count == 1 class TestUserClaimNameClaims(UrlForThisMixin): ENDPOINT = "api_v2.user_claim_name_claims" def test_call_failure_authentication(self, app): account = factories.DNAAccountFactory.build() claim_name = factories.ClaimNameFactory.build() with app.test_client() as client: resp = client.get( self.url_for_this( app, user_id=account.sub, resource_group_id=claim_name.resource_group.external_id, claim_name_id=claim_name.external_id, ) ) assert resp.status_code == 401 def test_call_failure_authorization(self, app, authenticated): account = factories.DNAAccountFactory.build() claim_name = factories.ClaimNameFactory.build() with app.test_client() as client: resp = client.get( self.url_for_this( app, user_id=account.sub, resource_group_id=claim_name.resource_group.external_id, claim_name_id=claim_name.external_id, ) ) assert resp.status_code == 401 def test_call_no_user( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): claim_name = factories.ClaimNameFactory.build() resource_group = claim_name.resource_group account = factories.DNAAccountFactory.build() pgdb_session.add(claim_name) pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, user_id="wrong_id", resource_group_id=resource_group.external_id, claim_name_id=claim_name.external_id, ) serialize_claims = mocker.patch.object(DNAAccount, "serialize_claims") serialize_claims.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not serialize_claims.called def test_call_no_resource_group( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): claim_name = factories.ClaimNameFactory.build() account = factories.DNAAccountFactory.build() pgdb_session.add(claim_name) pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, user_id=account.sub, resource_group_id="wrong_id", claim_name_id=claim_name.external_id, ) serialize_claims = mocker.patch.object(DNAAccount, "serialize_claims") serialize_claims.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not serialize_claims.called def test_call_no_claim_name( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): claim_name = factories.ClaimNameFactory.build() resource_group = claim_name.resource_group account = factories.DNAAccountFactory.build() pgdb_session.add(claim_name) pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, user_id=account.sub, resource_group_id=resource_group.external_id, claim_name_id="wrong_id", ) serialize_claims = mocker.patch.object(DNAAccount, "serialize_claims") serialize_claims.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not serialize_claims.called def test_call_no_cache( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): claim_name = factories.ClaimNameFactory.build() resource_group = claim_name.resource_group account = factories.DNAAccountFactory.build() pgdb_session.add(claim_name) pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, user_id=account.sub, resource_group_id=resource_group.external_id, claim_name_id=claim_name.external_id, ) serialize_claims = mocker.patch.object(DNAAccount, "serialize_claims") serialize_claims.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 200 assert ( serialize_claims.call_args.kwargs.get("claim_name").id == claim_name.id ) def test_call_expired( self, app, faker, mocker, pgdb_session, authorized_api_client, bypassed_cache, ): claim_name = factories.ClaimNameFactory.build() resource_group = claim_name.resource_group account = factories.DNAAccountFactory.build( expiration_date=faker.past_date() ) pgdb_session.add(claim_name) pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, user_id=account.sub, resource_group_id=resource_group.external_id, claim_name_id=claim_name.external_id, ) serialize_claims = mocker.patch.object(DNAAccount, "serialize_claims") serialize_claims.return_value = {} with app.test_client() as client: resp = client.get(url) assert resp.status_code == 404 assert not serialize_claims.called def test_call_cache_hit( self, app, faker, mocker, pgdb_session, authorized_api_client, redis_session, ): claim_name = factories.ClaimNameFactory.build() resource_group = claim_name.resource_group account = factories.DNAAccountFactory.build() pgdb_session.add(claim_name) pgdb_session.add(account) pgdb_session.commit() url = self.url_for_this( app, user_id=account.sub, resource_group_id=resource_group.external_id, claim_name_id=claim_name.external_id, ) serialize_claims = mocker.patch.object(DNAAccount, "serialize_claims") serialize_claims.return_value = {} with app.test_client() as client: client.get(url) resp = client.get(url) assert resp.status_code == 200 assert ( serialize_claims.call_args.kwargs.get("claim_name").id == claim_name.id ) assert serialize_claims.call_count == 1