def setUp(self):
		self.users = User(PropertyMock(
			url='test_url',
			team='test_team',
			signature='test_signature',
			token='old_value',
			verify=True,
			r=Mock(return_value=(200, {'token': '123'}))
		))
class TestResetPasswordMethod(unittest.TestCase):

	def setUp(self):
		self.users = User(PropertyMock(
			url='test_url',
			team='test_team',
			signature='test_signature',
			token='old_value',
			verify=True,
			r=Mock(return_value=(200, {'token': '123'}))
		))
		self.new_password = '******'

	def test_set_new_password_request(self):
		self.users.set_new_password(self.new_password)
		self.users.root.r.assert_called_with(
			'PUT',
			f'{self.users.root.url}/api/v1/users/attributes/password',
			body=json.dumps({'password': self.new_password}),
			headers=None,
			verify=self.users.root.verify
		)

	def test_set_new_password_with_incorrect_password_type(self):
		with self.assertRaisesRegex(AssertionError, 'Password value must be a string'):
			self.users.set_new_password(123)
	
	def test_set_new_password_with_empty_id_string(self):
		with self.assertRaisesRegex(AssertionError, 'Password value must not be an empty string'):
			self.users.set_new_password('')
		
	def test_set_new_password_with_short_password_value(self):
		with self.assertRaisesRegex(AssertionError, 'Length of password value must be greater  than 8'):
			self.users.set_new_password('Test')
class TestGetAllMethod(unittest.TestCase):

	def setUp(self):
		self.users = User(PropertyMock(
			url='test_url',
			team='test_team',
			signature='test_signature',
			token='old_value',
			verify=True,
			r=Mock(return_value=(200, {'token': '123'}))
		))

	def test_get_all_request(self):
		self.users.get_all()
		self.users.root.r.assert_called_with(
			'GET',
			f'{self.users.root.url}/api/v1/users',
			body=None,
			headers=None,
			verify=self.users.root.verify
		)
class TestCreateMethod(unittest.TestCase):

	def setUp(self):
		self.users = User(PropertyMock(
			url='test_url',
			team='test_team',
			signature='test_signature',
			token='old_value',
			verify=True,
			r=Mock(return_value=(200, {'token': '123'}))
		))
		self.data = {'a': 1, 'b': 2}

	def test_create_request(self):
		self.users.create(self.data)
		self.users.root.r.assert_called_with(
			'POST',
			f'{self.users.root.url}/api/v1/users',
			json.dumps(self.data),
			headers=None,
			verify=self.users.root.verify
		)

	def test_create_with_incorrect_data_type(self):
		with self.assertRaisesRegex(AssertionError, 'The data type must be a dictionary'):
			self.users.create(123)
	
	def test_create_with_empty_dict(self):
		with self.assertRaisesRegex(AssertionError, 'User data must not be an empty dictionary'):
			self.users.create({})
class TestGetByIdMethod(unittest.TestCase):

	def setUp(self):
		self.users = User(PropertyMock(
			url='test_url',
			team='test_team',
			signature='test_signature',
			token='old_value',
			verify=True,
			r=Mock(return_value=(200, {'token': '123'}))
		))
		self.id = 'test_id'

	def test_get_by_id_request(self):
		self.users.get_by_id(self.id)
		self.users.root.r.assert_called_with(
			'GET',
			f'{self.users.root.url}/api/v1/users/{self.id}',
			body=None,
			headers=None,
			verify=self.users.root.verify
		)

	def test_get_by_id_with_incorrect_id_type(self):
		with self.assertRaisesRegex(AssertionError, 'The ID must be a string'):
			self.users.get_by_id(None)
	
	def test_get_by_id_with_empty_id_string(self):
		with self.assertRaisesRegex(AssertionError, 'The ID must not be an empty string'):
			self.users.get_by_id('')
Example #6
0
    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 TestRemoveRoleMethod(unittest.TestCase):

	def setUp(self):
		self.users = User(PropertyMock(
			url='test_url',
			team='test_team',
			signature='test_signature',
			token='old_value',
			verify=True,
			r=Mock(return_value=(200, {'token': '123'}))
		))
		self.id = 'test_id'
		self.role = 'my_role'

	@patch('CybertonicaAPI.users.User.get_by_id',
		return_value=(200, {'roles':['a','b','my_role']})
	)
	@patch('CybertonicaAPI.users.User.update', return_value=PropertyMock())
	def test_remove_role_logic(self, update_mock, get_mock):
		self.users.remove_role(self.id, self.role)
		get_mock.assert_called_once()
		update_mock.assert_called_once()

		get_mock.assert_called_with(self.id)
		update_mock.assert_called_with(self.id, {'roles':['a','b']})

	def test_remove_role_with_incorrect_id_type(self):
		with self.assertRaisesRegex(AssertionError, 'The ID must be a string'):
			self.users.remove_role(123, self.role)
	
	def test_remove_role_with_empty_id_string(self):
		with self.assertRaisesRegex(AssertionError, 'The ID must not be an empty string'):
			self.users.remove_role('', self.role)
	
	def test_remove_role_with_incorrect_role_type(self):
		with self.assertRaisesRegex(AssertionError, 'Role name must be a string'):
			self.users.remove_role(self.id, 123)
	
	def test_remove_role_with_empty_role_string(self):
		with self.assertRaisesRegex(AssertionError, 'Role name must not be an empty string'):
			self.users.remove_role(self.id, '')
	
	@patch('CybertonicaAPI.users.User.get_by_id',
		return_value=(1, None)
	)
	def test_remove_role_get_by_id_return_not_200_status(self, get_mock):
		with self.assertRaisesRegex(AssertionError, 'The user data request was not successful'):
			self.users.remove_role(self.id,self.role)
	
	@patch('CybertonicaAPI.users.User.get_by_id',
		return_value=(200, None)
	)
	def test_remove_role_get_by_id_return_none_data(self, get_mock):
		with self.assertRaisesRegex(AssertionError, 'The user data type does not match the dictionary'):
			self.users.remove_role(self.id,self.role)
	
	@patch('CybertonicaAPI.users.User.get_by_id',
		return_value=(200, {'a':1})
	)
	def test_remove_role_get_by_id_return_dict_without_roles_key(self, get_mock):
		with self.assertRaisesRegex(AssertionError, 'The data structure does not contain the roles key'):
			self.users.remove_role(self.id,self.role)
	
	@patch('CybertonicaAPI.users.User.get_by_id',
		return_value=(200, {'a':1, "roles":['a']})
	)
	def test_remove_role_already_has_not_target_role(self, get_mock):
		with self.assertRaisesRegex(AssertionError, 'The user already has not a role'):
			self.users.remove_role(self.id,self.role)