Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Commit

Permalink
use black to format tests (#466)
Browse files Browse the repository at this point in the history
* use black to format tests

* update command

* update make command to not build docker infra twice

* format more tests
  • Loading branch information
Sean Preston authored May 10, 2022
1 parent 84d5eb9 commit 23dbb25
Show file tree
Hide file tree
Showing 36 changed files with 435 additions and 324 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ pytest-saas: compose-build

.PHONY: black
black: compose-build
@echo "Running black formatting against the src/ directory..."
@docker-compose run $(IMAGE_NAME) \
black src/
@echo "Running black formatting against the src/ and tests/ directories..."
@docker-compose run $(IMAGE_NAME) black tests/ && black src/
@echo "Fin"

.PHONY: clean
clean:
Expand Down
36 changes: 17 additions & 19 deletions tests/api/v1/endpoints/test_connection_config_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,18 +269,14 @@ def test_patch_connections_bulk_update(
mysql_connection = response_body["succeeded"][2]
assert mysql_connection["access"] == "read"
assert mysql_connection["updated_at"] is not None
mysql_resource = (
db.query(ConnectionConfig).filter_by(key="my_mysql_db").first()
)
mysql_resource = db.query(ConnectionConfig).filter_by(key="my_mysql_db").first()
assert mysql_resource.access.value == "read"
assert "secrets" not in mysql_connection

mssql_connection = response_body["succeeded"][3]
assert mssql_connection["access"] == "write"
assert mssql_connection["updated_at"] is not None
mssql_resource = (
db.query(ConnectionConfig).filter_by(key="my_mssql_db").first()
)
mssql_resource = db.query(ConnectionConfig).filter_by(key="my_mssql_db").first()
assert mssql_resource.access.value == "write"
assert "secrets" not in mssql_connection

Expand Down Expand Up @@ -687,11 +683,11 @@ def test_put_connection_config_redshift_secrets(
assert redshift_connection_config.last_test_succeeded is None

def test_put_connection_config_bigquery_secrets(
self,
api_client: TestClient,
db: Session,
generate_auth_header,
bigquery_connection_config_without_secrets,
self,
api_client: TestClient,
db: Session,
generate_auth_header,
bigquery_connection_config_without_secrets,
) -> None:
"""Note: this test does not attempt to actually connect to the db, via use of verify query param."""
auth_header = generate_auth_header(scopes=[CONNECTION_CREATE_OR_UPDATE])
Expand All @@ -708,18 +704,18 @@ def test_put_connection_config_bigquery_secrets(
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/something%40project-12345.iam.gserviceaccount.com"
}
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/something%40project-12345.iam.gserviceaccount.com",
},
}
resp = api_client.put(
url + "?verify=False",
headers=auth_header,
json=payload,
)
)
assert resp.status_code == 200
assert (
json.loads(resp.text)["msg"]
== f"Secrets updated for ConnectionConfig with key: {bigquery_connection_config_without_secrets.key}."
json.loads(resp.text)["msg"]
== f"Secrets updated for ConnectionConfig with key: {bigquery_connection_config_without_secrets.key}."
)
db.refresh(bigquery_connection_config_without_secrets)
assert bigquery_connection_config_without_secrets.secrets == {
Expand All @@ -735,8 +731,8 @@ def test_put_connection_config_bigquery_secrets(
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/something%40project-12345.iam.gserviceaccount.com"
}
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/something%40project-12345.iam.gserviceaccount.com",
},
}
assert bigquery_connection_config_without_secrets.last_test_timestamp is None
assert bigquery_connection_config_without_secrets.last_test_succeeded is None
Expand Down Expand Up @@ -824,7 +820,9 @@ def test_put_saas_example_connection_config_secrets(
saas_example_secrets,
):
auth_header = generate_auth_header(scopes=[CONNECTION_CREATE_OR_UPDATE])
url = f"{V1_URL_PREFIX}{CONNECTIONS}/{saas_example_connection_config.key}/secret"
url = (
f"{V1_URL_PREFIX}{CONNECTIONS}/{saas_example_connection_config.key}/secret"
)
payload = saas_example_secrets

