def test_field_set_init(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('sys_user') gr.initialize() name = 'System Administrator' gr.name = name self.assertEquals(gr.name, name) gr.set_value('name', 'Test') self.assertEquals(gr.name, 'Test') self.assertEquals(gr.get_value('name'), 'Test')
def test_batch_multi(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('problem') gr.fields = 'sys_id' gr.batch_size = 3 gr.limit = 9 gr.query() res = [r.sys_id for r in gr] self.assertEquals(len(res), 9)
def test_serialize(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('some_table') gr.initialize() gr.strfield = 'my string' gr.set_display_value('strfield', 'my string display value') gr.intfield = 5 data = gr.serialize() self.assertIsNotNone(data) self.assertEquals(data, {'intfield': 5, 'strfield': 'my string'})
def test_object_secondary_field(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('sys_user') gr.limit = 1 gr.query() self.assertTrue(gr.next()) gr.boom = 'aaaa' self.assertEquals(gr.boom, 'aaaa') gr.bye = [1, 2, 3] self.assertEquals(gr.bye, [1, 2, 3])
def test_attrs_changes(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('sys_user') r = gr.get('6816f79cc0a8016401c5a33be04be441') self.assertEquals(r, True) self.assertIsNotNone(gr.get_element('sys_id')) self.assertIsNone(gr.get_element('asdf')) self.assertEquals(gr.get_element('sys_id').changes(), False) gr.sys_id = '1234' self.assertEquals(gr.get_element('sys_id').changes(), True)
def test_insert(self): # I want to ensure the records sys_id is updated client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('problem') gr.initialize() gr.short_description = "Unit Test - Test insert id update" self.assertIsNone(gr.sys_id) res = gr.insert() self.assertIsNotNone(res) self.assertIsNotNone(gr.sys_id) # make sure it exists gr2 = client.GlideRecord('problem') self.assertTrue(gr2.get(res)) gr.delete() # make sure it is deleted gr4 = client.GlideRecord('problem') self.assertFalse(gr4.get(res))
def test_fields(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('sys_user') gr.fields = ['sys_id'] gr.limit = 4 gr.query() count = 0 while gr.next(): count = count + 1 assert len(gr._current().keys()) == 1 self.assertEquals(count, 4)
def test_next(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('sys_user') gr.add_active_query() gr.limit = 2 gr.query() #print(gr.serialize_all()) self.assertTrue(gr.next()) self.assertTrue(gr.has_next()) self.assertTrue(gr.next()) self.assertFalse(gr.has_next())
def test_attrs_changes(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('sys_user') gr.initialize() self.assertTrue(gr.is_new_record()) self.assertIsNone(gr.get_element('sys_id')) gr.sys_id = 'zzzz' # i am not considering a state of nothing to something a change, merely the start of existence self.assertEquals(gr.get_element('sys_id').changes(), False) gr.sys_id = '1234' self.assertEquals(gr.get_element('sys_id').changes(), True)
def test_field_getter_query(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('sys_user') self.assertEquals(gr.fields, None) gr.limit = 1 gr.query() self.assertIsNotNone(gr.fields) self.assertGreater(len(gr.fields), 10) gr.next() print(gr.fields) self.assertGreater(len(gr.fields), 10)
def test_field_limit(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('sys_user') gr.fields = 'sys_id,name' r = gr.get('6816f79cc0a8016401c5a33be04be441') print(gr.serialize()) self.assertEquals(r, True) sobj = gr.serialize() self.assertTrue('sys_id' in sobj) self.assertFalse('sys_created_on' in sobj)
def test_link_list(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('sys_user') gr.add_active_query() gr.add_query("name", "CONTAINS", "a") link = gr.get_link_list() print(link) self.assertTrue( link.endswith( 'sys_user_list.do?sysparm_query=active%3Dtrue%5EnameCONTAINSa') )
def test_join_query(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('sys_user') join_query = gr.add_join_query('sys_user_group', join_table_field='manager') join_query.add_query('active', 'true') self.assertEquals( gr.get_encoded_query(), 'JOINsys_user.sys_id=sys_user_group.manager!active=true') gr.query() self.assertGreater(gr.get_row_count(), 2)
def test_basic_fail(self): client = ServiceNowClient(self.c.server, ('admin', 'this is not a real password')) try: gr = client.GlideRecord('sys_user') gr.get('does not matter') assert 'Exception should have been thrown' except exceptions.AuthenticationException as e: self.assertTrue('User Not Authenticated' in str(e)) self.assertTrue('Required to provide Auth information' in str(e)) except Exception: assert 'Should have got an Auth exception'
def test_link_query(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('sys_user') gr.limit = 5 gr.query() link = gr.get_link(no_stack=True) print(link) self.assertTrue(link.endswith('sys_user.do?sys_id=null')) self.assertTrue(gr.next()) link = gr.get_link(no_stack=True) print(link) self.assertFalse(link.endswith('sys_user.do?sys_id=null'))
def test_field_limit_query(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('sys_user') gr.limit = 1 gr.fields = 'sys_id,name' gr.query() gr.next() print(gr.serialize()) sobj = gr.serialize() self.assertTrue('sys_id' in sobj) self.assertFalse('sys_created_on' in sobj)
def test_field_access(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('sys_user') gr.fields = 'sys_id,name' gr.get('6816f79cc0a8016401c5a33be04be441') print(gr.serialize()) name = 'System Administrator' self.assertEquals(gr.name, name) self.assertEquals(gr.get_value('name'), name) self.assertEquals(gr.get_display_value('name'), name)
def test_batching(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('syslog') gr.fields = ['sys_id' ] # not testing this, but just limit response size gr.query() gr.limit = 1100 count = 0 while gr.next(): self.assertFalse(gr.is_new_record()) count = count + 1 self.assertGreater(count, 600)
def _getOrCreateEmptyTestRecord(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('problem') gr.add_query('short_description', 'Unit Test - Attachments - Empty') gr.query() if gr.next(): return gr gr.initialize() gr.short_description = "Unit Test - Attachments - Empty" gr.description = "Second Field" gr.insert() return gr
def test_str(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('some_table') gr.initialize() gr.strfield = 'my string' gr.set_display_value('strfield', 'my string display value') gr.intfield = 5 data = str(gr) self.assertIsNotNone(data) # dict is unordered, so do some contains checks self.assertTrue(data.startswith('some_table')) self.assertTrue('my string' in data) self.assertTrue('intfield' in data)
def test_link(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('sys_user') gr.get('6816f79cc0a8016401c5a33be04be441') link = gr.get_link(no_stack=True) self.assertTrue( link.endswith( 'sys_user.do?sys_id=6816f79cc0a8016401c5a33be04be441')) link = gr.get_link() self.assertTrue( link.endswith( 'sys_user.do?sys_id=6816f79cc0a8016401c5a33be04be441&sysparm_stack=sys_user_list.do?sysparm_query=active=true' ))
def test_default_limit(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('problem') gr.add_active_query() params = gr._parameters() print(params) self.assertFalse('sysparm_limit' in params, 'We have a default limit for some reason') gr.limit = 40 print(gr.limit) params = gr._parameters() print(params) self.assertTrue('sysparm_limit' in params)
def test_crud(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('problem') gr.initialize() gr.short_description = "Unit Test - Insert" gr.description = "Second Field" gr.bunk_field = "Bunk Field" res = gr.insert() self.assertIsNotNone(res) gr2 = client.GlideRecord('problem') self.assertIsNotNone(gr2.get(res)) # We have validated inserting works, now can we update. gr2.short_description = "ABCDEFG0123" gr2.update() gr3 = client.GlideRecord('problem') gr3.get(res) self.assertEqual(gr3.short_description, "ABCDEFG0123") self.assertTrue(gr3.delete()) # make sure it is deleted gr4 = client.GlideRecord('problem') self.assertFalse(gr4.get(res))
def test_serialize_changes(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('problem') gr.fields = 'sys_id,short_description,state' gr.limit = 4 gr.query() gr.next() data = gr.serialize() self.assertIsNotNone(data) self.assertListEqual(list(data.keys()), ['sys_id', 'short_description', 'state']) self.assertListEqual(list(gr.serialize(changes_only=True).keys()), []) gr.short_description = 'new' self.assertListEqual(list(gr.serialize(changes_only=True).keys()), ['short_description'])
def test_pandas_value(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('problem') gr.fields = 'sys_id,short_description,state' gr.limit = 4 gr.query() print(gr.serialize_all(display_value='both')) data = gr.to_pandas(mode='value') print(data) self.assertIsInstance(data, dict) self.assertTrue('sys_id' in data) self.assertTrue('short_description' in data) self.assertTrue('state' in data) self.assertFalse('state__value' in data) self.assertEqual(len(data['sys_id']), 4)
def test_pandas_order_cols(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('problem') gr.fields = 'sys_id,short_description,state' gr.limit = 4 gr.query() print(gr.serialize_all(display_value='both')) data = gr.to_pandas() print(data) self.assertListEqual( list(data.keys()), ['sys_id', 'short_description', 'state__value', 'state__display']) data = gr.to_pandas(mode='display') print(data) self.assertListEqual(list(data.keys()), ['sys_id', 'short_description', 'state']) data = gr.to_pandas(columns=['jack', 'jill', 'hill'], mode='display') print(data) self.assertListEqual(list(data.keys()), ['jack', 'jill', 'hill'])
def test_oauth(self): # Manual setup using legacy oauth server = self.c.server creds = self.c.credentials client_id = '3e57bb02663102004d010ee8f561307a' #mobile # get the secret manually secret_url = '%s/api/now/mobileapp/plugin/secret' % server r = requests.get(secret_url, auth=creds) if r.status_code != 200: raise Exception('couldnt get secret') secret = r.json()['result']['secret'] self.assertIsNotNone(secret) oauth = OAuth2Session(client=LegacyApplicationClient(client_id=client_id)) token = oauth.fetch_token(token_url='%s/oauth_token.do' % server, username=creds[0], password=creds[1], client_id=client_id, client_secret=secret) client = ServiceNowClient(self.c.server, oauth) gr = client.GlideRecord('sys_user') gr.fields = 'sys_id' self.assertTrue(gr.get('6816f79cc0a8016401c5a33be04be441'))
def test_creds(self): with self.assertRaises(AuthenticationException) as context: client = ServiceNowClient(self.c.server, ('test','test')) gr = client.GlideRecord('sys_user') gr.get('asdf'); self.assertTrue(isinstance(context.exception, AuthenticationException))
def test_oauth_builtin(self): client = ServiceNowClient(self.c.server, ServiceNowOAuth2(self.c.credentials)) gr = client.GlideRecord('sys_user') gr.fields = 'sys_id' self.assertTrue(gr.get('6816f79cc0a8016401c5a33be04be441'))
def test_connect(self): client = ServiceNowClient(self.c.server, self.c.credentials) gr = client.GlideRecord('sys_user') r = gr.get('6816f79cc0a8016401c5a33be04be441') self.assertEquals(r, True)