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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import unittest
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
import json
|
| 5 |
+
from unittest.mock import patch, MagicMock
|
| 6 |
+
|
| 7 |
+
# Add parent directory to path for imports
|
| 8 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 9 |
+
|
| 10 |
+
# Import modules to test
|
| 11 |
+
from model_config import ModelConfigManager
|
| 12 |
+
|
| 13 |
+
class MockDB:
|
| 14 |
+
"""Mock database for testing."""
|
| 15 |
+
|
| 16 |
+
def __init__(self):
|
| 17 |
+
self.configs = {}
|
| 18 |
+
|
| 19 |
+
def get_config(self, config_id):
|
| 20 |
+
return self.configs.get(config_id)
|
| 21 |
+
|
| 22 |
+
def add_config(self, config_id, config):
|
| 23 |
+
self.configs[config_id] = config
|
| 24 |
+
return config_id
|
| 25 |
+
|
| 26 |
+
class TestModelConfigManager(unittest.TestCase):
|
| 27 |
+
"""Test the ModelConfigManager class."""
|
| 28 |
+
|
| 29 |
+
def setUp(self):
|
| 30 |
+
"""Set up test environment."""
|
| 31 |
+
self.db = MockDB()
|
| 32 |
+
self.config_dir = "test_model_configs"
|
| 33 |
+
|
| 34 |
+
# Create test directory
|
| 35 |
+
os.makedirs(self.config_dir, exist_ok=True)
|
| 36 |
+
|
| 37 |
+
# Create a test instance with the test directory
|
| 38 |
+
self.manager = ModelConfigManager(self.db)
|
| 39 |
+
self.manager.config_dir = self.config_dir
|
| 40 |
+
|
| 41 |
+
def tearDown(self):
|
| 42 |
+
"""Clean up after tests."""
|
| 43 |
+
# Remove test files
|
| 44 |
+
for filename in os.listdir(self.config_dir):
|
| 45 |
+
file_path = os.path.join(self.config_dir, filename)
|
| 46 |
+
if os.path.isfile(file_path):
|
| 47 |
+
os.unlink(file_path)
|
| 48 |
+
|
| 49 |
+
# Remove test directory
|
| 50 |
+
os.rmdir(self.config_dir)
|
| 51 |
+
|
| 52 |
+
def test_initialize_default_configs(self):
|
| 53 |
+
"""Test _initialize_default_configs method."""
|
| 54 |
+
# Check that default configs are created
|
| 55 |
+
self.manager._initialize_default_configs()
|
| 56 |
+
|
| 57 |
+
# Verify files exist
|
| 58 |
+
for model_type in self.manager.default_configs:
|
| 59 |
+
config_path = os.path.join(self.config_dir, f"{model_type}.json")
|
| 60 |
+
self.assertTrue(os.path.exists(config_path))
|
| 61 |
+
|
| 62 |
+
# Verify content
|
| 63 |
+
with open(config_path, "r") as f:
|
| 64 |
+
config = json.load(f)
|
| 65 |
+
|
| 66 |
+
self.assertEqual(config["name"], self.manager.default_configs[model_type]["name"])
|
| 67 |
+
self.assertEqual(config["description"], self.manager.default_configs[model_type]["description"])
|
| 68 |
+
self.assertEqual(config["parameters"], self.manager.default_configs[model_type]["parameters"])
|
| 69 |
+
|
| 70 |
+
def test_get_available_configs(self):
|
| 71 |
+
"""Test get_available_configs method."""
|
| 72 |
+
# Create test configs
|
| 73 |
+
test_configs = {
|
| 74 |
+
"test1": {
|
| 75 |
+
"name": "Test Config 1",
|
| 76 |
+
"description": "Test description 1",
|
| 77 |
+
"parameters": {"temperature": 0.7}
|
| 78 |
+
},
|
| 79 |
+
"test2": {
|
| 80 |
+
"name": "Test Config 2",
|
| 81 |
+
"description": "Test description 2",
|
| 82 |
+
"parameters": {"temperature": 0.8}
|
| 83 |
+
}
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
for config_id, config in test_configs.items():
|
| 87 |
+
config_path = os.path.join(self.config_dir, f"{config_id}.json")
|
| 88 |
+
with open(config_path, "w") as f:
|
| 89 |
+
json.dump(config, f)
|
| 90 |
+
|
| 91 |
+
# Get configs
|
| 92 |
+
configs = self.manager.get_available_configs()
|
| 93 |
+
|
| 94 |
+
# Verify results
|
| 95 |
+
self.assertEqual(len(configs), 2)
|
| 96 |
+
|
| 97 |
+
# Check that IDs are added
|
| 98 |
+
config_ids = [config["id"] for config in configs]
|
| 99 |
+
self.assertIn("test1", config_ids)
|
| 100 |
+
self.assertIn("test2", config_ids)
|
| 101 |
+
|
| 102 |
+
# Check content
|
| 103 |
+
for config in configs:
|
| 104 |
+
test_config = test_configs[config["id"]]
|
| 105 |
+
self.assertEqual(config["name"], test_config["name"])
|
| 106 |
+
self.assertEqual(config["description"], test_config["description"])
|
| 107 |
+
self.assertEqual(config["parameters"], test_config["parameters"])
|
| 108 |
+
|
| 109 |
+
def test_get_config(self):
|
| 110 |
+
"""Test get_config method."""
|
| 111 |
+
# Create test config
|
| 112 |
+
config = {
|
| 113 |
+
"name": "Test Config",
|
| 114 |
+
"description": "Test description",
|
| 115 |
+
"parameters": {"temperature": 0.7}
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
config_path = os.path.join(self.config_dir, "test.json")
|
| 119 |
+
with open(config_path, "w") as f:
|
| 120 |
+
json.dump(config, f)
|
| 121 |
+
|
| 122 |
+
# Get config
|
| 123 |
+
result = self.manager.get_config("test")
|
| 124 |
+
|
| 125 |
+
# Verify result
|
| 126 |
+
self.assertIsNotNone(result)
|
| 127 |
+
self.assertEqual(result["id"], "test")
|
| 128 |
+
self.assertEqual(result["name"], config["name"])
|
| 129 |
+
self.assertEqual(result["description"], config["description"])
|
| 130 |
+
self.assertEqual(result["parameters"], config["parameters"])
|
| 131 |
+
|
| 132 |
+
# Test non-existent config
|
| 133 |
+
result = self.manager.get_config("nonexistent")
|
| 134 |
+
self.assertIsNone(result)
|
| 135 |
+
|
| 136 |
+
def test_add_config(self):
|
| 137 |
+
"""Test add_config method."""
|
| 138 |
+
# Add config
|
| 139 |
+
name = "Test Config"
|
| 140 |
+
description = "Test description"
|
| 141 |
+
parameters = {"temperature": 0.7, "top_k": 50}
|
| 142 |
+
|
| 143 |
+
config_id = self.manager.add_config(name, description, parameters)
|
| 144 |
+
|
| 145 |
+
# Verify result
|
| 146 |
+
self.assertIsNotNone(config_id)
|
| 147 |
+
self.assertEqual(config_id, "test_config")
|
| 148 |
+
|
| 149 |
+
# Verify file exists
|
| 150 |
+
config_path = os.path.join(self.config_dir, f"{config_id}.json")
|
| 151 |
+
self.assertTrue(os.path.exists(config_path))
|
| 152 |
+
|
| 153 |
+
# Verify content
|
| 154 |
+
with open(config_path, "r") as f:
|
| 155 |
+
config = json.load(f)
|
| 156 |
+
|
| 157 |
+
self.assertEqual(config["name"], name)
|
| 158 |
+
self.assertEqual(config["description"], description)
|
| 159 |
+
self.assertEqual(config["parameters"], parameters)
|
| 160 |
+
|
| 161 |
+
def test_update_config(self):
|
| 162 |
+
"""Test update_config method."""
|
| 163 |
+
# Create test config
|
| 164 |
+
config = {
|
| 165 |
+
"name": "Test Config",
|
| 166 |
+
"description": "Test description",
|
| 167 |
+
"parameters": {"temperature": 0.7}
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
config_path = os.path.join(self.config_dir, "test.json")
|
| 171 |
+
with open(config_path, "w") as f:
|
| 172 |
+
json.dump(config, f)
|
| 173 |
+
|
| 174 |
+
# Update config
|
| 175 |
+
new_name = "Updated Config"
|
| 176 |
+
new_description = "Updated description"
|
| 177 |
+
new_parameters = {"temperature": 0.8, "top_k": 60}
|
| 178 |
+
|
| 179 |
+
success = self.manager.update_config("test", new_name, new_description, new_parameters)
|
| 180 |
+
|
| 181 |
+
# Verify result
|
| 182 |
+
self.assertTrue(success)
|
| 183 |
+
|
| 184 |
+
# Verify content
|
| 185 |
+
with open(config_path, "r") as f:
|
| 186 |
+
updated_config = json.load(f)
|
| 187 |
+
|
| 188 |
+
self.assertEqual(updated_config["name"], new_name)
|
| 189 |
+
self.assertEqual(updated_config["description"], new_description)
|
| 190 |
+
self.assertEqual(updated_config["parameters"], new_parameters)
|
| 191 |
+
|
| 192 |
+
# Test updating non-existent config
|
| 193 |
+
success = self.manager.update_config("nonexistent", "New Name", "New Description", {})
|
| 194 |
+
self.assertFalse(success)
|
| 195 |
+
|
| 196 |
+
def test_delete_config(self):
|
| 197 |
+
"""Test delete_config method."""
|
| 198 |
+
# Create test config
|
| 199 |
+
config = {
|
| 200 |
+
"name": "Test Config",
|
| 201 |
+
"description": "Test description",
|
| 202 |
+
"parameters": {"temperature": 0.7}
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
config_path = os.path.join(self.config_dir, "test.json")
|
| 206 |
+
with open(config_path, "w") as f:
|
| 207 |
+
json.dump(config, f)
|
| 208 |
+
|
| 209 |
+
# Delete config
|
| 210 |
+
success = self.manager.delete_config("test")
|
| 211 |
+
|
| 212 |
+
# Verify result
|
| 213 |
+
self.assertTrue(success)
|
| 214 |
+
self.assertFalse(os.path.exists(config_path))
|
| 215 |
+
|
| 216 |
+
# Test deleting non-existent config
|
| 217 |
+
success = self.manager.delete_config("nonexistent")
|
| 218 |
+
self.assertFalse(success)
|
| 219 |
+
|
| 220 |
+
# Test deleting default config
|
| 221 |
+
for model_type in self.manager.default_configs:
|
| 222 |
+
config_path = os.path.join(self.config_dir, f"{model_type}.json")
|
| 223 |
+
with open(config_path, "w") as f:
|
| 224 |
+
json.dump(self.manager.default_configs[model_type], f)
|
| 225 |
+
|
| 226 |
+
success = self.manager.delete_config(model_type)
|
| 227 |
+
self.assertFalse(success)
|
| 228 |
+
self.assertTrue(os.path.exists(config_path))
|
| 229 |
+
|
| 230 |
+
def test_apply_config_to_model_params(self):
|
| 231 |
+
"""Test apply_config_to_model_params method."""
|
| 232 |
+
# Create test config
|
| 233 |
+
config = {
|
| 234 |
+
"name": "Test Config",
|
| 235 |
+
"description": "Test description",
|
| 236 |
+
"parameters": {
|
| 237 |
+
"temperature": 0.7,
|
| 238 |
+
"top_k": 50,
|
| 239 |
+
"top_p": 0.9
|
| 240 |
+
}
|
| 241 |
+
}
|
| 242 |
+
|
| 243 |
+
config_path = os.path.join(self.config_dir, "test.json")
|
| 244 |
+
with open(config_path, "w") as f:
|
| 245 |
+
json.dump(config, f)
|
| 246 |
+
|
| 247 |
+
# Apply config
|
| 248 |
+
model_params = {
|
| 249 |
+
"temperature": 0.5,
|
| 250 |
+
"max_length": 100
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
result = self.manager.apply_config_to_model_params(model_params, "test")
|
| 254 |
+
|
| 255 |
+
# Verify result
|
| 256 |
+
self.assertEqual(result["temperature"], 0.7)
|
| 257 |
+
self.assertEqual(result["top_k"], 50)
|
| 258 |
+
self.assertEqual(result["top_p"], 0.9)
|
| 259 |
+
self.assertEqual(result["max_length"], 100)
|
| 260 |
+
|
| 261 |
+
# Test with non-existent config
|
| 262 |
+
result = self.manager.apply_config_to_model_params(model_params, "nonexistent")
|
| 263 |
+
self.assertEqual(result, model_params)
|
| 264 |
+
|
| 265 |
+
if __name__ == '__main__':
|
| 266 |
+
unittest.main()
|
test_login.py
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import unittest
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
import json
|
| 5 |
+
from unittest.mock import patch, MagicMock
|
| 6 |
+
|
| 7 |
+
# Add parent directory to path for imports
|
| 8 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 9 |
+
|
| 10 |
+
# Import modules to test
|
| 11 |
+
from auth import HuggingFaceAuth
|
| 12 |
+
|
| 13 |
+
class MockDB:
|
| 14 |
+
"""Mock database for testing."""
|
| 15 |
+
|
| 16 |
+
def __init__(self):
|
| 17 |
+
self.users = {}
|
| 18 |
+
|
| 19 |
+
def add_user(self, username, hf_user_id, is_admin=False):
|
| 20 |
+
user_id = len(self.users) + 1
|
| 21 |
+
self.users[hf_user_id] = {
|
| 22 |
+
'id': user_id,
|
| 23 |
+
'username': username,
|
| 24 |
+
'hf_user_id': hf_user_id,
|
| 25 |
+
'is_admin': is_admin
|
| 26 |
+
}
|
| 27 |
+
return user_id
|
| 28 |
+
|
| 29 |
+
def get_user(self, hf_user_id):
|
| 30 |
+
return self.users.get(hf_user_id)
|
| 31 |
+
|
| 32 |
+
def get_user_by_username(self, username):
|
| 33 |
+
for user in self.users.values():
|
| 34 |
+
if user['username'] == username:
|
| 35 |
+
return user
|
| 36 |
+
return None
|
| 37 |
+
|
| 38 |
+
def can_submit_today(self, user_id):
|
| 39 |
+
return True
|
| 40 |
+
|
| 41 |
+
def update_submission_date(self, user_id):
|
| 42 |
+
pass
|
| 43 |
+
|
| 44 |
+
class MockRequest:
|
| 45 |
+
"""Mock Gradio request for testing."""
|
| 46 |
+
|
| 47 |
+
def __init__(self, headers=None, cookies=None):
|
| 48 |
+
self.headers = headers or {}
|
| 49 |
+
self.cookies = cookies or {}
|
| 50 |
+
|
| 51 |
+
class TestHuggingFaceAuth(unittest.TestCase):
|
| 52 |
+
"""Test the HuggingFaceAuth class."""
|
| 53 |
+
|
| 54 |
+
def setUp(self):
|
| 55 |
+
"""Set up test environment."""
|
| 56 |
+
self.db = MockDB()
|
| 57 |
+
|
| 58 |
+
# Set environment variables for testing
|
| 59 |
+
os.environ['SPACE_ID'] = 'test_space'
|
| 60 |
+
os.environ['OAUTH_CLIENT_ID'] = 'test_client_id'
|
| 61 |
+
os.environ['OAUTH_CLIENT_SECRET'] = 'test_client_secret'
|
| 62 |
+
os.environ['OAUTH_SCOPES'] = 'openid profile'
|
| 63 |
+
os.environ['OPENID_PROVIDER_URL'] = 'https://huggingface.co'
|
| 64 |
+
|
| 65 |
+
self.auth = HuggingFaceAuth(self.db)
|
| 66 |
+
|
| 67 |
+
def tearDown(self):
|
| 68 |
+
"""Clean up after tests."""
|
| 69 |
+
# Remove environment variables
|
| 70 |
+
for var in ['SPACE_ID', 'OAUTH_CLIENT_ID', 'OAUTH_CLIENT_SECRET', 'OAUTH_SCOPES', 'OPENID_PROVIDER_URL']:
|
| 71 |
+
if var in os.environ:
|
| 72 |
+
del os.environ[var]
|
| 73 |
+
|
| 74 |
+
def test_init(self):
|
| 75 |
+
"""Test initialization."""
|
| 76 |
+
self.assertEqual(self.auth.admin_username, 'Quazim0t0')
|
| 77 |
+
self.assertTrue(self.auth.running_in_space)
|
| 78 |
+
self.assertEqual(self.auth.client_id, 'test_client_id')
|
| 79 |
+
self.assertEqual(self.auth.client_secret, 'test_client_secret')
|
| 80 |
+
self.assertEqual(self.auth.oauth_scopes, 'openid profile')
|
| 81 |
+
self.assertEqual(self.auth.openid_provider_url, 'https://huggingface.co')
|
| 82 |
+
|
| 83 |
+
@patch('auth.HfApi')
|
| 84 |
+
def test_login_user(self, mock_hf_api):
|
| 85 |
+
"""Test login_user method."""
|
| 86 |
+
# Mock HfApi.whoami
|
| 87 |
+
mock_instance = mock_hf_api.return_value
|
| 88 |
+
mock_instance.whoami.return_value = {
|
| 89 |
+
'id': 'test_user_id',
|
| 90 |
+
'name': 'Test User'
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
# Test successful login
|
| 94 |
+
user = self.auth.login_user('test_token')
|
| 95 |
+
self.assertIsNotNone(user)
|
| 96 |
+
self.assertEqual(user['username'], 'Test User')
|
| 97 |
+
self.assertEqual(user['hf_user_id'], 'test_user_id')
|
| 98 |
+
self.assertEqual(user['token'], 'test_token')
|
| 99 |
+
|
| 100 |
+
# Test admin login
|
| 101 |
+
mock_instance.whoami.return_value = {
|
| 102 |
+
'id': 'admin_user_id',
|
| 103 |
+
'name': 'Quazim0t0'
|
| 104 |
+
}
|
| 105 |
+
user = self.auth.login_user('admin_token')
|
| 106 |
+
self.assertIsNotNone(user)
|
| 107 |
+
self.assertEqual(user['username'], 'Quazim0t0')
|
| 108 |
+
self.assertTrue(user['is_admin'])
|
| 109 |
+
|
| 110 |
+
# Test failed login
|
| 111 |
+
mock_instance.whoami.return_value = None
|
| 112 |
+
user = self.auth.login_user('invalid_token')
|
| 113 |
+
self.assertIsNone(user)
|
| 114 |
+
|
| 115 |
+
def test_check_login_space_oauth(self):
|
| 116 |
+
"""Test check_login method with Space OAuth."""
|
| 117 |
+
# Test with HF-User header
|
| 118 |
+
request = MockRequest(headers={'HF-User': 'Test User'})
|
| 119 |
+
user = self.auth.check_login(request)
|
| 120 |
+
self.assertIsNotNone(user)
|
| 121 |
+
self.assertEqual(user['username'], 'Test User')
|
| 122 |
+
|
| 123 |
+
# Test with admin username
|
| 124 |
+
request = MockRequest(headers={'HF-User': 'Quazim0t0'})
|
| 125 |
+
user = self.auth.check_login(request)
|
| 126 |
+
self.assertIsNotNone(user)
|
| 127 |
+
self.assertEqual(user['username'], 'Quazim0t0')
|
| 128 |
+
self.assertTrue(user['is_admin'])
|
| 129 |
+
|
| 130 |
+
@patch('auth.HfApi')
|
| 131 |
+
def test_check_login_token(self, mock_hf_api):
|
| 132 |
+
"""Test check_login method with token."""
|
| 133 |
+
# Remove SPACE_ID to test token-based auth
|
| 134 |
+
del os.environ['SPACE_ID']
|
| 135 |
+
self.auth.running_in_space = False
|
| 136 |
+
|
| 137 |
+
# Mock HfApi.whoami
|
| 138 |
+
mock_instance = mock_hf_api.return_value
|
| 139 |
+
mock_instance.whoami.return_value = {
|
| 140 |
+
'id': 'test_user_id',
|
| 141 |
+
'name': 'Test User'
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
# Add user to database
|
| 145 |
+
self.db.add_user('Test User', 'test_user_id')
|
| 146 |
+
|
| 147 |
+
# Test with token in cookie
|
| 148 |
+
request = MockRequest(cookies={'hf_token': 'test_token'})
|
| 149 |
+
user = self.auth.check_login(request)
|
| 150 |
+
self.assertIsNotNone(user)
|
| 151 |
+
self.assertEqual(user['hf_user_id'], 'test_user_id')
|
| 152 |
+
|
| 153 |
+
# Test with token in header
|
| 154 |
+
request = MockRequest(headers={'HF-Token': 'test_token'})
|
| 155 |
+
user = self.auth.check_login(request)
|
| 156 |
+
self.assertIsNotNone(user)
|
| 157 |
+
self.assertEqual(user['hf_user_id'], 'test_user_id')
|
| 158 |
+
|
| 159 |
+
# Test with invalid token
|
| 160 |
+
mock_instance.whoami.return_value = None
|
| 161 |
+
request = MockRequest(cookies={'hf_token': 'invalid_token'})
|
| 162 |
+
user = self.auth.check_login(request)
|
| 163 |
+
self.assertIsNone(user)
|
| 164 |
+
|
| 165 |
+
def test_get_oauth_login_url(self):
|
| 166 |
+
"""Test get_oauth_login_url method."""
|
| 167 |
+
# Test with default redirect URI
|
| 168 |
+
url = self.auth.get_oauth_login_url()
|
| 169 |
+
self.assertIsNotNone(url)
|
| 170 |
+
self.assertIn('client_id=test_client_id', url)
|
| 171 |
+
self.assertIn('scope=openid%20profile', url)
|
| 172 |
+
self.assertIn('response_type=code', url)
|
| 173 |
+
|
| 174 |
+
# Test with custom redirect URI
|
| 175 |
+
url = self.auth.get_oauth_login_url('https://example.com/callback')
|
| 176 |
+
self.assertIsNotNone(url)
|
| 177 |
+
self.assertIn('redirect_uri=https://example.com/callback', url)
|
| 178 |
+
|
| 179 |
+
if __name__ == '__main__':
|
| 180 |
+
unittest.main()
|