Reorg code and tests

DON'T use main.py
This commit is contained in:
2024-02-24 12:02:12 +00:00
parent 662b75e224
commit 4be5d4c325
18 changed files with 271 additions and 85 deletions
+32 -30
View File
@@ -7,14 +7,6 @@ from ActiveCollabAPI.ACAuthenticator import ACAuthenticator
class TestACAuthenticator(TestCase):
def test_headers(self):
authenticator = ACAuthenticator(AC_LOGIN_BASE_URL)
headers = authenticator.headers()
self.assertIn('Content-Type', headers)
self.assertEqual('application/json; charset=utf8', headers['Content-Type'])
self.assertIn('Accept', headers)
self.assertEqual('application/json', headers['Accept'])
self.assertIn('User-Agent', headers)
def mock_login_success(self):
return {
@@ -37,10 +29,6 @@ class TestACAuthenticator(TestCase):
"intent": "random-string"}
}
def mock_login_failed(self):
return {
"is_ok": 3
}
def test_login_success_gets_user_and_accounts_list(self):
with patch("ActiveCollabAPI.ACAuthenticator.requests.post") as mock_post:
@@ -50,7 +38,7 @@ class TestACAuthenticator(TestCase):
email = "collab@example.com"
password = "VeryS3cret!"
authenticator = ACAuthenticator(AC_LOGIN_BASE_URL)
response = authenticator.login_with_check(email, password)
response = authenticator.login(email, password)
self.assertEqual(200, response.status_code)
res_data = response.json()
@@ -62,24 +50,38 @@ class TestACAuthenticator(TestCase):
self.assertGreater(len(accounts), 0)
self.assertEqual(12345678, accounts[0]["name"])
def test_login_failed_http401(self):
with self.assertRaises(Exception) as exc:
with patch("ActiveCollabAPI.ACAuthenticator.requests.post") as mock_post:
mock_post.return_value.status_code = 401
mock_post.return_value.json.return_value = self.mock_login_failed()
email = "collab@example.com"
password = "VeryS3cret!"
authenticator = ACAuthenticator(AC_LOGIN_BASE_URL)
authenticator.login_with_check(email, password)
def mock_login_failed(self):
return {
"is_ok": 0
}
def test_login_failed_not_ok(self):
with self.assertRaises(Exception) as exc:
with patch("ActiveCollabAPI.ACAuthenticator.requests.post") as mock_post:
mock_post.return_value.status_code = 200
mock_post.return_value.json.return_value = self.mock_login_failed()
with patch("ActiveCollabAPI.ACAuthenticator.requests.post") as mock_post:
mock_post.return_value.status_code = 200
mock_post.return_value.json.return_value = self.mock_login_failed()
email = "collab@example.com"
password = "VeryS3cret!"
authenticator = ACAuthenticator(AC_LOGIN_BASE_URL)
authenticator.login_with_check(email, password)
email = "collab@example.com"
password = "VeryS3cret!"
authenticator = ACAuthenticator(AC_LOGIN_BASE_URL)
response = authenticator.login(email, password)
self.assertEqual(200, response.status_code)
res_data = response.json()
self.assertNotEqual(1, res_data['is_ok'])
def mock_login_forbidden(self):
return {
}
def test_login_forbidden(self):
with patch("ActiveCollabAPI.ACAuthenticator.requests.post") as mock_post:
mock_post.return_value.status_code = 401
mock_post.return_value.json.return_value = self.mock_login_forbidden()
email = "collab@example.com"
password = "VeryS3cret!"
authenticator = ACAuthenticator(AC_LOGIN_BASE_URL)
response = authenticator.login(email, password)
self.assertEqual(401, response.status_code)
+59
View File
@@ -0,0 +1,59 @@
from unittest import TestCase
from unittest.mock import patch
from ActiveCollabAPI.ACTokenAuthenticator import ACTokenAuthenticator
class TestACTokenAuthenticator(TestCase):
def mock_token_success(self):
return {
"is_ok": 1,
"token": "abcdef"
}
def test_issue_token_intent_success(self):
with patch("ActiveCollabAPI.ACTokenAuthenticator.requests.post") as mock_post:
mock_post.return_value.status_code = 200
mock_post.return_value.json.return_value = self.mock_token_success()
auth = ACTokenAuthenticator("http://mock")
res = auth.issue_token_intent("this-is-the-token-intent-string")
self.assertEqual(200, res.status_code)
res_data = res.json()
self.assertEqual(1, res_data["is_ok"])
def mock_token_not_ok(self):
return {
"is_ok": 0,
"token": "abcdef"
}
def test_issue_token_intent_not_ok(self):
with patch("ActiveCollabAPI.ACTokenAuthenticator.requests.post") as mock_post:
mock_post.return_value.status_code = 200
mock_post.return_value.json.return_value = self.mock_token_not_ok()
auth = ACTokenAuthenticator("http://mock")
res = auth.issue_token_intent("this-is-the-token-intent-string")
self.assertEqual(200, res.status_code)
res_data = res.json()
self.assertNotEqual(1, res_data["is_ok"])
def mock_token_forbidden(self):
return {
}
def test_issue_token_intent_forbidden(self):
with patch("ActiveCollabAPI.ACTokenAuthenticator.requests.post") as mock_post:
mock_post.return_value.status_code = 401
mock_post.return_value.json.return_value = self.mock_token_forbidden()
auth = ACTokenAuthenticator("http://mock")
res = auth.issue_token_intent("this-is-the-token-intent-string")
self.assertEqual(401, res.status_code)