Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions tests/integration/failure_handling_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Live tests that the planners' failures are noticed.

The planners report failures on a queue instead of raising, so a scan that
failed can look like one that passed. These tests make a scan fail on purpose
and check that the failure is reported and nothing is written.

Run with:
python3 -m unittest tests.integration.failure_handling_test -v
"""

import os
import shutil
import tempfile
import unittest

from tests.integration import harness

_WRONG_SECRET = "not-the-real-client-secret"


def _files_case(shallow_only: bool = False):
"""Returns the first Files case in the scan inputs file, or skips."""
for name in ("sharepoint_site", "onedrive_user"):
try:
case = harness.case_or_skip("files", name)
except unittest.SkipTest:
continue
if not shallow_only or case.get("shallow_scan", True):
return case
raise unittest.SkipTest("No suitable 'files' case in the scan inputs file.")


class WrongClientSecretTest(unittest.TestCase):
"""A scan with a wrong client secret reports an error and writes nothing."""

def _assert_reported_as_error(self, run):
self.assertNotEqual(run.errors, [], "The scan did not report the failed sign-in.")
self.assertIsNone(run.result, "The scan reported completion despite the failed sign-in.")
self.assertIsNone(run.output_dir, "The scan wrote a report despite the failed sign-in.")
self.assertNotIn(_WRONG_SECRET, " ".join(run.errors), "The error message contains the client secret.")

def test_exchange(self):
case = harness.case_or_skip("exchange")
self._assert_reported_as_error(harness.run_planner("exchange", case, client_secret=_WRONG_SECRET))

def test_chat(self):
case = harness.case_or_skip("chat")
self._assert_reported_as_error(harness.run_planner("chat", case, client_secret=_WRONG_SECRET))

def test_files(self):
run = harness.run_planner("files", _files_case(), {"shallow_scan": False}, client_secret=_WRONG_SECRET)
self._assert_reported_as_error(run)


class CertificateTest(unittest.TestCase):
"""A shallow scan without a registered certificate is stopped before it scans.

Each test gives the planner an empty folder to keep its certificate in, so
the registered certificate is never touched.
"""

def setUp(self):
self.case = _files_case(shallow_only=True)
self.certificate_root = tempfile.mkdtemp(prefix="mp_it_certificate_")
self.addCleanup(shutil.rmtree, self.certificate_root, ignore_errors=True)

def _assert_stopped(self, run):
self.assertIsNotNone(run.certificate_check, "The certificate check did not run.")
self.assertFalse(run.certificate_check["ok"], "An unregistered certificate was accepted.")
self.assertIsNone(run.result, "The scan ran without a registered certificate.")
self.assertIsNone(run.output_dir, "The scan wrote a report without a registered certificate.")

def test_unregistered_certificate_stops_the_scan(self):
# With no certificate files the planner creates a new certificate, which
# the app registration does not know. Microsoft must reject it.
run = harness.run_planner(
"files", self.case, {"shallow_scan": True}, certificate_root=self.certificate_root
)
self._assert_stopped(run)

def test_missing_certificate_is_not_replaced_when_one_is_required(self):
run = harness.run_planner(
"files", self.case, {"shallow_scan": True},
certificate_root=self.certificate_root, require_certificate=True,
)
self._assert_stopped(run)
self.assertIn("missing", run.certificate_check["detail"])
self.assertEqual(os.listdir(self.certificate_root), [], "A new certificate was created.")


if __name__ == "__main__":
unittest.main()
Loading