11import pytest
22import requests
3+ from django .contrib import messages
34
45from airlock .api import Status
56from airlock .users import User
@@ -130,9 +131,9 @@ def test_workspace_request_file_creates(client_with_user, api):
130131 factories .write_workspace_file (workspace , "test/path.txt" )
131132
132133 assert api .get_current_request (workspace .name , user ) is None
133-
134134 response = client .post (
135- "/workspaces/add-file-to-request/test1" , data = {"path" : "test/path.txt" }
135+ "/workspaces/add-file-to-request/test1" ,
136+ data = {"path" : "test/path.txt" , "filegroup" : "default" },
136137 )
137138 assert response .status_code == 302
138139
@@ -153,7 +154,8 @@ def test_workspace_request_file_request_already_exists(client_with_user, api):
153154 assert release_request .filegroups == []
154155
155156 response = client .post (
156- "/workspaces/add-file-to-request/test1" , data = {"path" : "test/path.txt" }
157+ "/workspaces/add-file-to-request/test1" ,
158+ data = {"path" : "test/path.txt" , "filegroup" : "default" },
157159 )
158160 assert response .status_code == 302
159161 current_release_request = api .get_current_request (workspace .name , user )
@@ -164,6 +166,30 @@ def test_workspace_request_file_request_already_exists(client_with_user, api):
164166 assert str (filegroup .files [0 ].relpath ) == "test/path.txt"
165167
166168
169+ def test_workspace_request_file_with_new_filegroup (client_with_user , api ):
170+ client = client_with_user ({"workspaces" : ["test1" ]})
171+ user = User .from_session (client .session )
172+
173+ workspace = factories .create_workspace ("test1" )
174+ factories .write_workspace_file (workspace , "test/path.txt" )
175+
176+ assert api .get_current_request (workspace .name , user ) is None
177+ response = client .post (
178+ "/workspaces/add-file-to-request/test1" ,
179+ data = {
180+ "path" : "test/path.txt" ,
181+ # new filegroup overrides a selected existing one (or the default)
182+ "filegroup" : "default" ,
183+ "new_filegroup" : "new_group" ,
184+ },
185+ )
186+ assert response .status_code == 302
187+
188+ release_request = api .get_current_request (workspace .name , user )
189+ filegroup = release_request .filegroups [0 ]
190+ assert filegroup .name == "new_group"
191+
192+
167193def test_workspace_request_file_filegroup_already_exists (client_with_user , api ):
168194 client = client_with_user ({"workspaces" : ["test1" ]})
169195 user = User .from_session (client .session )
@@ -175,14 +201,18 @@ def test_workspace_request_file_filegroup_already_exists(client_with_user, api):
175201 filegroupmetadata = factories .create_filegroup (release_request , "default" )
176202 assert not filegroupmetadata .request_files .exists ()
177203
178- client .post ("/workspaces/add-file-to-request/test1" , data = {"path" : "test/path.txt" })
204+ client .post (
205+ "/workspaces/add-file-to-request/test1" ,
206+ data = {"path" : "test/path.txt" , "filegroup" : "default" },
207+ )
179208
180209 assert filegroupmetadata .request_files .count () == 1
181210 assert str (filegroupmetadata .request_files .first ().relpath ) == "test/path.txt"
182211
183212 # Attempt to add the same file again
184213 response = client .post (
185- "/workspaces/add-file-to-request/test1" , data = {"path" : "test/path.txt" }
214+ "/workspaces/add-file-to-request/test1" ,
215+ data = {"path" : "test/path.txt" , "filegroup" : "default" },
186216 )
187217 assert response .status_code == 302
188218 # No new file created
@@ -195,12 +225,44 @@ def test_workspace_request_file_request_path_does_not_exist(client_with_user):
195225 factories .create_workspace ("test1" )
196226
197227 response = client .post (
198- "/workspaces/add-file-to-request/test1" , data = {"path" : "test/path.txt" }
228+ "/workspaces/add-file-to-request/test1" ,
229+ data = {"path" : "test/path.txt" , "filegroup" : "default" },
199230 )
200231
201232 assert response .status_code == 404
202233
203234
235+ def test_workspace_request_file_invalid_new_filegroup (client_with_user , api ):
236+ client = client_with_user ({"workspaces" : ["test1" ]})
237+ user = User .from_session (client .session )
238+
239+ workspace = factories .create_workspace ("test1" )
240+ factories .write_workspace_file (workspace , "test/path.txt" )
241+
242+ release_request = factories .create_release_request (workspace , user )
243+ filegroupmetadata = factories .create_filegroup (release_request , "test_group" )
244+
245+ response = client .post (
246+ "/workspaces/add-file-to-request/test1" ,
247+ data = {
248+ "path" : "test/path.txt" ,
249+ "filegroup" : "default" ,
250+ "new_filegroup" : "test_group" ,
251+ },
252+ follow = True ,
253+ )
254+
255+ assert not filegroupmetadata .request_files .exists ()
256+ # redirects to the workspace file again, with error messages
257+ assert response .request ["PATH_INFO" ] == workspace .get_url_for_path ("test/path.txt" )
258+
259+ all_messages = [msg for msg in response .context ["messages" ]]
260+ assert len (all_messages ) == 1
261+ message = all_messages [0 ]
262+ assert message .level == messages .ERROR
263+ assert "already exists" in message .message
264+
265+
204266def test_request_index_no_user (client ):
205267 release_request = factories .create_release_request ("workspace" )
206268 response = client .get (f"/requests/view/{ release_request .id } /" )
0 commit comments