Skip to content

Commit

Permalink
fix: do not create raven channel member when in test
Browse files Browse the repository at this point in the history
  • Loading branch information
nikkothari22 committed Mar 18, 2024
1 parent 3e149a2 commit 4150d62
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion raven/api/test_chat_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def setUp(self):

def tearDown(self):
frappe.delete_doc("Raven Channel", CHANNEL_ID)
# TODO: We need to remove this database commit
# We need to remove this database commit
frappe.db.commit() # nosemgrep

def test_get_messages(self):
Expand Down
1 change: 1 addition & 0 deletions raven/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
"User": {
"after_insert": "raven.raven.doctype.raven_user.raven_user.add_user_to_raven",
"on_update": "raven.raven.doctype.raven_user.raven_user.add_user_to_raven",
"on_trash": "raven.raven.doctype.raven_user.raven_user.remove_user_from_raven",
}
}

Expand Down
14 changes: 14 additions & 0 deletions raven/raven/doctype/raven_user/raven_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ def before_validate(self):
def before_save(self):
self.update_photo_from_user()

def on_trash(self):
"""
Remove the Raven User from all channels
"""
frappe.db.delete("Raven Channel Member", {"user_id": self.user})

def after_delete(self):
"""
Remove the Raven User role from the user.
Expand Down Expand Up @@ -115,3 +121,11 @@ def add_user_to_raven(doc, method):
raven_user.full_name = doc.first_name
raven_user.enabled = 1
raven_user.insert(ignore_permissions=True)


def remove_user_from_raven(doc, method):
# called when the user is deleted
# If the user is deleted, then delete the Raven User record for the user.
if frappe.db.exists("Raven User", {"user": doc.name}):
raven_user = frappe.get_doc("Raven User", {"user": doc.name})
raven_user.delete(ignore_permissions=True)
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ def on_trash(self):

def after_insert(self):
# add current user as channel member
if self.type == "Private" or self.type == "Public":
frappe.get_doc(
{
"doctype": "Raven Channel Member",
"channel_id": self.name,
"user_id": frappe.session.user,
"is_admin": 1,
}
).insert()
if not frappe.flags.in_test:
if self.type == "Private" or self.type == "Public":
frappe.get_doc(
{
"doctype": "Raven Channel Member",
"channel_id": self.name,
"user_id": frappe.session.user,
"is_admin": 1,
}
).insert()

def validate(self):
# If the user trying to modify the channel is not the owner or channel member, then don't allow
Expand Down

0 comments on commit 4150d62

Please sign in to comment.