-
Notifications
You must be signed in to change notification settings - Fork 492
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NAS-130069 / 24.10 / Fix API key API (#14030)
* Fix API key API * Address review
- Loading branch information
1 parent
137411a
commit d60e4bc
Showing
4 changed files
with
69 additions
and
17 deletions.
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
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,49 @@ | ||
import contextlib | ||
|
||
from middlewared.test.integration.utils import call, client | ||
|
||
|
||
@contextlib.contextmanager | ||
def api_key(allowlist): | ||
key = call("api_key.create", {"name": "Test API Key", "allowlist": allowlist}) | ||
try: | ||
yield key | ||
finally: | ||
call("api_key.delete", key["id"]) | ||
|
||
|
||
def test_has_key_after_creation_but_not_read(): | ||
key = call("api_key.create", {"name": "Test", "allowlist": []}) | ||
try: | ||
assert "key" in key | ||
|
||
instance = call("api_key.get_instance", key["id"]) | ||
assert "key" not in instance | ||
|
||
update = call("api_key.update", key["id"], {}) | ||
assert "key" not in update | ||
finally: | ||
call("api_key.delete", key["id"]) | ||
|
||
|
||
def test_api_key_reset(): | ||
with api_key([]) as key: | ||
with client(auth=None) as c: | ||
assert c.call("auth.login_with_api_key", key["key"]) | ||
|
||
updated = call("api_key.update", key["id"], {"reset": True}) | ||
|
||
with client(auth=None) as c: | ||
assert not c.call("auth.login_with_api_key", key["key"]) | ||
|
||
with client(auth=None) as c: | ||
assert c.call("auth.login_with_api_key", updated["key"]) | ||
|
||
|
||
def test_api_key_delete(): | ||
with api_key([]) as key: | ||
with client(auth=None) as c: | ||
assert c.call("auth.login_with_api_key", key["key"]) | ||
|
||
with client(auth=None) as c: | ||
assert not c.call("auth.login_with_api_key", key["key"]) |