Best Python code snippet using rester_python
unit_tests.py
Source:unit_tests.py
...27 username = hexlify(dev_urandom_entropy(16))28 return username29def build_url(pathname):30 return '/v' + API_VERSION + pathname31def test_get_request(cls, endpoint, headers={}, status_code=200):32 resp = app.get(endpoint, headers=headers)33 data = json.loads(resp.data)34 cls.assertTrue(isinstance(data, dict))35 if not resp.status_code == status_code:36 print data37 cls.assertTrue(resp.status_code == status_code)38 return data39def test_post_request(cls, endpoint, payload, headers={}, status_code=200):40 resp = app.post(endpoint, data=json.dumps(payload), headers=headers)41 data = json.loads(resp.data)42 cls.assertTrue(isinstance(data, dict))43 cls.assertTrue(resp.status_code == status_code)44 return data45def check_data(cls, data, required_keys=[], banned_keys=[]):46 for k in required_keys:47 cls.assertTrue(k in data)48 for subkey in required_keys[k]:49 cls.assertTrue(subkey in data[k])50 for k in banned_keys:51 if len(banned_keys[k]) is 0:52 cls.assertTrue(k not in data)53 else:54 cls.assertTrue(k in data)55 for subkey in banned_keys[k]:56 cls.assertTrue(subkey not in data[k])57class LookupUsersTest(unittest.TestCase):58 def setUp(self):59 self.headers = {'Authorization': basic_auth(APP_ID, APP_SECRET)}60 self.required_subkeys = ['profile', 'verifications']61 self.banned_subkeys = ['error']62 def tearDown(self):63 pass64 def build_url(self, usernames):65 return build_url('/users/' + ','.join(usernames))66 def required_keys(self, usernames):67 keys = {}68 for username in usernames:69 keys[username] = self.required_subkeys70 return keys71 def banned_keys(self, usernames):72 keys = {}73 for username in usernames:74 keys[username] = self.banned_subkeys75 return keys76 def test_unprotected_demo_user_lookup(self):77 usernames = ['fredwilson']78 data = test_get_request(self, self.build_url(usernames),79 headers=self.headers, status_code=200)80 check_data(self, data, required_keys=self.required_keys(usernames),81 banned_keys=self.banned_keys(usernames))82 """83 def test_user_lookup_without_auth(self):84 usernames = ['naval']85 data = test_get_request(self, self.build_url(usernames),86 headers={}, status_code=401)87 check_data(self, data, required_keys={'error': ['message', 'type']},88 banned_keys={'naval': []})89 """90 def test_user_lookup_with_auth(self):91 usernames = ['naval']92 data = test_get_request(self, self.build_url(usernames),93 headers=self.headers, status_code=200)94 check_data(self, data, required_keys=self.required_keys(usernames),95 banned_keys=self.banned_keys(usernames))96 def test_user_lookup_with_multiple_users(self):97 usernames = ['fredwilson', 'naval', 'albertwenger']98 data = test_get_request(self, self.build_url(usernames),99 headers=self.headers, status_code=200)100 check_data(self, data, required_keys=self.required_keys(usernames),101 banned_keys=self.banned_keys(usernames))102class UserbaseTest(unittest.TestCase):103 def setUp(self):104 self.headers = {'Authorization': basic_auth(APP_ID, APP_SECRET)}105 def tearDown(self):106 pass107 def test_userbase_lookup(self):108 required_keys = {109 'usernames': [],110 }111 data = test_get_request(self, build_url('/users'),112 headers=self.headers, status_code=200)113 check_data(self, data, required_keys=required_keys)114 def test_recent_userbase_lookup(self):115 required_keys = {'usernames': []}116 data = test_get_request(self, build_url('/users'),117 headers=self.headers, status_code=200)118 check_data(self, data, required_keys=required_keys)119class UserbaseStatsTest(unittest.TestCase):120 def setUp(self):121 self.headers = {'Authorization': basic_auth(APP_ID, APP_SECRET)}122 def tearDown(self):123 pass124 def test_stats_lookup(self):125 required_keys = {126 'stats': ['registrations']127 }128 data = test_get_request(self, build_url('/stats/users'),129 headers=self.headers, status_code=200)130 check_data(self, data, required_keys=required_keys)131class SearchTest(unittest.TestCase):132 def setUp(self):133 self.headers = {'Authorization': basic_auth(APP_ID, APP_SECRET)}134 self.required_keys = {'results': []}135 def tearDown(self):136 pass137 def test_simple_search_query(self):138 query = 'wenger'139 data = test_get_request(self, build_url('/search?query=' + query),140 headers=self.headers)141 check_data(self, data, required_keys=self.required_keys)142 def test_twitter_search_query(self):143 query = 'twitter:albertwenger'144 data = test_get_request(self, build_url('/search?query=' + query),145 headers=self.headers)146 check_data(self, data, required_keys=self.required_keys)147 def test_domain_search_query(self):148 query = 'domain:muneebali.com'149 data = test_get_request(self, build_url('/search?query=' + query),150 headers=self.headers)151 check_data(self, data, required_keys=self.required_keys)152class LookupUnspentsTest(unittest.TestCase):153 def setUp(self):154 self.headers = {'Authorization': basic_auth(APP_ID, APP_SECRET)}155 self.required_keys = {'unspents': []}156 def tearDown(self):157 pass158 def build_url(self, address):159 return build_url('/addresses/' + address + '/unspents')160 def test_address_lookup(self):161 address = '19bXfGsGEXewR6TyAV3b89cSHBtFFewXt6'162 data = test_get_request(self, self.build_url(address),163 headers=self.headers)164 check_data(self, data, required_keys=self.required_keys)165class LookupNamesOwnedTest(unittest.TestCase):166 def setUp(self):167 self.headers = {'Authorization': basic_auth(APP_ID, APP_SECRET)}168 self.required_keys = {'results': []}169 def tearDown(self):170 pass171 def build_url(self, address):172 return build_url('/addresses/' + address + '/names')173 def test_address_lookup(self):174 address = '1QJQxDas5JhdiXhEbNS14iNjr8auFT96GP'175 data = test_get_request(self, self.build_url(address),176 headers=self.headers)177 check_data(self, data, required_keys=self.required_keys)178class RegisterUserTest(unittest.TestCase):179 def setUp(self):180 self.headers = {181 'Authorization': basic_auth(APP_ID, APP_SECRET),182 'Content-type': 'application/json'183 }184 self.required_keys = {'status': []}185 def tearDown(self):186 pass187 def test_user_registration(self):188 payload = dict(189 recipient_address='19MoWG8u88L6t766j7Vne21Mg4wHsCQ7vk',190 username=random_username(),191 profile={'name': {'formatted': 'John Doe'}}192 )193 data = test_post_request(self, build_url('/users'), payload,194 headers=self.headers)195 check_data(self, data, required_keys=self.required_keys)196class BroadcastTransactionTest(unittest.TestCase):197 def setUp(self):198 self.headers = {'Authorization': basic_auth(APP_ID, APP_SECRET)}199 self.required_keys = {'error': ['message', 'type']}200 self.banned_keys = {'transaction_hash': []}201 def tearDown(self):202 pass203 def test_bogus_transaction_broadcast(self):204 #bitcoind reject this, needs updating205 signed_hex = '00710000015e98119922f0b'206 payload = {'signed_hex': signed_hex}207 data = test_post_request(self, build_url('/transactions'), payload,208 headers=self.headers, status_code=400)209 check_data(self, data, required_keys=self.required_keys,210 banned_keys=self.banned_keys)211class DKIMPubkeyTest(unittest.TestCase):212 def setUp(self):213 self.headers = {'Authorization': basic_auth(APP_ID, APP_SECRET)}214 self.required_keys = {'public_key': [], 'key_type': []}215 def tearDown(self):216 pass217 def build_url(self, domain):218 return build_url('/domains/' + domain + '/dkim')219 def test_address_lookup(self):220 domain = 'onename.com'221 data = test_get_request(self, self.build_url(domain),222 headers=self.headers)223 check_data(self, data, required_keys=self.required_keys)224class EmailSaveTest(unittest.TestCase):225 def setUp(self):226 self.required_keys = {'status': []}227 def tearDown(self):228 pass229 def get_email_token(self):230 data = test_get_request(self, build_url('/emails'))231 return data['token']232 def test_email_save(self):233 email = 'test@onename.com'234 token = self.get_email_token()235 payload = {'email': email, 'token': token}236 data = test_post_request(self, build_url('/emails'), payload)237 check_data(self, data, required_keys=self.required_keys)238def test_main():239 test_support.run_unittest(240 LookupUsersTest,241 UserbaseTest,242 UserbaseStatsTest,243 SearchTest,244 LookupUnspentsTest,...
tests.py
Source:tests.py
...4from django.test import TestCase5from django.urls import reverse6from .regbackend import EmailLoginBackend7class LoginViewTests(TestCase):8 def test_get_request(self):9 response = self.client.get(reverse("account_login"))10 self.assertEqual(response.status_code, 200)11class RegisterViewTest(TestCase):12 def test_get_request(self):13 response = self.client.get(reverse("account_signup"))14 self.assertEqual(response.status_code, 200)15class PasswordChangeViewTest(TestCase):16 def test_get_request(self):17 response = self.client.get(reverse("account_change_password"))18 self.assertEqual(response.status_code, 302)19class PasswordResetViewTest(TestCase):20 def test_get_request(self):21 response = self.client.get(reverse("account_reset_password"))22 self.assertEqual(response.status_code, 200)23class EmailLoginBackendTests(TestCase):24 def setUp(self):25 self.credentials = {26 'username': 'test',27 'email': 'test@email.com',28 'password': 'test@123'}29 self.user = User.objects.create_user(**self.credentials)30 def test_valid_username_login(self):31 response = self.client.login(username=self.credentials['username'],32 password=self.credentials['password'])33 self.assertTrue(response)34 def test_invalid_username_login(self):...
test_views.py
Source:test_views.py
2from django.shortcuts import reverse3from leads.models import Lead4class SignupPageTest(TestCase):5 6 def test_get_request(self):7 response = self.client.get(reverse("signup"))8 self.assertEqual(response.status_code, 200)9 self.assertTemplateUsed(response, "registration/signup.html")10class LandingPageTest(TestCase):11 def test_get_request(self):12 response = self.client.get(reverse("landing-page"))13 self.assertEqual(response.status_code, 200)14 self.assertTemplateUsed(response, "landing.html")15class ListPageTest(TestCase):16 def test_get_request(self):17 response = self.client.get(reverse("leads:lead-list"))18 self.assertEqual(response.status_code, 200)19 self.assertTemplateUsed(response, "leads/lead_list.html")20''' class DetailPageTest(TestCase):21 def setup(self):22 Lead.objects.create(id=6)23 def test_get_request(self):24 response = self.client.get(reverse("leads:lead-detail", args=(6,)), follow=True)25 self.assertEqual(response.status_code, 404)26 self.assertTemplateUsed(response, "leads/lead_detail.html") '''27class CreatePageTest(TestCase):28 def test_get_request(self):29 response = self.client.get(reverse("leads:lead-create"))30 self.assertEqual(response.status_code, 200)31 self.assertTemplateUsed(response, "leads/lead_create.html")32''' class UpdatePageTest(TestCase):33 34 def test_get_request(self):35 pk = 136 response = self.client.get(reverse("leads:lead-update"))37 self.assertEqual(response.status_code, 200)38 self.assertTemplateUsed(response, "leads/lead_update.html") '''39''' class DeletePageTest(TestCase):40 41 def test_get_request(self):42 response = self.client.get(reverse("leads:lead-delete"))43 self.assertEqual(response.status_code, 200)...
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!!