def test_query_moira_lists_error(mock_moira_client): """ Test that a Moira exception is raised if moira client call fails with anything other than a java NPE """ mock_moira_client.return_value.user_list_membership.side_effect = Fault( "Not a java NPE") with pytest.raises(MoiraException): query_moira_lists(factories.UserFactory())
def test_query_moira_lists_no_lists(mock_moira_client): """ Test that an empty list is returned if Moira throws a java NPE """ mock_moira_client.return_value.user_list_membership.side_effect = Fault( "java.lang.NullPointerException") other_user = factories.UserFactory(email="*****@*****.**") assert query_moira_lists(other_user) == []
def test_list_members_exception(mock_moira_client): """ Test that a Moira exception is raised if moira client call fails with anything other than a java NPE """ mock_moira_client.return_value.list_members.side_effect = Exception( "exception") with pytest.raises(MoiraException): list_members(factories.UserFactory())
def request_data_su(): """ Fixture for tests requiring a superuser, request, and view """ user = factories.UserFactory(is_superuser=True) request = Mock(method="GET", user=user) view = Mock(kwargs={"user": user.username}) return SimpleNamespace(user=user, request=request, view=view)
def test_has_common_lists(mocker): """ Test that has_common_lists returns the correct boolean value """ mock_user_moira_lists = mocker.patch("ui.utils.user_moira_lists") mock_user_moira_lists.return_value = set(["a", "b"]) user = factories.UserFactory() assert has_common_lists(user, ["b", "c"]) is True assert has_common_lists(user, ["c"]) is False
def alt_moira_data(): """ Fixture for tests requiring a moira list, collection, and user """ alt_moira_list = MoiraListFactory() alt_collection = CollectionFactory() alt_user = factories.UserFactory() return SimpleNamespace(moira_list=alt_moira_list, collection=alt_collection, user=alt_user)
def test_query_moira_lists(mock_moira_client): """ Test that expected lists are returned. """ list_names = ["test_moira_list01", "test_moira_list02"] mock_moira_client.return_value.user_list_membership.return_value = [{ "listName": list_name } for list_name in list_names] other_user = factories.UserFactory(email="*****@*****.**") assert query_moira_lists(other_user) == list_names
def test_user_moira_lists_cache_hit(mocker): """ Test that returns from cache if cache has lists. """ mock_cache = mocker.patch("ui.utils.cache") mock_query_moira_lists = mocker.patch("ui.utils.query_moira_lists") cached_list_names = set(["some_list"]) mock_cache.get.return_value = cached_list_names result = user_moira_lists(factories.UserFactory()) assert result == cached_list_names assert not mock_query_moira_lists.called
def test_user_moira_lists_cache_miss(mocker, settings): """ Test that queries and caches lists if not already in cache. """ mock_cache = mocker.patch("ui.utils.cache") mock_query_moira_lists = mocker.patch("ui.utils.query_moira_lists") mock_cache.get.return_value = None user = factories.UserFactory() assert not mock_query_moira_lists.called assert not mock_cache.set.called result = user_moira_lists(user) expected_result = set(mock_query_moira_lists.return_value) assert result == expected_result assert mock_query_moira_lists.called_once_with(user) assert mock_cache.set.called_once_with( MOIRA_CACHE_KEY.format(user_id=user.id), expected_result, settings.MOIRA_CACHE_TIMEOUT, )