Best Python code snippet using autotest_python
test_users.py
Source:test_users.py
1from unittest import TestCase2import mock3from keycloak.admin import KeycloakAdmin4from keycloak.realm import KeycloakRealm5class KeycloakAdminUsersTestCase(TestCase):6 def setUp(self):7 self.realm = mock.MagicMock(spec_set=KeycloakRealm)8 self.admin = KeycloakAdmin(realm=self.realm)9 self.admin.set_token('some-token')10 def test_create(self):11 self.admin.realms.by_name('realm-name').users.create(12 username='my-username',13 credentials=[{'some': 'value'}],14 first_name='my-first-name',15 last_name='my-last-name',16 email='my-email',17 enabled=True18 )19 self.realm.client.get_full_url.assert_called_once_with(20 '/auth/admin/realms/realm-name/users'21 )22 self.realm.client.post.assert_called_once_with(23 url=self.realm.client.get_full_url.return_value,24 data='{'25 '"credentials": ['26 '{'27 '"some": "value"'28 '}'29 '], '30 '"email": "my-email", '31 '"enabled": true, '32 '"firstName": "my-first-name", '33 '"lastName": "my-last-name", '34 '"username": "my-username"'35 '}',36 headers={37 'Authorization': 'Bearer some-token',38 'Content-Type': 'application/json'39 }40 )41 def test_get_collection(self):42 self.admin.realms.by_name('realm-name').users.all()43 self.realm.client.get_full_url.assert_called_once_with(44 '/auth/admin/realms/realm-name/users'45 )46 self.realm.client.get.assert_called_once_with(47 url=self.realm.client.get_full_url.return_value,48 headers={49 'Authorization': 'Bearer some-token',50 'Content-Type': 'application/json'51 }52 )53 def test_get_single(self):54 self.admin.realms.by_name('realm-name').users.by_id('an-id').get()55 self.realm.client.get_full_url.assert_called_once_with(56 '/auth/admin/realms/realm-name/users/an-id'57 )58 self.realm.client.get.assert_called_once_with(59 url=self.realm.client.get_full_url.return_value,60 headers={61 'Authorization': 'Bearer some-token',62 'Content-Type': 'application/json'63 }64 )65 def test_get_single_user(self):66 self.admin.realms.by_name('realm-name').users.by_id('an-id').user67 self.realm.client.get_full_url.assert_called_once_with(68 '/auth/admin/realms/realm-name/users/an-id'69 )70 self.realm.client.get.assert_called_once_with(71 url=self.realm.client.get_full_url.return_value,72 headers={73 'Authorization': 'Bearer some-token',74 'Content-Type': 'application/json'75 }76 )77 @mock.patch('keycloak.admin.users.User.user', {"id": "user-id"})78 def test_update(self):79 user = self.admin.realms.by_name('realm-name').users.by_id("user-id")80 user.update(81 credentials=[{'some': 'value'}],82 first_name='my-first-name',83 last_name='my-last-name',84 email='my-email',85 enabled=True86 )87 self.realm.client.get_full_url.assert_called_with(88 '/auth/admin/realms/realm-name/users/user-id'89 )90 self.realm.client.put.assert_called_once_with(91 url=self.realm.client.get_full_url.return_value,92 data='{'93 '"credentials": ['94 '{'95 '"some": "value"'96 '}'97 '], '98 '"email": "my-email", '99 '"enabled": true, '100 '"firstName": "my-first-name", '101 '"id": "user-id", '102 '"lastName": "my-last-name"'103 '}',104 headers={105 'Authorization': 'Bearer some-token',106 'Content-Type': 'application/json'107 }108 )109 @mock.patch('keycloak.admin.users.User.user', {"id": "user-id"})110 def test_delete(self):111 user = self.admin.realms.by_name('realm-name').users.by_id("user-id")112 user.delete()113 self.realm.client.get_full_url.assert_called_with(114 '/auth/admin/realms/realm-name/users/user-id'115 )116 self.realm.client.delete.assert_called_once_with(117 url=self.realm.client.get_full_url.return_value,118 headers={119 'Authorization': 'Bearer some-token',120 'Content-Type': 'application/json'121 }122 )123 @mock.patch('keycloak.admin.users.User.user', {"id": "user-id"})124 def test_delete_group(self):125 user = self.admin.realms.by_name('realm-name').users.by_id("user-id")126 user.groups.delete('group-id')127 self.realm.client.get_full_url.assert_called_with(128 '/auth/admin/realms/realm-name/users/user-id/groups/group-id'129 )130 self.realm.client.delete.assert_called_once_with(131 url=self.realm.client.get_full_url.return_value,132 headers={133 'Authorization': 'Bearer some-token',134 'Content-Type': 'application/json'135 }136 )137 @mock.patch('keycloak.admin.users.User.user', {"id": "user-id"})138 def test_reset_password(self):139 user = self.admin.realms.by_name('realm-name').users.by_id("user-id")140 user.reset_password("password", True)141 self.realm.client.get_full_url.assert_called_with(142 '/auth/admin/realms/realm-name/users/user-id/reset-password'143 )144 self.realm.client.put.assert_called_once_with(145 url=self.realm.client.get_full_url.return_value,146 data='{"temporary": true, '147 '"type": "password", '148 '"value": "password"}',149 headers={150 'Authorization': 'Bearer some-token',151 'Content-Type': 'application/json'152 }153 )154 @mock.patch('keycloak.admin.users.User.user', {"id": "user-id"})155 def test_logout_user(self):156 user = self.admin.realms.by_name('realm-name').users.by_id("user-id")157 user.logout()158 self.realm.client.get_full_url.assert_called_with(159 '/auth/admin/realms/realm-name/users/user-id/logout'160 )161 self.realm.client.post.assert_called_once_with(162 url=self.realm.client.get_full_url.return_value,163 data=None,164 headers={165 'Authorization': 'Bearer some-token',166 'Content-Type': 'application/json'167 }...
test_urlparser.py
Source:test_urlparser.py
1import urlparser2# Test 13h = urlparser.HarvestManUrl('http://razor.occams.info/code/repo/?/govtrack/sec')4print h5assert(h.get_full_url()=='http://razor.occams.info/code/repo/?/govtrack/sec')6h2 = urlparser.HarvestManUrl('coderef.c', baseurl=h)7print h28assert(h2.get_full_url()=='http://razor.occams.info/code/repo/coderef.c')9h2 = urlparser.HarvestManUrl('?/govtrack/sec/coderef2.c',baseurl=h)10print h211assert(h2.get_full_url()=='http://razor.occams.info/code/repo/?/govtrack/sec/coderef2.c')12h2 = urlparser.HarvestManUrl("?/sec/coderef3.c", baseurl=h)13print h214assert(h2.get_full_url()=='http://razor.occams.info/code/repo/?/sec/coderef3.c')15h2 = urlparser.HarvestManUrl("?sec/coderef4.c", baseurl=h)16print h217assert(h2.get_full_url()=='http://razor.occams.info/code/repo/?sec/coderef4.c')18h2 = urlparser.HarvestManUrl("sec/coderef5.c", baseurl=h)19print h220assert(h2.get_full_url()=='http://razor.occams.info/code/repo/sec/coderef5.c')21h2 = urlparser.HarvestManUrl("/sec/coderef6.c", baseurl=h)22print h223assert(h2.get_full_url()=='http://razor.occams.info/sec/coderef6.c')24h2 = urlparser.HarvestManUrl("govtrack/sec/coderef7.c", baseurl=h)25print h226assert(h2.get_full_url()=='http://razor.occams.info/code/repo/govtrack/sec/coderef7.c')27h2 = urlparser.HarvestManUrl("govtrack/?/sec/../coderef8.c", baseurl=h)28print h229assert(h2.get_full_url()=='http://razor.occams.info/code/repo/govtrack/?/sec/../coderef8.c')30h2 = urlparser.HarvestManUrl("http://www.foo.com/govtrack/./sec/?/id/../coderef8.c", baseurl=h)31print h232assert(h2.get_full_url()=='http://www.foo.com/govtrack/sec/?/id/../coderef8.c')33h2 = urlparser.HarvestManUrl("../repo2/govtrack/./sec/?/id/../coderef8.c", baseurl=h)34print h235assert(h2.get_full_url()=='http://razor.occams.info/code/repo2/govtrack/sec/?/id/../coderef8.c')36# Test 237h = urlparser.HarvestManUrl('http://razor.occams.info/code/repo/?/govtrack/sec')38print h39h2 = urlparser.HarvestManUrl('../coderef.c', baseurl=h)40print h241assert(h2.get_full_url()=='http://razor.occams.info/code/coderef.c')42h2 = urlparser.HarvestManUrl('govtrack/?/sec/coderef.c', baseurl=h)43print h244assert(h2.get_full_url()=='http://razor.occams.info/code/repo/govtrack/?/sec/coderef.c')45h2 = urlparser.HarvestManUrl('../govtrack2/?/../sec/.././sec/coderef.c', baseurl=h)46print h247assert(h2.get_full_url()=='http://razor.occams.info/code/govtrack2/?/../sec/.././sec/coderef.c')48print 'Test 3'49# Test 350h = urlparser.HarvestManUrl('http://razor.occams.info/code/repo/?/govtrack/sec/?')51print h52h2 = urlparser.HarvestManUrl('?/govtrack/?/sec/coderef.c', baseurl=h)53print h254assert(h2.get_full_url()=='http://razor.occams.info/code/repo/?/govtrack/?/sec/coderef.c')55h2 = urlparser.HarvestManUrl('../gotrack2/../sec/?/../?/./sec/coderef.c', baseurl=h)56print h2...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!