Files
ActiveCollabAPI/tests/test_ACAuthenticator.py
T
2024-02-23 21:08:10 +00:00

86 lines
3.5 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 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 {
"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 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:
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_with_check(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 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 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()
email = "collab@example.com"
password = "VeryS3cret!"
authenticator = ACAuthenticator(AC_LOGIN_BASE_URL)
authenticator.login_with_check(email, password)