import pytest from flask import url_for from pytest_lazyfixture import lazy_fixture from atlas_um.helpers.either import Left, Right from atlas_um.helpers.common import remap_dict class TestUSMGraph: def test_failure_authentication(self, app): with app.test_client() as client: resp = client.get(url_for("usm_graph.search_users")) assert resp.status_code == 302 @pytest.mark.parametrize( "authorized_user", [ lazy_fixture("authorized_admin"), lazy_fixture("authorized_manager"), ], ) def test_search_users_success(self, app, faker, mocker, authorized_user): search_term = faker.pystr( min_chars=app.config.get("USM_GRAPH_MIN_SEARCH_TERM_LENGTH") ) mock_search_users = mocker.patch( "atlas_um.usm.client.USMClient.search_users" ) mocked_data = [ { "mail": faker.email(), "displayName": faker.name(), "jobTitle": faker.pystr(), "location": faker.pystr(), "preferredUsername": faker.pystr(), } ] * faker.pyint(max_value=10) mock_search_users.return_value = Right(mocked_data) expected_data = [ remap_dict( r, { "mail": "email", "displayName": "name", "country": "location", "jobTitle": "job_title", }, ) for r in mocked_data ] expected_response = {"results": expected_data} with app.test_client() as client: resp = client.get( url_for("usm_graph.search_users", search_term=search_term) ) assert mock_search_users.called assert resp.status_code == 200 assert resp.json == expected_response @pytest.mark.parametrize( "authorized_user", [ lazy_fixture("authorized_admin"), lazy_fixture("authorized_manager"), ], ) def test_search_users_failure_no_search_term( self, app, faker, mocker, authorized_user, ): mock_search_users = mocker.patch( "atlas_um.usm.client.USMClient.search_users" ) with app.test_client() as client: resp = client.get(url_for("usm_graph.search_users")) assert not mock_search_users.called assert resp.status_code == 400 assert b"search_term param is required" in resp.data @pytest.mark.parametrize( "authorized_user", [ lazy_fixture("authorized_admin"), lazy_fixture("authorized_manager"), ], ) def test_search_users_failure_short_search_term( self, app, faker, mocker, authorized_user, ): min_length = app.config.get("USM_GRAPH_MIN_SEARCH_TERM_LENGTH") search_term = faker.pystr(min_chars=1, max_chars=min_length - 1) mock_search_users = mocker.patch( "atlas_um.usm.client.USMClient.search_users" ) with app.test_client() as client: resp = client.get( url_for("usm_graph.search_users", search_term=search_term) ) assert not mock_search_users.called assert resp.status_code == 400 assert ( f"At least {min_length} letters required for search_term" in resp.data.decode() ) @pytest.mark.parametrize( "authorized_user", [ lazy_fixture("authorized_admin"), lazy_fixture("authorized_manager"), ], ) def test_search_users_failure_usm_error( self, app, faker, mocker, authorized_user, ): search_term = faker.pystr( min_chars=app.config.get("USM_GRAPH_MIN_SEARCH_TERM_LENGTH") ) mock_search_users = mocker.patch( "atlas_um.usm.client.USMClient.search_users" ) expected_data = "Some USM error" mock_search_users.return_value = Left(expected_data) with app.test_client() as client: resp = client.get( url_for("usm_graph.search_users", search_term=search_term) ) assert mock_search_users.called assert resp.status_code == 503 @pytest.mark.parametrize( "authorized_user", [ lazy_fixture("authorized_admin"), lazy_fixture("authorized_manager"), ], ) def test_search_users_failure_rate_limit( self, app, faker, mocker, exceed_rate_limit, authorized_user, ): search_term = faker.pystr( min_chars=app.config.get("USM_GRAPH_MIN_SEARCH_TERM_LENGTH") ) mock_search_users = mocker.patch( "atlas_um.usm.client.USMClient.search_users" ) with app.test_client() as client: resp = client.get( url_for("usm_graph.search_users", search_term=search_term) ) assert not mock_search_users.called assert resp.status_code == 429