resp = api_client.put(
Expand Down
4 changes: 3 additions & 1 deletion tests/api/v1/endpoints/test_dataset_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,9 @@ def test_patch_datasets_missing_saas_config(
generate_auth_header,
):
path = V1_URL_PREFIX + DATASETS
path_params = {"connection_key": saas_example_connection_config_without_saas_config.key}
path_params = {
"connection_key": saas_example_connection_config_without_saas_config.key
}
datasets_url = path.format(**path_params)

auth_header = generate_auth_header(scopes=[DATASET_CREATE_OR_UPDATE])
Expand Down
72 changes: 43 additions & 29 deletions tests/api/v1/endpoints/test_policy_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
ActionType,
Policy,
Rule,
RuleTarget, DrpAction,
RuleTarget,
DrpAction,
)
from fidesops.service.masking.strategy.masking_strategy_nullify import NULL_REWRITE
from fidesops.util.data_category import DataCategory, generate_fides_data_categories
Expand Down Expand Up @@ -143,7 +144,7 @@ def test_get_invalid_policy(
assert resp.status_code == 404

def test_get_policy_returns_drp_action(
self, api_client: TestClient, generate_auth_header, policy_drp_action, url
self, api_client: TestClient, generate_auth_header, policy_drp_action, url
):
auth_header = generate_auth_header(scopes=[scopes.POLICY_READ])
resp = api_client.get(
Expand Down Expand Up @@ -325,18 +326,18 @@ def test_create_policy_with_duplicate_key(
assert len(data["failed"]) == 2

def test_create_policy_with_duplicate_drp_action(
self,
url,
api_client: TestClient,
generate_auth_header,
policy_drp_action,
storage_config,
self,
url,
api_client: TestClient,
generate_auth_header,
policy_drp_action,
storage_config,
):
data = [
{
"name": "policy with pre-existing drp action",
"action_type": ActionType.access.value,
"drp_action": DrpAction.access.value
"drp_action": DrpAction.access.value,
}
]
auth_header = generate_auth_header(scopes=[scopes.POLICY_CREATE_OR_UPDATE])
Expand All @@ -347,21 +348,21 @@ def test_create_policy_with_duplicate_drp_action(
assert len(data["failed"]) == 1

def test_update_policy_with_duplicate_drp_action(
self,
db,
url,
api_client: TestClient,
generate_auth_header,
policy_drp_action,
storage_config,
self,
db,
url,
api_client: TestClient,
generate_auth_header,
policy_drp_action,
storage_config,
):
# creates a new drp policy
data = [
{
"key": "erasure_drp_policy",
"name": "erasure drp policy",
"action_type": ActionType.erasure.value,
"drp_action": DrpAction.deletion.value
"drp_action": DrpAction.deletion.value,
}
]
auth_header = generate_auth_header(scopes=[scopes.POLICY_CREATE_OR_UPDATE])
Expand All @@ -375,7 +376,7 @@ def test_update_policy_with_duplicate_drp_action(
"key": "erasure_drp_policy",
"name": "policy with pre-existing drp action",
"action_type": ActionType.access.value,
"drp_action": DrpAction.access.value
"drp_action": DrpAction.access.value,
}
]
auth_header = generate_auth_header(scopes=[scopes.POLICY_CREATE_OR_UPDATE])
Expand All @@ -391,19 +392,19 @@ def test_update_policy_with_duplicate_drp_action(
pol.delete(db=db)

def test_update_policy_with_drp_action(
self,
url,
api_client: TestClient,
generate_auth_header,
policy,
storage_config,
self,
url,
api_client: TestClient,
generate_auth_header,
policy,
storage_config,
):
data = [
{
"key": policy.key,
"name": "updated name",
"action_type": ActionType.access.value,
"drp_action": DrpAction.access.value
"drp_action": DrpAction.access.value,
}
]
auth_header = generate_auth_header(scopes=[scopes.POLICY_CREATE_OR_UPDATE])
Expand All @@ -413,7 +414,7 @@ def test_update_policy_with_drp_action(
assert len(response_data) == 1

def test_create_policy_invalid_drp_action(
self, url, api_client: TestClient, payload, generate_auth_header, storage_config
self, url, api_client: TestClient, payload, generate_auth_header, storage_config
):
payload = [
{
Expand All @@ -429,10 +430,19 @@ def test_create_policy_invalid_drp_action(
assert resp.status_code == 422

response_body = json.loads(resp.text)
assert "value is not a valid enumeration member; permitted: 'access', 'deletion', 'sale:opt_out', 'sale:opt_in', 'access:categories', 'access:specific'" == response_body["detail"][0]["msg"]
assert (
"value is not a valid enumeration member; permitted: 'access', 'deletion', 'sale:opt_out', 'sale:opt_in', 'access:categories', 'access:specific'"
== response_body["detail"][0]["msg"]
)

def test_create_policy_with_drp_action(
self, db, url, api_client: TestClient, payload, generate_auth_header, storage_config
self,
db,
url,
api_client: TestClient,
payload,
generate_auth_header,
storage_config,
):
payload = [
{
Expand Down Expand Up @@ -588,7 +598,11 @@ def test_create_rules_invalid_policy(
assert resp.status_code == 404

def test_create_rules_mismatching_drp_policy(
self, api_client: TestClient, generate_auth_header, policy_drp_action, storage_config
self,
api_client: TestClient,
generate_auth_header,
policy_drp_action,
storage_config,
):
data = [
{
Expand Down
23 changes: 14 additions & 9 deletions tests/api/v1/endpoints/test_privacy_request_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1383,16 +1383,18 @@ def test_deny_privacy_request_without_denial_reason(
assert response_body["succeeded"][0]["id"] == privacy_request.id
assert response_body["succeeded"][0]["reviewed_at"] is not None
assert response_body["succeeded"][0]["reviewed_by"] == user.id
denial_audit_log: AuditLog = AuditLog.filter(db=db, conditions=(
(AuditLog.privacy_request_id == privacy_request.id) &
(AuditLog.user_id == user.id)
)).first()
denial_audit_log: AuditLog = AuditLog.filter(
db=db,
conditions=(
(AuditLog.privacy_request_id == privacy_request.id)
& (AuditLog.user_id == user.id)
),
).first()

assert denial_audit_log.message is None

assert not submit_mock.called # Shouldn't run! Privacy request was denied


@mock.patch(
"fidesops.service.privacy_request.request_runner_service.PrivacyRequestRunner.submit"
)
Expand Down Expand Up @@ -1427,10 +1429,13 @@ def test_deny_privacy_request_with_denial_reason(
assert response_body["succeeded"][0]["id"] == privacy_request.id
assert response_body["succeeded"][0]["reviewed_at"] is not None
assert response_body["succeeded"][0]["reviewed_by"] == user.id
denial_audit_log: AuditLog = AuditLog.filter(db=db, conditions=(
(AuditLog.privacy_request_id == privacy_request.id) &
(AuditLog.user_id == user.id)
)).first()
denial_audit_log: AuditLog = AuditLog.filter(
db=db,
conditions=(
(AuditLog.privacy_request_id == privacy_request.id)
& (AuditLog.user_id == user.id)
),
).first()

assert denial_audit_log.message == denial_reason

Expand Down
31 changes: 21 additions & 10 deletions tests/api/v1/endpoints/test_saas_config_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ def test_put_validate_saas_config_reference_and_identity(
) -> None:
auth_header = generate_auth_header(scopes=[SAAS_CONFIG_READ])
saas_config = saas_example_config
param_values = saas_config["endpoints"][0]["requests"]["read"][
"param_values"
][0]
param_values = saas_config["endpoints"][0]["requests"]["read"]["param_values"][
0
]
param_values["identity"] = "email"
param_values["references"] = [
{
Expand All @@ -111,7 +111,10 @@ def test_put_validate_saas_config_reference_and_identity(
)
assert response.status_code == 422
details = json.loads(response.text)["detail"]
assert details[0]["msg"] == "Must have exactly one of 'identity', 'references', or 'connector_param'"
assert (
details[0]["msg"]
== "Must have exactly one of 'identity', 'references', or 'connector_param'"
)

def test_put_validate_saas_config_wrong_reference_direction(
self,
Expand All @@ -122,9 +125,9 @@ def test_put_validate_saas_config_wrong_reference_direction(
) -> None:
auth_header = generate_auth_header(scopes=[SAAS_CONFIG_READ])
saas_config = saas_example_config
param_values = saas_config["endpoints"][0]["requests"]["read"][
"param_values"
][0]
param_values = saas_config["endpoints"][0]["requests"]["read"]["param_values"][
0
]
param_values["references"] = [
{
"dataset": "postgres_example_test_dataset",
Expand All @@ -137,7 +140,11 @@ def test_put_validate_saas_config_wrong_reference_direction(
)
assert response.status_code == 422
details = json.loads(response.text)["detail"]
assert details[0]["msg"] == "References can only have a direction of 'from', found 'to'"
assert (
details[0]["msg"]
== "References can only have a direction of 'from', found 'to'"
)


@pytest.mark.unit_saas
class TestPutSaaSConfig:
Expand Down Expand Up @@ -190,7 +197,9 @@ def test_patch_saas_config_create(
generate_auth_header,
) -> None:
path = V1_URL_PREFIX + SAAS_CONFIG
path_params = {"connection_key": saas_example_connection_config_without_saas_config.key}
path_params = {
"connection_key": saas_example_connection_config_without_saas_config.key
}
saas_config_url = path.format(**path_params)

auth_header = generate_auth_header(scopes=[SAAS_CONFIG_CREATE_OR_UPDATE])
Expand All @@ -200,7 +209,9 @@ def test_patch_saas_config_create(
assert response.status_code == 200

updated_config = ConnectionConfig.get_by(
db=db, field="key", value=saas_example_connection_config_without_saas_config.key
db=db,
field="key",
value=saas_example_connection_config_without_saas_config.key,
)
db.expire(updated_config)
saas_config = updated_config.saas_config
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/application_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,9 @@ def policy(

@pytest.fixture(scope="function")
def policy_drp_action(
db: Session,
oauth_client: ClientDetail,
storage_config: StorageConfig,
db: Session,
oauth_client: ClientDetail,
storage_config: StorageConfig,
) -> Generator:
access_request_policy = Policy.create(
db=db,
Expand Down
Loading

0 comments on commit 23dbb25

Please sign in to comment.