def setUp(self): self.auth = Auth( PropertyMock(url='test_url', team='test_team', signature='test_signature', token='old_value', verify=True, r=Mock(return_value=(200, { 'token': '123' })))) self.user_data_without_times = { "email": "*****@*****.**", "password": "******", "team": "test_team", "firstName": "John", "lastName": "Smith", "login": "******" } self.user_data_with_times = { **self.user_data_without_times, **{ "invitedAt": 0, "updatedAt": 0 } } self.headers = {"content-type": "application/json"}
def setUp(self): self.auth = Auth( PropertyMock(url='test_url', team='test_team', signature='test_signature', token='old_value', verify=True, r=Mock(return_value=(200, { 'token': '123' })))) self.team = 'test' self.email = '*****@*****.**' self.headers = {"content-type": "application/json"}
def setUp(self): self.auth = Auth( PropertyMock(url='test_url', team='test_team', signature='test_signature', token='old_value', verify=True, r=Mock(return_value=(200, { 'token': '123' })))) self.headers = { "content-type": "application/json", "Authorization": f"Bearer {self.auth.root.token}" }
def setUp(self): self.auth = Auth( PropertyMock(url='test_url', team='test_team', signature='test_signature', token='old_value', verify=True, r=Mock(return_value=(200, { 'token': '123' })))) self.login = '******' self.password = '******' self.data = { "apiUser": self.login, "team": self.auth.root.team, "apiUserKeyHash": self.password } self.headers = {"content-type": "application/json"}
def setUp(self): self.auth = Auth( PropertyMock(url='test_url', team='test_team', signature='test_signature', token='old_value', verify=True, r=Mock(return_value=(200, { 'token': '123' }))))
class TestLogoutMethod(unittest.TestCase): def setUp(self): self.auth = Auth( PropertyMock(url='test_url', team='test_team', signature='test_signature', token='old_value', verify=True, r=Mock(return_value=(200, { 'token': '123' })))) self.headers = { "content-type": "application/json", "Authorization": f"Bearer {self.auth.root.token}" } def test_logout_request(self): self.auth.logout() self.auth.root.r.assert_called_with( 'POST', f'{self.auth.root.url}/api/v1/logout', None, self.headers, None, verify=self.auth.root.verify) def test_logout_clears_client_token_if_status_less_400(self): self.auth.logout() self.assertEqual(self.auth.root.token, '') def test_logout_does_not_clear_client_token_if_status_400_and_more(self): self.auth.root.r = Mock(return_value=(401, {'token': '123'})) expected_token = self.auth.root.token self.auth.logout() self.assertEqual(self.auth.root.token, expected_token)
def __init__(self, **settings): self.log = logging.getLogger(__name__) self.log.debug('Client has been init with %s', settings) assert 'url' in settings, 'url is required parameter' assert isinstance(settings['url'], str), 'url value must be a string' assert 'team' in settings, 'team is required parameter' assert isinstance(settings['team'], str), 'team value must be a string' self.url = str(settings['url']) self.af_url = str( settings['custom_af_url'] ) if 'custom_af_url' in settings else self.url + ':7499' self.team = str(settings['team']) self.api_user_id = str( settings['api_user_id']) if 'api_user_id' in settings else '' self.api_signature = str( settings['api_signature']) if 'api_signature' in settings else '' self.af_version = str( settings['af_version']) if 'af_version' in settings else 'v2.2' self.verify = bool( settings['verify']) if 'verify' in settings else True self.token = '' self.login_time = 0 self.ttl = 840 # Session TTL == 14 mins self.auth = Auth(self) self.subchannels = Subchannel(self) self.lists = List(self) self.users = User(self) self.channels = Channel(self) self.policies = Policy(self) self.events = Event(self) self.roles = Role(self) self.abtests = ABTest(self) self.af = AFClient(self) self.cases = Case(self) self.settings = Settings(self) self.queues = Queue(self) self.sessions = Session(self) self.tech = Tech(self) self.bi = BusinessIntelligence(self) self.schema = Schema(self) self.currencies = Currency(self) self.geo = Geo(self) self.bins = Bin(self) self.reports = Report(self)
class TestRegisterMethod(unittest.TestCase): def setUp(self): self.auth = Auth( PropertyMock(url='test_url', team='test_team', signature='test_signature', token='old_value', verify=True, r=Mock(return_value=(200, { 'token': '123' })))) self.user_data_without_times = { "email": "*****@*****.**", "password": "******", "team": "test_team", "firstName": "John", "lastName": "Smith", "login": "******" } self.user_data_with_times = { **self.user_data_without_times, **{ "invitedAt": 0, "updatedAt": 0 } } self.headers = {"content-type": "application/json"} def test_register_request_without_times(self): self.auth.register(self.user_data_without_times) self.auth.root.r.assert_called_with( 'POST', f'{self.auth.root.url}/api/v1/registration', json.dumps(self.user_data_with_times), self.headers, verify=self.auth.root.verify) def test_register_with_incorrect_data_type(self): with self.assertRaisesRegex(AssertionError, 'The data type must be a dictionary'): self.auth.register(123) def test_register_with_empty_dict(self): with self.assertRaisesRegex( AssertionError, 'User data must not be an empty dictionary'): self.auth.register({})
class TestLoginMethod(unittest.TestCase): def setUp(self): self.auth = Auth( PropertyMock(url='test_url', team='test_team', signature='test_signature', token='old_value', verify=True, r=Mock(return_value=(200, { 'token': '123' })))) self.login = '******' self.password = '******' self.data = { "apiUser": self.login, "team": self.auth.root.team, "apiUserKeyHash": self.password } self.headers = {"content-type": "application/json"} def test_login_request(self): self.auth.login(self.login, self.password) self.auth.root.r.assert_called_with( 'POST', f'{self.auth.root.url}/api/v1/login', json.dumps(self.data), self.headers, verify=self.auth.root.verify) def test_login_method_changes_client_token_if_code_is_200(self): self.auth.login(self.login, self.password) self.assertEqual(self.auth.root.token, '123') self.assertGreater(self.auth.root.login_time, 0) def test_login_method_changes_client_token_if_code_is_201(self): self.auth.root.r = Mock(return_value=(201, {'token': '123'})) self.auth.login(self.login, self.password) self.assertEqual(self.auth.root.token, '123') self.assertGreater(self.auth.root.login_time, 0) def test_login_method_does_not_change_client_token_if_code_is_not_200_201( self): self.auth.root.r = Mock(return_value=(401, {'token': '123'})) expected_token = self.auth.root.token self.auth.login(self.login, self.password) self.assertEqual(self.auth.root.token, expected_token) def test_login_with_incorrect_apiuser_type(self): with self.assertRaisesRegex(AssertionError, 'The api user must be a string'): self.auth.login(123, self.password) def test_login_with_incorrect_apiuserhash_type(self): with self.assertRaisesRegex(AssertionError, 'The api user key hash must be a string'): self.auth.login(self.login, 123) def test_login_with_empty_apiuser_string(self): with self.assertRaisesRegex( AssertionError, 'The api user must not be an empty string'): self.auth.login('', self.password) def test_login_with_empty_apiuserhash_string(self): with self.assertRaisesRegex( AssertionError, 'The api user key hash must not be an empty string'): self.auth.login(self.login, '')
class TestRecoveryPasswordMethod(unittest.TestCase): def setUp(self): self.auth = Auth( PropertyMock(url='test_url', team='test_team', signature='test_signature', token='old_value', verify=True, r=Mock(return_value=(200, { 'token': '123' })))) self.team = 'test' self.email = '*****@*****.**' self.headers = {"content-type": "application/json"} def test_recovery_request(self): self.auth.recovery_password(self.team, self.email) self.auth.root.r.assert_called_with( 'GET', f'{self.auth.root.url}/api/v1/recovery/request?team={self.team}&email={self.email}', None, self.headers, None, verify=self.auth.root.verify) def test_recovery_with_incorrect_team_type(self): with self.assertRaisesRegex(AssertionError, 'Team must be a string'): self.auth.recovery_password(123, self.email) def test_recovery_with_incorrect_email_type(self): with self.assertRaisesRegex(AssertionError, 'Email must be a string'): self.auth.recovery_password(self.team, 123) def test_recovery_with_empty_team_string(self): with self.assertRaisesRegex(AssertionError, 'Team must not be an empty string'): self.auth.recovery_password('', self.email) def test_recovery_with_empty_email_string(self): with self.assertRaisesRegex(AssertionError, 'Email must not be an empty string'): self.auth.recovery_password(self.team, '') def test_recovery_with_invalid_email_address(self): with self.assertRaisesRegex(AssertionError, 'Email must be valid'): self.auth.recovery_password(self.team, 'invalid-email')