Commit 420e5547 authored by Rajendra Kadam's avatar Rajendra Kadam

Use regex to match message and refactor specs

parent 1a644860
......@@ -21,25 +21,30 @@ describe API::Groups do
group2.add_owner(user2)
end
shared_examples 'it uploads group avatar' do
it 'returns avatar url in response' do
make_upload_request
shared_examples 'group avatar upload' do
context 'when valid file is used' do
let(:file_path) { 'spec/fixtures/banana_sample.gif' }
group_id = json_response['id']
expect(json_response['avatar_url']).to eq('http://localhost/uploads/'\
it 'returns avatar url in response' do
make_upload_request
group_id = json_response['id']
expect(json_response['avatar_url']).to eq('http://localhost/uploads/'\
'-/system/group/avatar/'\
"#{group_id}/banana_sample.gif")
end
end
end
shared_examples 'it returns 400 for bad upload request' do
it 'returns 400' do
make_upload_request
expect(response).to have_gitlab_http_status(:bad_request)
expect(response.message).to eq('Bad Request')
shared_examples 'invalid file upload request' do
context 'when invalid' do
it 'returns 400' do
make_upload_request
expect(json_response['message']).to eq(message)
expect(response).to have_gitlab_http_status(:bad_request)
expect(response.message).to eq('Bad Request')
expect(json_response['message'].to_s).to match(/#{message}/)
end
end
end
......@@ -568,6 +573,24 @@ describe API::Groups do
put api("/groups/#{group1.id}", user1), params: group_param
end
it_behaves_like 'group avatar upload'
context 'group avatar update' do
context 'when file format is not supported' do
let(:file_path) { 'spec/fixtures/doc_sample.txt' }
let(:message) { 'file format is not supported. Please try one of the following supported formats: png, jpg, jpeg, gif, bmp, tiff, ico' }
it_behaves_like 'invalid file upload request'
end
context 'when file size is large' do
let(:file_path) { 'spec/fixtures/big-image.png' }
let(:message) { "is too big" }
it_behaves_like 'invalid file upload request'
end
end
context 'when authenticated as the group owner' do
it 'updates the group' do
put api("/groups/#{group1.id}", user1), params: {
......@@ -599,28 +622,6 @@ describe API::Groups do
expect(json_response['default_branch_protection']).to eq(::Gitlab::Access::MAINTAINER_PROJECT_ACCESS)
end
context 'group avatar update' do
context 'when appropriate file is used' do
let(:file_path) { 'spec/fixtures/banana_sample.gif' }
it_behaves_like 'it uploads group avatar'
end
context 'when invalid file is used' do
let(:file_path) { 'spec/fixtures/doc_sample.txt' }
let(:message) { { "avatar" => ["file format is not supported. Please try one of the following supported formats: png, jpg, jpeg, gif, bmp, tiff, ico"] } }
it_behaves_like 'it returns 400 for bad upload request'
end
context 'when file is too big' do
let(:file_path) { 'spec/fixtures/big-image.png' }
let(:message) { { "avatar" => ["is too big (should be at most 200 KB)"] } }
it_behaves_like 'it returns 400 for bad upload request'
end
end
it 'returns 404 for a non existing group' do
put api('/groups/1328', user1), params: { name: new_group_name }
......@@ -997,6 +998,24 @@ describe API::Groups do
post api("/groups", user3), params: group
end
it_behaves_like 'group avatar upload'
context 'group avatar upload' do
context 'when file format is not supported' do
let(:file_path) { 'spec/fixtures/doc_sample.txt' }
let(:message) { 'file format is not supported. Please try one of the following supported formats: png, jpg, jpeg, gif, bmp, tiff, ico' }
it_behaves_like 'invalid file upload request'
end
context 'when file size is large' do
let(:file_path) { 'spec/fixtures/big-image.png' }
let(:message) { "is too big" }
it_behaves_like 'invalid file upload request'
end
end
context "when authenticated as user without group permissions" do
it "does not create group" do
group = attributes_for_group_api
......@@ -1045,28 +1064,6 @@ describe API::Groups do
expect(json_response["visibility"]).to eq(Gitlab::VisibilityLevel.string_level(Gitlab::CurrentSettings.current_application_settings.default_group_visibility))
end
context 'group avatar upload' do
context 'when appropriate file is used' do
let(:file_path) { 'spec/fixtures/banana_sample.gif' }
it_behaves_like 'it uploads group avatar'
end
context 'when invalid file is used' do
let(:file_path) { 'spec/fixtures/doc_sample.txt' }
let(:message) { 'Failed to save group {:avatar=>["file format is not supported. Please try one of the following supported formats: png, jpg, jpeg, gif, bmp, tiff, ico"]}' }
it_behaves_like 'it returns 400 for bad upload request'
end
context 'when file is too big' do
let(:file_path) { 'spec/fixtures/big-image.png' }
let(:message) { 'Failed to save group {:avatar=>["is too big (should be at most 200 KB)"]}' }
it_behaves_like 'it returns 400 for bad upload request'
end
end
it "creates a nested group" do
parent = create(:group)
parent.add_owner(user3)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment