Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
New events for activating / deactivating users (#10136)
Tomasz Szymański5af266aa95f9
diff --git a/saleor/account/__init__.py b/saleor/account/__init__.pyindex 0673a9240c..7b702e499f 100755--- a/saleor/account/__init__.py+++ b/saleor/account/__init__.py@@ -6,6 +6,8 @@ class CustomerEvents:# Account related eventsACCOUNT_CREATED = "account_created"+ ACCOUNT_ACTIVATED = "account_activated"+ ACCOUNT_DEACTIVATED = "account_deactivated"PASSWORD_RESET_LINK_SENT = "password_reset_link_sent"PASSWORD_RESET = "password_reset"PASSWORD_CHANGED = "password_changed"@@ -25,6 +27,8 @@ class CustomerEvents:CHOICES = [(ACCOUNT_CREATED, "The account was created"),+ (ACCOUNT_ACTIVATED, "The account was activated"),+ (ACCOUNT_DEACTIVATED, "The account was deactivated"),(PASSWORD_RESET_LINK_SENT, "Password reset link was sent to the customer"),(PASSWORD_RESET, "The account password was reset"),(diff --git a/saleor/account/events.py b/saleor/account/events.pyindex 6ec98fd991..69eb3bb288 100644--- a/saleor/account/events.py+++ b/saleor/account/events.py@@ -14,6 +14,32 @@ def customer_account_created_event(*, user: User) -> Optional[CustomerEvent]:return CustomerEvent.objects.create(user=user, type=CustomerEvents.ACCOUNT_CREATED)+def customer_account_activated_event(+ *, staff_user: UserType, app: AppType, account_id: int+) -> Optional[CustomerEvent]:+ if not user_is_valid(staff_user):+ staff_user = None+ return CustomerEvent.objects.create(+ user=staff_user,+ app=app,+ type=CustomerEvents.ACCOUNT_ACTIVATED,+ parameters={"account_id": account_id},+ )+++def customer_account_deactivated_event(+ *, staff_user: UserType, app: AppType, account_id: int+) -> Optional[CustomerEvent]:+ if not user_is_valid(staff_user):+ staff_user = None+ return CustomerEvent.objects.create(+ user=staff_user,+ app=app,+ type=CustomerEvents.ACCOUNT_DEACTIVATED,+ parameters={"account_id": account_id},+ )++def customer_password_reset_link_sent_event(*, user_id: int) -> Optional[CustomerEvent]:return CustomerEvent.objects.create(user_id=user_id, type=CustomerEvents.PASSWORD_RESET_LINK_SENTdiff --git a/saleor/account/migrations/0066_alter_customerevent_type.py b/saleor/account/migrations/0066_alter_customerevent_type.pynew file mode 100644index 0000000000..a78d42e3fe--- /dev/null+++ b/saleor/account/migrations/0066_alter_customerevent_type.py@@ -0,0 +1,37 @@+# Generated by Django 3.2.14 on 2022-07-15 07:15++from django.db import migrations, models+++class Migration(migrations.Migration):++ dependencies = [+ ("account", "0065_address_warehouse_address_search_gin"),+ ]++ operations = [+ migrations.AlterField(+ model_name="customerevent",+ name="type",+ field=models.CharField(+ choices=[+ ("ACCOUNT_CREATED", "account_created"),+ ("ACCOUNT_ACTIVATED", "account_activated"),+ ("ACCOUNT_DEACTIVATED", "account_deactivated"),+ ("PASSWORD_RESET_LINK_SENT", "password_reset_link_sent"),+ ("PASSWORD_RESET", "password_reset"),+ ("EMAIL_CHANGED_REQUEST", "email_changed_request"),+ ("PASSWORD_CHANGED", "password_changed"),+ ("EMAIL_CHANGED", "email_changed"),+ ("PLACED_ORDER", "placed_order"),+ ("NOTE_ADDED_TO_ORDER", "note_added_to_order"),+ ("DIGITAL_LINK_DOWNLOADED", "digital_link_downloaded"),+ ("CUSTOMER_DELETED", "customer_deleted"),+ ("NAME_ASSIGNED", "name_assigned"),+ ("EMAIL_ASSIGNED", "email_assigned"),+ ("NOTE_ADDED", "note_added"),+ ],+ max_length=255,+ ),+ ),+ ]diff --git a/saleor/graphql/account/mutations/staff.py b/saleor/graphql/account/mutations/staff.pyindex 6395885460..defc82ac30 100644--- a/saleor/graphql/account/mutations/staff.py+++ b/saleor/graphql/account/mutations/staff.py@@ -112,6 +112,8 @@ class CustomerUpdate(CustomerCreate):# Compare the datahas_new_name = old_instance.get_full_name() != new_fullnamehas_new_email = old_instance.email != new_email+ was_activated = not old_instance.is_active and new_instance.is_active+ was_deactivated = old_instance.is_active and not new_instance.is_active# Generate the events accordinglyif has_new_email:@@ -124,6 +126,18 @@ class CustomerUpdate(CustomerCreate):account_events.assigned_name_to_a_customer_event(staff_user=staff_user, app=app, new_name=new_fullname)+ if was_activated:+ account_events.customer_account_activated_event(+ staff_user=info.context.user,+ app=info.context.app,+ account_id=old_instance.id,+ )+ if was_deactivated:+ account_events.customer_account_deactivated_event(+ staff_user=info.context.user,+ app=info.context.app,+ account_id=old_instance.id,+ )@classmethoddef perform_mutation(cls, _root, info, **data):diff --git a/saleor/graphql/account/tests/test_account.py b/saleor/graphql/account/tests/test_account.pyindex f2d6c81438..db16452a60 100644--- a/saleor/graphql/account/tests/test_account.py+++ b/saleor/graphql/account/tests/test_account.py@@ -1728,11 +1728,19 @@ def test_customer_update(assert data["user"]["languageCode"] == "PL"assert not data["user"]["isActive"]- # The name was changed, an event should have been triggered- name_changed_event = account_events.CustomerEvent.objects.get()+ (+ name_changed_event,+ deactivated_event,+ ) = account_events.CustomerEvent.objects.order_by("pk")+assert name_changed_event.type == account_events.CustomerEvents.NAME_ASSIGNEDassert name_changed_event.user.pk == staff_user.pkassert name_changed_event.parameters == {"message": customer.get_full_name()}++ assert deactivated_event.type == account_events.CustomerEvents.ACCOUNT_DEACTIVATED+ assert deactivated_event.user.pk == staff_user.pk+ assert deactivated_event.parameters == {"account_id": customer_user.id}+customer_user.refresh_from_db()assert (generate_address_search_document_value(billing_address)@@ -1789,6 +1797,107 @@ def test_customer_update_generates_event_when_changing_email(assert email_changed_event.parameters == {"message": "[email protected]"}+UPDATE_CUSTOMER_IS_ACTIVE_MUTATION = """+ mutation UpdateCustomer(+ $id: ID!, $isActive: Boolean) {+ customerUpdate(id: $id, input: {+ isActive: $isActive,+ }) {+ errors {+ field+ message+ }+ }+ }+"""+++def test_customer_update_generates_event_when_deactivating(+ staff_api_client, staff_user, customer_user, address, permission_manage_users+):+ query = UPDATE_CUSTOMER_IS_ACTIVE_MUTATION++ user_id = graphene.Node.to_global_id("User", customer_user.id)++ variables = {"id": user_id, "isActive": False}+ staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_users]+ )++ account_deactivated_event = account_events.CustomerEvent.objects.get()+ assert (+ account_deactivated_event.type+ == account_events.CustomerEvents.ACCOUNT_DEACTIVATED+ )+ assert account_deactivated_event.user.pk == staff_user.pk+ assert account_deactivated_event.parameters == {"account_id": customer_user.id}+++def test_customer_update_generates_event_when_activating(+ staff_api_client, staff_user, customer_user, address, permission_manage_users+):+ customer_user.is_active = False+ customer_user.save(update_fields=["is_active"])++ query = UPDATE_CUSTOMER_IS_ACTIVE_MUTATION++ user_id = graphene.Node.to_global_id("User", customer_user.id)++ variables = {"id": user_id, "isActive": True}+ staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_users]+ )++ account_activated_event = account_events.CustomerEvent.objects.get()+ assert (+ account_activated_event.type == account_events.CustomerEvents.ACCOUNT_ACTIVATED+ )+ assert account_activated_event.user.pk == staff_user.pk+ assert account_activated_event.parameters == {"account_id": customer_user.id}+++def test_customer_update_generates_event_when_deactivating_as_app(+ app_api_client, staff_user, customer_user, address, permission_manage_users+):+ query = UPDATE_CUSTOMER_IS_ACTIVE_MUTATION++ user_id = graphene.Node.to_global_id("User", customer_user.id)++ variables = {"id": user_id, "isActive": False}+ app_api_client.post_graphql(query, variables, permissions=[permission_manage_users])++ account_deactivated_event = account_events.CustomerEvent.objects.get()+ assert (+ account_deactivated_event.type+ == account_events.CustomerEvents.ACCOUNT_DEACTIVATED+ )+ assert account_deactivated_event.user is None+ assert account_deactivated_event.app.pk == app_api_client.app.pk+ assert account_deactivated_event.parameters == {"account_id": customer_user.id}+++def test_customer_update_generates_event_when_activating_as_app(+ app_api_client, staff_user, customer_user, address, permission_manage_users+):+ customer_user.is_active = False+ customer_user.save(update_fields=["is_active"])++ query = UPDATE_CUSTOMER_IS_ACTIVE_MUTATION++ user_id = graphene.Node.to_global_id("User", customer_user.id)++ variables = {"id": user_id, "isActive": True}+ app_api_client.post_graphql(query, variables, permissions=[permission_manage_users])++ account_activated_event = account_events.CustomerEvent.objects.get()+ assert (+ account_activated_event.type == account_events.CustomerEvents.ACCOUNT_ACTIVATED+ )+ assert account_activated_event.user is None+ assert account_activated_event.app.pk == app_api_client.app.pk+ assert account_activated_event.parameters == {"account_id": customer_user.id}++def test_customer_update_without_any_changes_generates_no_event(staff_api_client, customer_user, address, permission_manage_users):diff --git a/saleor/graphql/account/tests/test_account_events.py b/saleor/graphql/account/tests/test_account_events.pyindex 4307bebe78..ddec806d3b 100644--- a/saleor/graphql/account/tests/test_account_events.py+++ b/saleor/graphql/account/tests/test_account_events.py@@ -101,6 +101,54 @@ def test_account_event_customer_account_was_created(assert expected_data == received_data+def test_account_event_customer_account_was_activated(+ staff_api_client, staff_user, permission_manage_staff, permission_manage_apps+):+ event = account_events.customer_account_activated_event(+ staff_user=staff_user, app=None, account_id=7+ )+ expected_data = {+ "id": _model_to_node_id(event),+ "user": {"id": _model_to_node_id(staff_user)},+ "app": None,+ "count": None,+ "message": None,+ "order": None,+ "orderLine": None,+ "type": account_events.CustomerEvents.ACCOUNT_ACTIVATED.upper(),+ }++ received_data = _get_event_from_graphql(+ staff_api_client, staff_user, (permission_manage_staff, permission_manage_apps)+ )++ assert expected_data == received_data+++def test_account_event_customer_account_was_deactivated(+ staff_api_client, staff_user, permission_manage_staff, permission_manage_apps+):+ event = account_events.customer_account_activated_event(+ staff_user=staff_user, app=None, account_id=7+ )+ expected_data = {+ "id": _model_to_node_id(event),+ "user": {"id": _model_to_node_id(staff_user)},+ "app": None,+ "count": None,+ "message": None,+ "order": None,+ "orderLine": None,+ "type": account_events.CustomerEvents.ACCOUNT_ACTIVATED.upper(),+ }++ received_data = _get_event_from_graphql(+ staff_api_client, staff_user, (permission_manage_staff, permission_manage_apps)+ )++ assert expected_data == received_data++def test_account_event_sent_password_reset_email_to_customer_event(staff_api_client, customer_user, permission_manage_users, permission_manage_apps):diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex d9837bef73..f780c70471 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -9276,6 +9276,8 @@ type CustomerEvent implements Node {"""An enumeration."""enum CustomerEventsEnum {ACCOUNT_CREATED+ ACCOUNT_ACTIVATED+ ACCOUNT_DEACTIVATEDPASSWORD_RESET_LINK_SENTPASSWORD_RESETEMAIL_CHANGED_REQUEST