Skip to content

Commit

Permalink
UI bug fixes (#283)
Browse files Browse the repository at this point in the history
* fixed not being able to grant on tables with duplicate names

* Added tests for AccessForm

* Fixed flake8 error
  • Loading branch information
jamesstottmoj authored Sep 19, 2024
1 parent d9552df commit 7f3d8b1
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 1 deletion.
6 changes: 5 additions & 1 deletion ap/database_access/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ class Meta:
def clean_user(self):
user = self.cleaned_data.get("user")
try:
self._meta.model.objects.get(name=self.table_name, database_access__user=user)
self._meta.model.objects.get(
name=self.table_name,
database_access__name=self.database_name,
database_access__user=user,
)
raise forms.ValidationError("Selected user already has access to this table.")
except self._meta.model.DoesNotExist:
pass
Expand Down
72 changes: 72 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,75 @@ def users(db, superuser):
user_id="testing_other_user_id",
),
}


@pytest.fixture
def permissions(db):

return {
"select_table": baker.make(
"database_access.Permission",
name="select",
entity="table",
display_name="Select",
),
"select_db": baker.make(
"database_access.Permission",
name="select",
entity="database",
display_name="Select",
),
"describe": baker.make(
"database_access.Permission",
name="describe",
entity="database",
display_name="Describe",
),
}


@pytest.fixture
def database_access(db, users, permissions):
return {
"test_db_1": baker.make(
"database_access.DatabaseAccess",
user=users["normal_user"],
name="test_db_1",
permissions=[permissions["describe"]],
grantable_permissions=[permissions["describe"]],
),
"test_db_2": baker.make(
"database_access.DatabaseAccess",
user=users["other_user"],
name="test_db_2",
permissions=[permissions["describe"]],
grantable_permissions=[permissions["describe"]],
),
}


@pytest.fixture
def table_access(db, database_access, permissions):
return {
"table_1": baker.make(
"database_access.TableAccess",
database_access=database_access["test_db_1"],
name="table_1",
permissions=[permissions["select_table"]],
grantable_permissions=[permissions["select_table"]],
),
"table_2": baker.make(
"database_access.TableAccess",
database_access=database_access["test_db_1"],
name="table_2",
permissions=[permissions["select_table"]],
grantable_permissions=[],
),
"table_1_db_2": baker.make(
"database_access.TableAccess",
database_access=database_access["test_db_2"],
name="table_1",
permissions=[permissions["select_table"]],
grantable_permissions=[permissions["select_table"]],
),
}
57 changes: 57 additions & 0 deletions tests/database_access/test_forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from django import forms

from ap.database_access.forms import AccessForm


class TestAccessForm:

def create_form(self, users, form_data, table_name, database_name, grantable_access=None):
form = AccessForm(
data=form_data,
table_name=table_name,
database_name=database_name,
grantable_access=grantable_access,
)

form.cleaned_data = {"user": users["normal_user"]}

return form

def test_clean_user(self, users, permissions, table_access):
form_data = {
"user": users["normal_user"],
"permissions": [permissions["select_table"]],
"grantable_permissions": [],
}

form = self.create_form(users, form_data, "table_3", "test_db_1")
user = form.clean_user()
assert user == users["normal_user"]

def test_clean_user_duplicate_table_name(self, users, permissions, table_access):
"""Tests that user is returned even though name exists in a different"""
form_data = {
"user": users["normal_user"],
"permissions": [permissions["select_table"]],
"grantable_permissions": [],
}

form = self.create_form(users, form_data, "table_1", "test_db_2")
user = form.clean_user()
assert user == users["normal_user"]

def test_clean_user_duplicate(self, users, permissions, table_access):
"""Tests that validation error is raised if duplicate table name and database name"""
form_data = {
"user": users["normal_user"],
"permissions": [permissions["select_table"]],
"grantable_permissions": [],
}

form = self.create_form(users, form_data, "table_1", "test_db_1")

try:
form.clean_user()
AssertionError("Should Fail")
except forms.ValidationError:
assert True

0 comments on commit 7f3d8b1

Please sign in to comment.