@@ -3104,6 +3104,149 @@ def test_create_with_invalid_workspace_uuid(self) -> None:
31043104 },
31053105 )
31063106
3107+ def test_create_with_project_add_permission (self ) -> None :
3108+ self .grant_perm_to_user ("project.add" )
3109+
3110+ response = self .do_request (
3111+ "api:project-list" ,
3112+ method = "post" ,
3113+ code = 201 ,
3114+ request = {
3115+ "name" : "API project" ,
3116+ "slug" : "api-project" ,
3117+ "web" : "https://weblate.org/" ,
3118+ },
3119+ )
3120+ project = Project .objects .get (pk = response .data ["id" ])
3121+ self .assertIsNone (project .workspace )
3122+
3123+ def test_create_with_workspace_permission (self ) -> None :
3124+ with modify_settings (INSTALLED_APPS = {"prepend" : "weblate.billing" }):
3125+ workspace = Workspace .objects .create (name = "API workspace" )
3126+ workspace .add_owner (self .user )
3127+
3128+ response = self .do_request (
3129+ "api:project-list" ,
3130+ method = "post" ,
3131+ code = 201 ,
3132+ request = {
3133+ "name" : "API project" ,
3134+ "slug" : "api-project" ,
3135+ "web" : "https://weblate.org/" ,
3136+ "workspace" : str (workspace .pk ),
3137+ },
3138+ )
3139+ project = Project .objects .get (pk = response .data ["id" ])
3140+ self .assertEqual (project .workspace_id , workspace .pk )
3141+
3142+ def test_create_with_workspace_permission_denied (self ) -> None :
3143+ workspace = Workspace .objects .create (name = "API workspace" )
3144+
3145+ response = self .do_request (
3146+ "api:project-list" ,
3147+ method = "post" ,
3148+ code = 403 ,
3149+ request = {
3150+ "name" : "API project" ,
3151+ "slug" : "api-project" ,
3152+ "web" : "https://weblate.org/" ,
3153+ "workspace" : str (workspace .pk ),
3154+ },
3155+ )
3156+ self .assertEqual (
3157+ {
3158+ "errors" : [
3159+ {
3160+ "attr" : None ,
3161+ "code" : "permission_denied" ,
3162+ "detail" : "Can not create projects" ,
3163+ }
3164+ ],
3165+ "type" : "client_error" ,
3166+ },
3167+ response .data ,
3168+ )
3169+
3170+ def test_create_with_invalid_billing_workspace (self ) -> None :
3171+ with modify_settings (INSTALLED_APPS = {"prepend" : "weblate.billing" }):
3172+ billing = create_test_billing (self .user , invoice = False )
3173+ billing .in_limits = False
3174+ billing .save (update_fields = ["in_limits" ])
3175+
3176+ response = self .do_request (
3177+ "api:project-list" ,
3178+ method = "post" ,
3179+ code = 403 ,
3180+ request = {
3181+ "name" : "API project" ,
3182+ "slug" : "api-project" ,
3183+ "web" : "https://weblate.org/" ,
3184+ "workspace" : str (billing .workspace_id ),
3185+ },
3186+ )
3187+ self .assertEqual (
3188+ {
3189+ "errors" : [
3190+ {
3191+ "attr" : None ,
3192+ "code" : "permission_denied" ,
3193+ "detail" : "No valid billing found or limit exceeded." ,
3194+ }
3195+ ],
3196+ "type" : "client_error" ,
3197+ },
3198+ response .data ,
3199+ )
3200+
3201+ def test_create_with_single_workspace (self ) -> None :
3202+ with modify_settings (INSTALLED_APPS = {"remove" : "weblate.billing" }):
3203+ workspace = Workspace .objects .create (name = "API workspace" )
3204+ workspace .add_owner (self .user )
3205+
3206+ response = self .do_request (
3207+ "api:project-list" ,
3208+ method = "post" ,
3209+ code = 201 ,
3210+ request = {
3211+ "name" : "API project" ,
3212+ "slug" : "api-project" ,
3213+ "web" : "https://weblate.org/" ,
3214+ },
3215+ )
3216+ project = Project .objects .get (pk = response .data ["id" ])
3217+ self .assertEqual (project .workspace_id , workspace .pk )
3218+
3219+ def test_create_with_multiple_workspaces_requires_workspace (self ) -> None :
3220+ with modify_settings (INSTALLED_APPS = {"remove" : "weblate.billing" }):
3221+ first_workspace = Workspace .objects .create (name = "First API workspace" )
3222+ first_workspace .add_owner (self .user )
3223+ second_workspace = Workspace .objects .create (name = "Second API workspace" )
3224+ second_workspace .add_owner (self .user )
3225+
3226+ response = self .do_request (
3227+ "api:project-list" ,
3228+ method = "post" ,
3229+ code = 400 ,
3230+ request = {
3231+ "name" : "API project" ,
3232+ "slug" : "api-project" ,
3233+ "web" : "https://weblate.org/" ,
3234+ },
3235+ )
3236+ self .assertEqual (
3237+ {
3238+ "errors" : [
3239+ {
3240+ "attr" : "workspace" ,
3241+ "code" : "invalid" ,
3242+ "detail" : "Specify a workspace when multiple workspaces can be used." ,
3243+ }
3244+ ],
3245+ "type" : "validation_error" ,
3246+ },
3247+ response .data ,
3248+ )
3249+
31073250 def test_create_with_billing (self ) -> None :
31083251 with modify_settings (INSTALLED_APPS = {"remove" : "weblate.billing" }):
31093252 response = self .do_request (
@@ -3165,11 +3308,11 @@ def test_create_with_billing(self) -> None:
31653308 "name" : "API project" ,
31663309 "slug" : "api-project" ,
31673310 "web" : "https://weblate.org/" ,
3168- "workspace" : str (billing .workspace_id ),
31693311 },
31703312 )
31713313 project = Project .objects .get (pk = response .data ["id" ])
31723314 self .assertEqual (project .billing , billing )
3315+ self .assertEqual (project .workspace_id , billing .workspace_id )
31733316
31743317 response = self .do_request (
31753318 "api:project-list" ,
@@ -3179,7 +3322,6 @@ def test_create_with_billing(self) -> None:
31793322 "name" : "API project 2" ,
31803323 "slug" : "api-project-2" ,
31813324 "web" : "https://weblate.org/" ,
3182- "workspace" : str (billing .workspace_id ),
31833325 },
31843326 )
31853327 self .assertEqual (
0 commit comments