refactor(test): add login_as_admin in global conftest (#20703)

This commit is contained in:
Jesse Yang
2022-07-14 18:10:31 -07:00
committed by GitHub
parent 5beb1aa2f5
commit 2cb4fd31f1
27 changed files with 494 additions and 454 deletions

View File

@@ -16,7 +16,9 @@
# under the License.
from __future__ import annotations
from typing import Any, Dict, TYPE_CHECKING
from typing import Any, Dict
from flask.testing import FlaskClient
from superset.dashboards.filter_sets.consts import (
DASHBOARD_OWNER_TYPE,
@@ -27,7 +29,6 @@ from superset.dashboards.filter_sets.consts import (
OWNER_TYPE_FIELD,
USER_OWNER_TYPE,
)
from tests.integration_tests.base_tests import login
from tests.integration_tests.dashboards.filter_sets.consts import (
ADMIN_USERNAME_FOR_TEST,
DASHBOARD_OWNER_USERNAME,
@@ -38,9 +39,7 @@ from tests.integration_tests.dashboards.filter_sets.utils import (
get_filter_set_by_dashboard_id,
get_filter_set_by_name,
)
if TYPE_CHECKING:
from flask.testing import FlaskClient
from tests.integration_tests.test_app import login
def assert_filterset_was_not_created(filter_set_data: Dict[str, Any]) -> None:

View File

@@ -18,7 +18,6 @@ from __future__ import annotations
from typing import Any, Dict, List, TYPE_CHECKING
from tests.integration_tests.base_tests import login
from tests.integration_tests.dashboards.filter_sets.consts import (
DASHBOARD_OWNER_USERNAME,
FILTER_SET_OWNER_USERNAME,
@@ -29,6 +28,7 @@ from tests.integration_tests.dashboards.filter_sets.utils import (
collect_all_ids,
get_filter_set_by_name,
)
from tests.integration_tests.test_app import login
if TYPE_CHECKING:
from flask.testing import FlaskClient

View File

@@ -18,7 +18,6 @@ from __future__ import annotations
from typing import Any, Dict, List, Set, TYPE_CHECKING
from tests.integration_tests.base_tests import login
from tests.integration_tests.dashboards.filter_sets.consts import (
DASHBOARD_OWNER_USERNAME,
FILTER_SET_OWNER_USERNAME,
@@ -28,6 +27,7 @@ from tests.integration_tests.dashboards.filter_sets.utils import (
call_get_filter_sets,
collect_all_ids,
)
from tests.integration_tests.test_app import login
if TYPE_CHECKING:
from flask.testing import FlaskClient

View File

@@ -26,7 +26,6 @@ from superset.dashboards.filter_sets.consts import (
OWNER_TYPE_FIELD,
PARAMS_PROPERTY,
)
from tests.integration_tests.base_tests import login
from tests.integration_tests.dashboards.filter_sets.consts import (
DASHBOARD_OWNER_USERNAME,
FILTER_SET_OWNER_USERNAME,
@@ -37,6 +36,7 @@ from tests.integration_tests.dashboards.filter_sets.utils import (
collect_all_ids,
get_filter_set_by_name,
)
from tests.integration_tests.test_app import login
if TYPE_CHECKING:
from flask.testing import FlaskClient

View File

@@ -18,6 +18,7 @@ import json
from unittest.mock import patch
import pytest
from flask.ctx import AppContext
from flask_appbuilder.security.sqla.models import User
from sqlalchemy.orm import Session
@@ -26,8 +27,6 @@ from superset.extensions import cache_manager
from superset.models.dashboard import Dashboard
from superset.temporary_cache.commands.entry import Entry
from superset.temporary_cache.utils import cache_key
from tests.integration_tests.base_tests import login
from tests.integration_tests.fixtures.client import client
from tests.integration_tests.fixtures.world_bank_dashboard import (
load_world_bank_dashboard_with_slices,
load_world_bank_data,
@@ -40,19 +39,17 @@ UPDATED_VALUE = json.dumps({"test": "updated value"})
@pytest.fixture
def dashboard_id(load_world_bank_dashboard_with_slices) -> int:
with app.app_context() as ctx:
session: Session = ctx.app.appbuilder.get_session
dashboard = session.query(Dashboard).filter_by(slug="world_health").one()
return dashboard.id
def dashboard_id(app_context: AppContext, load_world_bank_dashboard_with_slices) -> int:
session: Session = app_context.app.appbuilder.get_session
dashboard = session.query(Dashboard).filter_by(slug="world_health").one()
return dashboard.id
@pytest.fixture
def admin_id() -> int:
with app.app_context() as ctx:
session: Session = ctx.app.appbuilder.get_session
admin = session.query(User).filter_by(username="admin").one_or_none()
return admin.id
def admin_id(app_context: AppContext) -> int:
session: Session = app_context.app.appbuilder.get_session
admin = session.query(User).filter_by(username="admin").one_or_none()
return admin.id
@pytest.fixture(autouse=True)
@@ -61,55 +58,62 @@ def cache(dashboard_id, admin_id):
cache_manager.filter_state_cache.set(cache_key(dashboard_id, KEY), entry)
def test_post(client, dashboard_id: int):
login(client, "admin")
payload = {
"value": INITIAL_VALUE,
}
resp = client.post(f"api/v1/dashboard/{dashboard_id}/filter_state", json=payload)
def test_post(test_client, login_as_admin, dashboard_id: int):
resp = test_client.post(
f"api/v1/dashboard/{dashboard_id}/filter_state",
json={
"value": INITIAL_VALUE,
},
)
assert resp.status_code == 201
def test_post_bad_request_non_string(client, dashboard_id: int):
login(client, "admin")
payload = {
"value": 1234,
}
resp = client.post(f"api/v1/dashboard/{dashboard_id}/filter_state", json=payload)
def test_post_bad_request_non_string(test_client, login_as_admin, dashboard_id: int):
resp = test_client.post(
f"api/v1/dashboard/{dashboard_id}/filter_state",
json={
"value": 1234,
},
)
assert resp.status_code == 400
def test_post_bad_request_non_json_string(client, dashboard_id: int):
login(client, "admin")
def test_post_bad_request_non_json_string(
test_client, login_as_admin, dashboard_id: int
):
payload = {
"value": "foo",
}
resp = client.post(f"api/v1/dashboard/{dashboard_id}/filter_state", json=payload)
resp = test_client.post(
f"api/v1/dashboard/{dashboard_id}/filter_state", json=payload
)
assert resp.status_code == 400
@patch("superset.security.SupersetSecurityManager.raise_for_dashboard_access")
def test_post_access_denied(mock_raise_for_dashboard_access, client, dashboard_id: int):
login(client, "admin")
def test_post_access_denied(
mock_raise_for_dashboard_access, test_client, login_as_admin, dashboard_id: int
):
mock_raise_for_dashboard_access.side_effect = DashboardAccessDeniedError()
payload = {
"value": INITIAL_VALUE,
}
resp = client.post(f"api/v1/dashboard/{dashboard_id}/filter_state", json=payload)
resp = test_client.post(
f"api/v1/dashboard/{dashboard_id}/filter_state", json=payload
)
assert resp.status_code == 403
def test_post_same_key_for_same_tab_id(client, dashboard_id: int):
login(client, "admin")
def test_post_same_key_for_same_tab_id(test_client, login_as_admin, dashboard_id: int):
payload = {
"value": INITIAL_VALUE,
}
resp = client.post(
resp = test_client.post(
f"api/v1/dashboard/{dashboard_id}/filter_state?tab_id=1", json=payload
)
data = json.loads(resp.data.decode("utf-8"))
first_key = data.get("key")
resp = client.post(
resp = test_client.post(
f"api/v1/dashboard/{dashboard_id}/filter_state?tab_id=1", json=payload
)
data = json.loads(resp.data.decode("utf-8"))
@@ -117,17 +121,18 @@ def test_post_same_key_for_same_tab_id(client, dashboard_id: int):
assert first_key == second_key
def test_post_different_key_for_different_tab_id(client, dashboard_id: int):
login(client, "admin")
def test_post_different_key_for_different_tab_id(
test_client, login_as_admin, dashboard_id: int
):
payload = {
"value": INITIAL_VALUE,
}
resp = client.post(
resp = test_client.post(
f"api/v1/dashboard/{dashboard_id}/filter_state?tab_id=1", json=payload
)
data = json.loads(resp.data.decode("utf-8"))
first_key = data.get("key")
resp = client.post(
resp = test_client.post(
f"api/v1/dashboard/{dashboard_id}/filter_state?tab_id=2", json=payload
)
data = json.loads(resp.data.decode("utf-8"))
@@ -135,42 +140,45 @@ def test_post_different_key_for_different_tab_id(client, dashboard_id: int):
assert first_key != second_key
def test_post_different_key_for_no_tab_id(client, dashboard_id: int):
login(client, "admin")
def test_post_different_key_for_no_tab_id(
test_client, login_as_admin, dashboard_id: int
):
payload = {
"value": INITIAL_VALUE,
}
resp = client.post(f"api/v1/dashboard/{dashboard_id}/filter_state", json=payload)
resp = test_client.post(
f"api/v1/dashboard/{dashboard_id}/filter_state", json=payload
)
data = json.loads(resp.data.decode("utf-8"))
first_key = data.get("key")
resp = client.post(f"api/v1/dashboard/{dashboard_id}/filter_state", json=payload)
resp = test_client.post(
f"api/v1/dashboard/{dashboard_id}/filter_state", json=payload
)
data = json.loads(resp.data.decode("utf-8"))
second_key = data.get("key")
assert first_key != second_key
def test_put(client, dashboard_id: int):
login(client, "admin")
payload = {
"value": UPDATED_VALUE,
}
resp = client.put(
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}", json=payload
def test_put(test_client, login_as_admin, dashboard_id: int):
resp = test_client.put(
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}",
json={
"value": UPDATED_VALUE,
},
)
assert resp.status_code == 200
def test_put_same_key_for_same_tab_id(client, dashboard_id: int):
login(client, "admin")
def test_put_same_key_for_same_tab_id(test_client, login_as_admin, dashboard_id: int):
payload = {
"value": INITIAL_VALUE,
}
resp = client.put(
resp = test_client.put(
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}?tab_id=1", json=payload
)
data = json.loads(resp.data.decode("utf-8"))
first_key = data.get("key")
resp = client.put(
resp = test_client.put(
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}?tab_id=1", json=payload
)
data = json.loads(resp.data.decode("utf-8"))
@@ -178,17 +186,18 @@ def test_put_same_key_for_same_tab_id(client, dashboard_id: int):
assert first_key == second_key
def test_put_different_key_for_different_tab_id(client, dashboard_id: int):
login(client, "admin")
def test_put_different_key_for_different_tab_id(
test_client, login_as_admin, dashboard_id: int
):
payload = {
"value": INITIAL_VALUE,
}
resp = client.put(
resp = test_client.put(
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}?tab_id=1", json=payload
)
data = json.loads(resp.data.decode("utf-8"))
first_key = data.get("key")
resp = client.put(
resp = test_client.put(
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}?tab_id=2", json=payload
)
data = json.loads(resp.data.decode("utf-8"))
@@ -196,17 +205,18 @@ def test_put_different_key_for_different_tab_id(client, dashboard_id: int):
assert first_key != second_key
def test_put_different_key_for_no_tab_id(client, dashboard_id: int):
login(client, "admin")
def test_put_different_key_for_no_tab_id(
test_client, login_as_admin, dashboard_id: int
):
payload = {
"value": INITIAL_VALUE,
}
resp = client.put(
resp = test_client.put(
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}", json=payload
)
data = json.loads(resp.data.decode("utf-8"))
first_key = data.get("key")
resp = client.put(
resp = test_client.put(
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}", json=payload
)
data = json.loads(resp.data.decode("utf-8"))
@@ -214,97 +224,94 @@ def test_put_different_key_for_no_tab_id(client, dashboard_id: int):
assert first_key != second_key
def test_put_bad_request_non_string(client, dashboard_id: int):
login(client, "admin")
payload = {
"value": 1234,
}
resp = client.put(
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}", json=payload
def test_put_bad_request_non_string(test_client, login_as_admin, dashboard_id: int):
resp = test_client.put(
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}",
json={
"value": 1234,
},
)
assert resp.status_code == 400
def test_put_bad_request_non_json_string(client, dashboard_id: int):
login(client, "admin")
payload = {
"value": "foo",
}
resp = client.put(
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}", json=payload
def test_put_bad_request_non_json_string(
test_client, login_as_admin, dashboard_id: int
):
resp = test_client.put(
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}",
json={
"value": "foo",
},
)
assert resp.status_code == 400
@patch("superset.security.SupersetSecurityManager.raise_for_dashboard_access")
def test_put_access_denied(mock_raise_for_dashboard_access, client, dashboard_id: int):
login(client, "admin")
def test_put_access_denied(
mock_raise_for_dashboard_access, test_client, login_as_admin, dashboard_id: int
):
mock_raise_for_dashboard_access.side_effect = DashboardAccessDeniedError()
payload = {
"value": UPDATED_VALUE,
}
resp = client.put(
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}", json=payload
resp = test_client.put(
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}",
json={
"value": UPDATED_VALUE,
},
)
assert resp.status_code == 403
def test_put_not_owner(client, dashboard_id: int):
login(client, "gamma")
payload = {
"value": UPDATED_VALUE,
}
resp = client.put(
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}", json=payload
def test_put_not_owner(test_client, login_as, dashboard_id: int):
login_as("gamma")
resp = test_client.put(
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}",
json={
"value": UPDATED_VALUE,
},
)
assert resp.status_code == 403
def test_get_key_not_found(client, dashboard_id: int):
login(client, "admin")
resp = client.get(f"api/v1/dashboard/{dashboard_id}/filter_state/unknown-key/")
def test_get_key_not_found(test_client, login_as_admin, dashboard_id: int):
resp = test_client.get(f"api/v1/dashboard/{dashboard_id}/filter_state/unknown-key/")
assert resp.status_code == 404
def test_get_dashboard_not_found(client):
login(client, "admin")
resp = client.get(f"api/v1/dashboard/{-1}/filter_state/{KEY}")
def test_get_dashboard_not_found(test_client, login_as_admin):
resp = test_client.get(f"api/v1/dashboard/{-1}/filter_state/{KEY}")
assert resp.status_code == 404
def test_get(client, dashboard_id: int):
login(client, "admin")
resp = client.get(f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}")
def test_get_dashboard_filter_state(test_client, login_as_admin, dashboard_id: int):
resp = test_client.get(f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}")
assert resp.status_code == 200
data = json.loads(resp.data.decode("utf-8"))
assert INITIAL_VALUE == data.get("value")
@patch("superset.security.SupersetSecurityManager.raise_for_dashboard_access")
def test_get_access_denied(mock_raise_for_dashboard_access, client, dashboard_id):
login(client, "admin")
def test_get_access_denied(
mock_raise_for_dashboard_access, test_client, login_as_admin, dashboard_id
):
mock_raise_for_dashboard_access.side_effect = DashboardAccessDeniedError()
resp = client.get(f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}")
resp = test_client.get(f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}")
assert resp.status_code == 403
def test_delete(client, dashboard_id: int):
login(client, "admin")
resp = client.delete(f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}")
def test_delete(test_client, login_as_admin, dashboard_id: int):
resp = test_client.delete(f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}")
assert resp.status_code == 200
@patch("superset.security.SupersetSecurityManager.raise_for_dashboard_access")
def test_delete_access_denied(
mock_raise_for_dashboard_access, client, dashboard_id: int
mock_raise_for_dashboard_access, test_client, login_as_admin, dashboard_id: int
):
login(client, "admin")
mock_raise_for_dashboard_access.side_effect = DashboardAccessDeniedError()
resp = client.delete(f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}")
resp = test_client.delete(f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}")
assert resp.status_code == 403
def test_delete_not_owner(client, dashboard_id: int):
login(client, "gamma")
resp = client.delete(f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}")
def test_delete_not_owner(test_client, login_as, dashboard_id: int):
login_as("gamma")
resp = test_client.delete(f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}")
assert resp.status_code == 403

View File

@@ -29,8 +29,6 @@ from superset.key_value.models import KeyValueEntry
from superset.key_value.types import KeyValueResource
from superset.key_value.utils import decode_permalink_id
from superset.models.dashboard import Dashboard
from tests.integration_tests.base_tests import login
from tests.integration_tests.fixtures.client import client
from tests.integration_tests.fixtures.world_bank_dashboard import (
load_world_bank_dashboard_with_slices,
load_world_bank_data,
@@ -67,9 +65,10 @@ def permalink_salt() -> Iterator[str]:
db.session.commit()
def test_post(client, dashboard_id: int, permalink_salt: str) -> None:
login(client, "admin")
resp = client.post(f"api/v1/dashboard/{dashboard_id}/permalink", json=STATE)
def test_post(
test_client, login_as_admin, dashboard_id: int, permalink_salt: str
) -> None:
resp = test_client.post(f"api/v1/dashboard/{dashboard_id}/permalink", json=STATE)
assert resp.status_code == 201
data = resp.json
key = data["key"]
@@ -79,7 +78,9 @@ def test_post(client, dashboard_id: int, permalink_salt: str) -> None:
assert (
data
== client.post(f"api/v1/dashboard/{dashboard_id}/permalink", json=STATE).json
== test_client.post(
f"api/v1/dashboard/{dashboard_id}/permalink", json=STATE
).json
), "Should always return the same permalink key for the same payload"
db.session.query(KeyValueEntry).filter_by(id=id_).delete()
@@ -87,27 +88,26 @@ def test_post(client, dashboard_id: int, permalink_salt: str) -> None:
@patch("superset.security.SupersetSecurityManager.raise_for_dashboard_access")
def test_post_access_denied(mock_raise_for_dashboard_access, client, dashboard_id: int):
login(client, "admin")
def test_post_access_denied(
mock_raise_for_dashboard_access, test_client, login_as_admin, dashboard_id: int
):
mock_raise_for_dashboard_access.side_effect = DashboardAccessDeniedError()
resp = client.post(f"api/v1/dashboard/{dashboard_id}/permalink", json=STATE)
resp = test_client.post(f"api/v1/dashboard/{dashboard_id}/permalink", json=STATE)
assert resp.status_code == 403
def test_post_invalid_schema(client, dashboard_id: int):
login(client, "admin")
resp = client.post(
def test_post_invalid_schema(test_client, login_as_admin, dashboard_id: int):
resp = test_client.post(
f"api/v1/dashboard/{dashboard_id}/permalink", json={"foo": "bar"}
)
assert resp.status_code == 400
def test_get(client, dashboard_id: int, permalink_salt: str):
login(client, "admin")
key = client.post(f"api/v1/dashboard/{dashboard_id}/permalink", json=STATE).json[
"key"
]
resp = client.get(f"api/v1/dashboard/permalink/{key}")
def test_get(test_client, login_as_admin, dashboard_id: int, permalink_salt: str):
key = test_client.post(
f"api/v1/dashboard/{dashboard_id}/permalink", json=STATE
).json["key"]
resp = test_client.get(f"api/v1/dashboard/permalink/{key}")
assert resp.status_code == 200
result = resp.json
assert result["dashboardId"] == str(dashboard_id)