From 7cad54827047c4e3de5f7cebb3337a1cf8ce98e2 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Sat, 21 Feb 2015 22:37:35 +0530 Subject: [PATCH] [tests] --- frappe/__init__.py | 12 +- frappe/core/doctype/docshare/test_docshare.py | 3 + frappe/core/page/data_import_tool/importer.py | 2 - .../test_exporter_fixtures.py | 143 +++++++++--------- .../customize_form/test_customize_form.py | 4 +- frappe/defaults.py | 14 +- frappe/desk/doctype/event/test_event.py | 29 ++-- frappe/desk/doctype/event/test_records.json | 6 +- frappe/email/bulk.py | 2 +- .../email_account/test_email_account.py | 10 +- frappe/email/receive.py | 2 +- frappe/email/smtp.py | 8 +- frappe/model/db_query.py | 10 +- frappe/model/document.py | 4 +- frappe/permissions.py | 10 +- frappe/test_runner.py | 11 ++ frappe/tests/test_data_import.py | 4 +- frappe/tests/test_document.py | 6 +- frappe/tests/test_email.py | 5 +- 19 files changed, 155 insertions(+), 130 deletions(-) diff --git a/frappe/__init__.py b/frappe/__init__.py index 31f507de06..6b153d383f 100644 --- a/frappe/__init__.py +++ b/frappe/__init__.py @@ -378,7 +378,7 @@ def get_user(username): else: return User(username) -def has_permission(doctype, ptype="read", doc=None, user=None): +def has_permission(doctype, ptype="read", doc=None, user=None, verbose=False): """Raises `frappe.PermissionError` if not permitted. :param doctype: DocType for which permission is to be check. @@ -386,7 +386,7 @@ def has_permission(doctype, ptype="read", doc=None, user=None): :param doc: [optional] Checks User permissions for given doc. :param user: [optional] Check for given user. Default: current user.""" import frappe.permissions - return frappe.permissions.has_permission(doctype, ptype, doc, user=user) + return frappe.permissions.has_permission(doctype, ptype, doc, verbose=verbose, user=user) def is_table(doctype): """Returns True if `istable` property (indicating child Table) is set for given DocType.""" @@ -441,6 +441,14 @@ def get_doc(arg1, arg2=None): import frappe.model.document return frappe.model.document.get_doc(arg1, arg2) +def get_last_doc(doctype): + """Get last created document of this type.""" + d = get_all(doctype, ["name"], order_by="creation desc", limit_page_length=1) + if d: + return get_doc(doctype, d[0].name) + else: + raise DoesNotExistError + def get_single(doctype): """Return a `frappe.model.document.Document` object of the given Single doctype.""" return get_doc(doctype, doctype) diff --git a/frappe/core/doctype/docshare/test_docshare.py b/frappe/core/doctype/docshare/test_docshare.py index 05e61497c6..b71c6a3631 100644 --- a/frappe/core/doctype/docshare/test_docshare.py +++ b/frappe/core/doctype/docshare/test_docshare.py @@ -69,9 +69,12 @@ class TestDocShare(unittest.TestCase): def test_remove_share(self): frappe.share.add("Event", self.event.name, self.user, share=1) + frappe.set_user(self.user) self.assertTrue(self.event.has_permission("share")) + frappe.set_user("Administrator") frappe.share.remove("Event", self.event.name, self.user) + frappe.set_user(self.user) self.assertFalse(self.event.has_permission("share")) diff --git a/frappe/core/page/data_import_tool/importer.py b/frappe/core/page/data_import_tool/importer.py index 6b01b760c8..d4c95278f6 100644 --- a/frappe/core/page/data_import_tool/importer.py +++ b/frappe/core/page/data_import_tool/importer.py @@ -178,8 +178,6 @@ def upload(rows = None, submit_after_import=None, ignore_encoding_errors=False, check_data_length() make_column_map() - frappe.db.begin() - if overwrite==None: overwrite = params.get('overwrite') diff --git a/frappe/core/page/data_import_tool/test_exporter_fixtures.py b/frappe/core/page/data_import_tool/test_exporter_fixtures.py index 862268b00f..d617346739 100644 --- a/frappe/core/page/data_import_tool/test_exporter_fixtures.py +++ b/frappe/core/page/data_import_tool/test_exporter_fixtures.py @@ -7,251 +7,248 @@ from frappe.core.page.data_import_tool.data_import_tool import export_csv import unittest class TestDataImportFixtures(unittest.TestCase): - def setUp(self): - print "\nTeste for export explicit fixtures" - print "see fixtures csv test files in sites folder" - + pass + #start test for Custom Script def test_Custom_Script_fixture_simple(self): fixture = "Custom Script" path = frappe.scrub(fixture) + "_original_style.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Custom_Script_fixture_simple_name_equal_default(self): fixture = ["Custom Script", {"name":["Item-Client"]}] path = frappe.scrub(fixture[0]) + "_simple_name_equal_default.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Custom_Script_fixture_simple_name_equal(self): fixture = ["Custom Script", {"name":["Item-Client"],"op":"="}] path = frappe.scrub(fixture[0]) + "_simple_name_equal.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Custom_Script_fixture_simple_name_not_equal(self): fixture = ["Custom Script", {"name":["Item-Client"],"op":"!="}] path = frappe.scrub(fixture[0]) + "_simple_name_not_equal.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + #without [] around the name... def test_Custom_Script_fixture_simple_name_at_least_equal(self): fixture = ["Custom Script", {"name":"Item-Cli"}] path = frappe.scrub(fixture[0]) + "_simple_name_at_least_equal.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Custom_Script_fixture_multi_name_equal(self): fixture = ["Custom Script", {"name":["Item-Client", "Customer-Client"],"op":"="}] path = frappe.scrub(fixture[0]) + "_multi_name_equal.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Custom_Script_fixture_multi_name_not_equal(self): fixture = ["Custom Script", {"name":["Item-Client", "Customer-Client"],"op":"!="}] path = frappe.scrub(fixture[0]) + "_multi_name_not_equal.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Custom_Script_fixture_empty_object(self): fixture = ["Custom Script", {}] path = frappe.scrub(fixture[0]) + "_empty_object_should_be_all.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Custom_Script_fixture_just_list(self): fixture = ["Custom Script"] path = frappe.scrub(fixture[0]) + "_just_list_should_be_all.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + # Custom Script regular expression def test_Custom_Script_fixture_rex_no_flags(self): fixture = ["Custom Script", {"name":r"^[i|A]"}] path = frappe.scrub(fixture[0]) + "_rex_no_flags.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Custom_Script_fixture_rex_with_flags(self): fixture = ["Custom Script", {"name":r"^[i|A]", "flags":"L,M"}] path = frappe.scrub(fixture[0]) + "_rex_with_flags.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - - + + #start test for Custom Field def test_Custom_Field_fixture_simple(self): fixture = "Custom Field" path = frappe.scrub(fixture) + "_original_style.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Custom_Field_fixture_simple_name_equal_default(self): fixture = ["Custom Field", {"name":["Item-vat"]}] path = frappe.scrub(fixture[0]) + "_simple_name_equal_default.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Custom_Field_fixture_simple_name_equal(self): fixture = ["Custom Field", {"name":["Item-vat"],"op":"="}] path = frappe.scrub(fixture[0]) + "_simple_name_equal.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Custom_Field_fixture_simple_name_not_equal(self): fixture = ["Custom Field", {"name":["Item-vat"],"op":"!="}] path = frappe.scrub(fixture[0]) + "_simple_name_not_equal.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + #without [] around the name... def test_Custom_Field_fixture_simple_name_at_least_equal(self): fixture = ["Custom Field", {"name":"Item-va"}] path = frappe.scrub(fixture[0]) + "_simple_name_at_least_equal.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Custom_Field_fixture_multi_name_equal(self): fixture = ["Custom Field", {"name":["Item-vat", "Bin-vat"],"op":"="}] path = frappe.scrub(fixture[0]) + "_multi_name_equal.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Custom_Field_fixture_multi_name_not_equal(self): fixture = ["Custom Field", {"name":["Item-vat", "Bin-vat"],"op":"!="}] path = frappe.scrub(fixture[0]) + "_multi_name_not_equal.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Custom_Field_fixture_empty_object(self): fixture = ["Custom Field", {}] path = frappe.scrub(fixture[0]) + "_empty_object_should_be_all.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Custom_Field_fixture_just_list(self): fixture = ["Custom Field"] path = frappe.scrub(fixture[0]) + "_just_list_should_be_all.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + # Custom Field regular expression def test_Custom_Field_fixture_rex_no_flags(self): fixture = ["Custom Field", {"name":r"^[r|L]"}] path = frappe.scrub(fixture[0]) + "_rex_no_flags.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Custom_Field_fixture_rex_with_flags(self): fixture = ["Custom Field", {"name":r"^[i|A]", "flags":"L,M"}] path = frappe.scrub(fixture[0]) + "_rex_with_flags.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - - + + #start test for Doctype def test_Doctype_fixture_simple(self): fixture = "ToDo" path = "Doctype_" + frappe.scrub(fixture) + "_original_style_should_be_all.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Doctype_fixture_simple_name_equal_default(self): fixture = ["ToDo", {"name":["TDI00000008"]}] path = "Doctype_" + frappe.scrub(fixture[0]) + "_simple_name_equal_default.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Doctype_fixture_simple_name_equal(self): fixture = ["ToDo", {"name":["TDI00000002"],"op":"="}] path = "Doctype_" + frappe.scrub(fixture[0]) + "_simple_name_equal.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Doctype_simple_name_not_equal(self): fixture = ["ToDo", {"name":["TDI00000002"],"op":"!="}] path = "Doctype_" + frappe.scrub(fixture[0]) + "_simple_name_not_equal.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + #without [] around the name... def test_Doctype_fixture_simple_name_at_least_equal(self): fixture = ["ToDo", {"name":"TDI"}] path = "Doctype_" + frappe.scrub(fixture[0]) + "_simple_name_at_least_equal.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Doctype_multi_name_equal(self): fixture = ["ToDo", {"name":["TDI00000002", "TDI00000008"],"op":"="}] path = "Doctype_" + frappe.scrub(fixture[0]) + "_multi_name_equal.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Doctype_multi_name_not_equal(self): fixture = ["ToDo", {"name":["TDI00000002", "TDI00000008"],"op":"!="}] path = "Doctype_" + frappe.scrub(fixture[0]) + "_multi_name_not_equal.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Doctype_fixture_empty_object(self): fixture = ["ToDo", {}] path = "Doctype_" + frappe.scrub(fixture[0]) + "_empty_object_should_be_all.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Doctype_fixture_just_list(self): fixture = ["ToDo"] path = "Doctype_" + frappe.scrub(fixture[0]) + "_just_list_should_be_all.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + # Doctype regular expression def test_Doctype_fixture_rex_no_flags(self): fixture = ["ToDo", {"name":r"^TDi"}] path = "Doctype_" + frappe.scrub(fixture[0]) + "_rex_no_flags_should_be_all.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - + def test_Doctype_fixture_rex_with_flags(self): fixture = ["ToDo", {"name":r"^TDi", "flags":"L,M"}] path = "Doctype_" + frappe.scrub(fixture[0]) + "_rex_with_flags_should_be_none.csv" - print "teste done {}".format(path) + # print "teste done {}".format(path) export_csv(fixture, path) self.assertTrue(True) - - \ No newline at end of file + diff --git a/frappe/custom/doctype/customize_form/test_customize_form.py b/frappe/custom/doctype/customize_form/test_customize_form.py index 01590c6f08..54ab141b3b 100644 --- a/frappe/custom/doctype/customize_form/test_customize_form.py +++ b/frappe/custom/doctype/customize_form/test_customize_form.py @@ -43,11 +43,11 @@ class TestCustomizeForm(unittest.TestCase): d = self.get_customize_form("Event") self.assertEquals(d.doc_type, "Event") - self.assertEquals(len(d.get("fields")), 30) + self.assertEquals(len(d.get("fields")), 28) d = self.get_customize_form("User") self.assertEquals(d.doc_type, "User") - self.assertEquals(len(d.get("fields")), 48) + self.assertEquals(len(d.get("fields")), 50) self.assertEquals(d.get("fields")[-1].fieldname, "test_custom_field") self.assertEquals(d.get("fields", {"fieldname": "location"})[0].in_list_view, 1) diff --git a/frappe/defaults.py b/frappe/defaults.py index 6b108ce39d..be43e8140c 100644 --- a/frappe/defaults.py +++ b/frappe/defaults.py @@ -82,14 +82,8 @@ def set_default(key, value, parent, parenttype="__default"): :param value: Default value. :param parent: Usually, **User** to whom the default belongs. :param parenttype: [optional] default is `__default`.""" - if frappe.db.sql("""select defkey from `tabDefaultValue` where - defkey=%s and parent=%s """, (key, parent)): - # update - frappe.db.sql("""update `tabDefaultValue` set defvalue=%s, parenttype=%s - where parent=%s and defkey=%s""", (value, parenttype, parent, key)) - _clear_cache(parent) - else: - add_default(key, value, parent) + frappe.db.sql("""delete from `tabDefaultValue` where defkey=%s and parent=%s""", (key, parent)) + add_default(key, value, parent) def add_default(key, value, parent, parenttype=None): d = frappe.get_doc({ @@ -100,7 +94,7 @@ def add_default(key, value, parent, parenttype=None): "defkey": key, "defvalue": value }) - d.db_insert() + d.insert(ignore_permissions=True) _clear_cache(parent) def clear_default(key=None, value=None, parent=None, name=None, parenttype=None): @@ -154,6 +148,7 @@ def get_defaults_for(parent="__default"): """get all defaults""" defaults = frappe.cache().get_value("__defaults:" + parent) if defaults==None: + # sort descending because first default must get preceedence res = frappe.db.sql("""select defkey, defvalue from `tabDefaultValue` where parent = %s order by creation""", (parent,), as_dict=1) @@ -166,6 +161,7 @@ def get_defaults_for(parent="__default"): if d.defvalue not in defaults[d.defkey]: defaults[d.defkey].append(d.defvalue) + elif d.defvalue is not None: defaults[d.defkey] = d.defvalue diff --git a/frappe/desk/doctype/event/test_event.py b/frappe/desk/doctype/event/test_event.py index 20e26a397e..6fb49a2f84 100644 --- a/frappe/desk/doctype/event/test_event.py +++ b/frappe/desk/doctype/event/test_event.py @@ -11,37 +11,44 @@ import json from frappe.desk.doctype.event.event import get_events test_records = frappe.get_test_records('Event') +test_user = "test1@example.com" + class TestEvent(unittest.TestCase): # def setUp(self): - # user = frappe.get_doc("User", "test1@example.com") + # user = frappe.get_doc("User", test_user) # user.add_roles("Website Manager") + def tearDown(self): frappe.set_user("Administrator") def test_allowed_public(self): - frappe.set_user("test1@example.com") + frappe.set_user(test_user) doc = frappe.get_doc("Event", frappe.db.get_value("Event", {"subject":"_Test Event 1"})) self.assertTrue(frappe.has_permission("Event", doc=doc)) def test_not_allowed_private(self): - frappe.set_user("test1@example.com") + frappe.set_user(test_user) doc = frappe.get_doc("Event", frappe.db.get_value("Event", {"subject":"_Test Event 2"})) self.assertFalse(frappe.has_permission("Event", doc=doc)) def test_allowed_private_if_in_event_user(self): - frappe.set_user("test1@example.com") - doc = frappe.get_doc("Event", frappe.db.get_value("Event", {"subject":"_Test Event 3"})) + name = frappe.db.get_value("Event", {"subject":"_Test Event 3"}) + frappe.share.add("Event", name, test_user, "read") + frappe.set_user(test_user) + doc = frappe.get_doc("Event", name) self.assertTrue(frappe.has_permission("Event", doc=doc)) + frappe.set_user("Administrator") + frappe.share.remove("Event", name, test_user) def test_event_list(self): - frappe.set_user("test1@example.com") + frappe.set_user(test_user) res = frappe.get_list("Event", filters=[["Event", "subject", "like", "_Test Event%"]], fields=["name", "subject"]) - self.assertEquals(len(res), 2) + self.assertEquals(len(res), 1) subjects = [r.subject for r in res] self.assertTrue("_Test Event 1" in subjects) - self.assertTrue("_Test Event 3" in subjects) + self.assertFalse("_Test Event 3" in subjects) self.assertFalse("_Test Event 2" in subjects) def test_revert_logic(self): @@ -74,7 +81,7 @@ class TestEvent(unittest.TestCase): # add another one add({ - "assign_to": "test1@example.com", + "assign_to": test_user, "doctype": "Event", "name": ev.name, "description": "Test Assignment" @@ -82,11 +89,11 @@ class TestEvent(unittest.TestCase): ev = frappe.get_doc("Event", ev.name) - self.assertEquals(set(json.loads(ev._assign)), set(["test@example.com", "test1@example.com"])) + self.assertEquals(set(json.loads(ev._assign)), set(["test@example.com", test_user])) # close an assignment todo = frappe.get_doc("ToDo", {"reference_type": ev.doctype, "reference_name": ev.name, - "owner": "test1@example.com"}) + "owner": test_user}) todo.status = "Closed" todo.save() diff --git a/frappe/desk/doctype/event/test_records.json b/frappe/desk/doctype/event/test_records.json index b8afcfc8ca..889dd79809 100644 --- a/frappe/desk/doctype/event/test_records.json +++ b/frappe/desk/doctype/event/test_records.json @@ -16,8 +16,8 @@ "starts_on": "2014-01-01", "subject":"_Test Event 3", "event_type": "Private", - "users": [{ - "person": "test1@example.com" - }] + "roles": [ + {"role": "System Manager"} + ] } ] diff --git a/frappe/email/bulk.py b/frappe/email/bulk.py index 153d3e8760..761453e534 100644 --- a/frappe/email/bulk.py +++ b/frappe/email/bulk.py @@ -29,7 +29,7 @@ def send(recipients=None, sender=None, doctype='User', email_field='email', # No limit for own email settings smtp_server = SMTPServer() if smtp_server.email_account and not getattr(smtp_server.email_account, - "from_site_config", False): + "from_site_config", False) or frappe.flags.in_test: monthly_bulk_mail_limit = frappe.conf.get('monthly_bulk_mail_limit') or 500 if (this_month + len(recipients)) > monthly_bulk_mail_limit: diff --git a/frappe/email/doctype/email_account/test_email_account.py b/frappe/email/doctype/email_account/test_email_account.py index 7d0f78b210..a6c9ec6258 100644 --- a/frappe/email/doctype/email_account/test_email_account.py +++ b/frappe/email/doctype/email_account/test_email_account.py @@ -47,8 +47,8 @@ class TestEmailAccount(unittest.TestCase): make(subject = "test-mail-000", content="test mail 000", recipients="test_receiver@example.com", send_email=True, sender="test_sender@example.com") - sent_mail = email.message_from_string(frappe.flags.sent_mail) - self.assertTrue("test-mail-000" in sent_mail.get("Subject")) + mail = email.message_from_string(frappe.get_last_doc("Bulk Email").message) + self.assertTrue("test-mail-000" in mail.get("Subject")) def test_sendmail(self): frappe.flags.sent_mail = None @@ -64,8 +64,7 @@ class TestEmailAccount(unittest.TestCase): content="test mail 001", subject="test-mail-002", doctype="Email Account", name="_Test Email Account 1", print_format="Standard", send_email=True) - sent_mail = email.message_from_string(frappe.flags.sent_mail) - + sent_mail = email.message_from_string(frappe.get_last_doc("Bulk Email").message) self.assertTrue("test-mail-002" in sent_mail.get("Subject")) def test_threading(self): @@ -77,8 +76,7 @@ class TestEmailAccount(unittest.TestCase): recipients="test_receiver@example.com", sender="test@example.com", send_email=True) - sent_mail = email.message_from_string(frappe.flags.sent_mail) - + sent_mail = email.message_from_string(frappe.get_last_doc("Bulk Email").message) with open(os.path.join(os.path.dirname(__file__), "test_mails", "reply-1.raw"), "r") as f: raw = f.read() raw = raw.replace("<-- in-reply-to -->", sent_mail.get("Message-Id")) diff --git a/frappe/email/receive.py b/frappe/email/receive.py index 4c41f3a9a3..a83da530bd 100644 --- a/frappe/email/receive.py +++ b/frappe/email/receive.py @@ -200,7 +200,7 @@ class Email: def set_content_and_type(self): self.content, self.content_type = '[Blank Email]', 'text/plain' if self.text_content: - self.content, self.content_type = EmailReplyParser.parse_reply(self.text_content) + self.content, self.content_type = EmailReplyParser.parse_reply(self.text_content), 'text/plain' else: self.content, self.content_type = self.html_content, 'text/html' diff --git a/frappe/email/smtp.py b/frappe/email/smtp.py index 4831a72703..f8bd1bb5d9 100644 --- a/frappe/email/smtp.py +++ b/frappe/email/smtp.py @@ -11,14 +11,14 @@ from frappe import _ def send(email, as_bulk=False, append_to=None): """send the message or add it to Outbox Email""" - if frappe.flags.mute_emails or frappe.conf.get("mute_emails") or False: - frappe.msgprint(_("Emails are muted")) - return - if frappe.flags.in_test: frappe.flags.sent_mail = email.as_string() return + if frappe.flags.mute_emails or frappe.conf.get("mute_emails") or False: + frappe.msgprint(_("Emails are muted")) + return + try: smtpserver = SMTPServer(append_to=append_to) if hasattr(smtpserver, "always_use_login_id_as_sender") and \ diff --git a/frappe/model/db_query.py b/frappe/model/db_query.py index 3960d36e11..8733bb1fa5 100644 --- a/frappe/model/db_query.py +++ b/frappe/model/db_query.py @@ -8,7 +8,7 @@ import frappe, json import frappe.defaults import frappe.share import frappe.permissions -from frappe.utils import flt +from frappe.utils import flt, cint from frappe import _ class DatabaseQuery(object): @@ -22,8 +22,8 @@ class DatabaseQuery(object): self.flags = frappe._dict() def execute(self, query=None, fields=None, filters=None, or_filters=None, - docstatus=None, group_by=None, order_by=None, limit_start=0, - limit_page_length=20, as_list=False, with_childnames=False, debug=False, + docstatus=None, group_by=None, order_by=None, limit_start=False, + limit_page_length=False, as_list=False, with_childnames=False, debug=False, ignore_permissions=False, user=None): if not ignore_permissions and not frappe.has_permission(self.doctype, "read", user=user): raise frappe.PermissionError, self.doctype @@ -35,8 +35,8 @@ class DatabaseQuery(object): self.docstatus = docstatus or [] self.group_by = group_by self.order_by = order_by - self.limit_start = int(limit_start) if limit_start else 0 - self.limit_page_length = int(limit_page_length) if limit_page_length else 20 + self.limit_start = 0 if (limit_start is False) else cint(limit_start) + self.limit_page_length = 20 if (limit_page_length is False) else cint(limit_page_length) self.with_childnames = with_childnames self.debug = debug self.as_list = as_list diff --git a/frappe/model/document.py b/frappe/model/document.py index bb866785ce..a2e7d9bc65 100644 --- a/frappe/model/document.py +++ b/frappe/model/document.py @@ -126,14 +126,14 @@ class Document(BaseDocument): if not self.has_permission(permtype): self.raise_no_permission_to(permlabel or permtype) - def has_permission(self, permtype="read"): + def has_permission(self, permtype="read", verbose=False): """Call `frappe.has_permission` if `self.flags.ignore_permissions` is not set. :param permtype: one of `read`, `write`, `submit`, `cancel`, `delete`""" if self.flags.ignore_permissions: return True - return frappe.has_permission(self.doctype, permtype, self) + return frappe.has_permission(self.doctype, permtype, self, verbose=verbose) def raise_no_permission_to(self, perm_type): """Raise `frappe.PermissionError`.""" diff --git a/frappe/permissions.py b/frappe/permissions.py index d8765b1222..f543b0bf16 100644 --- a/frappe/permissions.py +++ b/frappe/permissions.py @@ -16,28 +16,33 @@ def check_admin_or_system_manager(user=None): if ("System Manager" not in frappe.get_roles(user)) and (user!="Administrator"): frappe.throw(_("Not permitted"), frappe.PermissionError) -def has_permission(doctype, ptype="read", doc=None, verbose=True, user=None): +def has_permission(doctype, ptype="read", doc=None, verbose=False, user=None): """check if user has permission""" if not user: user = frappe.session.user if frappe.is_table(doctype): + if verbose: print "Table type, always true" return True meta = frappe.get_meta(doctype) if ptype=="submit" and not cint(meta.is_submittable): + if verbose: print "Not submittable" return False if ptype=="import" and not cint(meta.allow_import): + if verbose: print "Not importable" return False if user=="Administrator": + if verbose: print "Administrator" return True def false_if_not_shared(): if doc and ptype in ("read", "write", "share"): shared = frappe.share.get_shared(meta.name, user, [ptype]) if doc.name in shared: + if verbose: print "Shared" return True return False @@ -53,11 +58,14 @@ def has_permission(doctype, ptype="read", doc=None, verbose=True, user=None): if role_permissions["apply_user_permissions"].get(ptype): if not user_has_permission(doc, verbose=verbose, user=user, user_permission_doctypes=role_permissions.get("user_permission_doctypes")): + if verbose: print "No user permission" return false_if_not_shared() if not has_controller_permissions(doc, ptype, user=user): + if verbose: print "No controller permission" return false_if_not_shared() + if verbose: print "Has Role" return True def get_doc_permissions(doc, verbose=False, user=None): diff --git a/frappe/test_runner.py b/frappe/test_runner.py index 98b9a654ad..6103aff615 100644 --- a/frappe/test_runner.py +++ b/frappe/test_runner.py @@ -28,6 +28,8 @@ def main(app=None, module=None, doctype=None, verbose=False, tests=(), force=Fal for fn in frappe.get_hooks("before_tests", app_name=app): frappe.get_attr(fn)() + set_test_email_config() + if doctype: ret = run_tests_for_doctype(doctype, verbose=verbose, tests=tests, force=force) elif module: @@ -42,6 +44,15 @@ def main(app=None, module=None, doctype=None, verbose=False, tests=(), force=Fal return ret +def set_test_email_config(): + frappe.conf.update({ + "auto_email_id": "test@example.com", + "mail_server": "smtp.example.com", + "mail_login": "test@example.com", + "mail_password": "test", + "admin_password": "admin" + }) + def run_all_tests(app=None, verbose=False): import os diff --git a/frappe/tests/test_data_import.py b/frappe/tests/test_data_import.py index 7e8bfe6803..f28f08d6ff 100644 --- a/frappe/tests/test_data_import.py +++ b/frappe/tests/test_data_import.py @@ -79,8 +79,8 @@ class TestDataImport(unittest.TestCase): content[-1][2] = "__Test Event" content[-1][3] = "Private" content[-1][4] = "2014-01-01 10:00:00.000000" - content[-1][content[15].index("person")] = "Administrator" + content[-1][content[15].index("role")] = "System Manager" importer.upload(content) ev = frappe.get_doc("Event", {"subject":"__Test Event"}) - self.assertTrue("Administrator" in [d.person for d in ev.users]) + self.assertTrue("System Manager" in [d.role for d in ev.roles]) diff --git a/frappe/tests/test_document.py b/frappe/tests/test_document.py index c8c6d70beb..ad1812f0a6 100644 --- a/frappe/tests/test_document.py +++ b/frappe/tests/test_document.py @@ -45,9 +45,9 @@ class TestDocument(unittest.TestCase): "subject":"test-doc-test-event 2", "starts_on": "2014-01-01", "event_type": "Public", - "users": [ + "roles": [ { - "person": "Administrator" + "role": "System Manager" } ] }) @@ -57,7 +57,7 @@ class TestDocument(unittest.TestCase): "test-doc-test-event 2") d1 = frappe.get_doc("Event", d.name) - self.assertTrue(d1.users[0].person, "Administrator") + self.assertEquals(d1.roles[0].role, "System Manager") def test_update(self): d = self.test_insert() diff --git a/frappe/tests/test_email.py b/frappe/tests/test_email.py index de3ae5083e..1828db6973 100644 --- a/frappe/tests/test_email.py +++ b/frappe/tests/test_email.py @@ -2,7 +2,6 @@ # MIT License. See license.txt from __future__ import unicode_literals -import os, sys import unittest, frappe @@ -17,7 +16,7 @@ class TestEmail(unittest.TestCase): def test_send(self): from frappe.email import sendmail - #sendmail('test@example.com', subject='Test Mail', msg="Test Content") + sendmail('test@example.com', subject='Test Mail', msg="Test Content") def test_bulk(self): from frappe.email.bulk import send @@ -64,7 +63,7 @@ class TestEmail(unittest.TestCase): self.assertTrue('Unsubscribe' in bulk[0]['message']) def test_bulk_limit(self): - from frappe.email.bulk import unsubscribe, send, BulkLimitCrossedError + from frappe.email.bulk import send, BulkLimitCrossedError self.assertRaises(BulkLimitCrossedError, send, recipients=['test@example.com']*1000, sender="admin@example.com",