diff options
Diffstat (limited to 'spec/requests/api/project_snippets_spec.rb')
-rw-r--r-- | spec/requests/api/project_snippets_spec.rb | 98 |
1 files changed, 40 insertions, 58 deletions
diff --git a/spec/requests/api/project_snippets_spec.rb b/spec/requests/api/project_snippets_spec.rb index 89ade15c1f6..22189dc3299 100644 --- a/spec/requests/api/project_snippets_spec.rb +++ b/spec/requests/api/project_snippets_spec.rb @@ -94,21 +94,11 @@ describe API::ProjectSnippets do expect(json_response['title']).to eq(snippet.title) expect(json_response['description']).to eq(snippet.description) - expect(json_response['file_name']).to eq(snippet.file_name) + expect(json_response['file_name']).to eq(snippet.file_name_on_repo) expect(json_response['ssh_url_to_repo']).to eq(snippet.ssh_url_to_repo) expect(json_response['http_url_to_repo']).to eq(snippet.http_url_to_repo) end - context 'when feature flag :version_snippets is disabled' do - before do - stub_feature_flags(version_snippets: false) - - get api("/projects/#{project.id}/snippets/#{snippet.id}", user) - end - - it_behaves_like 'snippet response without repository URLs' - end - it 'returns 404 for invalid snippet id' do get api("/projects/#{project.id}/snippets/#{non_existing_record_id}", user) @@ -129,7 +119,7 @@ describe API::ProjectSnippets do title: 'Test Title', file_name: 'test.rb', description: 'test description', - code: 'puts "hello world"', + content: 'puts "hello world"', visibility: 'public' } end @@ -148,19 +138,7 @@ describe API::ProjectSnippets do blob = snippet.repository.blob_at('master', params[:file_name]) - expect(blob.data).to eq params[:code] - end - - context 'when feature flag :version_snippets is disabled' do - it 'does not create snippet repository' do - stub_feature_flags(version_snippets: false) - - expect do - subject - end.to change { ProjectSnippet.count }.by(1) - - expect(snippet.repository_exists?).to be_falsey - end + expect(blob.data).to eq params[:content] end end @@ -202,7 +180,7 @@ describe API::ProjectSnippets do expect(response).to have_gitlab_http_status(:created) snippet = ProjectSnippet.find(json_response['id']) - expect(snippet.content).to eq(params[:code]) + expect(snippet.content).to eq(params[:content]) expect(snippet.description).to eq(params[:description]) expect(snippet.title).to eq(params[:title]) expect(snippet.file_name).to eq(params[:file_name]) @@ -219,7 +197,7 @@ describe API::ProjectSnippets do expect(response).to have_gitlab_http_status(:created) snippet = ProjectSnippet.find(json_response['id']) - expect(snippet.content).to eq(params[:code]) + expect(snippet.content).to eq(params[:content]) expect(snippet.description).to eq(params[:description]) expect(snippet.title).to eq(params[:title]) expect(snippet.file_name).to eq(params[:file_name]) @@ -230,43 +208,44 @@ describe API::ProjectSnippets do subject { post api("/projects/#{project.id}/snippets/", admin), params: params } end - it 'creates a new snippet with content parameter' do - params[:content] = params.delete(:code) + it 'returns 400 for missing parameters' do + params.delete(:title) post api("/projects/#{project.id}/snippets/", admin), params: params - expect(response).to have_gitlab_http_status(:created) - snippet = ProjectSnippet.find(json_response['id']) - expect(snippet.content).to eq(params[:content]) - expect(snippet.description).to eq(params[:description]) - expect(snippet.title).to eq(params[:title]) - expect(snippet.file_name).to eq(params[:file_name]) - expect(snippet.visibility_level).to eq(Snippet::PUBLIC) + expect(response).to have_gitlab_http_status(:bad_request) end - it 'returns 400 when both code and content parameters specified' do - params[:content] = params[:code] + it 'returns 400 if content is blank' do + params[:content] = '' post api("/projects/#{project.id}/snippets/", admin), params: params expect(response).to have_gitlab_http_status(:bad_request) - expect(json_response['error']).to eq('code, content are mutually exclusive') + expect(json_response['error']).to eq 'content is empty' end - it 'returns 400 for missing parameters' do - params.delete(:title) + it 'returns 400 if title is blank' do + params[:title] = '' post api("/projects/#{project.id}/snippets/", admin), params: params expect(response).to have_gitlab_http_status(:bad_request) + expect(json_response['error']).to eq 'title is empty' end - it 'returns 400 for empty code field' do - params[:code] = '' + context 'when save fails because the repository could not be created' do + before do + allow_next_instance_of(Snippets::CreateService) do |instance| + allow(instance).to receive(:create_repository).and_raise(Snippets::CreateService::CreateRepositoryError) + end + end - post api("/projects/#{project.id}/snippets/", admin), params: params + it 'returns 400' do + post api("/projects/#{project.id}/snippets", admin), params: params - expect(response).to have_gitlab_http_status(:bad_request) + expect(response).to have_gitlab_http_status(:bad_request) + end end context 'when the snippet is spam' do @@ -320,7 +299,7 @@ describe API::ProjectSnippets do new_content = 'New content' new_description = 'New description' - update_snippet(params: { code: new_content, description: new_description, visibility: 'private' }) + update_snippet(params: { content: new_content, description: new_description, visibility: 'private' }) expect(response).to have_gitlab_http_status(:ok) snippet.reload @@ -341,13 +320,6 @@ describe API::ProjectSnippets do expect(snippet.description).to eq(new_description) end - it 'returns 400 when both code and content parameters specified' do - update_snippet(params: { code: 'some content', content: 'other content' }) - - expect(response).to have_gitlab_http_status(:bad_request) - expect(json_response['error']).to eq('code, content are mutually exclusive') - end - it 'returns 404 for invalid snippet id' do update_snippet(snippet_id: non_existing_record_id, params: { title: 'foo' }) @@ -361,12 +333,17 @@ describe API::ProjectSnippets do expect(response).to have_gitlab_http_status(:bad_request) end - it 'returns 400 for empty code field' do - new_content = '' + it 'returns 400 if content is blank' do + update_snippet(params: { content: '' }) + + expect(response).to have_gitlab_http_status(:bad_request) + end - update_snippet(params: { code: new_content }) + it 'returns 400 if title is blank' do + update_snippet(params: { title: '' }) expect(response).to have_gitlab_http_status(:bad_request) + expect(json_response['error']).to eq 'title is empty' end it_behaves_like 'update with repository actions' do @@ -460,14 +437,13 @@ describe API::ProjectSnippets do end describe 'GET /projects/:project_id/snippets/:id/raw' do - let(:snippet) { create(:project_snippet, author: admin, project: project) } + let_it_be(:snippet) { create(:project_snippet, :repository, author: admin, project: project) } it 'returns raw text' do get api("/projects/#{snippet.project.id}/snippets/#{snippet.id}/raw", admin) expect(response).to have_gitlab_http_status(:ok) expect(response.content_type).to eq 'text/plain' - expect(response.body).to eq(snippet.content) end it 'returns 404 for invalid snippet id' do @@ -482,5 +458,11 @@ describe API::ProjectSnippets do let(:request) { get api("/projects/#{project_no_snippets.id}/snippets/123/raw", admin) } end end + + it_behaves_like 'snippet blob content' do + let_it_be(:snippet_with_empty_repo) { create(:project_snippet, :empty_repo, author: admin, project: project) } + + subject { get api("/projects/#{snippet.project.id}/snippets/#{snippet.id}/raw", snippet.author) } + end end end |