Best Python code snippet using avocado_python
test_portal_attachment.py
Source:test_portal_attachment.py
...27 'csrf_token': http.WebRequest.csrf_token(self),28 }29 create_url = base_url + '/portal/attachment/add'30 files = [('file', ('test.txt', b'test', 'plain/text'))]31 res = self.url_open(url=create_url, data=create_data, files=files)32 self.assertEqual(res.status_code, 400)33 self.assertIn("you do not have the rights", res.text)34 # Test public user can create attachment with token35 create_data['access_token'] = invoice._portal_ensure_token()36 res = self.url_open(url=create_url, data=create_data, files=files)37 self.assertEqual(res.status_code, 200)38 create_res = json.loads(res.content.decode('utf-8'))39 self.assertEqual(create_res['name'], "new attachment")40 # Test created attachment is private41 res_binary = self.url_open('/web/content/%d' % create_res['id'])42 self.assertEqual(res_binary.status_code, 404)43 # Test created access_token is working44 res_binary = self.url_open('/web/content/%d?access_token=%s' % (create_res['id'], create_res['access_token']))45 self.assertEqual(res_binary.status_code, 200)46 # Test mimetype is neutered as non-admin47 files = [('file', ('test.svg', b'<svg></svg>', 'image/svg+xml'))]48 res = self.url_open(url=create_url, data=create_data, files=files)49 self.assertEqual(res.status_code, 200)50 create_res = json.loads(res.content.decode('utf-8'))51 self.assertEqual(create_res['mimetype'], 'text/plain')52 res_binary = self.url_open('/web/content/%d?access_token=%s' % (create_res['id'], create_res['access_token']))53 self.assertEqual(res_binary.headers['Content-Type'], 'text/plain')54 self.assertEqual(res_binary.content, b'<svg></svg>')55 res_image = self.url_open('/web/image/%d?access_token=%s' % (create_res['id'], create_res['access_token']))56 self.assertEqual(res_image.headers['Content-Type'], 'text/plain')57 self.assertEqual(res_image.content, b'<svg></svg>')58 # Test attachment can't be removed without valid token59 remove_data = {60 'attachment_id': create_res['id'],61 'access_token': 'wrong',62 }63 remove_url = base_url + '/portal/attachment/remove'64 res = self.opener.post(url=remove_url, json={'params': remove_data})65 self.assertEqual(res.status_code, 200)66 self.assertTrue(self.env['ir.attachment'].search([('id', '=', create_res['id'])]))67 self.assertIn("you do not have the rights", res.text)68 # Test attachment can be removed with token if "pending" state69 remove_data['access_token'] = create_res['access_token']70 res = self.opener.post(url=remove_url, json={'params': remove_data})71 self.assertEqual(res.status_code, 200)72 remove_res = json.loads(res.content.decode('utf-8'))['result']73 self.assertFalse(self.env['ir.attachment'].search([('id', '=', create_res['id'])]))74 self.assertTrue(remove_res is True)75 # Test attachment can't be removed if not "pending" state76 attachment = self.env['ir.attachment'].create({77 'name': "an attachment",78 'access_token': self.env['ir.attachment']._generate_access_token(),79 })80 remove_data = {81 'attachment_id': attachment.id,82 'access_token': attachment.access_token,83 }84 res = self.opener.post(url=remove_url, json={'params': remove_data})85 self.assertEqual(res.status_code, 200)86 self.assertTrue(self.env['ir.attachment'].search([('id', '=', attachment.id)]))87 self.assertIn("not in a pending state", res.text)88 # Test attachment can't be removed if attached to a message89 attachment.write({90 'res_model': 'mail.compose.message',91 'res_id': 0,92 })93 attachment.flush()94 message = self.env['mail.message'].create({95 'attachment_ids': [(6, 0, attachment.ids)],96 })97 remove_data = {98 'attachment_id': attachment.id,99 'access_token': attachment.access_token,100 }101 res = self.opener.post(url=remove_url, json={'params': remove_data})102 self.assertEqual(res.status_code, 200)103 self.assertTrue(attachment.exists())104 self.assertIn("it is linked to a message", res.text)105 message.unlink()106 # Test attachment can't be associated if no attachment token.107 post_url = base_url + '/mail/chatter_post'108 post_data = {109 'res_model': invoice._name,110 'res_id': invoice.id,111 'message': "test message 1",112 'attachment_ids': attachment.id,113 'attachment_tokens': 'false',114 'csrf_token': http.WebRequest.csrf_token(self),115 }116 res = self.url_open(url=post_url, data=post_data)117 self.assertEqual(res.status_code, 400)118 self.assertIn("The attachment %s does not exist or you do not have the rights to access it." % attachment.id, res.text)119 # Test attachment can't be associated if no main document token120 post_data['attachment_tokens'] = attachment.access_token121 res = self.url_open(url=post_url, data=post_data)122 self.assertEqual(res.status_code, 403)123 self.assertIn("Sorry, you are not allowed to access documents of type 'Journal Entries' (account.move).", res.text)124 # Test attachment can't be associated if not "pending" state125 post_data['token'] = invoice._portal_ensure_token()126 self.assertFalse(invoice.message_ids)127 attachment.write({'res_model': 'model'})128 res = self.url_open(url=post_url, data=post_data)129 self.assertEqual(res.status_code, 200)130 invoice.invalidate_cache(fnames=['message_ids'], ids=invoice.ids)131 self.assertEqual(len(invoice.message_ids), 1)132 self.assertEqual(invoice.message_ids.body, "<p>test message 1</p>")133 self.assertFalse(invoice.message_ids.attachment_ids)134 # Test attachment can't be associated if not correct user135 attachment.write({'res_model': 'mail.compose.message'})136 post_data['message'] = "test message 2"137 res = self.url_open(url=post_url, data=post_data)138 self.assertEqual(res.status_code, 200)139 invoice.invalidate_cache(fnames=['message_ids'], ids=invoice.ids)140 self.assertEqual(len(invoice.message_ids), 2)141 self.assertEqual(invoice.message_ids[0].body, "<p>test message 2</p>")142 self.assertFalse(invoice.message_ids.attachment_ids)143 # Test attachment can be associated if all good (complete flow)144 create_data['name'] = "final attachment"145 res = self.url_open(url=create_url, data=create_data, files=files)146 self.assertEqual(res.status_code, 200)147 create_res = json.loads(res.content.decode('utf-8'))148 self.assertEqual(create_res['name'], "final attachment")149 post_data['message'] = "test message 3"150 post_data['attachment_ids'] = create_res['id']151 post_data['attachment_tokens'] = create_res['access_token']152 res = self.url_open(url=post_url, data=post_data)153 self.assertEqual(res.status_code, 200)154 invoice.invalidate_cache(fnames=['message_ids'], ids=invoice.ids)155 self.assertEqual(len(invoice.message_ids), 3)156 self.assertEqual(invoice.message_ids[0].body, "<p>test message 3</p>")...
test_redirect.py
Source:test_redirect.py
...33 """34 url = '/test_website/country/' + slug(country_ad)35 redirect_url = url.replace('test_website', 'redirected')36 # [Public User] Open the original url and check redirect OK37 r = self.url_open(url)38 self.assertEqual(r.status_code, 200)39 self.assertTrue(r.url.endswith(redirect_url), "Ensure URL got redirected")40 self.assertTrue(country_ad.name in r.text, "Ensure the controller returned the expected value")41 self.assertTrue(redirect_url in r.text, "Ensure the url_for has replaced the href URL in the DOM")42 # [Logged In User] Open the original url and check redirect OK43 self.authenticate("portal_user", "portal_user")44 r = self.url_open(url)45 self.assertEqual(r.status_code, 200)46 self.assertTrue(r.url.endswith(redirect_url), "Ensure URL got redirected (2)")47 self.assertTrue('Logged In' in r.text, "Ensure logged in")48 self.assertTrue(country_ad.name in r.text, "Ensure the controller returned the expected value (2)")49 self.assertTrue(redirect_url in r.text, "Ensure the url_for has replaced the href URL in the DOM")50 @mute_logger('odoo.addons.http_routing.models.ir_http') # mute 403 warning51 def test_02_redirect_308_RequestUID(self):52 self.env['website.rewrite'].create({53 'name': 'Test Website Redirect',54 'redirect_type': '308',55 'url_from': '/test_website/200/<model("test.model"):rec>',56 'url_to': '/test_website/308/<model("test.model"):rec>',57 })58 rec_published = self.env['test.model'].create({'name': 'name', 'website_published': True})59 rec_unpublished = self.env['test.model'].create({'name': 'name', 'website_published': False})60 WebsiteHttp = odoo.addons.website.models.ir_http.Http61 def _get_error_html(env, code, value):62 return str(code).split('_')[-1], "CUSTOM %s" % code63 with patch.object(WebsiteHttp, '_get_error_html', _get_error_html):64 # Patch will avoid to display real 404 page and regenerate assets each time and unlink old one.65 # And it allow to be sur that exception id handled by handle_exception and return a "managed error" page.66 # published67 resp = self.url_open("/test_website/200/name-%s" % rec_published.id, allow_redirects=False)68 self.assertEqual(resp.status_code, 308)69 self.assertEqual(resp.headers.get('Location'), self.base_url + "/test_website/308/name-%s" % rec_published.id)70 resp = self.url_open("/test_website/308/name-%s" % rec_published.id, allow_redirects=False)71 self.assertEqual(resp.status_code, 200)72 resp = self.url_open("/test_website/200/xx-%s" % rec_published.id, allow_redirects=False)73 self.assertEqual(resp.status_code, 308)74 self.assertEqual(resp.headers.get('Location'), self.base_url + "/test_website/308/xx-%s" % rec_published.id)75 resp = self.url_open("/test_website/308/xx-%s" % rec_published.id, allow_redirects=False)76 self.assertEqual(resp.status_code, 301)77 self.assertEqual(resp.headers.get('Location'), self.base_url + "/test_website/308/name-%s" % rec_published.id)78 resp = self.url_open("/test_website/200/xx-%s" % rec_published.id, allow_redirects=True)79 self.assertEqual(resp.status_code, 200)80 self.assertEqual(resp.url, self.base_url + "/test_website/308/name-%s" % rec_published.id)81 # unexisting82 resp = self.url_open("/test_website/200/name-100", allow_redirects=False)83 self.assertEqual(resp.status_code, 308)84 self.assertEqual(resp.headers.get('Location'), self.base_url + "/test_website/308/name-100")85 resp = self.url_open("/test_website/308/name-100", allow_redirects=False)86 self.assertEqual(resp.status_code, 404)87 self.assertEqual(resp.text, "CUSTOM 404")88 resp = self.url_open("/test_website/200/xx-100", allow_redirects=False)89 self.assertEqual(resp.status_code, 308)90 self.assertEqual(resp.headers.get('Location'), self.base_url + "/test_website/308/xx-100")91 resp = self.url_open("/test_website/308/xx-100", allow_redirects=False)92 self.assertEqual(resp.status_code, 404)93 self.assertEqual(resp.text, "CUSTOM 404")94 # unpublish95 resp = self.url_open("/test_website/200/name-%s" % rec_unpublished.id, allow_redirects=False)96 self.assertEqual(resp.status_code, 308)97 self.assertEqual(resp.headers.get('Location'), self.base_url + "/test_website/308/name-%s" % rec_unpublished.id)98 resp = self.url_open("/test_website/308/name-%s" % rec_unpublished.id, allow_redirects=False)99 self.assertEqual(resp.status_code, 403)100 self.assertEqual(resp.text, "CUSTOM 403")101 resp = self.url_open("/test_website/200/xx-%s" % rec_unpublished.id, allow_redirects=False)102 self.assertEqual(resp.status_code, 308)103 self.assertEqual(resp.headers.get('Location'), self.base_url + "/test_website/308/xx-%s" % rec_unpublished.id)104 resp = self.url_open("/test_website/308/xx-%s" % rec_unpublished.id, allow_redirects=False)105 self.assertEqual(resp.status_code, 403)106 self.assertEqual(resp.text, "CUSTOM 403")107 # with seo_name as slug108 rec_published.seo_name = "seo_name"109 rec_unpublished.seo_name = "seo_name"110 resp = self.url_open("/test_website/200/seo-name-%s" % rec_published.id, allow_redirects=False)111 self.assertEqual(resp.status_code, 308)112 self.assertEqual(resp.headers.get('Location'), self.base_url + "/test_website/308/seo-name-%s" % rec_published.id)113 resp = self.url_open("/test_website/308/seo-name-%s" % rec_published.id, allow_redirects=False)114 self.assertEqual(resp.status_code, 200)115 resp = self.url_open("/test_website/200/xx-%s" % rec_unpublished.id, allow_redirects=False)116 self.assertEqual(resp.status_code, 308)117 self.assertEqual(resp.headers.get('Location'), self.base_url + "/test_website/308/xx-%s" % rec_unpublished.id)118 resp = self.url_open("/test_website/308/xx-%s" % rec_unpublished.id, allow_redirects=False)119 self.assertEqual(resp.status_code, 403)120 self.assertEqual(resp.text, "CUSTOM 403")121 resp = self.url_open("/test_website/200/xx-100", allow_redirects=False)122 self.assertEqual(resp.status_code, 308)123 self.assertEqual(resp.headers.get('Location'), self.base_url + "/test_website/308/xx-100")124 resp = self.url_open("/test_website/308/xx-100", allow_redirects=False)125 self.assertEqual(resp.status_code, 404)...
test_iot_controller.py
Source:test_iot_controller.py
...63 {"address": self.address_2, "value": "test value 3"},64 ]65 )66 def test_single_controller(self):67 res = self.url_open(68 "/iot/%s/action" % self.serial,69 data={"passphrase": self.input_passphrase, "value": "123"},70 )71 self.assertEqual(res.json()["status"], "ok")72 def test_single_controller_archived_device(self):73 self.device.write({"active": False})74 res = self.url_open(75 "/iot/%s/action" % self.serial,76 data={"passphrase": self.input_passphrase, "value": "123"},77 )78 self.assertEqual(res.json()["status"], "error")79 def test_multi_input_controller_error_passphrase(self):80 res = self.url_open(81 "/iot/%s/multi_input" % self.device_identification,82 data={"values": self.values},83 ).json()84 self.assertEqual(res["status"], "error")85 def test_multi_input_controller_error_values(self):86 res = self.url_open(87 "/iot/%s/multi_input" % self.device_identification,88 data={"passphrase": self.passphrase},89 ).json()90 self.assertEqual(res["status"], "error")91 def test_multi_input_controller_syntax_error(self):92 res = self.url_open(93 "/iot/%s/multi_input" % self.device_identification,94 data={"passphrase": self.passphrase, "values": "{}"},95 ).json()96 self.assertEqual(res["status"], "error")97 def test_multi_input_controller_malformed_error(self):98 res = self.url_open(99 "/iot/%s/multi_input" % self.device_identification,100 data={"passphrase": self.passphrase, "values": "{1234}"},101 ).json()102 self.assertEqual(res["status"], "error")103 def test_multi_input_controller(self):104 res = self.url_open(105 "/iot/%s/multi_input" % self.device_identification,106 data={"passphrase": self.passphrase, "values": self.values},107 )108 result = res.json()109 for response in result:110 self.assertEqual(response["status"], "ok")111 def test_multi_input_controller_unauthorized_iot_exists(self):112 res = self.url_open(113 "/iot/%s/check" % self.serial, data={"passphrase": self.input_passphrase}114 ).json()115 self.assertEqual(res["state"], True)116 def test_multi_input_controller_unauthorized_iot_no_exists(self):117 res = self.url_open(118 "/iot/%s/check" % self.passphrase,119 data={"passphrase": self.input_passphrase},120 ).json()...
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!!