-
-
Notifications
You must be signed in to change notification settings - Fork 362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add database_privileges.replace_for_roles
RPC function.
#3781
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
13ec019
Add initial privilege replacer SQL function
mathemancer 7d7f9ba
Merge branch 'develop' into replace_db_priv_rpc
mathemancer 2ef8abf
use name getter function to handle (un)quoting
mathemancer 76e14a2
Add SQL tests for privilege replacement
mathemancer 3ed5f7c
Add python wrapper for role replacer
mathemancer 376aa6b
update param name to conform with other RPC functions
mathemancer 931c065
wire up db privilege replacer to RPC endpoint
mathemancer d8e8a18
add wiring test for database privilege replacer
mathemancer 86530e5
improve/add docs for role privilege replacer
mathemancer c4294e2
Merge branch 'develop' into replace_db_priv_rpc
mathemancer a60bf26
Fix typing literal/union syntax
mathemancer 89cc77d
update test to conform to actual function call
mathemancer 199c0d2
cast the role OID to bigint as per our guidelines
mathemancer bc0ba3f
cast stringified oids to bigint for 'role' related functions
Anish9901 aa74ad2
Merge branch 'develop' into replace_db_priv_rpc
Anish9901 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import json | ||
from db.connection import exec_msar_func | ||
|
||
|
||
def replace_database_privileges_for_roles(conn, privileges): | ||
return exec_msar_func( | ||
conn, 'replace_database_privileges_for_roles', json.dumps(privileges) | ||
).fetchone()[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import json | ||
from unittest.mock import patch | ||
from db.roles.operations import update as rupdate | ||
|
||
|
||
def test_replace_database_privileges_for_roles(): | ||
priv_spec = [{"role_oid": 1234, "privileges": ["CONNECT", "CREATE"]}] | ||
with patch.object(rupdate, 'exec_msar_func') as mock_exec: | ||
mock_exec.return_value.fetchone = lambda: ('a', 'b') | ||
result = rupdate.replace_database_privileges_for_roles('conn', priv_spec) | ||
mock_exec.assert_called_once_with( | ||
'conn', 'replace_database_privileges_for_roles', json.dumps(priv_spec) | ||
) | ||
assert result == 'a' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" | ||
This file tests the database_privileges RPC functions. | ||
|
||
Fixtures: | ||
rf(pytest-django): Provides mocked `Request` objects. | ||
monkeypatch(pytest): Lets you monkeypatch an object for testing. | ||
""" | ||
from contextlib import contextmanager | ||
|
||
from mathesar.rpc import database_privileges | ||
from mathesar.models.users import User | ||
|
||
|
||
def test_database_privileges_set_for_roles(rf, monkeypatch): | ||
_username = 'alice' | ||
_password = 'pass1234' | ||
_database_id = 2 | ||
_privileges = [{"role_oid": 12345, "direct": ["CONNECT"]}] | ||
request = rf.post('/api/rpc/v0/', data={}) | ||
request.user = User(username=_username, password=_password) | ||
|
||
@contextmanager | ||
def mock_connect(database_id, user): | ||
if database_id == _database_id and user.username == _username: | ||
try: | ||
yield True | ||
finally: | ||
pass | ||
else: | ||
raise AssertionError('incorrect parameters passed') | ||
|
||
def mock_replace_privileges( | ||
conn, | ||
privileges, | ||
): | ||
if privileges != _privileges: | ||
raise AssertionError('incorrect parameters passed') | ||
return _privileges + [{"role_oid": 67890, "direct": ["CONNECT", "TEMPORARY"]}] | ||
|
||
monkeypatch.setattr(database_privileges, 'connect', mock_connect) | ||
monkeypatch.setattr( | ||
database_privileges, | ||
'replace_database_privileges_for_roles', | ||
mock_replace_privileges | ||
) | ||
expect_response = [ | ||
{"role_oid": 12345, "direct": ["CONNECT"]}, | ||
{"role_oid": 67890, "direct": ["CONNECT", "TEMPORARY"]} | ||
] | ||
actual_response = database_privileges.replace_for_roles( | ||
privileges=_privileges, database_id=_database_id, request=request | ||
) | ||
assert actual_response == expect_response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this will work, will it? I'd need to check, but I'm pretty sure
jsonb_to_recordset
needs accurate key names in theAS
clause. So, we need to leave it asdirect
to match with what we expect in the spec when called.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked. It won't work that way without either making some adapter in Python to convert the JSON between formats (do not want) or changing the API spec (also do not want).