from flask import url_for from atlas_um.api.v1 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, pgdb, ) from tests.atlas_um import factories class TestResourceGroups: 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( 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] 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 # Helpers def url_for_this(self, app, **kwargs): if "endpoint" not in kwargs: kwargs["endpoint"] = "api_v1.resource_groups" with app.app_context(): u = url_for(**kwargs) return u class TestClaimNames: 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_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 = {} with app.test_client() as client: query = ClaimName.query.active().by_group_external_id( resource_group.external_id ) resp = client.get(url) with app.test_request_context(url): dump_expects = mocker.call( APIQueryPagination( query=QueryOrdering( query, allowed_fields=[ ClaimName.friendly, ClaimName.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, ): 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 # Helpers def url_for_this(self, app, **kwargs): if "endpoint" not in kwargs: kwargs["endpoint"] = "api_v1.claim_names" with app.app_context(): u = url_for(**kwargs) return u class TestGlobalClaimNames: 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 = ( pgdb.session.query(ClaimValue) .join(ClaimName) .filter(ClaimName.resource_group_id == None) # noqa ) 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( APIQueryPagination( query=QueryOrdering( query, allowed_fields=[ ClaimName.friendly, ClaimName.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_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 # Helpers def url_for_this(self, app, **kwargs): if "endpoint" not in kwargs: kwargs["endpoint"] = "api_v1.global_claim_names" with app.app_context(): u = url_for(**kwargs) return u class TestClaimValues: 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_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 = {} with app.test_client() as client: query = ClaimValue.query.active().by_name_external_ids( claim_name.resource_group.external_id, claim_name.external_id ) resp = client.get(url) with app.test_request_context(url): dump_expects = mocker.call( APIQueryPagination( query=QueryOrdering( query, allowed_fields=[ ClaimValue.friendly, ClaimValue.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_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 # Helpers def url_for_this(self, app, **kwargs): if "endpoint" not in kwargs: kwargs["endpoint"] = "api_v1.claim_values" with app.app_context(): u = url_for(**kwargs) return u class TestGlobalClaimValues: 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_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) query = ( pgdb.session.query(ClaimValue) .join(ClaimName) .filter( ClaimName.resource_group_id == None, # noqa ClaimName.external_id == claim_name.external_id, ) ) 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: resp = client.get(url) with app.test_request_context(url): dump_expects = mocker.call( APIQueryPagination( query=QueryOrdering( query, allowed_fields=[ ClaimValue.friendly, ClaimValue.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_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) 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 # Helpers def url_for_this(self, app, **kwargs): if "endpoint" not in kwargs: kwargs["endpoint"] = "api_v1.global_claim_values" with app.app_context(): u = url_for(**kwargs) return u class TestClaimValueUsers: 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_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 = {} with app.test_client() as client: query = DNAAccount.query.activated().by_claim_external_ids( claim_name.resource_group.external_id, claim_name.external_id, claim_value.external_id, ) resp = client.get(url) with app.test_request_context(url): dump_expects = mocker.call( APIQueryPagination( query=QueryOrdering( query, allowed_fields=[ DNAAccount.sub, DNAAccount.email, DNAAccount.given_name, DNAAccount.family_name, ], fields_mapping={"id": "sub"}, ).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_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 # Helpers def url_for_this(self, app, **kwargs): if "endpoint" not in kwargs: kwargs["endpoint"] = "api_v1.claim_value_users" with app.app_context(): u = url_for(**kwargs) return u class TestUsers: 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( APIQueryPagination( query=QueryOrdering( query, allowed_fields=[ DNAAccount.sub, DNAAccount.email, DNAAccount.given_name, DNAAccount.family_name, ], fields_mapping={"id": "sub"}, ).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, ): 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 # Helpers def url_for_this(self, app, **kwargs): if "endpoint" not in kwargs: kwargs["endpoint"] = "api_v1.users" with app.app_context(): u = url_for(**kwargs) return u class TestUserDetails: 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_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.UserDetailsResponseSchema, "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.UserDetailsResponseSchema, "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.UserDetailsResponseSchema, "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 # Helpers def url_for_this(self, app, **kwargs): if "endpoint" not in kwargs: kwargs["endpoint"] = "api_v1.user_details" with app.app_context(): u = url_for(**kwargs) return u class TestUserClaims: 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_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 # Helpers def url_for_this(self, app, **kwargs): if "endpoint" not in kwargs: kwargs["endpoint"] = "api_v1.user_claims" with app.app_context(): u = url_for(**kwargs) return u class TestUserResourceGroupClaims: 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_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 # Helpers def url_for_this(self, app, **kwargs): if "endpoint" not in kwargs: kwargs["endpoint"] = "api_v1.user_resource_group_claims" with app.app_context(): u = url_for(**kwargs) return u class TestUserResourceGroups: 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_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( 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_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 # Helpers def url_for_this(self, app, **kwargs): if "endpoint" not in kwargs: kwargs["endpoint"] = "api_v1.user_resource_groups" with app.app_context(): u = url_for(**kwargs) return u class TestUserClaimNames: 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_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_sub_group_external_id( account.sub, resource_group.external_id ) ) resp = client.get(url) with app.test_request_context(url): dump_expects = mocker.call( APIQueryPagination( query=QueryOrdering( query, allowed_fields=[ ClaimName.friendly, ClaimName.external_id, ], fields_mapping={"id": "external_id"}, ).query ) ) 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 # Helpers def url_for_this(self, app, **kwargs): if "endpoint" not in kwargs: kwargs["endpoint"] = "api_v1.user_claim_names" with app.app_context(): u = url_for(**kwargs) return u class TestUserClaimValues: 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_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 = {} with app.test_client() as client: query = ( ClaimValue.query.active().by_dna_account_sub_name_external_ids( account.sub, claim_name.resource_group.external_id, claim_name.external_id, ) ) resp = client.get(url) with app.test_request_context(url): dump_expects = mocker.call( APIQueryPagination( query=QueryOrdering( query, allowed_fields=[ ClaimValue.friendly, ClaimValue.external_id, ], fields_mapping={"id": "external_id"}, ).query ) ) 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 # Helpers def url_for_this(self, app, **kwargs): if "endpoint" not in kwargs: kwargs["endpoint"] = "api_v1.user_claim_values" with app.app_context(): u = url_for(**kwargs) return u class TestUserclaimNameClaims: 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_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 # Helpers def url_for_this(self, app, **kwargs): if "endpoint" not in kwargs: kwargs["endpoint"] = "api_v1.user_claim_name_claims" with app.app_context(): u = url_for(**kwargs) return u