@@ -9,6 +9,13 @@ def get_url() -> str:
99 return reverse ("django_email_learning:api_platform:organizations_view" )
1010
1111
12+ def update_url (organization_id : int ) -> str :
13+ return reverse (
14+ "django_email_learning:api_platform:single_organization_view" ,
15+ kwargs = {"organization_id" : organization_id },
16+ )
17+
18+
1219@pytest .fixture (autouse = True )
1320def second_organization (db ):
1421 org = Organization (name = "Second Org" , description = "The second organization" )
@@ -46,40 +53,32 @@ def test_post_organizations_view_as_superadmin(superadmin_client):
4653 assert response .json ().get ("name" ) == "New Org"
4754
4855
49- def test_create_organization_ignore_none_exisiting_logo_file (superadmin_client ):
50- payload = {
51- "name" : "Org with Logo" ,
52- "description" : "Organization with non-existing logo file" ,
53- "logo" : "non_existing_logo.png" ,
54- }
55- response = superadmin_client .post (
56- get_url (), data = payload , content_type = "application/json"
57- )
58- assert response .status_code == 201
59- assert response .json ().get ("name" ) == "Org with Logo"
60- assert response .json ().get ("logo" ) is None
56+ @pytest .fixture
57+ def existing_logo_path ():
58+ with override_settings (
59+ STORAGES = {"default" : {"BACKEND" : "django.core.files.storage.InMemoryStorage" }}
60+ ):
61+ logo_path = "test_logo.png"
62+ with default_storage .open (logo_path , "w" ) as f :
63+ f .write ("dummy image content" )
64+ yield logo_path
6165
6266
63- @override_settings (
64- STORAGES = {"default" : {"BACKEND" : "django.core.files.storage.InMemoryStorage" }}
65- )
66- def test_create_organization_for_existing_logo_file (superadmin_client ):
67+ def test_create_organization_for_existing_logo_file (
68+ superadmin_client , existing_logo_path
69+ ):
6770 # Create a dummy logo file in the default storage
68- logo_path = "existing_logo.png"
69- with default_storage .open (logo_path , "w" ) as f :
70- f .write ("dummy image content" )
71-
7271 payload = {
7372 "name" : "OrgName" ,
7473 "description" : "Organization with existing logo file" ,
75- "logo" : logo_path ,
74+ "logo" : existing_logo_path ,
7675 }
7776 response = superadmin_client .post (
7877 get_url (), data = payload , content_type = "application/json"
7978 )
8079 assert response .status_code == 201
8180 assert response .json ().get ("name" ) == "OrgName"
82- assert response .json ().get ("logo" ).endswith (f"/{ logo_path } " )
81+ assert response .json ().get ("logo" ).endswith (f"/{ existing_logo_path } " )
8382
8483
8584@pytest .mark .parametrize (
@@ -97,3 +96,33 @@ def test_post_organizations_view_as_anonymous(anonymous_client):
9796 get_url (), data = payload , content_type = "application/json"
9897 )
9998 assert response .status_code == 401
99+
100+
101+ def test_update_organizations_view (superadmin_client , existing_logo_path ):
102+ organization = Organization .objects .first ()
103+ initial_name = organization .name
104+ initial_description = organization .description
105+ initial_logo = organization .logo
106+ payload = {
107+ "name" : "Updated Org" ,
108+ "description" : "Updated description" ,
109+ "logo" : existing_logo_path ,
110+ }
111+ response = superadmin_client .post (
112+ update_url (organization .id ), data = payload , content_type = "application/json"
113+ )
114+ assert response .status_code == 200
115+
116+ assert response .json ().get ("name" ) == "Updated Org"
117+ assert response .json ().get ("description" ) == "Updated description"
118+ assert response .json ().get ("logo" ).endswith (f"/{ existing_logo_path } " )
119+ assert response .json ().get ("name" ) != initial_name
120+ assert response .json ().get ("description" ) != initial_description
121+ assert response .json ().get ("logo" ) != initial_logo
122+
123+
124+ @pytest .mark .parametrize ("client" , ["viewer" , "editor" ], indirect = True )
125+ def test_edit_organization_requires_platform_admin_or_superadmin (client ):
126+ payload = {"name" : "Updated Org" , "description" : "Updated description" }
127+ response = client .post (update_url (1 ), data = payload , content_type = "application/json" )
128+ assert response .status_code == 403
0 commit comments