Files
ActiveCollabAPI/tests/test_ACAuthenticator.py
T
carsten 4be5d4c325 Reorg code and tests
DON'T use main.py
2024-02-24 12:02:12 +00:00

88 lines
3.2 KiB
Python

import json
from unittest import TestCase
from unittest.mock import patch
from ActiveCollabAPI import AC_LOGIN_BASE_URL
from ActiveCollabAPI.ACAuthenticator import ACAuthenticator
class TestACAuthenticator(TestCase):
def mock_login_success(self):
return {
"accounts": [
{
"name": 12345678,
"url": "https:\/\/app.activecollab.com\/12345678",
"display_name": "433339",
"user_display_name": "#12345678",
"position": 1,
"class": "ActiveCollab\\Shepherd\\Model\\Account\\ActiveCollab\\FeatherAccount",
"status": "trial"
}
],
"is_ok": 1,
"user": {
"avatar_url": "https:\/\/activecollab.com\/avatar.php?user_id=12345678&size=80",
"first_name": "users first",
"last_name": "users last",
"intent": "random-string"}
}
def test_login_success_gets_user_and_accounts_list(self):
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_success()
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.assertEqual(1, res_data['is_ok'])
user = res_data["user"]
self.assertEqual("users first", user["first_name"])
self.assertEqual("users last", user["last_name"])
accounts = res_data["accounts"]
self.assertGreater(len(accounts), 0)
self.assertEqual(12345678, accounts[0]["name"])
def mock_login_failed(self):
return {
"is_ok": 0
}
def test_login_failed_not_ok(self):
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)
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)