def test_will_log_in(self): with app.test_request_context('/fake'): with patch('confidant.authnz.userauth.request') as request_mock: request_mock.headers = { app.config['HEADER_AUTH_USERNAME_HEADER']: 'unittestuser', app.config['HEADER_AUTH_EMAIL_HEADER']: '*****@*****.**', } resp = authnz.user_mod.log_in() self.assertEqual(resp.status_code, 302) self.assertEqual(resp.headers['Location'], '/')
def test_get_logged_in_user(self): with app.test_request_context('/v1/user/email'): with self.assertRaises(authnz.UserUnknownError): authnz.get_logged_in_user() with patch('confidant.authnz.g') as g_mock: g_mock.username = '******' self.assertEqual(authnz.get_logged_in_user(), 'unittestuser') session_data = {'user': {'email': '*****@*****.**'}} with patch('confidant.authnz.userauth.session', session_data): self.assertEqual(authnz.get_logged_in_user(), '*****@*****.**')
def test_will_extract_from_request(self): with app.test_request_context('/fake'): # No headers given: an error with self.assertRaises(authnz.UserUnknownError): authnz.get_logged_in_user() # Both headers given: success with patch('confidant.authnz.userauth.request') as request_mock: request_mock.headers = { app.config['HEADER_AUTH_USERNAME_HEADER']: 'unittestuser', app.config['HEADER_AUTH_EMAIL_HEADER']: '*****@*****.**', } self.assertEqual(authnz.get_logged_in_user(), '*****@*****.**')
def test_get_logged_in_user(self): with app.test_request_context('/v1/user/email'): with self.assertRaises(authnz.UserUnknownError): authnz.get_logged_in_user() with patch('confidant.authnz.g') as g_mock: g_mock.username = '******' self.assertEqual(authnz.get_logged_in_user(), 'unittestuser') session_data = { 'user': {'email': '*****@*****.**'} } with patch('confidant.authnz.userauth.session', session_data): self.assertEqual( authnz.get_logged_in_user(), '*****@*****.**' )
def test__get_kms_auth_data(self): with app.test_request_context('/v1/user/email'): self.assertEqual(authnz._get_kms_auth_data(), {}) with patch('confidant.authnz.request') as request_mock: request_mock.authorization = { 'username': '******', 'password': '******' } request_mock.headers = {} self.assertEqual( authnz._get_kms_auth_data(), { 'version': 1, 'user_type': 'service', 'from': 'confidant-unittest', 'token': 'encrypted' }) with patch('confidant.authnz.request') as request_mock: request_mock.authorization = { 'username': '******', 'password': '******' } request_mock.headers = {} self.assertEqual( authnz._get_kms_auth_data(), { 'version': 2, 'user_type': 'user', 'from': 'testuser', 'token': 'encrypted' }) with patch('confidant.authnz.request') as request_mock: request_mock.authorization = {} request_mock.headers = { 'X-Auth-From': 'confidant-unittest', 'X-Auth-Token': 'encrypted' } self.assertEqual( authnz._get_kms_auth_data(), { 'version': 1, 'user_type': 'service', 'from': 'confidant-unittest', 'token': 'encrypted' }) with patch('confidant.authnz.request') as request_mock: request_mock.authorization = {} request_mock.headers = { 'X-Auth-From': '2/user/testuser', 'X-Auth-Token': 'encrypted' } self.assertEqual( authnz._get_kms_auth_data(), { 'version': 2, 'user_type': 'user', 'from': 'testuser', 'token': 'encrypted' }) with patch('confidant.authnz.request') as request_mock: request_mock.authorization = { 'username': '******', 'password': '' } request_mock.headers = {} with self.assertRaisesRegexp( authnz.AuthenticationError, 'No password provided via basic auth.'): authnz._get_kms_auth_data(), with patch('confidant.authnz.request') as request_mock: request_mock.authorization = {} request_mock.headers = { 'X-Auth-From': '2/user/testuser', 'X-Auth-Token': '' } with self.assertRaisesRegexp( authnz.AuthenticationError, 'No X-Auth-Token provided via auth headers.'): authnz._get_kms_auth_data(),
def test__get_kms_auth_data(self): with app.test_request_context('/v1/user/email'): self.assertEqual( authnz._get_kms_auth_data(), {} ) with patch('confidant.authnz.request') as request_mock: request_mock.authorization = { 'username': '******', 'password': '******' } request_mock.headers = {} self.assertEqual( authnz._get_kms_auth_data(), {'version': 1, 'user_type': 'service', 'from': 'confidant-unittest', 'token': 'encrypted'} ) with patch('confidant.authnz.request') as request_mock: request_mock.authorization = { 'username': '******', 'password': '******' } request_mock.headers = {} self.assertEqual( authnz._get_kms_auth_data(), {'version': 2, 'user_type': 'user', 'from': 'testuser', 'token': 'encrypted'} ) with patch('confidant.authnz.request') as request_mock: request_mock.authorization = {} request_mock.headers = { 'X-Auth-From': 'confidant-unittest', 'X-Auth-Token': 'encrypted' } self.assertEqual( authnz._get_kms_auth_data(), {'version': 1, 'user_type': 'service', 'from': 'confidant-unittest', 'token': 'encrypted'} ) with patch('confidant.authnz.request') as request_mock: request_mock.authorization = {} request_mock.headers = { 'X-Auth-From': '2/user/testuser', 'X-Auth-Token': 'encrypted' } self.assertEqual( authnz._get_kms_auth_data(), {'version': 2, 'user_type': 'user', 'from': 'testuser', 'token': 'encrypted'} ) with patch('confidant.authnz.request') as request_mock: request_mock.authorization = { 'username': '******', 'password': '' } request_mock.headers = {} with self.assertRaisesRegexp( authnz.AuthenticationError, 'No password provided via basic auth.' ): authnz._get_kms_auth_data(), with patch('confidant.authnz.request') as request_mock: request_mock.authorization = {} request_mock.headers = { 'X-Auth-From': '2/user/testuser', 'X-Auth-Token': '' } with self.assertRaisesRegexp( authnz.AuthenticationError, 'No X-Auth-Token provided via auth headers.' ): authnz._get_kms_auth_data(),