diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-19 01:45:44 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-19 01:45:44 +0000 |
commit | 85dc423f7090da0a52c73eb66faf22ddb20efff9 (patch) | |
tree | 9160f299afd8c80c038f08e1545be119f5e3f1e1 /spec/requests/api/graphql/mutations/snippets/create_spec.rb | |
parent | 15c2c8c66dbe422588e5411eee7e68f1fa440bb8 (diff) | |
download | gitlab-ce-85dc423f7090da0a52c73eb66faf22ddb20efff9.tar.gz |
Add latest changes from gitlab-org/gitlab@13-4-stable-ee
Diffstat (limited to 'spec/requests/api/graphql/mutations/snippets/create_spec.rb')
-rw-r--r-- | spec/requests/api/graphql/mutations/snippets/create_spec.rb | 126 |
1 files changed, 41 insertions, 85 deletions
diff --git a/spec/requests/api/graphql/mutations/snippets/create_spec.rb b/spec/requests/api/graphql/mutations/snippets/create_spec.rb index 56a5f4907c1..1bb446de708 100644 --- a/spec/requests/api/graphql/mutations/snippets/create_spec.rb +++ b/spec/requests/api/graphql/mutations/snippets/create_spec.rb @@ -7,22 +7,24 @@ RSpec.describe 'Creating a Snippet' do let_it_be(:user) { create(:user) } let_it_be(:project) { create(:project) } - let(:content) { 'Initial content' } + let(:description) { 'Initial description' } let(:title) { 'Initial title' } - let(:file_name) { 'Initial file_name' } let(:visibility_level) { 'public' } + let(:action) { :create } + let(:file_1) { { filePath: 'example_file1', content: 'This is the example file 1' }} + let(:file_2) { { filePath: 'example_file2', content: 'This is the example file 2' }} + let(:actions) { [{ action: action }.merge(file_1), { action: action }.merge(file_2)] } let(:project_path) { nil } let(:uploaded_files) { nil } let(:mutation_vars) do { - content: content, description: description, visibility_level: visibility_level, - file_name: file_name, title: title, project_path: project_path, - uploaded_files: uploaded_files + uploaded_files: uploaded_files, + blob_actions: actions } end @@ -62,24 +64,47 @@ RSpec.describe 'Creating a Snippet' do context 'when the user has permission' do let(:current_user) { user } - context 'with PersonalSnippet' do - it 'creates the Snippet' do + shared_examples 'does not create snippet' do + it 'does not create the Snippet' do expect do subject - end.to change { Snippet.count }.by(1) + end.not_to change { Snippet.count } end - it 'returns the created Snippet' do + it 'does not return Snippet' do subject - expect(mutation_response['snippet']['blob']['richData']).to be_nil - expect(mutation_response['snippet']['blob']['plainData']).to match(content) + expect(mutation_response['snippet']).to be_nil + end + end + + shared_examples 'creates snippet' do + it 'returns the created Snippet' do + expect do + subject + end.to change { Snippet.count }.by(1) + expect(mutation_response['snippet']['title']).to eq(title) expect(mutation_response['snippet']['description']).to eq(description) - expect(mutation_response['snippet']['fileName']).to eq(file_name) expect(mutation_response['snippet']['visibilityLevel']).to eq(visibility_level) - expect(mutation_response['snippet']['project']).to be_nil + expect(mutation_response['snippet']['blobs'][0]['plainData']).to match(file_1[:content]) + expect(mutation_response['snippet']['blobs'][0]['fileName']).to match(file_1[:file_path]) + expect(mutation_response['snippet']['blobs'][1]['plainData']).to match(file_2[:content]) + expect(mutation_response['snippet']['blobs'][1]['fileName']).to match(file_2[:file_path]) end + + context 'when action is invalid' do + let(:file_1) { { filePath: 'example_file1' }} + + it_behaves_like 'a mutation that returns errors in the response', errors: ['Snippet actions have invalid data'] + it_behaves_like 'does not create snippet' + end + + it_behaves_like 'snippet edit usage data counters' + end + + context 'with PersonalSnippet' do + it_behaves_like 'creates snippet' end context 'with ProjectSnippet' do @@ -89,23 +114,7 @@ RSpec.describe 'Creating a Snippet' do project.add_developer(current_user) end - it 'creates the Snippet' do - expect do - subject - end.to change { Snippet.count }.by(1) - end - - it 'returns the created Snippet' do - subject - - expect(mutation_response['snippet']['blob']['richData']).to be_nil - expect(mutation_response['snippet']['blob']['plainData']).to match(content) - expect(mutation_response['snippet']['title']).to eq(title) - expect(mutation_response['snippet']['description']).to eq(description) - expect(mutation_response['snippet']['fileName']).to eq(file_name) - expect(mutation_response['snippet']['visibilityLevel']).to eq(visibility_level) - expect(mutation_response['snippet']['project']['fullPath']).to eq(project_path) - end + it_behaves_like 'creates snippet' context 'when the project path is invalid' do let(:project_path) { 'foobar' } @@ -122,61 +131,8 @@ RSpec.describe 'Creating a Snippet' do it_behaves_like 'a mutation that returns top-level errors', errors: [Gitlab::Graphql::Authorize::AuthorizeResource::RESOURCE_ACCESS_ERROR] end - end - - shared_examples 'does not create snippet' do - it 'does not create the Snippet' do - expect do - subject - end.not_to change { Snippet.count } - end - - it 'does not return Snippet' do - subject - - expect(mutation_response['snippet']).to be_nil - end - end - - context 'when snippet is created using the files param' do - let(:action) { :create } - let(:file_1) { { filePath: 'example_file1', content: 'This is the example file 1' }} - let(:file_2) { { filePath: 'example_file2', content: 'This is the example file 2' }} - let(:actions) { [{ action: action }.merge(file_1), { action: action }.merge(file_2)] } - let(:mutation_vars) do - { - description: description, - visibility_level: visibility_level, - project_path: project_path, - title: title, - blob_actions: actions - } - end - - it 'creates the Snippet' do - expect do - subject - end.to change { Snippet.count }.by(1) - end - - it 'returns the created Snippet' do - subject - expect(mutation_response['snippet']['title']).to eq(title) - expect(mutation_response['snippet']['description']).to eq(description) - expect(mutation_response['snippet']['visibilityLevel']).to eq(visibility_level) - expect(mutation_response['snippet']['blobs'][0]['plainData']).to match(file_1[:content]) - expect(mutation_response['snippet']['blobs'][0]['fileName']).to match(file_1[:file_path]) - expect(mutation_response['snippet']['blobs'][1]['plainData']).to match(file_2[:content]) - expect(mutation_response['snippet']['blobs'][1]['fileName']).to match(file_2[:file_path]) - end - - context 'when action is invalid' do - let(:file_1) { { filePath: 'example_file1' }} - - it_behaves_like 'a mutation that returns errors in the response', errors: ['Snippet actions have invalid data'] - it_behaves_like 'does not create snippet' - end + it_behaves_like 'snippet edit usage data counters' end context 'when there are ActiveRecord validation errors' do @@ -187,7 +143,7 @@ RSpec.describe 'Creating a Snippet' do end context 'when there non ActiveRecord errors' do - let(:file_name) { 'invalid://file/path' } + let(:file_1) { { filePath: 'invalid://file/path', content: 'foobar' }} it_behaves_like 'a mutation that returns errors in the response', errors: ['Repository Error creating the snippet - Invalid file name'] it_behaves_like 'does not create snippet' |