def test_filter_repositories(username, include_public, filter_to_namespace, repo_kind, initialized_db): namespace = username if filter_to_namespace else None if '+' in username and filter_to_namespace: namespace, _ = parse_robot_username(username) user = get_namespace_user(username) query = (Repository.select().distinct().join( Namespace, on=(Repository.namespace_user == Namespace.id )).switch(Repository).join(RepositoryPermission, JOIN.LEFT_OUTER)) # Prime the cache. Repository.kind.get_id('image') with assert_query_count(1): found = list( filter_to_repos_for_user(query, user.id, namespace=namespace, include_public=include_public, repo_kind=repo_kind)) expected = list( _get_visible_repositories_for_user(user, repo_kind=repo_kind, namespace=namespace, include_public=include_public)) assert len(found) == len(expected) assert {r.id for r in found} == {r.id for r in expected}
def _filter(self, query): if self._namespace_filter: logger.info("Filtering by namespace `%s`", self._namespace_filter) namespace_user = get_namespace_user(self._namespace_filter) query = query.join(Repository).where(Repository.namespace_user == namespace_user) return query