Skip to content
Snippets Groups Projects
Commit 329ad98d authored by Valentin Rigal's avatar Valentin Rigal
Browse files

Update tests

parent ed70fa71
No related branches found
No related tags found
No related merge requests found
......@@ -8,6 +8,7 @@ from arkindex.documents.models import Corpus
from arkindex.project.mixins import ACLMixin, NewCorpusACLMixin, RepositoryACLMixin
from arkindex.project.tests import FixtureTestCase
from arkindex.users.models import Group, Right, Role, User
from arkindex.users.utils import filter_rights
class TestACLMixin(FixtureTestCase):
......@@ -57,9 +58,8 @@ class TestACLMixin(FixtureTestCase):
cls.repo_type = ContentType.objects.get_for_model(Repository)
cls.group_type = ContentType.objects.get_for_model(Group)
def test_right_via_group_restriction(self):
# User rights on corpora via a group are restricted to the group level
acl_mixin = ACLMixin(self.user3)
def test_filter_right(self):
# List all the rights with a privilege greater than 80 for user 3
params = {
'user_id': self.user3.id,
'group_id': self.group2.id,
......@@ -69,9 +69,25 @@ class TestACLMixin(FixtureTestCase):
}
with self.assertExactQueries('corpus_rights_filter.sql', params=params):
# List to queryset to fire the DB request
corpora = list(acl_mixin.rights_filter(Corpus, 80))
corpora = list(filter_rights(self.user3, Corpus, 80))
self.assertCountEqual(corpora, [self.corpus1])
def test_filter_right_including_public(self):
# Listing readable objects involve listing public objects with a SQL Union
params = {
'user_id': self.user1.id,
'group_id': self.group2.id,
'group_type_id': self.group_type.id,
'corpus_type_id': self.corpus_type.id,
'level': Role.Guest.value,
}
with self.assertExactQueries('corpus_rights_filter_public.sql', params=params):
corpora = list(filter_rights(self.user1, Corpus, Role.Guest.value))
self.assertCountEqual(
list(corpora),
[self.corpus1, self.corpus]
)
def test_right_direct_access(self):
# User 2 has a direct access to the above corpus with a level of 90
acl_mixin = ACLMixin(self.user2)
......@@ -100,6 +116,13 @@ class TestACLMixin(FixtureTestCase):
read_access = corpus_acl_mixin.has_read_access(self.corpus1)
self.assertTrue(read_access)
def test_public_corpus_has_read_access(self):
# Any user should have a read access to public corpora
corpus_acl_mixin = NewCorpusACLMixin(AnonymousUser())
with self.assertNumQueries(0):
read_access = corpus_acl_mixin.has_read_access(self.corpus)
self.assertTrue(read_access)
def test_corpus_acl_mixin_has_write_access(self):
# User2 has a direct access to Corpus1 with an adequate level
corpus_acl_mixin = NewCorpusACLMixin(self.user2)
......@@ -122,34 +145,13 @@ class TestACLMixin(FixtureTestCase):
admin_access = corpus_acl_mixin.has_admin_access(self.corpus1)
self.assertEqual(admin_access, access_check)
def test_corpus_acl_mixin_readable(self):
# Corpora with the public attribute are accessible by any user with guest rights
corpus_acl_mixin = NewCorpusACLMixin(self.user1)
params = {
'user_id': self.user1.id,
'group_id': self.group2.id,
'group_type_id': self.group_type.id,
'corpus_type_id': self.corpus_type.id,
'level': Role.Guest.value,
}
with self.assertExactQueries('corpus_rights_filter_public.sql', params=params):
corpora = list(corpus_acl_mixin.readable_corpora)
self.assertCountEqual(
[(corpus, corpus.max_level) for corpus in corpora],
[
(self.corpus1, 75),
# Defaults to guest role
(self.corpus, Role.Guest.value),
]
)
def test_corpus_acl_mixin_writable(self):
corpus_acl_mixin = NewCorpusACLMixin(self.user1)
with self.assertNumQueries(1):
corpora = list(corpus_acl_mixin.writable_corpora)
self.assertCountEqual(
[(corpus, corpus.max_level) for corpus in corpora],
[(self.corpus1, 75)]
list(corpora),
[self.corpus1]
)
def test_repo_acl_mixin_has_read_access(self):
......@@ -184,8 +186,8 @@ class TestACLMixin(FixtureTestCase):
with self.assertNumQueries(1):
repos = list(repo_acl_mixin.readable_repositories)
self.assertCountEqual(
[(repo, repo.max_level) for repo in repos],
[(self.repo1, 10)]
list(repos),
[self.repo1]
)
def test_repo_acl_mixin_executable(self):
......@@ -196,16 +198,12 @@ class TestACLMixin(FixtureTestCase):
def test_corpus_readable_orderable(self):
# Assert corpora retrieved via the mixin are still orderable
corpus_acl_mixin = NewCorpusACLMixin(self.user3, order_by_fields=('name',))
corpus_acl_mixin = NewCorpusACLMixin(self.user3)
with self.assertNumQueries(1):
corpora = list(corpus_acl_mixin.readable_corpora)
corpora = list(corpus_acl_mixin.readable_corpora.order_by('name'))
self.assertListEqual(
[(c.name, str(c.id)[:4], c.max_level) for c in corpora],
[
('Corpus1', 'bbbb', 100),
('Corpus2', 'aaaa', 75),
('Unit Tests', str(self.corpus.id)[:4], Role.Guest.value)
]
[c.name for c in corpora],
['Corpus1', 'Corpus2', 'Unit Tests']
)
def test_super_admin_readable_corpora(self):
......@@ -214,8 +212,8 @@ class TestACLMixin(FixtureTestCase):
with self.assertNumQueries(1):
corpora = list(corpus_acl_mixin.readable_corpora)
self.assertCountEqual(
[(c.name, c.max_level) for c in corpora],
[(c.name, Role.Admin.value) for c in Corpus.objects.all()]
list(corpora),
list(Corpus.objects.all())
)
def test_anonymous_user_readable_corpora(self):
......@@ -224,8 +222,8 @@ class TestACLMixin(FixtureTestCase):
with self.assertNumQueries(1):
corpora = list(corpus_acl_mixin.readable_corpora)
self.assertCountEqual(
[(c.name, c.max_level) for c in corpora],
[(c.name, Role.Guest.value) for c in Corpus.objects.filter(public=True)]
list(corpora),
list(Corpus.objects.filter(public=True))
)
def test_corpus_right_and_public(self):
......@@ -235,10 +233,6 @@ class TestACLMixin(FixtureTestCase):
with self.assertNumQueries(1):
corpora = list(corpus_acl_mixin.readable_corpora)
self.assertCountEqual(
[(c.id, c.max_level) for c in corpora],
[
(self.corpus1.id, 100),
(self.corpus2.id, 75),
(self.corpus.id, 42)
]
list(corpora),
[self.corpus1, self.corpus2, self.corpus]
)
SELECT DISTINCT ON ("documents_corpus"."id") "documents_corpus"."created",
"documents_corpus"."updated",
"documents_corpus"."id",
"documents_corpus"."name",
"documents_corpus"."description",
"documents_corpus"."repository_id",
"documents_corpus"."public",
LEAST("users_right"."level", T5."level") AS "max_level"
SELECT "documents_corpus"."created",
"documents_corpus"."updated",
"documents_corpus"."id",
"documents_corpus"."name",
"documents_corpus"."description",
"documents_corpus"."repository_id",
"documents_corpus"."public",
LEAST("users_right"."level", T5."level") AS "max_level"
FROM "documents_corpus"
INNER JOIN "users_right" ON ("documents_corpus"."id" = "users_right"."content_id"
AND ("users_right"."content_type_id" = {corpus_type_id}))
......@@ -15,4 +15,3 @@ LEFT OUTER JOIN "users_right" T5 ON ("users_group"."id" = T5."content_id"
WHERE (("users_right"."user_id" = {user_id}
OR T5."user_id" = {user_id})
AND LEAST("users_right"."level", T5."level") >= {level})
ORDER BY "documents_corpus"."id" ASC
(SELECT DISTINCT ON ("documents_corpus"."id") "documents_corpus"."created",
"documents_corpus"."updated",
"documents_corpus"."id",
"documents_corpus"."name",
"documents_corpus"."description",
"documents_corpus"."repository_id",
"documents_corpus"."public",
LEAST("users_right"."level", T5."level") AS "max_level"
(SELECT "documents_corpus"."created",
"documents_corpus"."updated",
"documents_corpus"."id",
"documents_corpus"."name",
"documents_corpus"."description",
"documents_corpus"."repository_id",
"documents_corpus"."public",
LEAST("users_right"."level", T5."level") AS "max_level"
FROM "documents_corpus"
INNER JOIN "users_right" ON ("documents_corpus"."id" = "users_right"."content_id"
AND ("users_right"."content_type_id" = {corpus_type_id}))
......@@ -14,8 +14,7 @@
AND (T5."content_type_id" = {group_type_id}))
WHERE (("users_right"."user_id" = {user_id}
OR T5."user_id" = {user_id})
AND LEAST("users_right"."level", T5."level") >= {level})
ORDER BY "documents_corpus"."id" ASC, "max_level" DESC)
AND LEAST("users_right"."level", T5."level") >= {level}))
UNION
(SELECT "documents_corpus"."created",
"documents_corpus"."updated",
......@@ -27,4 +26,3 @@ UNION
10 AS "max_level"
FROM "documents_corpus"
WHERE "documents_corpus"."public")
ORDER BY (3) ASC
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment