openwisp / openwisp-users

Implementation of user management and multi-tenancy for OpenWISP
https://openwisp.io/docs/dev/users/
BSD 3-Clause "New" or "Revised" License
163 stars 74 forks source link

[fix] Invalidate org membership cache when organization is_active flag is changed #357 #379

Closed kaushikaryan04 closed 5 months ago

kaushikaryan04 commented 5 months ago

When the status ( is_active ) of an organization changes it now invalidates the cache of all the organizationsusers.

Fixes https://github.com/openwisp/openwisp-users/issues/357.

kaushikaryan04 commented 5 months ago

I added the suggested changes but the some tests are failing as the no of running queries is one more than expected

FAIL: test_organization_slug_post_custom_validation_api (openwisp_users.tests.test_api.test_api.TestUsersApi) [Expecting 6 SQL queries]
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/aryankaushik/Desktop/openwisp_users_main/env/lib/python3.9/site-packages/openwisp_utils/tests.py", line 148, in __exit__
    self.test_case.assertEqual(
AssertionError: 7 != 6 : 7 queries executed, 6 expected
Captured queries were:
1. SELECT "openwisp_users_user"."password", "openwisp_users_user"."last_login", "openwisp_users_user"."is_superuser", "openwisp_users_user"."username", "openwisp_users_user"."first_name", "openwisp_users_user"."last_name", "openwisp_users_user"."is_staff", "openwisp_users_user"."is_active", "openwisp_users_user"."date_joined", "openwisp_users_user"."id", "openwisp_users_user"."email", "openwisp_users_user"."bio", "openwisp_users_user"."url", "openwisp_users_user"."company", "openwisp_users_user"."location", "openwisp_users_user"."phone_number", "openwisp_users_user"."birth_date", "openwisp_users_user"."notes", "openwisp_users_user"."language", "openwisp_users_user"."password_updated" FROM "openwisp_users_user" WHERE "openwisp_users_user"."id" = 'dc2f49d54db24f2cb4e09102804fe994' LIMIT 21
2. SELECT "openwisp_users_organization"."name", "openwisp_users_organization"."is_active", "openwisp_users_organization"."created", "openwisp_users_organization"."modified", "openwisp_users_organization"."slug", "openwisp_users_organization"."id", "openwisp_users_organization"."description", "openwisp_users_organization"."email", "openwisp_users_organization"."url" FROM "openwisp_users_organization" WHERE "openwisp_users_organization"."slug" = 'test-org' ORDER BY "openwisp_users_organization"."name" ASC LIMIT 1
3. SELECT 1 AS "a" FROM "openwisp_users_organization" WHERE "openwisp_users_organization"."slug" = 'test-org' LIMIT 1
4. SELECT 1 AS "a" FROM "openwisp_users_organization" WHERE "openwisp_users_organization"."id" = '6854249477df4b6dbd7fb2fd49cd5d94' LIMIT 1
5. SELECT "openwisp_users_organization"."name", "openwisp_users_organization"."is_active", "openwisp_users_organization"."created", "openwisp_users_organization"."modified", "openwisp_users_organization"."slug", "openwisp_users_organization"."id", "openwisp_users_organization"."description", "openwisp_users_organization"."email", "openwisp_users_organization"."url" FROM "openwisp_users_organization" WHERE "openwisp_users_organization"."id" = '16302821e4cc46d4814f1fdc679c4059' LIMIT 21
6. SELECT "openwisp_users_organization"."name", "openwisp_users_organization"."is_active", "openwisp_users_organization"."created", "openwisp_users_organization"."modified", "openwisp_users_organization"."slug", "openwisp_users_organization"."id", "openwisp_users_organization"."description", "openwisp_users_organization"."email", "openwisp_users_organization"."url" FROM "openwisp_users_organization" WHERE (NOT ("openwisp_users_organization"."id" = '16302821e4cc46d4814f1fdc679c4059') AND "openwisp_users_organization"."slug" = 'test-org') ORDER BY "openwisp_users_organization"."name" ASC
7. INSERT INTO "openwisp_users_organization" ("name", "is_active", "created", "modified", "slug", "id", "description", "email", "url") VALUES ('test-org', 1, '2024-05-27 17:41:05.471960', '2024-05-27 17:41:05.472193', 'test-org', '16302821e4cc46d4814f1fdc679c4059', '', '', '')

======================================================================
FAIL: test_remove_organization_owner_api (openwisp_users.tests.test_api.test_api.TestUsersApi) [Expecting 11 SQL queries]
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/aryankaushik/Desktop/openwisp_users_main/env/lib/python3.9/site-packages/openwisp_utils/tests.py", line 148, in __exit__
    self.test_case.assertEqual(
AssertionError: 12 != 11 : 12 queries executed, 11 expected
Captured queries were:
1. SELECT "openwisp_users_user"."password", "openwisp_users_user"."last_login", "openwisp_users_user"."is_superuser", "openwisp_users_user"."username", "openwisp_users_user"."first_name", "openwisp_users_user"."last_name", "openwisp_users_user"."is_staff", "openwisp_users_user"."is_active", "openwisp_users_user"."date_joined", "openwisp_users_user"."id", "openwisp_users_user"."email", "openwisp_users_user"."bio", "openwisp_users_user"."url", "openwisp_users_user"."company", "openwisp_users_user"."location", "openwisp_users_user"."phone_number", "openwisp_users_user"."birth_date", "openwisp_users_user"."notes", "openwisp_users_user"."language", "openwisp_users_user"."password_updated" FROM "openwisp_users_user" WHERE "openwisp_users_user"."id" = '7d5f9551f23d457b96c0a5dfc0d32cde' LIMIT 21
2. SELECT "openwisp_users_organization"."name", "openwisp_users_organization"."is_active", "openwisp_users_organization"."created", "openwisp_users_organization"."modified", "openwisp_users_organization"."slug", "openwisp_users_organization"."id", "openwisp_users_organization"."description", "openwisp_users_organization"."email", "openwisp_users_organization"."url" FROM "openwisp_users_organization" WHERE "openwisp_users_organization"."id" = '1c1754ecbcf44722ac045f5141f33348' LIMIT 21
3. SELECT 1 AS "a" FROM "openwisp_users_organizationowner" WHERE "openwisp_users_organizationowner"."organization_id" = '1c1754ecbcf44722ac045f5141f33348' LIMIT 1
4. SELECT 1 AS "a" FROM "openwisp_users_organizationowner" WHERE "openwisp_users_organizationowner"."organization_id" = '1c1754ecbcf44722ac045f5141f33348' LIMIT 1
5. SELECT "openwisp_users_organizationowner"."created", "openwisp_users_organizationowner"."modified", "openwisp_users_organizationowner"."id", "openwisp_users_organizationowner"."organization_user_id", "openwisp_users_organizationowner"."organization_id" FROM "openwisp_users_organizationowner" WHERE "openwisp_users_organizationowner"."organization_id" = '1c1754ecbcf44722ac045f5141f33348' ORDER BY "openwisp_users_organizationowner"."id" ASC LIMIT 1
6. DELETE FROM "openwisp_users_organizationowner" WHERE "openwisp_users_organizationowner"."id" IN ('5ab96a836f684456aeb2883004219aa9')
7. SELECT "openwisp_users_organizationuser"."created", "openwisp_users_organizationuser"."modified", "openwisp_users_organizationuser"."is_admin", "openwisp_users_organizationuser"."id", "openwisp_users_organizationuser"."user_id", "openwisp_users_organizationuser"."organization_id" FROM "openwisp_users_organizationuser" WHERE "openwisp_users_organizationuser"."id" = 'af21273fe11346a69838f9b0ad14c77a' LIMIT 21
8. SELECT "openwisp_users_user"."password", "openwisp_users_user"."last_login", "openwisp_users_user"."is_superuser", "openwisp_users_user"."username", "openwisp_users_user"."first_name", "openwisp_users_user"."last_name", "openwisp_users_user"."is_staff", "openwisp_users_user"."is_active", "openwisp_users_user"."date_joined", "openwisp_users_user"."id", "openwisp_users_user"."email", "openwisp_users_user"."bio", "openwisp_users_user"."url", "openwisp_users_user"."company", "openwisp_users_user"."location", "openwisp_users_user"."phone_number", "openwisp_users_user"."birth_date", "openwisp_users_user"."notes", "openwisp_users_user"."language", "openwisp_users_user"."password_updated" FROM "openwisp_users_user" WHERE "openwisp_users_user"."id" = '243282a1f4c34e098c1956d90fb75b86' LIMIT 21
9. SELECT "openwisp_users_organizationuser"."created", "openwisp_users_organizationuser"."modified", "openwisp_users_organizationuser"."is_admin", "openwisp_users_organizationuser"."id", "openwisp_users_organizationuser"."user_id", "openwisp_users_organizationuser"."organization_id", "openwisp_users_organization"."name", "openwisp_users_organization"."is_active", "openwisp_users_organization"."created", "openwisp_users_organization"."modified", "openwisp_users_organization"."slug", "openwisp_users_organization"."id", "openwisp_users_organization"."description", "openwisp_users_organization"."email", "openwisp_users_organization"."url", "openwisp_users_organizationowner"."created", "openwisp_users_organizationowner"."modified", "openwisp_users_organizationowner"."id", "openwisp_users_organizationowner"."organization_user_id", "openwisp_users_organizationowner"."organization_id" FROM "openwisp_users_organizationuser" INNER JOIN "openwisp_users_organization" ON ("openwisp_users_organizationuser"."organization_id" = "openwisp_users_organization"."id") LEFT OUTER JOIN "openwisp_users_organizationowner" ON ("openwisp_users_organizationuser"."id" = "openwisp_users_organizationowner"."organization_user_id") WHERE ("openwisp_users_organization"."is_active" AND "openwisp_users_organizationuser"."user_id" = '243282a1f4c34e098c1956d90fb75b86') ORDER BY "openwisp_users_organization"."name" ASC, "openwisp_users_organizationuser"."user_id" ASC
10. SELECT "openwisp_users_organization"."name", "openwisp_users_organization"."is_active", "openwisp_users_organization"."created", "openwisp_users_organization"."modified", "openwisp_users_organization"."slug", "openwisp_users_organization"."id", "openwisp_users_organization"."description", "openwisp_users_organization"."email", "openwisp_users_organization"."url" FROM "openwisp_users_organization" WHERE "openwisp_users_organization"."id" = '1c1754ecbcf44722ac045f5141f33348' LIMIT 21
11. UPDATE "openwisp_users_organization" SET "name" = 'org1', "is_active" = 1, "created" = '2024-05-27 17:41:08.827987', "modified" = '2024-05-27 17:41:08.974391', "slug" = 'org1', "description" = '', "email" = '', "url" = '' WHERE "openwisp_users_organization"."id" = '1c1754ecbcf44722ac045f5141f33348'
12. SELECT "openwisp_users_organizationowner"."created", "openwisp_users_organizationowner"."modified", "openwisp_users_organizationowner"."id", "openwisp_users_organizationowner"."organization_user_id", "openwisp_users_organizationowner"."organization_id" FROM "openwisp_users_organizationowner" WHERE "openwisp_users_organizationowner"."organization_id" = '1c1754ecbcf44722ac045f5141f33348' LIMIT 21

And one testcase which is failing is this. I am working on this it is failing because of the line which is invalidating cache in tasks.py which I am working on but any suggestion are appreciated.

======================================================================
FAIL: test_can_change_inline_org_owner (openwisp_users.tests.test_admin.TestUsersAdmin) [owner can edit inline org owner]
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/aryankaushik/Desktop/openwisp_users_main/openwisp-users/openwisp_users/tests/test_admin.py", line 1423, in test_can_change_inline_org_owner
    self.assertEqual(org_owners.count(), 1)
AssertionError: 0 != 1
kaushikaryan04 commented 5 months ago

Okay I will change the query count and will follow the commit message guidelines. About the Issue I have tried a lot of things. If you find something out let me know.

kaushikaryan04 commented 5 months ago

I have made a commit with those issues solved

kaushikaryan04 commented 5 months ago

Made the changes suggested by you. Thanks for guiding line by line. I will try to understand more about this project meanwhile.

nemesifier commented 5 months ago

@kaushikaryan04 come in the OpenWISP dev chat to coordinate when you can :pray:

coveralls commented 5 months ago

Coverage Status

coverage: 97.84% (-0.09%) from 97.929% when pulling 63544b4100e1a686e33ed657ea349df321a7b127 on kaushikaryan04:issues/357-inactive-organizations-users-visible into e85cf622f7dd612006b62db093dbf2f6df7b381b on openwisp:master.