Skip to content

Commit b63db11

Browse files
authored
[Cosmos] User agent string validation tests (Azure#35733)
* Create test_user_agent_suffix.py * Update test_user_agent_suffix.py * add async * handling for unicode * Update test_user_agent_suffix_async.py
1 parent 1550fec commit b63db11

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# The MIT License (MIT)
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
4+
import unittest
5+
import test_config
6+
import pytest
7+
from azure.cosmos import CosmosClient
8+
9+
10+
@pytest.mark.cosmosEmulator
11+
class TestUserAgentSuffix(unittest.TestCase):
12+
"""Python User Agent Suffix Tests.
13+
"""
14+
15+
client: CosmosClient = None
16+
host = test_config.TestConfig.host
17+
masterKey = test_config.TestConfig.masterKey
18+
connectionPolicy = test_config.TestConfig.connectionPolicy
19+
20+
@classmethod
21+
def setUpClass(cls):
22+
if (cls.masterKey == '[YOUR_KEY_HERE]' or
23+
cls.host == '[YOUR_ENDPOINT_HERE]'):
24+
raise Exception(
25+
"You must specify your Azure Cosmos account values for "
26+
"'masterKey' and 'host' at the top of this class to run the "
27+
"tests.")
28+
29+
def test_user_agent_suffix_no_special_character(self):
30+
user_agent_suffix = "TestUserAgent"
31+
self.client = CosmosClient(self.host, self.masterKey, user_agent=user_agent_suffix)
32+
self.created_database = self.client.get_database_client(test_config.TestConfig.TEST_DATABASE_ID)
33+
34+
read_result = self.created_database.read()
35+
assert read_result['id'] == self.created_database.id
36+
37+
def test_user_agent_suffix_special_character(self):
38+
user_agent_suffix = "TéstUserAgent's" # cspell:disable-line
39+
self.client = CosmosClient(self.host, self.masterKey, user_agent=user_agent_suffix)
40+
self.created_database = self.client.get_database_client(test_config.TestConfig.TEST_DATABASE_ID)
41+
42+
read_result = self.created_database.read()
43+
assert read_result['id'] == self.created_database.id
44+
45+
def test_user_agent_suffix_unicode_character(self):
46+
user_agent_suffix = "UnicodeChar鱀InUserAgent"
47+
try:
48+
self.client = CosmosClient(self.host, self.masterKey, user_agent=user_agent_suffix)
49+
self.created_database = self.client.get_database_client(test_config.TestConfig.TEST_DATABASE_ID)
50+
self.created_database.read()
51+
pytest.fail("Unicode characters should not be allowed.")
52+
except UnicodeEncodeError as e:
53+
assert "ordinal not in range(256)" in e.reason
54+
55+
def test_user_agent_suffix_space_character(self):
56+
user_agent_suffix = "UserAgent with space$%_^()*&"
57+
self.client = CosmosClient(self.host, self.masterKey, user_agent=user_agent_suffix)
58+
self.created_database = self.client.get_database_client(test_config.TestConfig.TEST_DATABASE_ID)
59+
60+
read_result = self.created_database.read()
61+
assert read_result['id'] == self.created_database.id
62+
63+
64+
if __name__ == '__main__':
65+
unittest.main()
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# The MIT License (MIT)
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
4+
import unittest
5+
import pytest
6+
import test_config
7+
from azure.cosmos.aio import CosmosClient
8+
9+
10+
@pytest.mark.cosmosEmulator
11+
class TestUserAgentSuffixAsync(unittest.IsolatedAsyncioTestCase):
12+
"""Python User Agent Suffix Tests.
13+
"""
14+
15+
configs = test_config.TestConfig
16+
host = configs.host
17+
masterKey = configs.masterKey
18+
TEST_DATABASE_ID = configs.TEST_DATABASE_ID
19+
20+
@classmethod
21+
def setUpClass(cls):
22+
if (cls.masterKey == '[YOUR_KEY_HERE]' or
23+
cls.host == '[YOUR_ENDPOINT_HERE]'):
24+
raise Exception(
25+
"You must specify your Azure Cosmos account values for "
26+
"'masterKey' and 'host' at the top of this class to run the "
27+
"tests.")
28+
29+
async def test_user_agent_suffix_no_special_character_async(self):
30+
user_agent_suffix = "TestUserAgent"
31+
self.client = CosmosClient(self.host, self.masterKey, user_agent=user_agent_suffix)
32+
self.created_database = self.client.get_database_client(test_config.TestConfig.TEST_DATABASE_ID)
33+
34+
read_result = await self.created_database.read()
35+
assert read_result['id'] == self.created_database.id
36+
await self.client.close()
37+
38+
async def test_user_agent_suffix_special_character_async(self):
39+
user_agent_suffix = "TéstUserAgent's" # cspell:disable-line
40+
self.client = CosmosClient(self.host, self.masterKey, user_agent=user_agent_suffix)
41+
self.created_database = self.client.get_database_client(test_config.TestConfig.TEST_DATABASE_ID)
42+
43+
read_result = await self.created_database.read()
44+
assert read_result['id'] == self.created_database.id
45+
await self.client.close()
46+
47+
async def test_user_agent_suffix_unicode_character_async(self):
48+
user_agent_suffix = "UnicodeChar鱀InUserAgent"
49+
self.client = CosmosClient(self.host, self.masterKey, user_agent=user_agent_suffix)
50+
self.created_database = self.client.get_database_client(test_config.TestConfig.TEST_DATABASE_ID)
51+
52+
read_result = await self.created_database.read()
53+
assert read_result['id'] == self.created_database.id
54+
await self.client.close()
55+
56+
async def test_user_agent_suffix_space_character_async(self):
57+
user_agent_suffix = "UserAgent with space$%_^()*&"
58+
self.client = CosmosClient(self.host, self.masterKey, user_agent=user_agent_suffix)
59+
self.created_database = self.client.get_database_client(test_config.TestConfig.TEST_DATABASE_ID)
60+
61+
read_result = await self.created_database.read()
62+
assert read_result['id'] == self.created_database.id
63+
await self.client.close()
64+
65+
66+
if __name__ == "__main__":
67+
unittest.main()

0 commit comments

Comments
 (0)