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)