def test_expansion(self): e = Expansion('foo', 'bar', 'baz') e.add_property('bar', limit=5) p = sorted(e.get_params().split(',')) self.assertEqual(p, ['bar(limit:5)', 'baz', 'foo'])
def test_expansion_limit_offset(self): e = Expansion() e.add_property('quux', limit=5, offset=10) p = e.get_params() self.assertTrue(p == 'quux(limit:5,offset:10)' or p == 'quux(offset:10,limit:5)')
def test_resource_expansion(self): class Res(Resource): def get_resource_attributes(self): return {'related': Resource} ds = MagicMock() ds.get_resource.return_value = { 'href': 'test/resource', 'name': 'Test Resource', 'related': { 'href': 'related/resource', 'name': 'Related Resource' } } r = Res(MagicMock(data_store=ds), href='test/resource', expand=Expansion('related')) name = r.name ds.get_resource.assert_called_once_with('test/resource', params={'expand': 'related'}) self.assertEqual(name, 'Test Resource') self.assertEqual(r.related.name, 'Related Resource')
def test_resource_to_json(self): ds = MagicMock() class Res(Resource, SaveMixin, DictMixin): writable_attrs = ('foo_val', 'bar', 'res') r = Res(MagicMock(data_store=ds), href='test/resource1') res = Res(MagicMock(data_store=ds), href='test/resource2') data = {'foo_val': True, 'bar': False} # basic resource r.update(data) r.bar = 'quux' data.update({'href': 'test/resource1', 'bar': 'quux'}) self.assertEqual(data, json.loads(r.to_json())) # resource of resource if not extended res.foo_val = [1, 2, 3] r.res = res data.update({'res': {'href': 'test/resource2'}}) self.assertEqual(data, json.loads(r.to_json())) # resource of resource if extended res.foo_val = [1, 2, 3] r.res = res r._expand = Expansion(*['res']) data.update({'res': {'foo_val': [1, 2, 3], 'href': 'test/resource2'}}) self.assertEqual(data, json.loads(r.to_json()))
def test_creation_with_expansion(self): ds = MagicMock() ds.create_resource.return_value = { 'href': 'test/resource', 'name': 'Test Resource', } rl = CollectionResource( client=MagicMock(data_store=ds, BASE_URL='http://www.example.com'), properties={ 'href': '/', }) e = Expansion() e.add_property('bar', limit=5) rl.create({}, expand=e, some_param=True) ds.create_resource.assert_called_once_with('http://www.example.com/', {}, params={'someParam': True, 'expand': 'bar(limit:5)'})
def test_creation_with_expansion(self): ds = MagicMock() ds.create_resource.return_value = { 'href': 'test/resource', 'name': 'Test Resource', } rl = CollectionResource(client=MagicMock( data_store=ds, BASE_URL='http://www.example.com'), properties={ 'href': '/', }) e = Expansion() e.add_property('bar', limit=5) rl.create({}, expand=e, some_param=True) ds.create_resource.assert_called_once_with('http://www.example.com/', {}, params={ 'someParam': True, 'expand': 'bar(limit:5)' })
def test_collection_resource_to_json(self): ds = MagicMock() ds.get_resource.return_value = { 'href': '/', 'offset': 0, 'limit': 25, 'size': 2, 'items': [{ 'href': 'test/resource' }, { 'href': 'another/resource' }] } props = { 'href': '/', 'offset': 0, 'limit': 25, 'size': 2, 'items': [{ 'href': 'test/resource' }, { 'href': 'another/resource' }] } rl = CollectionResource(client=MagicMock(data_store=ds), properties=props) self.assertEqual(props, json.loads(rl.to_json())) # collection resource as attribute rds = MagicMock() class Res(Resource, SaveMixin, DictMixin): writable_attrs = ('foo_val', 'bar', 'rl') res = Res(MagicMock(data_store=rds), href='test/resource1') res.rl = rl data = {'foo_val': True, 'bar': False} res.update(data) data.update({'href': 'test/resource1'}) data.update({'rl': props}) res._expand = Expansion(*['rl']) self.assertEqual(data, json.loads(res.to_json()))
def test_expansion(self): expansion = Expansion() expansion.add_property('accounts', offset=0, limit=3) dir = self.client.directories.get(self.dir.href, expansion) self.assertEqual(len(dir.accounts), 5)
def test_tenant_expansion(self): e = Expansion() e.add_property('bar', limit=5) client = Client(api_key={'id': 'MyId', 'secret': 'Shush!'}, expand=e) self.assertIsInstance(client.tenant._expand, Expansion)
def test_live(self): # test directory creation name = self.generate_name("my_dir") directory = self.client.directories.create({ 'name': name, 'description': "This is my raindir!" }) self.created_directories.append(directory) self.assertEqual(directory.name, name) self.assertTrue(directory.is_enabled()) # test existing directory creation with self.assertRaises(Error): application = self.client.directories.create({ "name": name}) self.created_applications.append(application) # test directory group creation group_name = self.generate_name("my_group_1") group = directory.groups.create({ "name": group_name, "description": "This is my support group", "enabled": "enabled" }) self.created_groups.append(group) self.assertEqual(group.name, group_name) self.assertEqual(group.description, "This is my support group") self.assertTrue(group.is_enabled()) # test directory account creation username = self.generate_name("william") account = directory.accounts.create({ 'username': username, 'email': username + "@titan.com", 'given_name': "William", 'middle_name': "Thomas", 'surname': "Riker", 'password': "******", "custom_data": { "rank": "Captain", "birthDate": "2305-07-13", "birthPlace": "La Barre, France", "favoriteDrink": "Earl Grey tea" } }) self.assertTrue(account.is_enabled()) # test custom data account.custom_data['birthDate'] = 'whenever' self.assertEqual(account.custom_data['rank'], 'Captain') self.assertEqual(account.custom_data['birthDate'], 'whenever') account.custom_data.save() account = directory.accounts.get(account.href) self.assertEqual(account.custom_data['birthDate'], 'whenever') del account.custom_data['birthDate'] with self.assertRaises(KeyError): account.custom_data['birthDate'] account = directory.accounts.get(account.href) self.assertEqual(account.custom_data['birthDate'], "whenever") del account.custom_data['birthDate'] account.custom_data.save() account = directory.accounts.get(account.href) with self.assertRaises(KeyError): account.custom_data['birthDate'] # test application creation name = self.generate_name("my_app") application = self.client.applications.create({ "name": name, "description": "This is my rainapp", "enabled": "enabled" }) self.created_applications.append(application) # test invalid application authentication with self.assertRaises(Error): account = application.authenticate_account(username, "xaiK3auc") # test application creation with directory app_name = self.generate_name("my_app") dir_name = self.generate_name("my_dir") application2 = self.client.applications.create({ "name": app_name, "description": "This is my rainapp", "enabled": "enabled" }, create_directory=dir_name) self.created_applications.append(application2) self.assertEqual(application2.name, app_name) self.assertEqual(application2.description, "This is my rainapp") self.assertTrue(application2.is_enabled()) directory2 = self.client.directories.search({"name": dir_name})[0] self.created_directories.append(directory2) self.assertEqual(directory2.name, dir_name) self.assertTrue(directory2.is_enabled()) # test account to group addition group_membership = group.add_account(account) self.created_group_memberships.append(group_membership) self.assertIsInstance(group_membership, GroupMembership) self.assertEqual( group_membership.account.email, username + "@titan.com") self.assertEqual(group_membership.group.name, group_name) # test account store creation account_store_mapping = self.client.account_store_mappings.create({ 'application': application, 'account_store': directory}) self.created_account_stores.append(account_store_mapping) self.assertFalse(account_store_mapping.is_default_account_store) # test valid application authentication login_attempt = application.authenticate_account(username, "xaiK3auc") account = login_attempt.account self.assertEqual(account.email, username + "@titan.com") self.assertEqual(account.middle_name, "Thomas") # test unsuccesful account creation on application username2 = self.generate_name("ltcmdata") with self.assertRaises(Error): application.accounts.create({ 'username': username2, 'email': username2 + "@enterprise.com", 'given_name': "Lieutenant", 'surname': "Commander", 'password': "******" }) # test default account store account_store_mapping.is_default_account_store = True account_store_mapping.save() account2 = application.accounts.create( { 'username': username2, 'email': username2 + "@enterprise.com", 'given_name': "Lieutenant", 'surname': "Commander", 'password': "******" }) self.created_accounts.append(account2) self.assertEqual(account2.email, username2 + "@enterprise.com") self.assertIsNone(account2.middle_name, None) # test unsuccesful group creation on application group2_name = self.generate_name("Android civil rights group") with self.assertRaises(Error): application.groups.create({ 'name': group2_name }) # test default group store account_store_mapping.is_default_group_store = True account_store_mapping.save() group2 = application.groups.create({ 'name': group2_name }) self.created_groups.append(group2) self.assertEqual(group2.name, group2_name) # create multiple groups to test on for i in range(0, 8): group = directory.groups.create( { 'name': "test_groupi_{0}".format(i), 'description': 'random_groups' }) self.created_groups.append(group) # test pagination groups = directory.groups.search({"description": "random_groups"}) self.assertEqual(len(groups), 8) self.assertEqual(len(groups[3:10]), 5) # test expansion expansion = Expansion('accounts', 'groups') expansion.add_property('groups', offset=0, limit=3) directory = self.client.directories.get(directory.href, expansion) self.assertEqual(len(directory.groups), 3) # test sorting groups = directory.groups.order("description asc") self.assertEqual(groups[-1].description, "This is my support group") groups = directory.groups.order("description desc") self.assertEqual(groups[0].description, "This is my support group") # test search groups = directory.groups.search({"description": "random_groups"}) self.assertTrue(len(groups), 7) groups = directory.groups.search( {"description": "random_groups", "name": "test_groupi_1"}) self.assertEqual(len(groups), 1) self.assertEqual(groups[0].name, "test_groupi_1")
def test_live(self): # test directory creation name = self.generate_name("my_dir") directory = self.client.directories.create({ 'name': name, 'description': "This is my raindir!" }) self.created_directories.append(directory) self.assertEqual(directory.name, name) self.assertTrue(directory.is_enabled()) # test existing directory creation with self.assertRaises(Error): application = self.client.directories.create({"name": name}) self.created_applications.append(application) # test directory group creation group_name = self.generate_name("my_group_1") group = directory.groups.create({ "name": group_name, "description": "This is my support group", "enabled": "enabled" }) self.created_groups.append(group) self.assertEqual(group.name, group_name) self.assertEqual(group.description, "This is my support group") self.assertTrue(group.is_enabled()) # test directory account creation username = self.generate_name("william") account = directory.accounts.create({ 'username': username, 'email': username + "@titan.com", 'given_name': "William", 'middle_name': "Thomas", 'surname': "Riker", 'password': "******", "custom_data": { "rank": "Captain", "birthDate": "2305-07-13", "birthPlace": "La Barre, France", "favoriteDrink": "Earl Grey tea" } }) self.assertTrue(account.is_enabled()) # test custom data account.custom_data['birthDate'] = 'whenever' self.assertEqual(account.custom_data['rank'], 'Captain') self.assertEqual(account.custom_data['birthDate'], 'whenever') account.custom_data.save() account = directory.accounts.get(account.href) self.assertEqual(account.custom_data['birthDate'], 'whenever') del account.custom_data['birthDate'] with self.assertRaises(KeyError): account.custom_data['birthDate'] account = directory.accounts.get(account.href) self.assertEqual(account.custom_data['birthDate'], "whenever") del account.custom_data['birthDate'] account.custom_data.save() account = directory.accounts.get(account.href) with self.assertRaises(KeyError): account.custom_data['birthDate'] # test application creation name = self.generate_name("my_app") application = self.client.applications.create({ "name": name, "description": "This is my rainapp", "enabled": "enabled" }) self.created_applications.append(application) # test invalid application authentication with self.assertRaises(Error): account = application.authenticate_account(username, "xaiK3auc") # test application creation with directory app_name = self.generate_name("my_app") dir_name = self.generate_name("my_dir") application2 = self.client.applications.create( { "name": app_name, "description": "This is my rainapp", "enabled": "enabled" }, create_directory=dir_name) self.created_applications.append(application2) self.assertEqual(application2.name, app_name) self.assertEqual(application2.description, "This is my rainapp") self.assertTrue(application2.is_enabled()) directory2 = self.client.directories.search({"name": dir_name})[0] self.created_directories.append(directory2) self.assertEqual(directory2.name, dir_name) self.assertTrue(directory2.is_enabled()) # test account to group addition group_membership = group.add_account(account) self.created_group_memberships.append(group_membership) self.assertIsInstance(group_membership, GroupMembership) self.assertEqual(group_membership.account.email, username + "@titan.com") self.assertEqual(group_membership.group.name, group_name) # test account store creation account_store_mapping = self.client.account_store_mappings.create({ 'application': application, 'account_store': directory }) self.created_account_stores.append(account_store_mapping) self.assertFalse(account_store_mapping.is_default_account_store) # test valid application authentication login_attempt = application.authenticate_account(username, "xaiK3auc") account = login_attempt.account self.assertEqual(account.email, username + "@titan.com") self.assertEqual(account.middle_name, "Thomas") # test unsuccesful account creation on application username2 = self.generate_name("ltcmdata") with self.assertRaises(Error): application.accounts.create({ 'username': username2, 'email': username2 + "@enterprise.com", 'given_name': "Lieutenant", 'surname': "Commander", 'password': "******" }) # test default account store account_store_mapping.is_default_account_store = True account_store_mapping.save() account2 = application.accounts.create({ 'username': username2, 'email': username2 + "@enterprise.com", 'given_name': "Lieutenant", 'surname': "Commander", 'password': "******" }) self.created_accounts.append(account2) self.assertEqual(account2.email, username2 + "@enterprise.com") self.assertIsNone(account2.middle_name, None) # test unsuccesful group creation on application group2_name = self.generate_name("Android civil rights group") with self.assertRaises(Error): application.groups.create({'name': group2_name}) # test default group store account_store_mapping.is_default_group_store = True account_store_mapping.save() group2 = application.groups.create({'name': group2_name}) self.created_groups.append(group2) self.assertEqual(group2.name, group2_name) # create multiple groups to test on for i in range(0, 8): group = directory.groups.create({ 'name': "test_groupi_{0}".format(i), 'description': 'random_groups' }) self.created_groups.append(group) # test pagination groups = directory.groups.search({"description": "random_groups"}) self.assertEqual(len(groups), 8) self.assertEqual(len(groups[3:10]), 5) # test expansion expansion = Expansion('accounts', 'groups') expansion.add_property('groups', offset=0, limit=3) directory = self.client.directories.get(directory.href, expansion) self.assertEqual(len(directory.groups), 3) # test sorting groups = directory.groups.order("description asc") self.assertEqual(groups[-1].description, "This is my support group") groups = directory.groups.order("description desc") self.assertEqual(groups[0].description, "This is my support group") # test search groups = directory.groups.search({"description": "random_groups"}) self.assertTrue(len(groups), 7) groups = directory.groups.search({ "description": "random_groups", "name": "test_groupi_1" }) self.assertEqual(len(groups), 1) self.assertEqual(groups[0].name, "test_groupi_1")