-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from iotile/fix-make-login-via-token-file-work
Relates to AFX-2175 Fix login with token via config file
- Loading branch information
Showing
8 changed files
with
166 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,3 +130,4 @@ dmypy.json | |
|
||
# IDEs | ||
.vscode | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[c-test] | ||
token=test-token |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import json | ||
import os.path | ||
import unittest | ||
from argparse import Namespace | ||
|
||
import mock | ||
import requests_mock | ||
|
||
from archfx_cloud.utils.main import BaseMain | ||
|
||
|
||
class MainTestCase(unittest.TestCase): | ||
@requests_mock.Mocker() | ||
@mock.patch('archfx_cloud.utils.main.argparse.ArgumentParser.parse_args') | ||
@mock.patch('archfx_cloud.utils.main.getpass.getpass') | ||
def test_main_login_email_password_ok( | ||
self, mock_request, mock_getpass, mock_parse_args | ||
): | ||
mock_request.post( | ||
'https://test.archfx.io/api/v1/auth/login/', | ||
text=json.dumps({'jwt': 'big-token', 'username': 'user1'}), | ||
) | ||
mock_request.post( | ||
'https://test.archfx.io/api/v1/auth/logout/', status_code=204 | ||
) | ||
mock_getpass.return_value = 'password' | ||
mock_parse_args.return_value = Namespace( | ||
customer='test', server_type='prod', email='[email protected]' | ||
) | ||
|
||
main = BaseMain() | ||
main.main() | ||
|
||
self.assertEqual(len(mock_request.request_history), 2) | ||
self.assertEqual( | ||
mock_request.request_history[0].url, | ||
'https://test.archfx.io/api/v1/auth/login/', | ||
) | ||
self.assertTrue( | ||
'Authorization' not in mock_request.request_history[0].headers | ||
) | ||
self.assertEqual( | ||
json.loads(mock_request.request_history[0].body), | ||
{'email': '[email protected]', 'password': 'password'}, | ||
) | ||
self.assertEqual( | ||
mock_request.request_history[1].url, | ||
'https://test.archfx.io/api/v1/auth/logout/', | ||
) | ||
self.assertEqual( | ||
mock_request.request_history[1].headers['Authorization'], | ||
'jwt big-token', | ||
) | ||
|
||
@requests_mock.Mocker() | ||
@mock.patch('archfx_cloud.utils.main.argparse.ArgumentParser.parse_args') | ||
def test_main_login_token_file_ok(self, mock_request, mock_parse_args): | ||
mock_request.get( | ||
'https://test.archfx.io/api/v1/account/', | ||
text=json.dumps({'results': [{'email': '[email protected]'}]}), | ||
) | ||
mock_request.post( | ||
'https://test.archfx.io/api/v1/auth/logout/', status_code=204 | ||
) | ||
mock_parse_args.return_value = Namespace( | ||
customer='test', | ||
server_type='prod', | ||
) | ||
|
||
main = BaseMain( | ||
config_path=f'{os.path.dirname(__file__)}/data/test_main_login_token_file.ini' | ||
) | ||
main.main() | ||
|
||
self.assertEqual(len(mock_request.request_history), 2) | ||
self.assertEqual( | ||
mock_request.request_history[0].url, | ||
'https://test.archfx.io/api/v1/account/', | ||
) | ||
self.assertEqual( | ||
mock_request.request_history[0].headers['Authorization'], | ||
'token test-token', | ||
) | ||
self.assertEqual( | ||
mock_request.request_history[1].url, | ||
'https://test.archfx.io/api/v1/auth/logout/', | ||
) | ||
self.assertEqual( | ||
mock_request.request_history[1].headers['Authorization'], | ||
'token test-token', | ||
) | ||
|
||
@mock.patch('archfx_cloud.utils.main.argparse.ArgumentParser.parse_args') | ||
def test_main_login_token_file_ko(self, mock_parse_args): | ||
mock_parse_args.return_value = Namespace( | ||
customer='test', | ||
server_type='prod', | ||
email=None, | ||
) | ||
|
||
main = BaseMain(config_path=f'non_existing_config_file.ini') | ||
|
||
with self.assertRaises(SystemExit) as e: | ||
main.main() | ||
|
||
self.assertEqual(e.exception.code, 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
version = '0.13.0' | ||
version = '0.14.0' |