import os import tempfile from pathlib import Path from unittest.mock import patch from django.core.management import CommandError from django.test import SimpleTestCase from pingcap_sso.management.commands.upgrade_account_frontend import Command class UpgradeAccountFrontendTest(SimpleTestCase): def test_normalize_nested_static_assets_dir(self): with tempfile.TemporaryDirectory() as temporary_dir: assets_dir = Path(temporary_dir) / 'dist' nested_static_dir = assets_dir / 'accounts' / 'static' nested_static_dir.mkdir(parents=True) (nested_static_dir / 'asset.js').write_text('asset') command = Command() command.ASSETS_DIR = str(assets_dir) command.NESTED_STATIC_DIR = str(nested_static_dir) command.STATIC_DIR = str(assets_dir / 'static') command._normalize_static_assets_dir() self.assertTrue((assets_dir / 'static' / 'asset.js').exists()) self.assertFalse(nested_static_dir.exists()) def test_canonical_static_assets_dir_is_unchanged(self): with tempfile.TemporaryDirectory() as temporary_dir: assets_dir = Path(temporary_dir) / 'dist' static_dir = assets_dir / 'static' static_dir.mkdir(parents=True) (static_dir / 'asset.js').write_text('asset') command = Command() command.ASSETS_DIR = str(assets_dir) command.NESTED_STATIC_DIR = str(assets_dir / 'accounts' / 'static') command.STATIC_DIR = str(static_dir) command._normalize_static_assets_dir() self.assertTrue((static_dir / 'asset.js').exists()) def test_github_token_can_be_read_from_environment(self): command = Command() with patch.dict(os.environ, {command.GITHUB_TOKEN_ENV: 'token'}): self.assertEqual(command._get_github_token(), 'token') def test_missing_github_token_is_rejected(self): command = Command() with patch.dict(os.environ, {command.GITHUB_TOKEN_ENV: ''}): with self.assertRaises(CommandError): command._get_github_token()