mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-09-15 20:57:56 +08:00
* Add basic authentication in the server A pattern for creating authentication methods is introduced, with three authentication types defined: none - no authentication session - like the current session based auth used for user annotations test - used to test the login/logout process end to end The config endpoint now returns informations about the authentication, like if the user is authenticated and their username. The redirect uri's for login and logout are also returned if the authentication type requires login This is the first a several PRs for authentication. *. Update server tests to avoid hardcoded ports test_api and test_nan_rest now use a common function for starting a test server, than will initially choose a random port.
143 lines
6.3 KiB
Python
143 lines
6.3 KiB
Python
import unittest
|
|
from server.common.app_config import AppConfig
|
|
from server.test import PROJECT_ROOT, test_server
|
|
import requests
|
|
|
|
|
|
class AuthTest(unittest.TestCase):
|
|
def test_auth_none(self):
|
|
c = AppConfig()
|
|
c.update_server_config(
|
|
authentication__type=None, multi_dataset__dataroot=f"{PROJECT_ROOT}/server/test/test_datasets"
|
|
)
|
|
c.update_default_dataset_config(user_annotations__enable=False)
|
|
|
|
c.complete_config()
|
|
|
|
with test_server(app_config=c) as server:
|
|
session = requests.Session()
|
|
r = session.get(f"{server}/d/pbmc3k.cxg/api/v0.2/config")
|
|
data_config = r.json()
|
|
assert "authentication" not in data_config["config"]
|
|
|
|
def test_auth_session(self):
|
|
c = AppConfig()
|
|
c.update_server_config(
|
|
authentication__type="session", multi_dataset__dataroot=f"{PROJECT_ROOT}/server/test/test_datasets"
|
|
)
|
|
c.update_default_dataset_config(user_annotations__enable=True)
|
|
c.complete_config()
|
|
|
|
with test_server(app_config=c) as server:
|
|
session = requests.Session()
|
|
r = session.get(f"{server}/d/pbmc3k.cxg/api/v0.2/config")
|
|
data_config = r.json()
|
|
assert data_config["config"]["authentication"]["is_authenticated"]
|
|
assert not data_config["config"]["authentication"]["requires_client_login"]
|
|
assert data_config["config"]["authentication"]["username"] == "anonymous"
|
|
|
|
def test_auth_test(self):
|
|
c = AppConfig()
|
|
c.update_server_config(authentication__type="test")
|
|
c.update_server_config(
|
|
multi_dataset__dataroot=dict(
|
|
a1=dict(dataroot=f"{PROJECT_ROOT}/server/test/test_datasets", base_url="auth"),
|
|
a2=dict(dataroot=f"{PROJECT_ROOT}/server/test/test_datasets", base_url="no-auth"),
|
|
)
|
|
)
|
|
|
|
# specialize the configs
|
|
c.add_dataroot_config("a1", app__authentication_enable=True, user_annotations__enable=True)
|
|
c.add_dataroot_config("a2", app__authentication_enable=False, user_annotations__enable=False)
|
|
|
|
c.complete_config()
|
|
|
|
with test_server(app_config=c) as server:
|
|
session = requests.Session()
|
|
|
|
# auth datasets
|
|
r = session.get(f"{server}/auth/pbmc3k.cxg/api/v0.2/config")
|
|
data_config = r.json()
|
|
assert not data_config["config"]["authentication"]["is_authenticated"]
|
|
assert data_config["config"]["authentication"]["requires_client_login"]
|
|
assert data_config["config"]["authentication"]["username"] is None
|
|
assert data_config["config"]["parameters"]["annotations"]
|
|
|
|
login_uri = data_config["config"]["authentication"]["login"]
|
|
logout_uri = data_config["config"]["authentication"]["logout"]
|
|
|
|
assert login_uri == "/login?dataset=auth/pbmc3k.cxg"
|
|
assert logout_uri == "/logout?dataset=auth/pbmc3k.cxg"
|
|
|
|
r = session.get(f"{server}/{login_uri}")
|
|
# check that the login redirect worked
|
|
assert r.history[0].status_code == 302
|
|
assert r.url == f"{server}/auth/pbmc3k.cxg/"
|
|
|
|
r = session.get(f"{server}/auth/pbmc3k.cxg/api/v0.2/config")
|
|
data_config = r.json()
|
|
assert data_config["config"]["authentication"]["is_authenticated"]
|
|
assert data_config["config"]["authentication"]["username"] == "test_account"
|
|
assert data_config["config"]["parameters"]["annotations"]
|
|
|
|
r = session.get(f"{server}/{logout_uri}")
|
|
# check that the logout redirect worked
|
|
assert r.history[0].status_code == 302
|
|
assert r.url == f"{server}/auth/pbmc3k.cxg/"
|
|
r = session.get(f"{server}/auth/pbmc3k.cxg/api/v0.2/config")
|
|
data_config = r.json()
|
|
assert not data_config["config"]["authentication"]["is_authenticated"]
|
|
assert data_config["config"]["authentication"]["username"] is None
|
|
assert data_config["config"]["parameters"]["annotations"]
|
|
|
|
# no-auth datasets
|
|
r = session.get(f"{server}/no-auth/pbmc3k.cxg/api/v0.2/config")
|
|
data_config = r.json()
|
|
assert "authentication" not in data_config["config"]
|
|
assert not data_config["config"]["parameters"]["annotations"]
|
|
|
|
def test_auth_test_single(self):
|
|
c = AppConfig()
|
|
c.update_server_config(
|
|
authentication__type="test",
|
|
single_dataset__datapath=f"{PROJECT_ROOT}/server/test/test_datasets/pbmc3k.cxg")
|
|
|
|
c.complete_config()
|
|
|
|
with test_server(app_config=c) as server:
|
|
session = requests.Session()
|
|
|
|
r = session.get(f"{server}/api/v0.2/config")
|
|
data_config = r.json()
|
|
assert not data_config["config"]["authentication"]["is_authenticated"]
|
|
assert data_config["config"]["authentication"]["requires_client_login"]
|
|
assert data_config["config"]["authentication"]["username"] is None
|
|
assert data_config["config"]["parameters"]["annotations"]
|
|
|
|
login_uri = data_config["config"]["authentication"]["login"]
|
|
logout_uri = data_config["config"]["authentication"]["logout"]
|
|
|
|
assert login_uri == "/login"
|
|
assert logout_uri == "/logout"
|
|
|
|
r = session.get(f"{server}/{login_uri}")
|
|
# check that the login redirect worked
|
|
assert r.history[0].status_code == 302
|
|
assert r.url == f"{server}/"
|
|
|
|
r = session.get(f"{server}/api/v0.2/config")
|
|
data_config = r.json()
|
|
assert data_config["config"]["authentication"]["is_authenticated"]
|
|
assert data_config["config"]["authentication"]["username"] == "test_account"
|
|
assert data_config["config"]["parameters"]["annotations"]
|
|
|
|
r = session.get(f"{server}/{logout_uri}")
|
|
# check that the logout redirect worked
|
|
assert r.history[0].status_code == 302
|
|
assert r.url == f"{server}/"
|
|
r = session.get(f"{server}/api/v0.2/config")
|
|
data_config = r.json()
|
|
assert not data_config["config"]["authentication"]["is_authenticated"]
|
|
assert data_config["config"]["authentication"]["username"] is None
|
|
assert data_config["config"]["parameters"]["annotations"]
|