Commit 1a644860 authored by Rajendra Kadam's avatar Rajendra Kadam

Use shared examples in rspecs to group similar contexts

parent 53579e0a
...@@ -21,6 +21,28 @@ describe API::Groups do ...@@ -21,6 +21,28 @@ describe API::Groups do
group2.add_owner(user2) group2.add_owner(user2)
end end
shared_examples 'it uploads group avatar' do
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
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')
expect(json_response['message']).to eq(message)
end
end
describe "GET /groups" do describe "GET /groups" do
context "when unauthenticated" do context "when unauthenticated" do
it "returns public groups" do it "returns public groups" do
...@@ -539,6 +561,13 @@ describe API::Groups do ...@@ -539,6 +561,13 @@ describe API::Groups do
describe 'PUT /groups/:id' do describe 'PUT /groups/:id' do
let(:new_group_name) { 'New Group'} let(:new_group_name) { 'New Group'}
def make_upload_request
group_param = {
avatar: fixture_file_upload(file_path)
}
put api("/groups/#{group1.id}", user1), params: group_param
end
context 'when authenticated as the group owner' do context 'when authenticated as the group owner' do
it 'updates the group' do it 'updates the group' do
put api("/groups/#{group1.id}", user1), params: { put api("/groups/#{group1.id}", user1), params: {
...@@ -570,43 +599,26 @@ describe API::Groups do ...@@ -570,43 +599,26 @@ describe API::Groups do
expect(json_response['default_branch_protection']).to eq(::Gitlab::Access::MAINTAINER_PROJECT_ACCESS) expect(json_response['default_branch_protection']).to eq(::Gitlab::Access::MAINTAINER_PROJECT_ACCESS)
end end
it 'updates avatar' do context 'group avatar update' do
group_param = { context 'when appropriate file is used' do
avatar: fixture_file_upload('spec/fixtures/banana_sample.gif', 'image/gif') let(:file_path) { 'spec/fixtures/banana_sample.gif' }
}
put api("/groups/#{group1.id}", user1), params: group_param
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['avatar_url']).to eq('http://localhost/uploads/'\
'-/system/group/avatar/'\
"#{group1.id}/banana_sample.gif")
end
it 'returns 400 if file type is invalid to update avatar' do
group_param = {
avatar: fixture_file_upload('spec/fixtures/doc_sample.txt', 'text/plain')
}
put api("/groups/#{group1.id}", user1), params: group_param it_behaves_like 'it uploads group avatar'
end
expect(response).to have_gitlab_http_status(:bad_request) context 'when invalid file is used' do
expect(response.message).to eq('Bad Request') let(:file_path) { 'spec/fixtures/doc_sample.txt' }
expect(json_response['message']['avatar']).to eq(['file format is not supported.'\ let(:message) { { "avatar" => ["file format is not supported. Please try one of the following supported formats: png, jpg, jpeg, gif, bmp, tiff, ico"] } }
' Please try one of the following supported formats:'\
' png, jpg, jpeg, gif, bmp, tiff, ico'])
end
it 'returns 400 if file size exceeds allowed limit for group avatar' do it_behaves_like 'it returns 400 for bad upload request'
group_param = { end
avatar: fixture_file_upload('spec/fixtures/big-image.png', 'image/png')
}
put api("/groups/#{group1.id}", user1), params: group_param 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)"] } }
expect(response).to have_gitlab_http_status(:bad_request) it_behaves_like 'it returns 400 for bad upload request'
expect(response.message).to eq('Bad Request') end
expect(json_response['message']['avatar']).to eq(['is too big (should be at most 200 KB)'])
end end
it 'returns 404 for a non existing group' do it 'returns 404 for a non existing group' do
...@@ -979,6 +991,12 @@ describe API::Groups do ...@@ -979,6 +991,12 @@ describe API::Groups do
end end
describe "POST /groups" do describe "POST /groups" do
def make_upload_request
group = attributes_for_group_api request_access_enabled: false
group[:avatar] = fixture_file_upload(file_path)
post api("/groups", user3), params: group
end
context "when authenticated as user without group permissions" do context "when authenticated as user without group permissions" do
it "does not create group" do it "does not create group" do
group = attributes_for_group_api group = attributes_for_group_api
...@@ -1027,42 +1045,26 @@ describe API::Groups do ...@@ -1027,42 +1045,26 @@ describe API::Groups do
expect(json_response["visibility"]).to eq(Gitlab::VisibilityLevel.string_level(Gitlab::CurrentSettings.current_application_settings.default_group_visibility)) expect(json_response["visibility"]).to eq(Gitlab::VisibilityLevel.string_level(Gitlab::CurrentSettings.current_application_settings.default_group_visibility))
end end
it "uploads avatar for a group" do context 'group avatar upload' do
group = attributes_for_group_api request_access_enabled: false context 'when appropriate file is used' do
group[:avatar] = fixture_file_upload('spec/fixtures/banana_sample.gif', 'image/gif') let(:file_path) { 'spec/fixtures/banana_sample.gif' }
post api("/groups", user3), params: group
group_id = json_response['id']
expect(json_response['avatar_url']).to eq('http://localhost/uploads/'\
'-/system/group/avatar/'\
"#{group_id}/banana_sample.gif")
end
it 'returns a 400 if file type is invalid for group avatar' do
group = attributes_for_group_api request_access_enabled: false
group[:avatar] = fixture_file_upload('spec/fixtures/doc_sample.txt', 'text/plain')
post api("/groups", user3), params: group it_behaves_like 'it uploads group avatar'
end
expect(response).to have_gitlab_http_status(:bad_request) context 'when invalid file is used' do
expect(response.message).to eq('Bad Request') let(:file_path) { 'spec/fixtures/doc_sample.txt' }
expect(json_response['message']).to eq('Failed to save group'\ 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"]}' }
' {:avatar=>["file format is not supported.'\
' Please try one of the following supported formats:'\
' png, jpg, jpeg, gif, bmp, tiff, ico"]}')
end
it 'returns 400 if group avatar file size exceeds allowed limit' do it_behaves_like 'it returns 400 for bad upload request'
group = attributes_for_group_api request_access_enabled: false end
group[:avatar] = fixture_file_upload('spec/fixtures/big-image.png', 'image/png')
post api("/groups", user3), params: group 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)"]}' }
expect(response).to have_gitlab_http_status(:bad_request) it_behaves_like 'it returns 400 for bad upload request'
expect(response.message).to eq('Bad Request') end
expect(json_response['message']).to eq('Failed to save group'\
' {:avatar=>["is too big (should be at most 200 KB)"]}')
end end
it "creates a nested group" do it "creates a nested group" do
......
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