diff options
author | Stan Hu <stanhu@gmail.com> | 2017-03-24 15:02:23 -0700 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2017-03-24 15:02:23 -0700 |
commit | e1bb8c99cf7840c8b5d981111857b7d16b9cc1bd (patch) | |
tree | 9737570f75dbb84a325537cd1863301e42234fee /spec | |
parent | f6ededfa07a2fe9f3dd1c4d7e247b0f784f8181a (diff) | |
parent | f1e1e51311b6f50c98b3e1476942107269c28a97 (diff) | |
download | gitlab-ce-e1bb8c99cf7840c8b5d981111857b7d16b9cc1bd.tar.gz |
Merge branch 'master' into sh-bring-back-option-to-be-notified-of-own-activity
Diffstat (limited to 'spec')
86 files changed, 2044 insertions, 535 deletions
diff --git a/spec/controllers/admin/application_settings_controller_spec.rb b/spec/controllers/admin/application_settings_controller_spec.rb new file mode 100644 index 00000000000..84a1ce773a1 --- /dev/null +++ b/spec/controllers/admin/application_settings_controller_spec.rb @@ -0,0 +1,28 @@ +require 'spec_helper' + +describe Admin::ApplicationSettingsController do + include StubENV + + let(:admin) { create(:admin) } + + before do + sign_in(admin) + stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false') + end + + describe 'PATCH #update' do + it 'updates the default_project_visibility for string value' do + patch :update, application_setting: { default_project_visibility: "20" } + + expect(response).to redirect_to(admin_application_settings_path) + expect(ApplicationSetting.current.default_project_visibility).to eq Gitlab::VisibilityLevel::PUBLIC + end + + it 'falls back to default with default_project_visibility setting is omitted' do + patch :update, application_setting: {} + + expect(response).to redirect_to(admin_application_settings_path) + expect(ApplicationSetting.current.default_project_visibility).to eq Gitlab::VisibilityLevel::PRIVATE + end + end +end diff --git a/spec/controllers/import/bitbucket_controller_spec.rb b/spec/controllers/import/bitbucket_controller_spec.rb index fa4cc0ebbe0..51f23e4eeb9 100644 --- a/spec/controllers/import/bitbucket_controller_spec.rb +++ b/spec/controllers/import/bitbucket_controller_spec.rb @@ -112,6 +112,17 @@ describe Import::BitbucketController do post :create, format: :js end end + + context 'when the Bitbucket user is unauthorized' do + render_views + + it 'returns unauthorized' do + allow(controller).to receive(:current_user).and_return(user) + allow(user).to receive(:can?).and_return(false) + + post :create, format: :js + end + end end context "when the repository owner is not the Bitbucket user" do diff --git a/spec/controllers/projects/builds_controller_specs.rb b/spec/controllers/projects/builds_controller_specs.rb new file mode 100644 index 00000000000..d501f7b3155 --- /dev/null +++ b/spec/controllers/projects/builds_controller_specs.rb @@ -0,0 +1,47 @@ +require 'spec_helper' + +describe Projects::BuildsController do + include ApiHelpers + + let(:project) { create(:empty_project, :public) } + + describe 'GET trace.json' do + let(:pipeline) { create(:ci_pipeline, project: project) } + let(:build) { create(:ci_build, pipeline: pipeline) } + let(:user) { create(:user) } + + context 'when user is logged in as developer' do + before do + project.add_developer(user) + sign_in(user) + get_trace + end + + it 'traces build log' do + expect(response).to have_http_status(:ok) + expect(json_response['id']).to eq build.id + expect(json_response['status']).to eq build.status + end + end + + context 'when user is logged in as non member' do + before do + sign_in(user) + get_trace + end + + it 'traces build log' do + expect(response).to have_http_status(:ok) + expect(json_response['id']).to eq build.id + expect(json_response['status']).to eq build.status + end + end + + def get_trace + get :trace, namespace_id: project.namespace, + project_id: project, + id: build.id, + format: :json + end + end +end diff --git a/spec/controllers/projects/issues_controller_spec.rb b/spec/controllers/projects/issues_controller_spec.rb index 8263301c439..c467ab9fb8a 100644 --- a/spec/controllers/projects/issues_controller_spec.rb +++ b/spec/controllers/projects/issues_controller_spec.rb @@ -152,6 +152,24 @@ describe Projects::IssuesController do it_behaves_like 'update invalid issuable', Issue + context 'changing the assignee' do + it 'limits the attributes exposed on the assignee' do + assignee = create(:user) + project.add_developer(assignee) + + put :update, + namespace_id: project.namespace.to_param, + project_id: project, + id: issue.iid, + issue: { assignee_id: assignee.id }, + format: :json + body = JSON.parse(response.body) + + expect(body['assignee'].keys) + .to match_array(%w(name username avatar_url)) + end + end + context 'when moving issue to another private project' do let(:another_project) { create(:empty_project, :private) } @@ -223,10 +241,27 @@ describe Projects::IssuesController do expect(spam_logs.first.recaptcha_verified).to be_falsey end - it 'renders verify template' do - update_spam_issue + context 'as HTML' do + it 'renders verify template' do + update_spam_issue + + expect(response).to render_template(:verify) + end + end + + context 'as JSON' do + before do + update_issue({ title: 'Spam Title', description: 'Spam lives here' }, format: :json) + end + + it 'renders json errors' do + expect(json_response) + .to eql("errors" => ["Your issue has been recognized as spam. Please, change the content or solve the reCAPTCHA to proceed."]) + end - expect(response).to render_template(:verify) + it 'returns 422 status' do + expect(response).to have_http_status(422) + end end end diff --git a/spec/controllers/projects/merge_requests_controller_spec.rb b/spec/controllers/projects/merge_requests_controller_spec.rb index 250d64f7055..c310d830e81 100644 --- a/spec/controllers/projects/merge_requests_controller_spec.rb +++ b/spec/controllers/projects/merge_requests_controller_spec.rb @@ -203,6 +203,24 @@ describe Projects::MergeRequestsController do end describe 'PUT update' do + context 'changing the assignee' do + it 'limits the attributes exposed on the assignee' do + assignee = create(:user) + project.add_developer(assignee) + + put :update, + namespace_id: project.namespace.to_param, + project_id: project, + id: merge_request.iid, + merge_request: { assignee_id: assignee.id }, + format: :json + body = JSON.parse(response.body) + + expect(body['assignee'].keys) + .to match_array(%w(name username avatar_url)) + end + end + context 'there is no source project' do let(:project) { create(:project) } let(:fork_project) { create(:forked_project_with_submodules) } diff --git a/spec/factories/ci/builds.rb b/spec/factories/ci/builds.rb index 6b0d084614b..f78086211f7 100644 --- a/spec/factories/ci/builds.rb +++ b/spec/factories/ci/builds.rb @@ -172,7 +172,7 @@ FactoryGirl.define do { image: 'ruby:2.1', services: ['postgres'], - after_script: "ls\ndate", + after_script: %w(ls date), artifacts: { name: 'artifacts_file', untracked: false, diff --git a/spec/factories/merge_requests.rb b/spec/factories/merge_requests.rb index 21487541507..ae0bbbd6aeb 100644 --- a/spec/factories/merge_requests.rb +++ b/spec/factories/merge_requests.rb @@ -4,7 +4,6 @@ FactoryGirl.define do author association :source_project, :repository, factory: :project target_project { source_project } - project { target_project } # $ git log --pretty=oneline feature..master # 5937ac0a7beb003549fc5fd26fc247adbce4a52e Add submodule from gitlab.com diff --git a/spec/features/admin/admin_settings_spec.rb b/spec/features/admin/admin_settings_spec.rb index b4095095887..5099441dce2 100644 --- a/spec/features/admin/admin_settings_spec.rb +++ b/spec/features/admin/admin_settings_spec.rb @@ -9,6 +9,13 @@ feature 'Admin updates settings', feature: true do visit admin_application_settings_path end + scenario 'Change visibility settings' do + choose "application_setting_default_project_visibility_20" + click_button 'Save' + + expect(page).to have_content "Application settings saved successfully" + end + scenario 'Change application settings' do uncheck 'Gravatar enabled' fill_in 'Home page URL', with: 'https://about.gitlab.com/' @@ -27,6 +34,7 @@ feature 'Admin updates settings', feature: true do fill_in 'Username', with: 'test_user' fill_in 'service_push_channel', with: '#test_channel' page.check('Notify only broken pipelines') + page.check('Notify only default branch') check_all_events click_on 'Save' diff --git a/spec/features/atom/dashboard_issues_spec.rb b/spec/features/atom/dashboard_issues_spec.rb index a7c22615b89..58b14e09740 100644 --- a/spec/features/atom/dashboard_issues_spec.rb +++ b/spec/features/atom/dashboard_issues_spec.rb @@ -2,7 +2,8 @@ require 'spec_helper' describe "Dashboard Issues Feed", feature: true do describe "GET /issues" do - let!(:user) { create(:user) } + let!(:user) { create(:user, email: 'private1@example.com', public_email: 'public1@example.com') } + let!(:assignee) { create(:user, email: 'private2@example.com', public_email: 'public2@example.com') } let!(:project1) { create(:project) } let!(:project2) { create(:project) } @@ -31,7 +32,7 @@ describe "Dashboard Issues Feed", feature: true do end context "issue with basic fields" do - let!(:issue2) { create(:issue, author: user, assignee: user, project: project2, description: 'test desc') } + let!(:issue2) { create(:issue, author: user, assignee: assignee, project: project2, description: 'test desc') } it "renders issue fields" do visit issues_dashboard_path(:atom, private_token: user.private_token) @@ -39,8 +40,8 @@ describe "Dashboard Issues Feed", feature: true do entry = find(:xpath, "//feed/entry[contains(summary/text(),'#{issue2.title}')]") expect(entry).to be_present - expect(entry).to have_selector('author email', text: issue2.author_email) - expect(entry).to have_selector('assignee email', text: issue2.author_email) + expect(entry).to have_selector('author email', text: issue2.author_public_email) + expect(entry).to have_selector('assignee email', text: issue2.assignee_public_email) expect(entry).not_to have_selector('labels') expect(entry).not_to have_selector('milestone') expect(entry).to have_selector('description', text: issue2.description) @@ -50,7 +51,7 @@ describe "Dashboard Issues Feed", feature: true do context "issue with label and milestone" do let!(:milestone1) { create(:milestone, project: project1, title: 'v1') } let!(:label1) { create(:label, project: project1, title: 'label1') } - let!(:issue1) { create(:issue, author: user, assignee: user, project: project1, milestone: milestone1) } + let!(:issue1) { create(:issue, author: user, assignee: assignee, project: project1, milestone: milestone1) } before do issue1.labels << label1 @@ -62,8 +63,8 @@ describe "Dashboard Issues Feed", feature: true do entry = find(:xpath, "//feed/entry[contains(summary/text(),'#{issue1.title}')]") expect(entry).to be_present - expect(entry).to have_selector('author email', text: issue1.author_email) - expect(entry).to have_selector('assignee email', text: issue1.author_email) + expect(entry).to have_selector('author email', text: issue1.author_public_email) + expect(entry).to have_selector('assignee email', text: issue1.assignee_public_email) expect(entry).to have_selector('labels label', text: label1.title) expect(entry).to have_selector('milestone', text: milestone1.title) expect(entry).not_to have_selector('description') diff --git a/spec/features/atom/issues_spec.rb b/spec/features/atom/issues_spec.rb index a01a050a013..b3903ec2faf 100644 --- a/spec/features/atom/issues_spec.rb +++ b/spec/features/atom/issues_spec.rb @@ -2,10 +2,11 @@ require 'spec_helper' describe 'Issues Feed', feature: true do describe 'GET /issues' do - let!(:user) { create(:user) } + let!(:user) { create(:user, email: 'private1@example.com', public_email: 'public1@example.com') } + let!(:assignee) { create(:user, email: 'private2@example.com', public_email: 'public2@example.com') } let!(:group) { create(:group) } let!(:project) { create(:project) } - let!(:issue) { create(:issue, author: user, project: project) } + let!(:issue) { create(:issue, author: user, assignee: assignee, project: project) } before do project.team << [user, :developer] @@ -20,7 +21,8 @@ describe 'Issues Feed', feature: true do expect(response_headers['Content-Type']). to have_content('application/atom+xml') expect(body).to have_selector('title', text: "#{project.name} issues") - expect(body).to have_selector('author email', text: issue.author_email) + expect(body).to have_selector('author email', text: issue.author_public_email) + expect(body).to have_selector('assignee email', text: issue.author_public_email) expect(body).to have_selector('entry summary', text: issue.title) end end @@ -33,7 +35,8 @@ describe 'Issues Feed', feature: true do expect(response_headers['Content-Type']). to have_content('application/atom+xml') expect(body).to have_selector('title', text: "#{project.name} issues") - expect(body).to have_selector('author email', text: issue.author_email) + expect(body).to have_selector('author email', text: issue.author_public_email) + expect(body).to have_selector('assignee email', text: issue.author_public_email) expect(body).to have_selector('entry summary', text: issue.title) end end diff --git a/spec/features/boards/add_issues_modal_spec.rb b/spec/features/boards/add_issues_modal_spec.rb index d17a418b8c3..1c0f97d8a1c 100644 --- a/spec/features/boards/add_issues_modal_spec.rb +++ b/spec/features/boards/add_issues_modal_spec.rb @@ -23,6 +23,20 @@ describe 'Issue Boards add issue modal', :feature, :js do wait_for_vue_resource end + it 'resets filtered search state' do + visit namespace_project_board_path(project.namespace, project, board, search: 'testing') + + wait_for_vue_resource + + click_button('Add issues') + + page.within('.add-issues-modal') do + expect(find('.form-control').value).to eq('') + expect(page).to have_selector('.clear-search', visible: false) + expect(find('.form-control')[:placeholder]).to eq('Search or filter results...') + end + end + context 'modal interaction' do it 'opens modal' do click_button('Add issues') diff --git a/spec/features/commits_spec.rb b/spec/features/commits_spec.rb index 0e305c52358..881f1fca4d1 100644 --- a/spec/features/commits_spec.rb +++ b/spec/features/commits_spec.rb @@ -11,12 +11,16 @@ describe 'Commits' do stub_ci_pipeline_to_return_yaml_file end + let(:creator) { create(:user) } + let!(:pipeline) do create(:ci_pipeline, project: project, + user: creator, ref: project.default_branch, sha: project.commit.sha, - status: :success) + status: :success, + created_at: 5.months.ago) end context 'commit status is Generic Commit Status' do @@ -80,7 +84,8 @@ describe 'Commits' do it 'shows pipeline`s data' do expect(page).to have_content pipeline.sha[0..7] expect(page).to have_content pipeline.git_commit_message - expect(page).to have_content pipeline.git_author_name + expect(page).to have_content pipeline.user.name + expect(page).to have_content pipeline.created_at.strftime('%b %d, %Y') end end @@ -150,7 +155,7 @@ describe 'Commits' do it do expect(page).to have_content pipeline.sha[0..7] expect(page).to have_content pipeline.git_commit_message - expect(page).to have_content pipeline.git_author_name + expect(page).to have_content pipeline.user.name expect(page).to have_link('Download artifacts') expect(page).not_to have_link('Cancel running') expect(page).not_to have_link('Retry') @@ -169,7 +174,7 @@ describe 'Commits' do it do expect(page).to have_content pipeline.sha[0..7] expect(page).to have_content pipeline.git_commit_message - expect(page).to have_content pipeline.git_author_name + expect(page).to have_content pipeline.user.name expect(page).not_to have_link('Download artifacts') expect(page).not_to have_link('Cancel running') expect(page).not_to have_link('Retry') diff --git a/spec/features/dashboard/shortcuts_spec.rb b/spec/features/dashboard/shortcuts_spec.rb index 62a2c54c94c..3642c0bfb5b 100644 --- a/spec/features/dashboard/shortcuts_spec.rb +++ b/spec/features/dashboard/shortcuts_spec.rb @@ -21,6 +21,11 @@ feature 'Dashboard shortcuts', feature: true, js: true do find('body').native.send_key('m') check_page_title('Merge Requests') + + find('body').native.send_key('g') + find('body').native.send_key('t') + + check_page_title('Todos') end def check_page_title(title) diff --git a/spec/features/explore/groups_list_spec.rb b/spec/features/explore/groups_list_spec.rb index 773ae4b38bc..9daaaa8e555 100644 --- a/spec/features/explore/groups_list_spec.rb +++ b/spec/features/explore/groups_list_spec.rb @@ -7,6 +7,7 @@ describe 'Explore Groups page', js: true, feature: true do let!(:group) { create(:group) } let!(:public_group) { create(:group, :public) } let!(:private_group) { create(:group, :private) } + let!(:empty_project) { create(:empty_project, group: public_group) } before do group.add_owner(user) @@ -43,4 +44,23 @@ describe 'Explore Groups page', js: true, feature: true do expect(page).not_to have_content(private_group.full_name) expect(page.all('.js-groups-list-holder .content-list li').length).to eq 2 end + + it 'shows non-archived projects count' do + # Initially project is not archived + expect(find('.js-groups-list-holder .content-list li:first-child .stats span:first-child')).to have_text("1") + + # Archive project + empty_project.archive! + visit explore_groups_path + + # Check project count + expect(find('.js-groups-list-holder .content-list li:first-child .stats span:first-child')).to have_text("0") + + # Unarchive project + empty_project.unarchive! + visit explore_groups_path + + # Check project count + expect(find('.js-groups-list-holder .content-list li:first-child .stats span:first-child')).to have_text("1") + end end diff --git a/spec/features/merge_requests/create_new_mr_spec.rb b/spec/features/merge_requests/create_new_mr_spec.rb index 0832a3656a8..f1ad4a55246 100644 --- a/spec/features/merge_requests/create_new_mr_spec.rb +++ b/spec/features/merge_requests/create_new_mr_spec.rb @@ -12,6 +12,33 @@ feature 'Create New Merge Request', feature: true, js: true do login_as user end + it 'selects the source branch sha when a tag with the same name exists' do + visit namespace_project_merge_requests_path(project.namespace, project) + + click_link 'New Merge Request' + expect(page).to have_content('Source branch') + expect(page).to have_content('Target branch') + + first('.js-source-branch').click + first('.dropdown-source-branch .dropdown-content a', text: 'v1.1.0').click + + expect(page).to have_content "b83d6e3" + end + + it 'selects the target branch sha when a tag with the same name exists' do + visit namespace_project_merge_requests_path(project.namespace, project) + + click_link 'New Merge Request' + + expect(page).to have_content('Source branch') + expect(page).to have_content('Target branch') + + first('.js-target-branch').click + first('.dropdown-target-branch .dropdown-content a', text: 'v1.1.0').click + + expect(page).to have_content "b83d6e3" + end + it 'generates a diff for an orphaned branch' do visit namespace_project_merge_requests_path(project.namespace, project) @@ -46,6 +73,12 @@ feature 'Create New Merge Request', feature: true, js: true do end end + it 'populates source branch button' do + visit new_namespace_project_merge_request_path(project.namespace, project, change_branches: true, merge_request: { target_branch: 'master', source_branch: 'fix' }) + + expect(find('.js-source-branch')).to have_content('fix') + end + it 'allows to change the diff view' do visit new_namespace_project_merge_request_path(project.namespace, project, merge_request: { target_branch: 'master', source_branch: 'fix' }) diff --git a/spec/features/merge_requests/merge_immediately_with_pipeline_spec.rb b/spec/features/merge_requests/merge_immediately_with_pipeline_spec.rb index 0ceaf7bc830..79105b1ee46 100644 --- a/spec/features/merge_requests/merge_immediately_with_pipeline_spec.rb +++ b/spec/features/merge_requests/merge_immediately_with_pipeline_spec.rb @@ -35,6 +35,8 @@ feature 'Merge immediately', :feature, :js do click_link 'Merge Immediately' expect(find('.js-merge-when-pipeline-succeeds-button')).to have_content('Merge in progress') + + wait_for_ajax end end end diff --git a/spec/features/participants_autocomplete_spec.rb b/spec/features/participants_autocomplete_spec.rb index c2545b0c259..decad589c23 100644 --- a/spec/features/participants_autocomplete_spec.rb +++ b/spec/features/participants_autocomplete_spec.rb @@ -1,101 +1,62 @@ require 'spec_helper' -feature 'Member autocomplete', feature: true do - include WaitForAjax - +feature 'Member autocomplete', :js do let(:project) { create(:project, :public) } let(:user) { create(:user) } - let(:participant) { create(:user) } let(:author) { create(:user) } + let(:note) { create(:note, noteable: noteable, project: noteable.project) } before do - allow_any_instance_of(Commit).to receive(:author).and_return(author) - login_as user + note # actually create the note + login_as(user) end - shared_examples "open suggestions" do - it 'displays suggestions' do - expect(page).to have_selector('.atwho-view', visible: true) - end - - it 'suggests author' do - page.within('.atwho-view', visible: true) do - expect(page).to have_content(author.username) + shared_examples "open suggestions when typing @" do + before do + page.within('.new-note') do + find('#note_note').send_keys('@') end end - it 'suggests participant' do + it 'suggests noteable author and note author' do page.within('.atwho-view', visible: true) do - expect(page).to have_content(participant.username) + expect(page).to have_content(author.username) + expect(page).to have_content(note.author.username) end end end - context 'adding a new note on a Issue', js: true do + context 'adding a new note on a Issue' do + let(:noteable) { create(:issue, author: author, project: project) } before do - issue = create(:issue, author: author, project: project) - create(:note, note: 'Ultralight Beam', noteable: issue, - project: project, author: participant) - visit_issue(project, issue) + visit namespace_project_issue_path(project.namespace, project, noteable) end - context 'when typing @' do - include_examples "open suggestions" - before do - open_member_suggestions - end - end + include_examples "open suggestions when typing @" end - context 'adding a new note on a Merge Request ', js: true do + context 'adding a new note on a Merge Request' do + let(:noteable) do + create(:merge_request, source_project: project, + target_project: project, author: author) + end before do - merge = create(:merge_request, source_project: project, target_project: project, author: author) - create(:note, note: 'Ultralight Beam', noteable: merge, - project: project, author: participant) - visit_merge_request(project, merge) + visit namespace_project_merge_request_path(project.namespace, project, noteable) end - context 'when typing @' do - include_examples "open suggestions" - before do - open_member_suggestions - end - end + include_examples "open suggestions when typing @" end - context 'adding a new note on a Commit ', js: true do - let(:commit) { project.commit } + context 'adding a new note on a Commit' do + let(:noteable) { project.commit } + let(:note) { create(:note_on_commit, project: project, commit_id: project.commit.id) } before do - allow(commit).to receive(:author).and_return(author) - create(:note_on_commit, author: participant, project: project, commit_id: project.repository.commit.id, note: 'No More Parties in LA') - visit_commit(project, commit) - end - - context 'when typing @' do - include_examples "open suggestions" - before do - open_member_suggestions - end - end - end + allow_any_instance_of(Commit).to receive(:author).and_return(author) - def open_member_suggestions - page.within('.new-note') do - find('#note_note').send_keys('@') + visit namespace_project_commit_path(project.namespace, project, noteable) end - wait_for_ajax - end - - def visit_issue(project, issue) - visit namespace_project_issue_path(project.namespace, project, issue) - end - - def visit_merge_request(project, merge) - visit namespace_project_merge_request_path(project.namespace, project, merge) - end - def visit_commit(project, commit) - visit namespace_project_commit_path(project.namespace, project, commit) + include_examples "open suggestions when typing @" end end diff --git a/spec/features/projects/blobs/user_create_spec.rb b/spec/features/projects/blobs/user_create_spec.rb index 03d08c12612..5686868a0c4 100644 --- a/spec/features/projects/blobs/user_create_spec.rb +++ b/spec/features/projects/blobs/user_create_spec.rb @@ -2,6 +2,7 @@ require 'spec_helper' feature 'New blob creation', feature: true, js: true do include WaitForAjax + include TargetBranchHelpers given(:user) { create(:user) } given(:role) { :developer } @@ -20,19 +21,6 @@ feature 'New blob creation', feature: true, js: true do execute_script("ace.edit('editor').setValue('#{content}')") end - def select_branch_index(index) - first('button.js-target-branch').click - wait_for_ajax - all('a[data-group="Branches"]')[index].click - end - - def create_new_branch(name) - first('button.js-target-branch').click - click_link 'Create new branch' - fill_in 'new_branch_name', with: name - click_button 'Create' - end - def commit_file click_button 'Commit Changes' end @@ -53,12 +41,12 @@ feature 'New blob creation', feature: true, js: true do context 'with different target branch' do background do edit_file - select_branch_index(0) + select_branch('feature') commit_file end scenario 'creates the blob in the different branch' do - expect(page).to have_content 'test' + expect(page).to have_content 'feature' expect(page).to have_content 'successfully created' end end diff --git a/spec/features/projects/compare_spec.rb b/spec/features/projects/compare_spec.rb index 030043d14aa..b2a3b111c9e 100644 --- a/spec/features/projects/compare_spec.rb +++ b/spec/features/projects/compare_spec.rb @@ -53,6 +53,7 @@ describe "Compare", js: true do dropdown = find(".js-compare-#{dropdown_type}-dropdown") dropdown.find(".compare-dropdown-toggle").click dropdown.fill_in("Filter by Git revision", with: selection) - find_link(selection, visible: true).click + wait_for_ajax + dropdown.find_all("a[data-ref=\"#{selection}\"]", visible: true).last.click end end diff --git a/spec/features/projects/deploy_keys_spec.rb b/spec/features/projects/deploy_keys_spec.rb new file mode 100644 index 00000000000..0b997f130ea --- /dev/null +++ b/spec/features/projects/deploy_keys_spec.rb @@ -0,0 +1,26 @@ +require 'spec_helper' + +describe 'Project deploy keys', feature: true do + let(:user) { create(:user) } + let(:project) { create(:project_empty_repo) } + + before do + project.team << [user, :master] + login_as(user) + end + + describe 'removing key' do + before do + create(:deploy_keys_project, project: project) + end + + it 'removes association between project and deploy key' do + visit namespace_project_settings_repository_path(project.namespace, project) + + page.within '.deploy-keys' do + expect { click_on 'Remove' } + .to change { project.deploy_keys.count }.by(-1) + end + end + end +end diff --git a/spec/features/projects/group_links_spec.rb b/spec/features/projects/group_links_spec.rb index 8b302a6aa23..4c28205da9b 100644 --- a/spec/features/projects/group_links_spec.rb +++ b/spec/features/projects/group_links_spec.rb @@ -8,7 +8,7 @@ feature 'Project group links', feature: true, js: true do let!(:group) { create(:group) } background do - project.team << [master, :master] + project.add_master(master) login_as(master) end @@ -29,4 +29,26 @@ feature 'Project group links', feature: true, js: true do end end end + + context 'nested group project' do + let!(:nested_group) { create(:group, parent: group) } + let!(:another_group) { create(:group) } + let!(:project) { create(:project, namespace: nested_group) } + + background do + group.add_master(master) + another_group.add_master(master) + end + + it 'does not show ancestors' do + visit namespace_project_settings_members_path(project.namespace, project) + + click_link 'Search for a group' + + page.within '.select2-drop' do + expect(page).to have_content(another_group.name) + expect(page).not_to have_content(group.name) + end + end + end end diff --git a/spec/features/projects/members/user_requests_access_spec.rb b/spec/features/projects/members/user_requests_access_spec.rb index b64c15e0adc..de25d45f447 100644 --- a/spec/features/projects/members/user_requests_access_spec.rb +++ b/spec/features/projects/members/user_requests_access_spec.rb @@ -61,7 +61,7 @@ feature 'Projects > Members > User requests access', feature: true do click_link('Settings') end - page.within('.page-with-layout-nav .sub-nav') do + page.within('.sub-nav') do click_link('Members') end end diff --git a/spec/features/projects/milestones/milestones_sorting_spec.rb b/spec/features/projects/milestones/milestones_sorting_spec.rb new file mode 100644 index 00000000000..da3eaed707a --- /dev/null +++ b/spec/features/projects/milestones/milestones_sorting_spec.rb @@ -0,0 +1,52 @@ +require 'spec_helper' + +feature 'Milestones sorting', :feature, :js do + include SortingHelper + let(:user) { create(:user) } + let(:project) { create(:empty_project, name: 'test', namespace: user.namespace) } + + before do + # Milestones + create(:milestone, + due_date: 10.days.from_now, + created_at: 2.hours.ago, + title: "aaa", project: project) + create(:milestone, + due_date: 11.days.from_now, + created_at: 1.hour.ago, + title: "bbb", project: project) + login_as(user) + end + + scenario 'visit project milestones and sort by due_date_asc' do + visit namespace_project_milestones_path(project.namespace, project) + + expect(page).to have_button('Due soon') + + # assert default sorting + within '.milestones' do + expect(page.all('ul.content-list > li').first.text).to include('aaa') + expect(page.all('ul.content-list > li').last.text).to include('bbb') + end + + click_button 'Due soon' + + sort_options = find('ul.dropdown-menu-sort li').all('a').collect(&:text) + + expect(sort_options[0]).to eq('Due soon') + expect(sort_options[1]).to eq('Due later') + expect(sort_options[2]).to eq('Start soon') + expect(sort_options[3]).to eq('Start later') + expect(sort_options[4]).to eq('Name, ascending') + expect(sort_options[5]).to eq('Name, descending') + + click_link 'Due later' + + expect(page).to have_button('Due later') + + within '.milestones' do + expect(page.all('ul.content-list > li').first.text).to include('bbb') + expect(page.all('ul.content-list > li').last.text).to include('aaa') + end + end +end diff --git a/spec/features/projects/pipelines/pipeline_spec.rb b/spec/features/projects/pipelines/pipeline_spec.rb index 9f06e52ab55..5a53e48f5f8 100644 --- a/spec/features/projects/pipelines/pipeline_spec.rb +++ b/spec/features/projects/pipelines/pipeline_spec.rb @@ -45,7 +45,7 @@ describe 'Pipeline', :feature, :js do include_context 'pipeline builds' let(:project) { create(:project) } - let(:pipeline) { create(:ci_pipeline, project: project, ref: 'master', sha: project.commit.id) } + let(:pipeline) { create(:ci_pipeline, project: project, ref: 'master', sha: project.commit.id, user: user) } before { visit namespace_project_pipeline_path(project.namespace, project, pipeline) } diff --git a/spec/features/projects/pipelines/pipelines_spec.rb b/spec/features/projects/pipelines/pipelines_spec.rb index 162056671e0..2272b19bc8f 100644 --- a/spec/features/projects/pipelines/pipelines_spec.rb +++ b/spec/features/projects/pipelines/pipelines_spec.rb @@ -442,7 +442,7 @@ describe 'Pipelines', :feature, :js do context 'when project is public' do let(:project) { create(:project, :public) } - it { expect(page).to have_content 'No pipelines to show' } + it { expect(page).to have_content 'Build with confidence' } it { expect(page).to have_http_status(:success) } end diff --git a/spec/features/projects/user_create_dir_spec.rb b/spec/features/projects/user_create_dir_spec.rb new file mode 100644 index 00000000000..2065abfb248 --- /dev/null +++ b/spec/features/projects/user_create_dir_spec.rb @@ -0,0 +1,72 @@ +require 'spec_helper' + +feature 'New directory creation', feature: true, js: true do + include WaitForAjax + include TargetBranchHelpers + + given(:user) { create(:user) } + given(:role) { :developer } + given(:project) { create(:project) } + + background do + login_as(user) + project.team << [user, role] + visit namespace_project_tree_path(project.namespace, project, 'master') + open_new_directory_modal + fill_in 'dir_name', with: 'new_directory' + end + + def open_new_directory_modal + first('.add-to-tree').click + click_link 'New directory' + end + + def create_directory + click_button 'Create directory' + end + + context 'with default target branch' do + background do + create_directory + end + + scenario 'creates the directory in the default branch' do + expect(page).to have_content 'master' + expect(page).to have_content 'The directory has been successfully created' + expect(page).to have_content 'new_directory' + end + end + + context 'with different target branch' do + background do + select_branch('feature') + create_directory + end + + scenario 'creates the directory in the different branch' do + expect(page).to have_content 'feature' + expect(page).to have_content 'The directory has been successfully created' + end + end + + context 'with a new target branch' do + given(:new_branch_name) { 'new-feature' } + + background do + create_new_branch(new_branch_name) + create_directory + end + + scenario 'creates the directory in the new branch' do + expect(page).to have_content new_branch_name + expect(page).to have_content 'The directory has been successfully created' + end + + scenario 'redirects to the merge request' do + expect(page).to have_content 'New Merge Request' + expect(page).to have_content "From #{new_branch_name} into master" + expect(page).to have_content 'Add new directory' + expect(current_path).to eq(new_namespace_project_merge_request_path(project.namespace, project)) + end + end +end diff --git a/spec/features/projects/wiki/user_git_access_wiki_page_spec.rb b/spec/features/projects/wiki/user_git_access_wiki_page_spec.rb new file mode 100644 index 00000000000..6825b95c8aa --- /dev/null +++ b/spec/features/projects/wiki/user_git_access_wiki_page_spec.rb @@ -0,0 +1,26 @@ +require 'spec_helper' + +describe 'Projects > Wiki > User views Git access wiki page', :feature do + let(:user) { create(:user) } + let(:project) { create(:project, :public) } + let(:wiki_page) do + WikiPages::CreateService.new( + project, + user, + title: 'home', + content: '[some link](other-page)' + ).execute + end + + before do + login_as(user) + end + + scenario 'Visit Wiki Page Current Commit' do + visit namespace_project_wiki_path(project.namespace, project, wiki_page) + + click_link 'Clone repository' + expect(page).to have_text("Clone repository #{project.wiki.path_with_namespace}") + expect(page).to have_text(project.wiki.http_url_to_repo(user)) + end +end diff --git a/spec/features/user_callout_spec.rb b/spec/features/user_callout_spec.rb index 336c4092c98..659cd7c7af7 100644 --- a/spec/features/user_callout_spec.rb +++ b/spec/features/user_callout_spec.rb @@ -2,6 +2,7 @@ require 'spec_helper' describe 'User Callouts', js: true do let(:user) { create(:user) } + let(:another_user) { create(:user) } let(:project) { create(:empty_project, path: 'gitlab', name: 'sample') } before do @@ -32,6 +33,11 @@ describe 'User Callouts', js: true do within('.user-callout') do find('.close-user-callout').click end - expect(page).not_to have_selector('#user-callout') + expect(page).not_to have_selector('.user-callout') + end + + it 'does not show callout on another users profile' do + visit user_path(another_user) + expect(page).not_to have_selector('.user-callout') end end diff --git a/spec/features/users/projects_spec.rb b/spec/features/users/projects_spec.rb new file mode 100644 index 00000000000..1d75fe434b0 --- /dev/null +++ b/spec/features/users/projects_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe 'Projects tab on a user profile', :feature, :js do + include WaitForAjax + + let(:user) { create(:user) } + let!(:project) { create(:empty_project, namespace: user.namespace) } + let!(:project2) { create(:empty_project, namespace: user.namespace) } + + before do + allow(Project).to receive(:default_per_page).and_return(1) + + login_as(user) + + visit user_path(user) + + page.within('.user-profile-nav') do + click_link('Personal projects') + end + + wait_for_ajax + end + + it 'paginates results' do + expect(page).to have_content(project2.name) + + click_link('Next') + + expect(page).to have_content(project.name) + end +end diff --git a/spec/helpers/avatars_helper_spec.rb b/spec/helpers/avatars_helper_spec.rb new file mode 100644 index 00000000000..581726c1d0e --- /dev/null +++ b/spec/helpers/avatars_helper_spec.rb @@ -0,0 +1,21 @@ +require 'rails_helper' + +describe AvatarsHelper do + let(:user) { create(:user) } + + describe '#user_avatar' do + subject { helper.user_avatar(user: user) } + + it "links to the user's profile" do + is_expected.to include("href=\"#{user_path(user)}\"") + end + + it "has the user's name as title" do + is_expected.to include("title=\"#{user.name}\"") + end + + it "contains the user's avatar image" do + is_expected.to include(CGI.escapeHTML(user.avatar_url(16))) + end + end +end diff --git a/spec/helpers/namespaces_helper_spec.rb b/spec/helpers/namespaces_helper_spec.rb new file mode 100644 index 00000000000..e5143a0263d --- /dev/null +++ b/spec/helpers/namespaces_helper_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +describe NamespacesHelper, type: :helper do + let!(:admin) { create(:admin) } + let!(:admin_group) { create(:group, :private) } + let!(:user) { create(:user) } + let!(:user_group) { create(:group, :private) } + + before do + admin_group.add_owner(admin) + user_group.add_owner(user) + end + + describe '#namespaces_options' do + it 'returns groups without being a member for admin' do + allow(helper).to receive(:current_user).and_return(admin) + + options = helper.namespaces_options(user_group.id, display_path: true, extra_group: user_group.id) + + expect(options).to include(admin_group.name) + expect(options).to include(user_group.name) + end + + it 'returns only allowed namespaces for user' do + allow(helper).to receive(:current_user).and_return(user) + + options = helper.namespaces_options + + expect(options).not_to include(admin_group.name) + expect(options).to include(user_group.name) + end + end +end diff --git a/spec/helpers/todos_helper_spec.rb b/spec/helpers/todos_helper_spec.rb index 21e0e74e008..50060a0925d 100644 --- a/spec/helpers/todos_helper_spec.rb +++ b/spec/helpers/todos_helper_spec.rb @@ -1,40 +1,6 @@ require "spec_helper" describe TodosHelper do - include GitlabRoutingHelper - - describe '#todo_target_path' do - let(:project) { create(:project) } - let(:merge_request) { create(:merge_request, target_project: project, source_project: project) } - let(:issue) { create(:issue, project: project) } - let(:note) { create(:note_on_issue, noteable: issue, project: project) } - - let(:mr_todo) { build(:todo, project: project, target: merge_request) } - let(:issue_todo) { build(:todo, project: project, target: issue) } - let(:note_todo) { build(:todo, project: project, target: issue, note: note) } - let(:build_failed_todo) { build(:todo, :build_failed, project: project, target: merge_request) } - - it 'returns correct path to the todo MR' do - expect(todo_target_path(mr_todo)). - to eq("/#{project.full_path}/merge_requests/#{merge_request.iid}") - end - - it 'returns correct path to the todo issue' do - expect(todo_target_path(issue_todo)). - to eq("/#{project.full_path}/issues/#{issue.iid}") - end - - it 'returns correct path to the todo note' do - expect(todo_target_path(note_todo)). - to eq("/#{project.full_path}/issues/#{issue.iid}#note_#{note.id}") - end - - it 'returns correct path to build_todo MR when pipeline failed' do - expect(todo_target_path(build_failed_todo)). - to eq("/#{project.full_path}/merge_requests/#{merge_request.iid}/pipelines") - end - end - describe '#todo_projects_options' do let(:projects) { create_list(:empty_project, 3) } let(:user) { create(:user) } diff --git a/spec/helpers/users_helper_spec.rb b/spec/helpers/users_helper_spec.rb new file mode 100644 index 00000000000..03f78de8e91 --- /dev/null +++ b/spec/helpers/users_helper_spec.rb @@ -0,0 +1,17 @@ +require 'rails_helper' + +describe UsersHelper do + let(:user) { create(:user) } + + describe '#user_link' do + subject { helper.user_link(user) } + + it "links to the user's profile" do + is_expected.to include("href=\"#{user_path(user)}\"") + end + + it "has the user's email as title" do + is_expected.to include("title=\"#{user.email}\"") + end + end +end diff --git a/spec/javascripts/boards/board_card_spec.js b/spec/javascripts/boards/board_card_spec.js index be31f644e20..73d18458366 100644 --- a/spec/javascripts/boards/board_card_spec.js +++ b/spec/javascripts/boards/board_card_spec.js @@ -1,10 +1,11 @@ -/* global Vue */ /* global List */ /* global ListLabel */ /* global listObj */ /* global boardsMockInterceptor */ /* global BoardService */ +import Vue from 'vue'; + require('~/boards/models/list'); require('~/boards/models/label'); require('~/boards/stores/boards_store'); diff --git a/spec/javascripts/boards/boards_store_spec.js b/spec/javascripts/boards/boards_store_spec.js index 1d1069600fc..e21f4ca2bc0 100644 --- a/spec/javascripts/boards/boards_store_spec.js +++ b/spec/javascripts/boards/boards_store_spec.js @@ -1,12 +1,13 @@ /* eslint-disable comma-dangle, one-var, no-unused-vars */ -/* global Vue */ /* global BoardService */ /* global boardsMockInterceptor */ -/* global Cookies */ /* global listObj */ /* global listObjDuplicate */ /* global ListIssue */ +import Vue from 'vue'; +import Cookies from 'js-cookie'; + require('~/lib/utils/url_utility'); require('~/boards/models/issue'); require('~/boards/models/label'); diff --git a/spec/javascripts/boards/issue_card_spec.js b/spec/javascripts/boards/issue_card_spec.js index 4340a571017..1a5e9e9fd07 100644 --- a/spec/javascripts/boards/issue_card_spec.js +++ b/spec/javascripts/boards/issue_card_spec.js @@ -1,9 +1,10 @@ -/* global Vue */ /* global ListUser */ /* global ListLabel */ /* global listObj */ /* global ListIssue */ +import Vue from 'vue'; + require('~/boards/models/issue'); require('~/boards/models/label'); require('~/boards/models/list'); diff --git a/spec/javascripts/boards/list_spec.js b/spec/javascripts/boards/list_spec.js index d49d3af33d9..66fc01fa1e5 100644 --- a/spec/javascripts/boards/list_spec.js +++ b/spec/javascripts/boards/list_spec.js @@ -1,5 +1,4 @@ /* eslint-disable comma-dangle */ -/* global Vue */ /* global boardsMockInterceptor */ /* global BoardService */ /* global List */ @@ -7,6 +6,8 @@ /* global listObj */ /* global listObjDuplicate */ +import Vue from 'vue'; + require('~/lib/utils/url_utility'); require('~/boards/models/issue'); require('~/boards/models/label'); diff --git a/spec/javascripts/boards/modal_store_spec.js b/spec/javascripts/boards/modal_store_spec.js index 1815847f3fa..80db816aff8 100644 --- a/spec/javascripts/boards/modal_store_spec.js +++ b/spec/javascripts/boards/modal_store_spec.js @@ -1,4 +1,3 @@ -/* global Vue */ /* global ListIssue */ require('~/boards/models/issue'); diff --git a/spec/javascripts/commit/pipelines/pipelines_spec.js b/spec/javascripts/commit/pipelines/pipelines_spec.js index 75efcc06585..bc2e092db65 100644 --- a/spec/javascripts/commit/pipelines/pipelines_spec.js +++ b/spec/javascripts/commit/pipelines/pipelines_spec.js @@ -33,7 +33,8 @@ describe('Pipelines table in Commits and Merge requests', () => { }); setTimeout(() => { - expect(component.$el.querySelector('.js-blank-state-title').textContent).toContain('No pipelines to show'); + expect(component.$el.querySelector('.empty-state')).toBeDefined(); + expect(component.$el.querySelector('.realtime-loading')).toBe(null); done(); }, 1); }); @@ -63,6 +64,7 @@ describe('Pipelines table in Commits and Merge requests', () => { setTimeout(() => { expect(component.$el.querySelectorAll('table > tbody > tr').length).toEqual(1); + expect(component.$el.querySelector('.realtime-loading')).toBe(null); done(); }, 0); }); @@ -92,7 +94,8 @@ describe('Pipelines table in Commits and Merge requests', () => { }); setTimeout(() => { - expect(component.$el.querySelector('.js-blank-state-title').textContent).toContain('No pipelines to show'); + expect(component.$el.querySelector('.js-pipelines-error-state')).toBeDefined(); + expect(component.$el.querySelector('.realtime-loading')).toBe(null); done(); }, 0); }); diff --git a/spec/javascripts/filtered_search/filtered_search_manager_spec.js b/spec/javascripts/filtered_search/filtered_search_manager_spec.js index ae9c263d1d7..848c7656a8d 100644 --- a/spec/javascripts/filtered_search/filtered_search_manager_spec.js +++ b/spec/javascripts/filtered_search/filtered_search_manager_spec.js @@ -58,7 +58,7 @@ const FilteredSearchSpecHelper = require('../helpers/filtered_search_spec_helper }); describe('search', () => { - const defaultParams = '?scope=all&utf8=✓&state=opened'; + const defaultParams = '?scope=all&utf8=%E2%9C%93&state=opened'; it('should search with a single word', (done) => { input.value = 'searchTerm'; @@ -246,5 +246,17 @@ const FilteredSearchSpecHelper = require('../helpers/filtered_search_spec_helper expect(gl.FilteredSearchVisualTokens.unselectTokens).toHaveBeenCalled(); }); }); + + describe('toggleInputContainerFocus', () => { + it('toggles on focus', () => { + input.focus(); + expect(document.querySelector('.filtered-search-input-container').classList.contains('focus')).toEqual(true); + }); + + it('toggles on blur', () => { + input.blur(); + expect(document.querySelector('.filtered-search-input-container').classList.contains('focus')).toEqual(false); + }); + }); }); })(); diff --git a/spec/javascripts/fixtures/pipelines.html.haml b/spec/javascripts/fixtures/pipelines.html.haml new file mode 100644 index 00000000000..418a38a0e2e --- /dev/null +++ b/spec/javascripts/fixtures/pipelines.html.haml @@ -0,0 +1,14 @@ +%div + #pipelines-list-vue{ data: { endpoint: 'foo', + "css-class" => 'foo', + "help-page-path" => 'foo', + "new-pipeline-path" => 'foo', + "can-create-pipeline" => 'true', + "all-path" => 'foo', + "pending-path" => 'foo', + "running-path" => 'foo', + "finished-path" => 'foo', + "branches-path" => 'foo', + "tags-path" => 'foo', + "has-ci" => 'foo', + "ci-lint-path" => 'foo' } } diff --git a/spec/javascripts/fixtures/pipelines_table.html.haml b/spec/javascripts/fixtures/pipelines_table.html.haml index fbe4a434f76..ad1682704bb 100644 --- a/spec/javascripts/fixtures/pipelines_table.html.haml +++ b/spec/javascripts/fixtures/pipelines_table.html.haml @@ -1,2 +1 @@ -#commit-pipeline-table-view{ data: { endpoint: "endpoint" } } -.pipeline-svgs{ data: { "commit_icon_svg": "svg"} } +#commit-pipeline-table-view{ data: { endpoint: "endpoint", "help-page-path": "foo" } } diff --git a/spec/javascripts/issuable_time_tracker_spec.js b/spec/javascripts/issuable_time_tracker_spec.js index cb068a4f879..0a830f25e29 100644 --- a/spec/javascripts/issuable_time_tracker_spec.js +++ b/spec/javascripts/issuable_time_tracker_spec.js @@ -1,7 +1,7 @@ -/* eslint-disable */ +/* eslint-disable no-unused-vars, space-before-function-paren, func-call-spacing, no-spaced-func, semi, max-len, quotes, space-infix-ops, padded-blocks */ + +import Vue from 'vue'; -require('jquery'); -require('vue'); require('~/issuable/time_tracking/components/time_tracker'); function initTimeTrackingComponent(opts) { diff --git a/spec/javascripts/lib/utils/common_utils_spec.js b/spec/javascripts/lib/utils/common_utils_spec.js index f4d3e77e515..d2e24eb7eb2 100644 --- a/spec/javascripts/lib/utils/common_utils_spec.js +++ b/spec/javascripts/lib/utils/common_utils_spec.js @@ -163,5 +163,72 @@ require('~/lib/utils/common_utils'); expect(gl.utils.isMetaClick(e)).toBe(true); }); }); + + describe('gl.utils.backOff', () => { + it('solves the promise from the callback', (done) => { + const expectedResponseValue = 'Success!'; + gl.utils.backOff((next, stop) => ( + new Promise((resolve) => { + resolve(expectedResponseValue); + }).then((resp) => { + stop(resp); + }) + )).then((respBackoff) => { + expect(respBackoff).toBe(expectedResponseValue); + done(); + }); + }); + + it('catches the rejected promise from the callback ', (done) => { + const errorMessage = 'Mistakes were made!'; + gl.utils.backOff((next, stop) => { + new Promise((resolve, reject) => { + reject(new Error(errorMessage)); + }).then((resp) => { + stop(resp); + }).catch(err => stop(err)); + }).catch((errBackoffResp) => { + expect(errBackoffResp instanceof Error).toBe(true); + expect(errBackoffResp.message).toBe(errorMessage); + done(); + }); + }); + + it('solves the promise correctly after retrying a third time', (done) => { + let numberOfCalls = 1; + const expectedResponseValue = 'Success!'; + gl.utils.backOff((next, stop) => ( + new Promise((resolve) => { + resolve(expectedResponseValue); + }).then((resp) => { + if (numberOfCalls < 3) { + numberOfCalls += 1; + next(); + } else { + stop(resp); + } + }) + )).then((respBackoff) => { + expect(respBackoff).toBe(expectedResponseValue); + expect(numberOfCalls).toBe(3); + done(); + }); + }, 10000); + + it('rejects the backOff promise after timing out', (done) => { + const expectedResponseValue = 'Success!'; + gl.utils.backOff(next => ( + new Promise((resolve) => { + resolve(expectedResponseValue); + }).then(() => { + setTimeout(next(), 5000); // it will time out + }) + ), 3000).catch((errBackoffResp) => { + expect(errBackoffResp instanceof Error).toBe(true); + expect(errBackoffResp.message).toBe('BACKOFF_TIMEOUT'); + done(); + }); + }, 10000); + }); }); })(); diff --git a/spec/javascripts/lib/utils/poll_spec.js b/spec/javascripts/lib/utils/poll_spec.js new file mode 100644 index 00000000000..c794a632417 --- /dev/null +++ b/spec/javascripts/lib/utils/poll_spec.js @@ -0,0 +1,163 @@ +import Vue from 'vue'; +import VueResource from 'vue-resource'; +import Poll from '~/lib/utils/poll'; + +Vue.use(VueResource); + +class ServiceMock { + constructor(endpoint) { + this.service = Vue.resource(endpoint); + } + + fetch() { + return this.service.get(); + } +} + +describe('Poll', () => { + let callbacks; + + beforeEach(() => { + callbacks = { + success: () => {}, + error: () => {}, + }; + + spyOn(callbacks, 'success'); + spyOn(callbacks, 'error'); + }); + + it('calls the success callback when no header for interval is provided', (done) => { + const successInterceptor = (request, next) => { + next(request.respondWith(JSON.stringify([]), { status: 200 })); + }; + + Vue.http.interceptors.push(successInterceptor); + + new Poll({ + resource: new ServiceMock('endpoint'), + method: 'fetch', + successCallback: callbacks.success, + errorCallback: callbacks.error, + }).makeRequest(); + + setTimeout(() => { + expect(callbacks.success).toHaveBeenCalled(); + expect(callbacks.error).not.toHaveBeenCalled(); + done(); + }, 0); + + Vue.http.interceptors = _.without(Vue.http.interceptors, successInterceptor); + }); + + it('calls the error callback whe the http request returns an error', (done) => { + const errorInterceptor = (request, next) => { + next(request.respondWith(JSON.stringify([]), { status: 500 })); + }; + + Vue.http.interceptors.push(errorInterceptor); + + new Poll({ + resource: new ServiceMock('endpoint'), + method: 'fetch', + successCallback: callbacks.success, + errorCallback: callbacks.error, + }).makeRequest(); + + setTimeout(() => { + expect(callbacks.success).not.toHaveBeenCalled(); + expect(callbacks.error).toHaveBeenCalled(); + done(); + }, 0); + + Vue.http.interceptors = _.without(Vue.http.interceptors, errorInterceptor); + }); + + it('should call the success callback when the interval header is -1', (done) => { + const intervalInterceptor = (request, next) => { + next(request.respondWith(JSON.stringify([]), { status: 200, headers: { 'poll-interval': -1 } })); + }; + + Vue.http.interceptors.push(intervalInterceptor); + + new Poll({ + resource: new ServiceMock('endpoint'), + method: 'fetch', + successCallback: callbacks.success, + errorCallback: callbacks.error, + }).makeRequest(); + + setTimeout(() => { + expect(callbacks.success).toHaveBeenCalled(); + expect(callbacks.error).not.toHaveBeenCalled(); + done(); + }, 0); + + Vue.http.interceptors = _.without(Vue.http.interceptors, intervalInterceptor); + }); + + it('starts polling when http status is 200 and interval header is provided', (done) => { + const pollInterceptor = (request, next) => { + next(request.respondWith(JSON.stringify([]), { status: 200, headers: { 'poll-interval': 2 } })); + }; + + Vue.http.interceptors.push(pollInterceptor); + + const service = new ServiceMock('endpoint'); + spyOn(service, 'fetch').and.callThrough(); + + new Poll({ + resource: service, + method: 'fetch', + data: { page: 1 }, + successCallback: callbacks.success, + errorCallback: callbacks.error, + }).makeRequest(); + + setTimeout(() => { + expect(service.fetch.calls.count()).toEqual(2); + expect(service.fetch).toHaveBeenCalledWith({ page: 1 }); + expect(callbacks.success).toHaveBeenCalled(); + expect(callbacks.error).not.toHaveBeenCalled(); + done(); + }, 5); + + Vue.http.interceptors = _.without(Vue.http.interceptors, pollInterceptor); + }); + + describe('stop', () => { + it('stops polling when method is called', (done) => { + const pollInterceptor = (request, next) => { + next(request.respondWith(JSON.stringify([]), { status: 200, headers: { 'poll-interval': 2 } })); + }; + + Vue.http.interceptors.push(pollInterceptor); + + const service = new ServiceMock('endpoint'); + spyOn(service, 'fetch').and.callThrough(); + + const Polling = new Poll({ + resource: service, + method: 'fetch', + data: { page: 1 }, + successCallback: () => { + Polling.stop(); + }, + errorCallback: callbacks.error, + }); + + spyOn(Polling, 'stop').and.callThrough(); + + Polling.makeRequest(); + + setTimeout(() => { + expect(service.fetch.calls.count()).toEqual(1); + expect(service.fetch).toHaveBeenCalledWith({ page: 1 }); + expect(Polling.stop).toHaveBeenCalled(); + done(); + }, 100); + + Vue.http.interceptors = _.without(Vue.http.interceptors, pollInterceptor); + }); + }); +}); diff --git a/spec/javascripts/test_bundle.js b/spec/javascripts/test_bundle.js index 5cdb6473eda..d658f680f97 100644 --- a/spec/javascripts/test_bundle.js +++ b/spec/javascripts/test_bundle.js @@ -8,9 +8,6 @@ jasmine.getJSONFixtures().fixturesPath = 'base/spec/javascripts/fixtures'; require('~/commons/index.js'); window.$ = window.jQuery = require('jquery'); window._ = require('underscore'); -window.Cookies = require('js-cookie'); -window.Vue = require('vue'); -window.Vue.use(require('vue-resource')); // stub expected globals window.gl = window.gl || {}; @@ -38,7 +35,8 @@ testsContext.keys().forEach(function (path) { if (process.env.BABEL_ENV === 'coverage') { // exempt these files from the coverage report const troubleMakers = [ - './blob_edit/blob_edit_bundle.js', + './blob_edit/blob_bundle.js', + './boards/boards_bundle.js', './cycle_analytics/components/stage_plan_component.js', './cycle_analytics/components/stage_staging_component.js', './cycle_analytics/components/stage_test_component.js', diff --git a/spec/javascripts/user_callout_spec.js b/spec/javascripts/user_callout_spec.js index 205e72af600..2398149d3ad 100644 --- a/spec/javascripts/user_callout_spec.js +++ b/spec/javascripts/user_callout_spec.js @@ -1,7 +1,7 @@ -const UserCallout = require('~/user_callout'); +import Cookies from 'js-cookie'; +import UserCallout from '~/user_callout'; const USER_CALLOUT_COOKIE = 'user_callout_dismissed'; -const Cookie = window.Cookies; describe('UserCallout', function () { const fixtureName = 'static/user_callout.html.raw'; @@ -9,7 +9,7 @@ describe('UserCallout', function () { beforeEach(() => { loadFixtures(fixtureName); - Cookie.remove(USER_CALLOUT_COOKIE); + Cookies.remove(USER_CALLOUT_COOKIE); this.userCallout = new UserCallout(); this.closeButton = $('.close-user-callout'); @@ -18,25 +18,25 @@ describe('UserCallout', function () { }); it('does not show when cookie is set not defined', () => { - expect(Cookie.get(USER_CALLOUT_COOKIE)).toBeUndefined(); + expect(Cookies.get(USER_CALLOUT_COOKIE)).toBeUndefined(); expect(this.userCalloutContainer.is(':visible')).toBe(true); }); it('shows when cookie is set to false', () => { - Cookie.set(USER_CALLOUT_COOKIE, 'false'); + Cookies.set(USER_CALLOUT_COOKIE, 'false'); - expect(Cookie.get(USER_CALLOUT_COOKIE)).toBeDefined(); + expect(Cookies.get(USER_CALLOUT_COOKIE)).toBeDefined(); expect(this.userCalloutContainer.is(':visible')).toBe(true); }); it('hides when user clicks on the dismiss-icon', () => { this.closeButton.click(); - expect(Cookie.get(USER_CALLOUT_COOKIE)).toBe('true'); + expect(Cookies.get(USER_CALLOUT_COOKIE)).toBe('true'); }); it('hides when user clicks on the "check it out" button', () => { this.userCalloutBtn.click(); - expect(Cookie.get(USER_CALLOUT_COOKIE)).toBe('true'); + expect(Cookies.get(USER_CALLOUT_COOKIE)).toBe('true'); }); }); @@ -46,7 +46,7 @@ describe('UserCallout when cookie is present', function () { beforeEach(() => { loadFixtures(fixtureName); - Cookie.set(USER_CALLOUT_COOKIE, 'true'); + Cookies.set(USER_CALLOUT_COOKIE, 'true'); this.userCallout = new UserCallout(); this.userCalloutContainer = $('.user-callout'); }); diff --git a/spec/javascripts/vue_pipelines_index/empty_state_spec.js b/spec/javascripts/vue_pipelines_index/empty_state_spec.js new file mode 100644 index 00000000000..733337168dc --- /dev/null +++ b/spec/javascripts/vue_pipelines_index/empty_state_spec.js @@ -0,0 +1,38 @@ +import Vue from 'vue'; +import emptyStateComp from '~/vue_pipelines_index/components/empty_state'; + +describe('Pipelines Empty State', () => { + let component; + let EmptyStateComponent; + + beforeEach(() => { + EmptyStateComponent = Vue.extend(emptyStateComp); + + component = new EmptyStateComponent({ + propsData: { + helpPagePath: 'foo', + }, + }).$mount(); + }); + + it('should render empty state SVG', () => { + expect(component.$el.querySelector('.svg-content svg')).toBeDefined(); + }); + + it('should render emtpy state information', () => { + expect(component.$el.querySelector('h4').textContent).toContain('Build with confidence'); + + expect( + component.$el.querySelector('p').textContent, + ).toContain('Continous Integration can help catch bugs by running your tests automatically'); + + expect( + component.$el.querySelector('p').textContent, + ).toContain('Continuous Deployment can help you deliver code to your product environment'); + }); + + it('should render a link with provided help path', () => { + expect(component.$el.querySelector('.btn-info').getAttribute('href')).toEqual('foo'); + expect(component.$el.querySelector('.btn-info').textContent).toContain('Get started with Pipelines'); + }); +}); diff --git a/spec/javascripts/vue_pipelines_index/error_state_spec.js b/spec/javascripts/vue_pipelines_index/error_state_spec.js new file mode 100644 index 00000000000..524e018b1fa --- /dev/null +++ b/spec/javascripts/vue_pipelines_index/error_state_spec.js @@ -0,0 +1,23 @@ +import Vue from 'vue'; +import errorStateComp from '~/vue_pipelines_index/components/error_state'; + +describe('Pipelines Error State', () => { + let component; + let ErrorStateComponent; + + beforeEach(() => { + ErrorStateComponent = Vue.extend(errorStateComp); + + component = new ErrorStateComponent().$mount(); + }); + + it('should render error state SVG', () => { + expect(component.$el.querySelector('.svg-content svg')).toBeDefined(); + }); + + it('should render emtpy state information', () => { + expect( + component.$el.querySelector('h4').textContent, + ).toContain('The API failed to fetch the pipelines'); + }); +}); diff --git a/spec/javascripts/vue_pipelines_index/mock_data.js b/spec/javascripts/vue_pipelines_index/mock_data.js new file mode 100644 index 00000000000..2365a662b9f --- /dev/null +++ b/spec/javascripts/vue_pipelines_index/mock_data.js @@ -0,0 +1,107 @@ +export default { + pipelines: [{ + id: 115, + user: { + name: 'Root', + username: 'root', + id: 1, + state: 'active', + avatar_url: 'http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\u0026d=identicon', + web_url: 'http://localhost:3000/root', + }, + path: '/root/review-app/pipelines/115', + details: { + status: { + icon: 'icon_status_failed', + text: 'failed', + label: 'failed', + group: 'failed', + has_details: true, + details_path: '/root/review-app/pipelines/115', + }, + duration: null, + finished_at: '2017-03-17T19:00:15.996Z', + stages: [{ + name: 'build', + title: 'build: failed', + status: { + icon: 'icon_status_failed', + text: 'failed', + label: 'failed', + group: 'failed', + has_details: true, + details_path: '/root/review-app/pipelines/115#build', + }, + path: '/root/review-app/pipelines/115#build', + dropdown_path: '/root/review-app/pipelines/115/stage.json?stage=build', + }, + { + name: 'review', + title: 'review: skipped', + status: { + icon: 'icon_status_skipped', + text: 'skipped', + label: 'skipped', + group: 'skipped', + has_details: true, + details_path: '/root/review-app/pipelines/115#review', + }, + path: '/root/review-app/pipelines/115#review', + dropdown_path: '/root/review-app/pipelines/115/stage.json?stage=review', + }], + artifacts: [], + manual_actions: [{ + name: 'stop_review', + path: '/root/review-app/builds/3766/play', + }], + }, + flags: { + latest: true, + triggered: false, + stuck: false, + yaml_errors: false, + retryable: true, + cancelable: false, + }, + ref: { + name: 'thisisabranch', + path: '/root/review-app/tree/thisisabranch', + tag: false, + branch: true, + }, + commit: { + id: '9e87f87625b26c42c59a2ee0398f81d20cdfe600', + short_id: '9e87f876', + title: 'Update README.md', + created_at: '2017-03-15T22:58:28.000+00:00', + parent_ids: ['3744f9226e699faec2662a8b267e5d3fd0bfff0e'], + message: 'Update README.md', + author_name: 'Root', + author_email: 'admin@example.com', + authored_date: '2017-03-15T22:58:28.000+00:00', + committer_name: 'Root', + committer_email: 'admin@example.com', + committed_date: '2017-03-15T22:58:28.000+00:00', + author: { + name: 'Root', + username: 'root', + id: 1, + state: 'active', + avatar_url: 'http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\u0026d=identicon', + web_url: 'http://localhost:3000/root', + }, + author_gravatar_url: 'http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\u0026d=identicon', + commit_url: 'http://localhost:3000/root/review-app/commit/9e87f87625b26c42c59a2ee0398f81d20cdfe600', + commit_path: '/root/review-app/commit/9e87f87625b26c42c59a2ee0398f81d20cdfe600', + }, + retry_path: '/root/review-app/pipelines/115/retry', + created_at: '2017-03-15T22:58:33.436Z', + updated_at: '2017-03-17T19:00:15.997Z', + }], + count: { + all: 52, + running: 0, + pending: 0, + finished: 52, + }, +}; diff --git a/spec/javascripts/vue_pipelines_index/nav_controls_spec.js b/spec/javascripts/vue_pipelines_index/nav_controls_spec.js new file mode 100644 index 00000000000..659c4854a56 --- /dev/null +++ b/spec/javascripts/vue_pipelines_index/nav_controls_spec.js @@ -0,0 +1,93 @@ +import Vue from 'vue'; +import navControlsComp from '~/vue_pipelines_index/components/nav_controls'; + +describe('Pipelines Nav Controls', () => { + let NavControlsComponent; + + beforeEach(() => { + NavControlsComponent = Vue.extend(navControlsComp); + }); + + it('should render link to create a new pipeline', () => { + const mockData = { + newPipelinePath: 'foo', + hasCiEnabled: true, + helpPagePath: 'foo', + ciLintPath: 'foo', + canCreatePipeline: true, + }; + + const component = new NavControlsComponent({ + propsData: mockData, + }).$mount(); + + expect(component.$el.querySelector('.btn-create').textContent).toContain('Run Pipeline'); + expect(component.$el.querySelector('.btn-create').getAttribute('href')).toEqual(mockData.newPipelinePath); + }); + + it('should not render link to create pipeline if no permission is provided', () => { + const mockData = { + newPipelinePath: 'foo', + hasCiEnabled: true, + helpPagePath: 'foo', + ciLintPath: 'foo', + canCreatePipeline: false, + }; + + const component = new NavControlsComponent({ + propsData: mockData, + }).$mount(); + + expect(component.$el.querySelector('.btn-create')).toEqual(null); + }); + + it('should render link for CI lint', () => { + const mockData = { + newPipelinePath: 'foo', + hasCiEnabled: true, + helpPagePath: 'foo', + ciLintPath: 'foo', + canCreatePipeline: true, + }; + + const component = new NavControlsComponent({ + propsData: mockData, + }).$mount(); + + expect(component.$el.querySelector('.btn-default').textContent).toContain('CI Lint'); + expect(component.$el.querySelector('.btn-default').getAttribute('href')).toEqual(mockData.ciLintPath); + }); + + it('should render link to help page when CI is not enabled', () => { + const mockData = { + newPipelinePath: 'foo', + hasCiEnabled: false, + helpPagePath: 'foo', + ciLintPath: 'foo', + canCreatePipeline: true, + }; + + const component = new NavControlsComponent({ + propsData: mockData, + }).$mount(); + + expect(component.$el.querySelector('.btn-info').textContent).toContain('Get started with Pipelines'); + expect(component.$el.querySelector('.btn-info').getAttribute('href')).toEqual(mockData.helpPagePath); + }); + + it('should not render link to help page when CI is enabled', () => { + const mockData = { + newPipelinePath: 'foo', + hasCiEnabled: true, + helpPagePath: 'foo', + ciLintPath: 'foo', + canCreatePipeline: true, + }; + + const component = new NavControlsComponent({ + propsData: mockData, + }).$mount(); + + expect(component.$el.querySelector('.btn-info')).toEqual(null); + }); +}); diff --git a/spec/javascripts/vue_pipelines_index/pipelines_spec.js b/spec/javascripts/vue_pipelines_index/pipelines_spec.js new file mode 100644 index 00000000000..725f6cb2d7a --- /dev/null +++ b/spec/javascripts/vue_pipelines_index/pipelines_spec.js @@ -0,0 +1,114 @@ +import Vue from 'vue'; +import pipelinesComp from '~/vue_pipelines_index/pipelines'; +import Store from '~/vue_pipelines_index/stores/pipelines_store'; +import pipelinesData from './mock_data'; + +describe('Pipelines', () => { + preloadFixtures('static/pipelines.html.raw'); + + let PipelinesComponent; + + beforeEach(() => { + loadFixtures('static/pipelines.html.raw'); + + PipelinesComponent = Vue.extend(pipelinesComp); + }); + + describe('successfull request', () => { + describe('with pipelines', () => { + const pipelinesInterceptor = (request, next) => { + next(request.respondWith(JSON.stringify(pipelinesData), { + status: 200, + })); + }; + + beforeEach(() => { + Vue.http.interceptors.push(pipelinesInterceptor); + }); + + afterEach(() => { + Vue.http.interceptors = _.without( + Vue.http.interceptors, pipelinesInterceptor, + ); + }); + + it('should render table', (done) => { + const component = new PipelinesComponent({ + propsData: { + store: new Store(), + }, + }).$mount(); + + setTimeout(() => { + expect(component.$el.querySelector('.table-holder')).toBeDefined(); + expect(component.$el.querySelector('.realtime-loading')).toBe(null); + done(); + }); + }); + }); + + describe('without pipelines', () => { + const emptyInterceptor = (request, next) => { + next(request.respondWith(JSON.stringify([]), { + status: 200, + })); + }; + + beforeEach(() => { + Vue.http.interceptors.push(emptyInterceptor); + }); + + afterEach(() => { + Vue.http.interceptors = _.without( + Vue.http.interceptors, emptyInterceptor, + ); + }); + + it('should render empty state', (done) => { + const component = new PipelinesComponent({ + propsData: { + store: new Store(), + }, + }).$mount(); + + setTimeout(() => { + expect(component.$el.querySelector('.empty-state')).toBeDefined(); + expect(component.$el.querySelector('.realtime-loading')).toBe(null); + done(); + }); + }); + }); + }); + + describe('unsuccessfull request', () => { + const errorInterceptor = (request, next) => { + next(request.respondWith(JSON.stringify([]), { + status: 500, + })); + }; + + beforeEach(() => { + Vue.http.interceptors.push(errorInterceptor); + }); + + afterEach(() => { + Vue.http.interceptors = _.without( + Vue.http.interceptors, errorInterceptor, + ); + }); + + it('should render error state', (done) => { + const component = new PipelinesComponent({ + propsData: { + store: new Store(), + }, + }).$mount(); + + setTimeout(() => { + expect(component.$el.querySelector('.js-pipelines-error-state')).toBeDefined(); + expect(component.$el.querySelector('.realtime-loading')).toBe(null); + done(); + }); + }); + }); +}); diff --git a/spec/lib/gitlab/ci/build/step_spec.rb b/spec/lib/gitlab/ci/build/step_spec.rb index 2a314a744ca..49457b129e3 100644 --- a/spec/lib/gitlab/ci/build/step_spec.rb +++ b/spec/lib/gitlab/ci/build/step_spec.rb @@ -25,7 +25,7 @@ describe Gitlab::Ci::Build::Step do end context 'when after_script is not empty' do - let(:job) { create(:ci_build, options: { after_script: "ls -la\ndate" }) } + let(:job) { create(:ci_build, options: { after_script: ['ls -la', 'date'] }) } it 'fabricates an object' do expect(subject.name).to eq(:after_script) diff --git a/spec/lib/gitlab/url_blocker_spec.rb b/spec/lib/gitlab/url_blocker_spec.rb new file mode 100644 index 00000000000..a504d299307 --- /dev/null +++ b/spec/lib/gitlab/url_blocker_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe Gitlab::UrlBlocker, lib: true do + describe '#blocked_url?' do + it 'allows imports from configured web host and port' do + import_url = "http://#{Gitlab.config.gitlab.host}:#{Gitlab.config.gitlab.port}/t.git" + expect(described_class.blocked_url?(import_url)).to be false + end + + it 'allows imports from configured SSH host and port' do + import_url = "http://#{Gitlab.config.gitlab_shell.ssh_host}:#{Gitlab.config.gitlab_shell.ssh_port}/t.git" + expect(described_class.blocked_url?(import_url)).to be false + end + + it 'returns true for bad localhost hostname' do + expect(described_class.blocked_url?('https://localhost:65535/foo/foo.git')).to be true + end + + it 'returns true for bad port' do + expect(described_class.blocked_url?('https://gitlab.com:25/foo/foo.git')).to be true + end + + it 'returns true for invalid URL' do + expect(described_class.blocked_url?('http://:8080')).to be true + end + + it 'returns false for legitimate URL' do + expect(described_class.blocked_url?('https://gitlab.com/foo/foo.git')).to be false + end + end +end diff --git a/spec/lib/gitlab/url_sanitizer_spec.rb b/spec/lib/gitlab/url_sanitizer_spec.rb index 3fd361de458..fc144a2556a 100644 --- a/spec/lib/gitlab/url_sanitizer_spec.rb +++ b/spec/lib/gitlab/url_sanitizer_spec.rb @@ -5,6 +5,7 @@ describe Gitlab::UrlSanitizer, lib: true do let(:url_sanitizer) do described_class.new("https://github.com/me/project.git", credentials: credentials) end + let(:user) { double(:user, username: 'john.doe') } describe '.sanitize' do def sanitize_url(url) @@ -53,12 +54,33 @@ describe Gitlab::UrlSanitizer, lib: true do end end + describe '.valid?' do + it 'validates url strings' do + expect(described_class.valid?(nil)).to be(false) + expect(described_class.valid?('valid@project:url.git')).to be(true) + expect(described_class.valid?('123://invalid:url')).to be(false) + end + end + + describe '.http_credentials_for_user' do + it { expect(described_class.http_credentials_for_user(user)).to eq({ user: 'john.doe' }) } + it { expect(described_class.http_credentials_for_user('foo')).to eq({}) } + end + describe '#sanitized_url' do it { expect(url_sanitizer.sanitized_url).to eq("https://github.com/me/project.git") } end describe '#credentials' do it { expect(url_sanitizer.credentials).to eq(credentials) } + + context 'when user is given to #initialize' do + let(:url_sanitizer) do + described_class.new("https://github.com/me/project.git", credentials: described_class.http_credentials_for_user(user)) + end + + it { expect(url_sanitizer.credentials).to eq({ user: 'john.doe' }) } + end end describe '#full_url' do @@ -69,13 +91,13 @@ describe Gitlab::UrlSanitizer, lib: true do expect(sanitizer.full_url).to eq('user@server:project.git') end - end - describe '.valid?' do - it 'validates url strings' do - expect(described_class.valid?(nil)).to be(false) - expect(described_class.valid?('valid@project:url.git')).to be(true) - expect(described_class.valid?('123://invalid:url')).to be(false) + context 'when user is given to #initialize' do + let(:url_sanitizer) do + described_class.new("https://github.com/me/project.git", credentials: described_class.http_credentials_for_user(user)) + end + + it { expect(url_sanitizer.full_url).to eq("https://john.doe@github.com/me/project.git") } end end end diff --git a/spec/lib/gitlab/visibility_level_spec.rb b/spec/lib/gitlab/visibility_level_spec.rb new file mode 100644 index 00000000000..3255c6f1ef7 --- /dev/null +++ b/spec/lib/gitlab/visibility_level_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +describe Gitlab::VisibilityLevel, lib: true do + describe '.level_value' do + it 'converts "public" to integer value' do + expect(described_class.level_value('public')).to eq(Gitlab::VisibilityLevel::PUBLIC) + end + + it 'converts string integer to integer value' do + expect(described_class.level_value('20')).to eq(20) + end + + it 'defaults to PRIVATE when string value is not valid' do + expect(described_class.level_value('invalid')).to eq(Gitlab::VisibilityLevel::PRIVATE) + end + + it 'defaults to PRIVATE when integer value is not valid' do + expect(described_class.level_value(100)).to eq(Gitlab::VisibilityLevel::PRIVATE) + end + end +end diff --git a/spec/mailers/emails/profile_spec.rb b/spec/mailers/emails/profile_spec.rb index e1877d5fde0..5ca936f28f0 100644 --- a/spec/mailers/emails/profile_spec.rb +++ b/spec/mailers/emails/profile_spec.rb @@ -5,6 +5,16 @@ describe Notify do include EmailSpec::Matchers include_context 'gitlab email notification' + shared_examples 'a new user email' do + it 'is sent to the new user with the correct subject and body' do + aggregate_failures do + is_expected.to deliver_to new_user_address + is_expected.to have_subject(/^Account was created for you$/i) + is_expected.to have_body_text(new_user_address) + end + end + end + describe 'profile notifications' do describe 'for new users, the email' do let(:example_site_path) { root_path } diff --git a/spec/mailers/notify_spec.rb b/spec/mailers/notify_spec.rb index 6ee91576676..4b72eb2eaa3 100644 --- a/spec/mailers/notify_spec.rb +++ b/spec/mailers/notify_spec.rb @@ -24,14 +24,14 @@ describe Notify do let(:previous_assignee) { create(:user, name: 'Previous Assignee') } shared_examples 'an assignee email' do - it 'is sent as the author' do - sender = subject.header[:from].addrs[0] - expect(sender.display_name).to eq(current_user.name) - expect(sender.address).to eq(gitlab_sender) - end + it 'is sent to the assignee as the author' do + sender = subject.header[:from].addrs.first - it 'is sent to the assignee' do - is_expected.to deliver_to assignee.email + aggregate_failures do + expect(sender.display_name).to eq(current_user.name) + expect(sender.address).to eq(gitlab_sender) + expect(subject).to deliver_to(assignee.email) + end end end @@ -49,12 +49,11 @@ describe Notify do it_behaves_like 'it should show Gmail Actions View Issue link' it_behaves_like 'an unsubscribeable thread' - it 'has the correct subject' do - is_expected.to have_referable_subject(issue) - end - - it 'contains a link to the new issue' do - is_expected.to have_body_text namespace_project_issue_path(project.namespace, project, issue) + it 'has the correct subject and body' do + aggregate_failures do + is_expected.to have_referable_subject(issue) + is_expected.to have_body_text(namespace_project_issue_path(project.namespace, project, issue)) + end end context 'when enabled email_author_in_body' do @@ -63,7 +62,7 @@ describe Notify do end it 'contains a link to note author' do - is_expected.to have_html_escaped_body_text issue.author_name + is_expected.to have_html_escaped_body_text(issue.author_name) is_expected.to have_body_text 'wrote:' end end @@ -95,20 +94,13 @@ describe Notify do expect(sender.address).to eq(gitlab_sender) end - it 'has the correct subject' do - is_expected.to have_referable_subject(issue, reply: true) - end - - it 'contains the name of the previous assignee' do - is_expected.to have_html_escaped_body_text previous_assignee.name - end - - it 'contains the name of the new assignee' do - is_expected.to have_html_escaped_body_text assignee.name - end - - it 'contains a link to the issue' do - is_expected.to have_body_text namespace_project_issue_path(project.namespace, project, issue) + it 'has the correct subject and body' do + aggregate_failures do + is_expected.to have_referable_subject(issue, reply: true) + is_expected.to have_html_escaped_body_text(previous_assignee.name) + is_expected.to have_html_escaped_body_text(assignee.name) + is_expected.to have_body_text(namespace_project_issue_path(project.namespace, project, issue)) + end end end @@ -129,16 +121,12 @@ describe Notify do expect(sender.address).to eq(gitlab_sender) end - it 'has the correct subject' do - is_expected.to have_referable_subject(issue, reply: true) - end - - it 'contains the names of the added labels' do - is_expected.to have_body_text 'foo, bar, and baz' - end - - it 'contains a link to the issue' do - is_expected.to have_body_text namespace_project_issue_path(project.namespace, project, issue) + it 'has the correct subject and body' do + aggregate_failures do + is_expected.to have_referable_subject(issue, reply: true) + is_expected.to have_body_text('foo, bar, and baz') + is_expected.to have_body_text(namespace_project_issue_path(project.namespace, project, issue)) + end end end @@ -158,20 +146,13 @@ describe Notify do expect(sender.address).to eq(gitlab_sender) end - it 'has the correct subject' do - is_expected.to have_referable_subject(issue, reply: true) - end - - it 'contains the new status' do - is_expected.to have_body_text status - end - - it 'contains the user name' do - is_expected.to have_html_escaped_body_text current_user.name - end - - it 'contains a link to the issue' do - is_expected.to have_body_text(namespace_project_issue_path project.namespace, project, issue) + it 'has the correct subject and body' do + aggregate_failures do + is_expected.to have_referable_subject(issue, reply: true) + is_expected.to have_body_text(status) + is_expected.to have_html_escaped_body_text(current_user.name) + is_expected.to have_body_text(namespace_project_issue_path project.namespace, project, issue) + end end end @@ -189,18 +170,15 @@ describe Notify do is_expected.to have_body_text 'Issue was moved to another project' end - it 'has the correct subject' do - is_expected.to have_referable_subject(issue, reply: true) - end - - it 'contains link to new issue' do + it 'has the correct subject and body' do new_issue_url = namespace_project_issue_path(new_issue.project.namespace, new_issue.project, new_issue) - is_expected.to have_body_text new_issue_url - end - it 'contains a link to the original issue' do - is_expected.to have_body_text namespace_project_issue_path(project.namespace, project, issue) + aggregate_failures do + is_expected.to have_referable_subject(issue, reply: true) + is_expected.to have_body_text(new_issue_url) + is_expected.to have_body_text(namespace_project_issue_path(project.namespace, project, issue)) + end end end end @@ -220,20 +198,13 @@ describe Notify do it_behaves_like 'it should show Gmail Actions View Merge request link' it_behaves_like 'an unsubscribeable thread' - it 'has the correct subject' do - is_expected.to have_referable_subject(merge_request) - end - - it 'contains a link to the new merge request' do - is_expected.to have_body_text namespace_project_merge_request_path(project.namespace, project, merge_request) - end - - it 'contains the source branch for the merge request' do - is_expected.to have_body_text merge_request.source_branch - end - - it 'contains the target branch for the merge request' do - is_expected.to have_body_text merge_request.target_branch + it 'has the correct subject and body' do + aggregate_failures do + is_expected.to have_referable_subject(merge_request) + is_expected.to have_body_text(namespace_project_merge_request_path(project.namespace, project, merge_request)) + is_expected.to have_body_text(merge_request.source_branch) + is_expected.to have_body_text(merge_request.target_branch) + end end context 'when enabled email_author_in_body' do @@ -275,20 +246,13 @@ describe Notify do expect(sender.address).to eq(gitlab_sender) end - it 'has the correct subject' do - is_expected.to have_referable_subject(merge_request, reply: true) - end - - it 'contains the name of the previous assignee' do - is_expected.to have_html_escaped_body_text previous_assignee.name - end - - it 'contains the name of the new assignee' do - is_expected.to have_html_escaped_body_text assignee.name - end - - it 'contains a link to the merge request' do - is_expected.to have_body_text namespace_project_merge_request_path(project.namespace, project, merge_request) + it 'has the correct subject and body' do + aggregate_failures do + is_expected.to have_referable_subject(merge_request, reply: true) + is_expected.to have_html_escaped_body_text(previous_assignee.name) + is_expected.to have_body_text(namespace_project_merge_request_path(project.namespace, project, merge_request)) + is_expected.to have_html_escaped_body_text(assignee.name) + end end end @@ -309,16 +273,10 @@ describe Notify do expect(sender.address).to eq(gitlab_sender) end - it 'has the correct subject' do + it 'has the correct subject and body' do is_expected.to have_referable_subject(merge_request, reply: true) - end - - it 'contains the names of the added labels' do - is_expected.to have_body_text 'foo, bar, and baz' - end - - it 'contains a link to the merge request' do - is_expected.to have_body_text namespace_project_merge_request_path(project.namespace, project, merge_request) + is_expected.to have_body_text('foo, bar, and baz') + is_expected.to have_body_text(namespace_project_merge_request_path(project.namespace, project, merge_request)) end end @@ -338,20 +296,13 @@ describe Notify do expect(sender.address).to eq(gitlab_sender) end - it 'has the correct subject' do - is_expected.to have_referable_subject(merge_request, reply: true) - end - - it 'contains the new status' do - is_expected.to have_body_text status - end - - it 'contains the user name' do - is_expected.to have_html_escaped_body_text current_user.name - end - - it 'contains a link to the merge request' do - is_expected.to have_body_text namespace_project_merge_request_path(project.namespace, project, merge_request) + it 'has the correct subject and body' do + aggregate_failures do + is_expected.to have_referable_subject(merge_request, reply: true) + is_expected.to have_body_text(status) + is_expected.to have_html_escaped_body_text(current_user.name) + is_expected.to have_body_text(namespace_project_merge_request_path(project.namespace, project, merge_request)) + end end end @@ -371,16 +322,12 @@ describe Notify do expect(sender.address).to eq(gitlab_sender) end - it 'has the correct subject' do - is_expected.to have_referable_subject(merge_request, reply: true) - end - - it 'contains the new status' do - is_expected.to have_body_text 'merged' - end - - it 'contains a link to the merge request' do - is_expected.to have_body_text namespace_project_merge_request_path(project.namespace, project, merge_request) + it 'has the correct subject and body' do + aggregate_failures do + is_expected.to have_referable_subject(merge_request, reply: true) + is_expected.to have_body_text('merged') + is_expected.to have_body_text(namespace_project_merge_request_path(project.namespace, project, merge_request)) + end end end end @@ -395,16 +342,10 @@ describe Notify do it_behaves_like 'it should not have Gmail Actions links' it_behaves_like "a user cannot unsubscribe through footer link" - it 'has the correct subject' do - is_expected.to have_subject "#{project.name} | Project was moved" - end - - it 'contains name of project' do + it 'has the correct subject and body' do + is_expected.to have_subject("#{project.name} | Project was moved") is_expected.to have_html_escaped_body_text project.name_with_namespace - end - - it 'contains new user role' do - is_expected.to have_body_text project.ssh_url_to_repo + is_expected.to have_body_text(project.ssh_url_to_repo) end end @@ -597,14 +538,14 @@ describe Notify do shared_examples 'a note email' do it_behaves_like 'it should have Gmail Actions links' - it 'is sent as the author' do + it 'is sent to the given recipient as the author' do sender = subject.header[:from].addrs[0] - expect(sender.display_name).to eq(note_author.name) - expect(sender.address).to eq(gitlab_sender) - end - it 'is sent to the given recipient' do - is_expected.to deliver_to recipient.notification_email + aggregate_failures do + expect(sender.display_name).to eq(note_author.name) + expect(sender.address).to eq(gitlab_sender) + expect(subject).to deliver_to(recipient.notification_email) + end end it 'contains the message from the note' do @@ -641,12 +582,11 @@ describe Notify do it_behaves_like 'it should show Gmail Actions View Commit link' it_behaves_like 'a user cannot unsubscribe through footer link' - it 'has the correct subject' do - is_expected.to have_subject "Re: #{project.name} | #{commit.title.strip} (#{commit.short_id})" - end - - it 'contains a link to the commit' do - is_expected.to have_body_text commit.short_id + it 'has the correct subject and body' do + aggregate_failures do + is_expected.to have_subject("Re: #{project.name} | #{commit.title.strip} (#{commit.short_id})") + is_expected.to have_body_text(commit.short_id) + end end end @@ -664,12 +604,11 @@ describe Notify do it_behaves_like 'it should show Gmail Actions View Merge request link' it_behaves_like 'an unsubscribeable thread' - it 'has the correct subject' do - is_expected.to have_referable_subject(merge_request, reply: true) - end - - it 'contains a link to the merge request note' do - is_expected.to have_body_text note_on_merge_request_path + it 'has the correct subject and body' do + aggregate_failures do + is_expected.to have_referable_subject(merge_request, reply: true) + is_expected.to have_body_text note_on_merge_request_path + end end end @@ -687,12 +626,11 @@ describe Notify do it_behaves_like 'it should show Gmail Actions View Issue link' it_behaves_like 'an unsubscribeable thread' - it 'has the correct subject' do - is_expected.to have_referable_subject(issue, reply: true) - end - - it 'contains a link to the issue note' do - is_expected.to have_body_text note_on_issue_path + it 'has the correct subject and body' do + aggregate_failures do + is_expected.to have_referable_subject(issue, reply: true) + is_expected.to have_body_text(note_on_issue_path) + end end end end @@ -717,14 +655,14 @@ describe Notify do it_behaves_like 'it should have Gmail Actions links' - it 'is sent as the author' do + it 'is sent to the given recipient as the author' do sender = subject.header[:from].addrs[0] - expect(sender.display_name).to eq(note_author.name) - expect(sender.address).to eq(gitlab_sender) - end - it 'is sent to the given recipient' do - is_expected.to deliver_to recipient.notification_email + aggregate_failures do + expect(sender.display_name).to eq(note_author.name) + expect(sender.address).to eq(gitlab_sender) + expect(subject).to deliver_to(recipient.notification_email) + end end it 'contains the message from the note' do @@ -934,21 +872,20 @@ describe Notify do is_expected.to deliver_to 'new-email@mail.com' end - it 'has the correct subject' do - is_expected.to have_subject 'Confirmation instructions | A Nice Suffix' - end - - it 'includes a link to the site' do - is_expected.to have_body_text example_site_path + it 'has the correct subject and body' do + aggregate_failures do + is_expected.to have_subject('Confirmation instructions | A Nice Suffix') + is_expected.to have_body_text(example_site_path) + end end end describe 'email on push for a created branch' do let(:example_site_path) { root_path } let(:user) { create(:user) } - let(:tree_path) { namespace_project_tree_path(project.namespace, project, "master") } + let(:tree_path) { namespace_project_tree_path(project.namespace, project, "empty-branch") } - subject { Notify.repository_push_email(project.id, author_id: user.id, ref: 'refs/heads/master', action: :create) } + subject { Notify.repository_push_email(project.id, author_id: user.id, ref: 'refs/heads/empty-branch', action: :create) } it_behaves_like 'it should not have Gmail Actions links' it_behaves_like 'a user cannot unsubscribe through footer link' @@ -961,12 +898,11 @@ describe Notify do expect(sender.address).to eq(gitlab_sender) end - it 'has the correct subject' do - is_expected.to have_subject "[Git][#{project.full_path}] Pushed new branch master" - end - - it 'contains a link to the branch' do - is_expected.to have_body_text tree_path + it 'has the correct subject and body' do + aggregate_failures do + is_expected.to have_subject("[Git][#{project.full_path}] Pushed new branch empty-branch") + is_expected.to have_body_text(tree_path) + end end end @@ -988,12 +924,11 @@ describe Notify do expect(sender.address).to eq(gitlab_sender) end - it 'has the correct subject' do - is_expected.to have_subject "[Git][#{project.full_path}] Pushed new tag v1.0" - end - - it 'contains a link to the tag' do - is_expected.to have_body_text tree_path + it 'has the correct subject and body' do + aggregate_failures do + is_expected.to have_subject("[Git][#{project.full_path}] Pushed new tag v1.0") + is_expected.to have_body_text(tree_path) + end end end @@ -1064,24 +999,14 @@ describe Notify do expect(sender.address).to eq(gitlab_sender) end - it 'has the correct subject' do - is_expected.to have_subject "[Git][#{project.full_path}][master] #{commits.length} commits: Ruby files modified" - end - - it 'includes commits list' do - is_expected.to have_body_text 'Change some files' - end - - it 'includes diffs with character-level highlighting' do - is_expected.to have_body_text 'def</span> <span class="nf">archive_formats_regex' - end - - it 'contains a link to the diff' do - is_expected.to have_body_text diff_path - end - - it 'does not contain the misleading footer' do - is_expected.not_to have_body_text 'you are a member of' + it 'has the correct subject and body' do + aggregate_failures do + is_expected.to have_subject("[Git][#{project.full_path}][master] #{commits.length} commits: Ruby files modified") + is_expected.to have_body_text('Change some files') + is_expected.to have_body_text('def</span> <span class="nf">archive_formats_regex') + is_expected.to have_body_text(diff_path) + is_expected.not_to have_body_text('you are a member of') + end end context "when set to send from committer email if domain matches" do @@ -1098,13 +1023,13 @@ describe Notify do end it "is sent from the committer email" do - sender = subject.header[:from].addrs[0] - expect(sender.address).to eq(user.email) - end + from = subject.header[:from].addrs.first + reply = subject.header[:reply_to].addrs.first - it "is set to reply to the committer email" do - sender = subject.header[:reply_to].addrs[0] - expect(sender.address).to eq(user.email) + aggregate_failures do + expect(from.address).to eq(user.email) + expect(reply.address).to eq(user.email) + end end end @@ -1115,13 +1040,13 @@ describe Notify do end it "is sent from the default email" do - sender = subject.header[:from].addrs[0] - expect(sender.address).to eq(gitlab_sender) - end + from = subject.header[:from].addrs.first + reply = subject.header[:reply_to].addrs.first - it "is set to reply to the default email" do - sender = subject.header[:reply_to].addrs[0] - expect(sender.address).to eq(gitlab_sender_reply_to) + aggregate_failures do + expect(from.address).to eq(gitlab_sender) + expect(reply.address).to eq(gitlab_sender_reply_to) + end end end @@ -1132,13 +1057,13 @@ describe Notify do end it "is sent from the default email" do - sender = subject.header[:from].addrs[0] - expect(sender.address).to eq(gitlab_sender) - end + from = subject.header[:from].addrs.first + reply = subject.header[:reply_to].addrs.first - it "is set to reply to the default email" do - sender = subject.header[:reply_to].addrs[0] - expect(sender.address).to eq(gitlab_sender_reply_to) + aggregate_failures do + expect(from.address).to eq(gitlab_sender) + expect(reply.address).to eq(gitlab_sender_reply_to) + end end end end @@ -1166,20 +1091,13 @@ describe Notify do expect(sender.address).to eq(gitlab_sender) end - it 'has the correct subject' do - is_expected.to have_subject "[Git][#{project.full_path}][master] #{commits.first.title}" - end - - it 'includes commits list' do - is_expected.to have_body_text 'Change some files' - end - - it 'includes diffs with character-level highlighting' do - is_expected.to have_body_text 'def</span> <span class="nf">archive_formats_regex' - end - - it 'contains a link to the diff' do - is_expected.to have_body_text diff_path + it 'has the correct subject and body' do + aggregate_failures do + is_expected.to have_subject("[Git][#{project.full_path}][master] #{commits.first.title}") + is_expected.to have_body_text('Change some files') + is_expected.to have_body_text('def</span> <span class="nf">archive_formats_regex') + is_expected.to have_body_text(diff_path) + end end end diff --git a/spec/models/concerns/has_status_spec.rb b/spec/models/concerns/has_status_spec.rb index f134da441c2..82abad0e2f6 100644 --- a/spec/models/concerns/has_status_spec.rb +++ b/spec/models/concerns/has_status_spec.rb @@ -110,6 +110,24 @@ describe HasStatus do it { is_expected.to eq 'running' } end + context 'when one status finished and second is still created' do + let!(:statuses) do + [create(type, status: :success), create(type, status: :created)] + end + + it { is_expected.to eq 'running' } + end + + context 'when there is a manual status before created status' do + let!(:statuses) do + [create(type, status: :success), + create(type, status: :manual, allow_failure: false), + create(type, status: :created)] + end + + it { is_expected.to eq 'manual' } + end + context 'when one status is a blocking manual action' do let!(:statuses) do [create(type, status: :failed), diff --git a/spec/models/concerns/issuable_spec.rb b/spec/models/concerns/issuable_spec.rb index 9574796a945..4522206fab1 100644 --- a/spec/models/concerns/issuable_spec.rb +++ b/spec/models/concerns/issuable_spec.rb @@ -44,6 +44,34 @@ describe Issue, "Issuable" do it { expect(described_class).to respond_to(:assigned) } end + describe 'author_name' do + it 'is delegated to author' do + expect(issue.author_name).to eq issue.author.name + end + + it 'returns nil when author is nil' do + issue.author_id = nil + issue.save(validate: false) + + expect(issue.author_name).to eq nil + end + end + + describe 'assignee_name' do + it 'is delegated to assignee' do + issue.update!(assignee: create(:user)) + + expect(issue.assignee_name).to eq issue.assignee.name + end + + it 'returns nil when assignee is nil' do + issue.assignee_id = nil + issue.save(validate: false) + + expect(issue.assignee_name).to eq nil + end + end + describe "before_save" do describe "#update_cache_counts" do context "when previous assignee exists" do diff --git a/spec/models/issue_spec.rb b/spec/models/issue_spec.rb index 73977d031f9..b8584301baa 100644 --- a/spec/models/issue_spec.rb +++ b/spec/models/issue_spec.rb @@ -670,4 +670,41 @@ describe Issue, models: true do expect(attrs_hash).to include('time_estimate') end end + + describe '#check_for_spam' do + let(:project) { create :project, visibility_level: visibility_level } + let(:issue) { create :issue, project: project } + + subject do + issue.assign_attributes(description: description) + issue.check_for_spam? + end + + context 'when project is public and spammable attributes changed' do + let(:visibility_level) { Gitlab::VisibilityLevel::PUBLIC } + let(:description) { 'woo' } + + it 'returns true' do + is_expected.to be_truthy + end + end + + context 'when project is private' do + let(:visibility_level) { Gitlab::VisibilityLevel::PRIVATE } + let(:description) { issue.description } + + it 'returns false' do + is_expected.to be_falsey + end + end + + context 'when spammable attributes have not changed' do + let(:visibility_level) { Gitlab::VisibilityLevel::PUBLIC } + let(:description) { issue.description } + + it 'returns false' do + is_expected.to be_falsey + end + end + end end diff --git a/spec/models/namespace_spec.rb b/spec/models/namespace_spec.rb index 757f3921450..67d48557184 100644 --- a/spec/models/namespace_spec.rb +++ b/spec/models/namespace_spec.rb @@ -215,10 +215,12 @@ describe Namespace, models: true do end describe '#descendants' do - let!(:group) { create(:group) } + let!(:group) { create(:group, path: 'git_lab') } let!(:nested_group) { create(:group, parent: group) } let!(:deep_nested_group) { create(:group, parent: nested_group) } let!(:very_deep_nested_group) { create(:group, parent: deep_nested_group) } + let!(:another_group) { create(:group, path: 'gitllab') } + let!(:another_group_nested) { create(:group, path: 'foo', parent: another_group) } it 'returns the correct descendants' do expect(very_deep_nested_group.descendants.to_a).to eq([]) diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 618ce2b6d53..59a2560ca06 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -218,6 +218,20 @@ describe Project, models: true do expect(project2.import_data).to be_nil end + it "does not allow blocked import_url localhost" do + project2 = build(:empty_project, import_url: 'http://localhost:9000/t.git') + + expect(project2).to be_invalid + expect(project2.errors[:import_url]).to include('imports are not allowed from that URL') + end + + it "does not allow blocked import_url port" do + project2 = build(:empty_project, import_url: 'http://github.com:25/t.git') + + expect(project2).to be_invalid + expect(project2.errors[:import_url]).to include('imports are not allowed from that URL') + end + describe 'project pending deletion' do let!(:project_pending_deletion) do create(:empty_project, @@ -1748,11 +1762,14 @@ describe Project, models: true do end describe 'inside_path' do - let!(:project1) { create(:empty_project) } + let!(:project1) { create(:empty_project, namespace: create(:namespace, path: 'name_pace')) } let!(:project2) { create(:empty_project) } + let!(:project3) { create(:empty_project, namespace: create(:namespace, path: 'namespace')) } let!(:path) { project1.namespace.full_path } - it { expect(Project.inside_path(path)).to eq([project1]) } + it 'returns correct project' do + expect(Project.inside_path(path)).to eq([project1]) + end end describe '#route_map_for' do @@ -1900,10 +1917,8 @@ describe Project, models: true do context 'when no user is given' do it 'returns the url to the repo without a username' do - url = project.http_url_to_repo - - expect(url).to eq(project.http_url_to_repo) - expect(url).not_to include('@') + expect(project.http_url_to_repo).to eq("#{project.web_url}.git") + expect(project.http_url_to_repo).not_to include('@') end end @@ -1911,7 +1926,7 @@ describe Project, models: true do it 'returns the url to the repo with the username' do user = build_stubbed(:user) - expect(project.http_url_to_repo(user)).to match(%r{https?:\/\/#{user.username}@}) + expect(project.http_url_to_repo(user)).to start_with("http://#{user.username}@") end end end diff --git a/spec/models/project_wiki_spec.rb b/spec/models/project_wiki_spec.rb index 58b57bd4fef..b5b9cd024b0 100644 --- a/spec/models/project_wiki_spec.rb +++ b/spec/models/project_wiki_spec.rb @@ -35,10 +35,23 @@ describe ProjectWiki, models: true do end describe "#http_url_to_repo" do - it "provides the full http url to the repo" do - gitlab_url = Gitlab.config.gitlab.url - repo_http_url = "#{gitlab_url}/#{subject.path_with_namespace}.git" - expect(subject.http_url_to_repo).to eq(repo_http_url) + let(:project) { create :empty_project } + + context 'when no user is given' do + it 'returns the url to the repo without a username' do + expected_url = "#{Gitlab.config.gitlab.url}/#{subject.path_with_namespace}.git" + + expect(project_wiki.http_url_to_repo).to eq(expected_url) + expect(project_wiki.http_url_to_repo).not_to include('@') + end + end + + context 'when user is given' do + it 'returns the url to the repo with the username' do + user = build_stubbed(:user) + + expect(project_wiki.http_url_to_repo(user)).to start_with("http://#{user.username}@") + end end end diff --git a/spec/models/route_spec.rb b/spec/models/route_spec.rb index 0b222022e62..171a51fcc5b 100644 --- a/spec/models/route_spec.rb +++ b/spec/models/route_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe Route, models: true do - let!(:group) { create(:group, path: 'gitlab', name: 'gitlab') } + let!(:group) { create(:group, path: 'git_lab', name: 'git_lab') } let!(:route) { group.route } describe 'relationships' do @@ -14,10 +14,24 @@ describe Route, models: true do it { is_expected.to validate_uniqueness_of(:path) } end + describe '.inside_path' do + let!(:nested_group) { create(:group, path: 'test', name: 'test', parent: group) } + let!(:deep_nested_group) { create(:group, path: 'foo', name: 'foo', parent: nested_group) } + let!(:another_group) { create(:group, path: 'other') } + let!(:similar_group) { create(:group, path: 'gitllab') } + let!(:another_group_nested) { create(:group, path: 'another', name: 'another', parent: similar_group) } + + it 'returns correct routes' do + expect(Route.inside_path('git_lab')).to match_array([nested_group.route, deep_nested_group.route]) + end + end + describe '#rename_descendants' do let!(:nested_group) { create(:group, path: 'test', name: 'test', parent: group) } let!(:deep_nested_group) { create(:group, path: 'foo', name: 'foo', parent: nested_group) } let!(:similar_group) { create(:group, path: 'gitlab-org', name: 'gitlab-org') } + let!(:another_group) { create(:group, path: 'gittlab', name: 'gitllab') } + let!(:another_group_nested) { create(:group, path: 'git_lab', name: 'git_lab', parent: another_group) } context 'path update' do context 'when route name is set' do @@ -28,6 +42,8 @@ describe Route, models: true do expect(described_class.exists?(path: 'bar/test')).to be_truthy expect(described_class.exists?(path: 'bar/test/foo')).to be_truthy expect(described_class.exists?(path: 'gitlab-org')).to be_truthy + expect(described_class.exists?(path: 'gittlab')).to be_truthy + expect(described_class.exists?(path: 'gittlab/git_lab')).to be_truthy end end @@ -43,14 +59,22 @@ describe Route, models: true do end context 'name update' do - before { route.update_attributes(name: 'bar') } - it "updates children routes with new path" do + route.update_attributes(name: 'bar') + expect(described_class.exists?(name: 'bar')).to be_truthy expect(described_class.exists?(name: 'bar / test')).to be_truthy expect(described_class.exists?(name: 'bar / test / foo')).to be_truthy expect(described_class.exists?(name: 'gitlab-org')).to be_truthy end + + it 'handles a rename from nil' do + # Note: using `update_columns` to skip all validation and callbacks + route.update_columns(name: nil) + + expect { route.update_attributes(name: 'bar') } + .to change { route.name }.from(nil).to('bar') + end end end end diff --git a/spec/models/snippet_spec.rb b/spec/models/snippet_spec.rb index 219ab1989ea..8095d01b69e 100644 --- a/spec/models/snippet_spec.rb +++ b/spec/models/snippet_spec.rb @@ -198,4 +198,47 @@ describe Snippet, models: true do expect(snippet.participants).to include(note1.author, note2.author) end end + + describe '#check_for_spam' do + let(:snippet) { create :snippet, visibility_level: visibility_level } + + subject do + snippet.assign_attributes(title: title) + snippet.check_for_spam? + end + + context 'when public and spammable attributes changed' do + let(:visibility_level) { Snippet::PUBLIC } + let(:title) { 'woo' } + + it 'returns true' do + is_expected.to be_truthy + end + end + + context 'when private' do + let(:visibility_level) { Snippet::PRIVATE } + let(:title) { snippet.title } + + it 'returns false' do + is_expected.to be_falsey + end + + it 'returns true when switching to public' do + snippet.save! + snippet.visibility_level = Snippet::PUBLIC + + expect(snippet.check_for_spam?).to be_truthy + end + end + + context 'when spammable attributes have not changed' do + let(:visibility_level) { Snippet::PUBLIC } + let(:title) { snippet.title } + + it 'returns false' do + is_expected.to be_falsey + end + end + end end diff --git a/spec/requests/api/issues_spec.rb b/spec/requests/api/issues_spec.rb index e7738ca3034..52f68fed2cc 100644 --- a/spec/requests/api/issues_spec.rb +++ b/spec/requests/api/issues_spec.rb @@ -534,6 +534,12 @@ describe API::Issues, api: true do describe "GET /projects/:id/issues" do let(:base_url) { "/projects/#{project.id}" } + it 'returns 404 when project does not exist' do + get api('/projects/1000/issues', non_member) + + expect(response).to have_http_status(404) + end + it "returns 404 on private projects for other users" do private_project = create(:empty_project, :private) create(:issue, project: private_project) diff --git a/spec/requests/api/v3/issues_spec.rb b/spec/requests/api/v3/issues_spec.rb index 1941ca0d7d8..51021eec63c 100644 --- a/spec/requests/api/v3/issues_spec.rb +++ b/spec/requests/api/v3/issues_spec.rb @@ -439,6 +439,12 @@ describe API::V3::Issues, api: true do describe "GET /projects/:id/issues" do let(:base_url) { "/projects/#{project.id}" } + it 'returns 404 when project does not exist' do + get v3_api('/projects/1000/issues', non_member) + + expect(response).to have_http_status(404) + end + it "returns 404 on private projects for other users" do private_project = create(:empty_project, :private) create(:issue, project: private_project) diff --git a/spec/services/boards/issues/list_service_spec.rb b/spec/services/boards/issues/list_service_spec.rb index 22115c6566d..d841bdaa292 100644 --- a/spec/services/boards/issues/list_service_spec.rb +++ b/spec/services/boards/issues/list_service_spec.rb @@ -30,6 +30,7 @@ describe Boards::Issues::ListService, services: true do let!(:closed_issue2) { create(:labeled_issue, :closed, project: project, labels: [p3]) } let!(:closed_issue3) { create(:issue, :closed, project: project) } let!(:closed_issue4) { create(:labeled_issue, :closed, project: project, labels: [p1]) } + let!(:closed_issue5) { create(:labeled_issue, :closed, project: project, labels: [development]) } before do project.team << [user, :developer] @@ -57,7 +58,7 @@ describe Boards::Issues::ListService, services: true do issues = described_class.new(project, user, params).execute - expect(issues).to eq [closed_issue4, closed_issue2, closed_issue3, closed_issue1] + expect(issues).to eq [closed_issue4, closed_issue2, closed_issue5, closed_issue3, closed_issue1] end it 'returns opened issues that have label list applied when listing issues from a label list' do diff --git a/spec/services/create_branch_service_spec.rb b/spec/services/create_branch_service_spec.rb new file mode 100644 index 00000000000..3f548688c20 --- /dev/null +++ b/spec/services/create_branch_service_spec.rb @@ -0,0 +1,24 @@ +require 'spec_helper' + +describe CreateBranchService, services: true do + let(:user) { create(:user) } + let(:service) { described_class.new(project, user) } + + describe '#execute' do + context 'when repository is empty' do + let(:project) { create(:project_empty_repo) } + + it 'creates master branch' do + service.execute('my-feature', 'master') + + expect(project.repository.branch_exists?('master')).to be_truthy + end + + it 'creates my-feature branch' do + service.execute('my-feature', 'master') + + expect(project.repository.branch_exists?('my-feature')).to be_truthy + end + end + end +end diff --git a/spec/services/issuable/bulk_update_service_spec.rb b/spec/services/issuable/bulk_update_service_spec.rb index 0475f38fe5e..7a1ac027310 100644 --- a/spec/services/issuable/bulk_update_service_spec.rb +++ b/spec/services/issuable/bulk_update_service_spec.rb @@ -138,7 +138,7 @@ describe Issuable::BulkUpdateService, services: true do let(:labels) { [bug, regression] } it 'updates the labels of all issues passed to the labels passed' do - expect(issues.map(&:reload).map(&:label_ids)).to all(eq(labels.map(&:id))) + expect(issues.map(&:reload).map(&:label_ids)).to all(match_array(labels.map(&:id))) end it 'does not update issues not passed in' do diff --git a/spec/services/projects/import_service_spec.rb b/spec/services/projects/import_service_spec.rb index ab6e8f537ba..e5917bb0b7a 100644 --- a/spec/services/projects/import_service_spec.rb +++ b/spec/services/projects/import_service_spec.rb @@ -120,6 +120,26 @@ describe Projects::ImportService, services: true do end end + context 'with blocked import_URL' do + it 'fails with localhost' do + project.import_url = 'https://localhost:9000/vim/vim.git' + + result = described_class.new(project, user).execute + + expect(result[:status]).to eq :error + expect(result[:message]).to end_with 'Blocked import URL.' + end + + it 'fails with port 25' do + project.import_url = "https://github.com:25/vim/vim.git" + + result = described_class.new(project, user).execute + + expect(result[:status]).to eq :error + expect(result[:message]).to end_with 'Blocked import URL.' + end + end + def stub_github_omniauth_provider provider = OpenStruct.new( 'name' => 'github', diff --git a/spec/services/spam_service_spec.rb b/spec/services/spam_service_spec.rb index 4ce3b95aa87..e09c05ccf32 100644 --- a/spec/services/spam_service_spec.rb +++ b/spec/services/spam_service_spec.rb @@ -19,42 +19,67 @@ describe SpamService, services: true do let(:issue) { create(:issue, project: project) } let(:request) { double(:request, env: {}) } - context 'when indicated as spam by akismet' do - before { allow(AkismetService).to receive(:new).and_return(double(is_spam?: true)) } + context 'when spammable attributes have not changed' do + before do + issue.closed_at = Time.zone.now - it 'doesnt check as spam when request is missing' do - check_spam(issue, nil, false) - - expect(issue.spam).to be_falsey + allow(AkismetService).to receive(:new).and_return(double(is_spam?: true)) end - it 'checks as spam' do - check_spam(issue, request, false) - - expect(issue.spam).to be_truthy + it 'returns false' do + expect(check_spam(issue, request, false)).to be_falsey end - it 'creates a spam log' do + it 'does not create a spam log' do expect { check_spam(issue, request, false) } - .to change { SpamLog.count }.from(0).to(1) + .not_to change { SpamLog.count } end + end - it 'doesnt yield block' do - expect(check_spam(issue, request, false)) - .to eql(SpamLog.last) + context 'when spammable attributes have changed' do + before do + issue.description = 'SPAM!' end - end - context 'when not indicated as spam by akismet' do - before { allow(AkismetService).to receive(:new).and_return(double(is_spam?: false)) } + context 'when indicated as spam by akismet' do + before do + allow(AkismetService).to receive(:new).and_return(double(is_spam?: true)) + end - it 'returns false' do - expect(check_spam(issue, request, false)).to be_falsey + it 'doesnt check as spam when request is missing' do + check_spam(issue, nil, false) + + expect(issue.spam).to be_falsey + end + + it 'checks as spam' do + check_spam(issue, request, false) + + expect(issue.spam).to be_truthy + end + + it 'creates a spam log' do + expect { check_spam(issue, request, false) } + .to change { SpamLog.count }.from(0).to(1) + end + + it 'doesnt yield block' do + expect(check_spam(issue, request, false)) + .to eql(SpamLog.last) + end end - it 'does not create a spam log' do - expect { check_spam(issue, request, false) } - .not_to change { SpamLog.count } + context 'when not indicated as spam by akismet' do + before { allow(AkismetService).to receive(:new).and_return(double(is_spam?: false)) } + + it 'returns false' do + expect(check_spam(issue, request, false)).to be_falsey + end + + it 'does not create a spam log' do + expect { check_spam(issue, request, false) } + .not_to change { SpamLog.count } + end end end end diff --git a/spec/services/system_hooks_service_spec.rb b/spec/services/system_hooks_service_spec.rb index db9f1231682..11037a4917b 100644 --- a/spec/services/system_hooks_service_spec.rb +++ b/spec/services/system_hooks_service_spec.rb @@ -5,6 +5,7 @@ describe SystemHooksService, services: true do let(:project) { create :project } let(:project_member) { create :project_member } let(:key) { create(:key, user: user) } + let(:deploy_key) { create(:key) } let(:group) { create(:group) } let(:group_member) { create(:group_member) } @@ -18,6 +19,8 @@ describe SystemHooksService, services: true do it { expect(event_data(project_member, :destroy)).to include(:event_name, :created_at, :updated_at, :project_name, :project_path, :project_path_with_namespace, :project_id, :user_name, :user_username, :user_email, :user_id, :access_level, :project_visibility) } it { expect(event_data(key, :create)).to include(:username, :key, :id) } it { expect(event_data(key, :destroy)).to include(:username, :key, :id) } + it { expect(event_data(deploy_key, :create)).to include(:key, :id) } + it { expect(event_data(deploy_key, :destroy)).to include(:key, :id) } it do project.old_path_with_namespace = 'renamed_from_path' diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ceb3209331f..5ab8f0d981a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -35,7 +35,8 @@ RSpec.configure do |config| config.include Warden::Test::Helpers, type: :request config.include LoginHelpers, type: :feature config.include SearchHelpers, type: :feature - config.include WaitForAjax, type: :feature + config.include WaitForRequests, :js + config.include WaitForAjax, :js config.include StubConfiguration config.include EmailHelpers, type: :mailer config.include TestEnv diff --git a/spec/support/notify_shared_examples.rb b/spec/support/notify_shared_examples.rb index a3724b801b3..16a425f2ca2 100644 --- a/spec/support/notify_shared_examples.rb +++ b/spec/support/notify_shared_examples.rb @@ -27,24 +27,14 @@ shared_examples 'a multiple recipients email' do end shared_examples 'an email sent from GitLab' do - it 'is sent from GitLab' do + it 'has the characteristics of an email sent from GitLab' do sender = subject.header[:from].addrs[0] - expect(sender.display_name).to eq(gitlab_sender_display_name) - expect(sender.address).to eq(gitlab_sender) - end - - it 'has a Reply-To address' do reply_to = subject.header[:reply_to].addresses - expect(reply_to).to eq([gitlab_sender_reply_to]) - end - - context 'when custom suffix for email subject is set' do - before do - stub_config_setting(email_subject_suffix: 'A Nice Suffix') - end - it 'ends the subject with the suffix' do - is_expected.to have_subject /\ \| A Nice Suffix$/ + aggregate_failures do + expect(sender.display_name).to eq(gitlab_sender_display_name) + expect(sender.address).to eq(gitlab_sender) + expect(reply_to).to eq([gitlab_sender_reply_to]) end end end @@ -56,43 +46,40 @@ shared_examples 'an email that contains a header with author username' do end shared_examples 'an email with X-GitLab headers containing project details' do - it 'has X-GitLab-Project* headers' do - is_expected.to have_header 'X-GitLab-Project', /#{project.name}/ - is_expected.to have_header 'X-GitLab-Project-Id', /#{project.id}/ - is_expected.to have_header 'X-GitLab-Project-Path', /#{project.path_with_namespace}/ + it 'has X-GitLab-Project headers' do + aggregate_failures do + is_expected.to have_header('X-GitLab-Project', /#{project.name}/) + is_expected.to have_header('X-GitLab-Project-Id', /#{project.id}/) + is_expected.to have_header('X-GitLab-Project-Path', /#{project.path_with_namespace}/) + end end end shared_examples 'a new thread email with reply-by-email enabled' do - let(:regex) { /\A<reply\-(.*)@#{Gitlab.config.gitlab.host}>\Z/ } - - it 'has a Message-ID header' do - is_expected.to have_header 'Message-ID', "<#{model.class.model_name.singular_route_key}_#{model.id}@#{Gitlab.config.gitlab.host}>" - end + it 'has the characteristics of a threaded email' do + host = Gitlab.config.gitlab.host + route_key = "#{model.class.model_name.singular_route_key}_#{model.id}" - it 'has a References header' do - is_expected.to have_header 'References', regex + aggregate_failures do + is_expected.to have_header('Message-ID', "<#{route_key}@#{host}>") + is_expected.to have_header('References', /\A<reply\-.*@#{host}>\Z/ ) + end end end shared_examples 'a thread answer email with reply-by-email enabled' do include_examples 'an email with X-GitLab headers containing project details' - let(:regex) { /\A<#{model.class.model_name.singular_route_key}_#{model.id}@#{Gitlab.config.gitlab.host}> <reply\-(.*)@#{Gitlab.config.gitlab.host}>\Z/ } - - it 'has a Message-ID header' do - is_expected.to have_header 'Message-ID', /\A<(.*)@#{Gitlab.config.gitlab.host}>\Z/ - end - - it 'has a In-Reply-To header' do - is_expected.to have_header 'In-Reply-To', "<#{model.class.model_name.singular_route_key}_#{model.id}@#{Gitlab.config.gitlab.host}>" - end - it 'has a References header' do - is_expected.to have_header 'References', regex - end + it 'has the characteristics of a threaded reply' do + host = Gitlab.config.gitlab.host + route_key = "#{model.class.model_name.singular_route_key}_#{model.id}" - it 'has a subject that begins with Re: ' do - is_expected.to have_subject /^Re: / + aggregate_failures do + is_expected.to have_header('Message-ID', /\A<.*@#{host}>\Z/) + is_expected.to have_header('In-Reply-To', "<#{route_key}@#{host}>") + is_expected.to have_header('References', /\A<#{route_key}@#{host}> <reply\-.*@#{host}>\Z/ ) + is_expected.to have_subject(/^Re: /) + end end end @@ -136,80 +123,77 @@ shared_examples 'an answer to an existing thread with reply-by-email enabled' do end end -shared_examples 'a new user email' do - it 'is sent to the new user' do - is_expected.to deliver_to new_user_address - end - - it 'has the correct subject' do - is_expected.to have_subject /^Account was created for you$/i - end - - it 'contains the new user\'s login name' do - is_expected.to have_body_text /#{new_user_address}/ - end -end - shared_examples 'it should have Gmail Actions links' do - it { is_expected.to have_body_text '<script type="application/ld+json">' } - it { is_expected.to have_body_text /ViewAction/ } + it do + aggregate_failures do + is_expected.to have_body_text('<script type="application/ld+json">') + is_expected.to have_body_text('ViewAction') + end + end end shared_examples 'it should not have Gmail Actions links' do - it { is_expected.not_to have_body_text '<script type="application/ld+json">' } - it { is_expected.not_to have_body_text /ViewAction/ } + it do + aggregate_failures do + is_expected.not_to have_body_text('<script type="application/ld+json">') + is_expected.not_to have_body_text('ViewAction') + end + end end shared_examples 'it should show Gmail Actions View Issue link' do it_behaves_like 'it should have Gmail Actions links' - it { is_expected.to have_body_text /View Issue/ } + it { is_expected.to have_body_text('View Issue') } end shared_examples 'it should show Gmail Actions View Merge request link' do it_behaves_like 'it should have Gmail Actions links' - it { is_expected.to have_body_text /View Merge request/ } + it { is_expected.to have_body_text('View Merge request') } end shared_examples 'it should show Gmail Actions View Commit link' do it_behaves_like 'it should have Gmail Actions links' - it { is_expected.to have_body_text /View Commit/ } + it { is_expected.to have_body_text('View Commit') } end shared_examples 'an unsubscribeable thread' do it_behaves_like 'an unsubscribeable thread with incoming address without %{key}' - it 'has a List-Unsubscribe header in the correct format' do - is_expected.to have_header 'List-Unsubscribe', /unsubscribe/ - is_expected.to have_header 'List-Unsubscribe', /mailto/ - is_expected.to have_header 'List-Unsubscribe', /^<.+,.+>$/ + it 'has a List-Unsubscribe header in the correct format, and a body link' do + aggregate_failures do + is_expected.to have_header('List-Unsubscribe', /unsubscribe/) + is_expected.to have_header('List-Unsubscribe', /mailto/) + is_expected.to have_header('List-Unsubscribe', /^<.+,.+>$/) + is_expected.to have_body_text('unsubscribe') + end end - - it { is_expected.to have_body_text /unsubscribe/ } end shared_examples 'an unsubscribeable thread with incoming address without %{key}' do include_context 'reply-by-email is enabled with incoming address without %{key}' - it 'has a List-Unsubscribe header in the correct format' do - is_expected.to have_header 'List-Unsubscribe', /unsubscribe/ - is_expected.not_to have_header 'List-Unsubscribe', /mailto/ - is_expected.to have_header 'List-Unsubscribe', /^<[^,]+>$/ + it 'has a List-Unsubscribe header in the correct format, and a body link' do + aggregate_failures do + is_expected.to have_header('List-Unsubscribe', /unsubscribe/) + is_expected.not_to have_header('List-Unsubscribe', /mailto/) + is_expected.to have_header('List-Unsubscribe', /^<[^,]+>$/) + is_expected.to have_body_text('unsubscribe') + end end - - it { is_expected.to have_body_text /unsubscribe/ } end shared_examples 'a user cannot unsubscribe through footer link' do - it 'does not have a List-Unsubscribe header' do - is_expected.not_to have_header 'List-Unsubscribe', /unsubscribe/ + it 'does not have a List-Unsubscribe header or a body link' do + aggregate_failures do + is_expected.not_to have_header('List-Unsubscribe', /unsubscribe/) + is_expected.not_to have_body_text('unsubscribe') + end end - - it { is_expected.not_to have_body_text /unsubscribe/ } end shared_examples 'an email with a labels subscriptions link in its footer' do - it { is_expected.to have_body_text /label subscriptions/ } + it { is_expected.to have_body_text('label subscriptions') } end diff --git a/spec/support/prometheus_helpers.rb b/spec/support/prometheus_helpers.rb index a52d8f37d14..4afdbd68304 100644 --- a/spec/support/prometheus_helpers.rb +++ b/spec/support/prometheus_helpers.rb @@ -1,10 +1,10 @@ module PrometheusHelpers def prometheus_memory_query(environment_slug) - %{sum(container_memory_usage_bytes{container_name="app",environment="#{environment_slug}"})/1024/1024} + %{(sum(container_memory_usage_bytes{container_name="app",environment="#{environment_slug}"}) / count(container_memory_usage_bytes{container_name="app",environment="#{environment_slug}"})) /1024/1024} end def prometheus_cpu_query(environment_slug) - %{sum(rate(container_cpu_usage_seconds_total{container_name="app",environment="#{environment_slug}"}[2m]))} + %{sum(rate(container_cpu_usage_seconds_total{container_name="app",environment="#{environment_slug}"}[2m])) / count(container_cpu_usage_seconds_total{container_name="app",environment="#{environment_slug}"}) * 100} end def prometheus_query_url(prometheus_query) diff --git a/spec/support/slack_mattermost_notifications_shared_examples.rb b/spec/support/slack_mattermost_notifications_shared_examples.rb index 704922b6cf4..b902fe90707 100644 --- a/spec/support/slack_mattermost_notifications_shared_examples.rb +++ b/spec/support/slack_mattermost_notifications_shared_examples.rb @@ -324,5 +324,24 @@ RSpec.shared_examples 'slack or mattermost notifications' do it_behaves_like 'call Slack/Mattermost API' end end + + context 'only notify for the default branch' do + context 'when enabled' do + let(:pipeline) do + create(:ci_pipeline, project: project, status: 'failed', ref: 'not-the-default-branch') + end + + before do + chat_service.notify_only_default_branch = true + end + + it 'does not call the Slack/Mattermost API for pipeline events' do + data = Gitlab::DataBuilder::Pipeline.build(pipeline) + result = chat_service.execute(data) + + expect(result).to be_falsy + end + end + end end end diff --git a/spec/support/target_branch_helpers.rb b/spec/support/target_branch_helpers.rb new file mode 100644 index 00000000000..3ee8f0f657e --- /dev/null +++ b/spec/support/target_branch_helpers.rb @@ -0,0 +1,16 @@ +module TargetBranchHelpers + def select_branch(name) + first('button.js-target-branch').click + wait_for_ajax + all('a[data-group="Branches"]').find do |el| + el.text == name + end.click + end + + def create_new_branch(name) + first('button.js-target-branch').click + click_link 'Create new branch' + fill_in 'new_branch_name', with: name + click_button 'Create' + end +end diff --git a/spec/support/test_env.rb b/spec/support/test_env.rb index f1d226b6ae3..648b0380f18 100644 --- a/spec/support/test_env.rb +++ b/spec/support/test_env.rb @@ -37,9 +37,10 @@ module TestEnv 'conflict-too-large' => '39fa04f', 'deleted-image-test' => '6c17798', 'wip' => 'b9238ee', - 'csv' => '3dd0896' + 'csv' => '3dd0896', + 'v1.1.0' => 'b83d6e3' }.freeze - + # gitlab-test-fork is a fork of gitlab-fork, but we don't necessarily # need to keep all the branches in sync. # We currently only need a subset of the branches diff --git a/spec/support/wait_for_requests.rb b/spec/support/wait_for_requests.rb new file mode 100644 index 00000000000..0bfa7f72ff8 --- /dev/null +++ b/spec/support/wait_for_requests.rb @@ -0,0 +1,32 @@ +module WaitForRequests + extend self + + # This is inspired by http://www.salsify.com/blog/engineering/tearing-capybara-ajax-tests + def wait_for_requests_complete + Gitlab::Testing::RequestBlockerMiddleware.block_requests! + wait_for('pending AJAX requests complete') do + Gitlab::Testing::RequestBlockerMiddleware.num_active_requests.zero? + end + ensure + Gitlab::Testing::RequestBlockerMiddleware.allow_requests! + end + + # Waits until the passed block returns true + def wait_for(condition_name, max_wait_time: Capybara.default_max_wait_time, polling_interval: 0.01) + wait_until = Time.now + max_wait_time.seconds + loop do + break if yield + if Time.now > wait_until + raise "Condition not met: #{condition_name}" + else + sleep(polling_interval) + end + end + end +end + +RSpec.configure do |config| + config.after(:each, :js) do + wait_for_requests_complete + end +end diff --git a/spec/support/wait_for_vue_resource.rb b/spec/support/wait_for_vue_resource.rb index 1029f84716f..4a4e2e16ee7 100644 --- a/spec/support/wait_for_vue_resource.rb +++ b/spec/support/wait_for_vue_resource.rb @@ -1,7 +1,7 @@ module WaitForVueResource def wait_for_vue_resource(spinner: true) Timeout.timeout(Capybara.default_max_wait_time) do - loop until page.evaluate_script('Vue.activeResources').zero? + loop until page.evaluate_script('window.activeVueResources').zero? end end end diff --git a/spec/tasks/gitlab/gitaly_rake_spec.rb b/spec/tasks/gitlab/gitaly_rake_spec.rb new file mode 100644 index 00000000000..d95baddf546 --- /dev/null +++ b/spec/tasks/gitlab/gitaly_rake_spec.rb @@ -0,0 +1,78 @@ +require 'rake_helper' + +describe 'gitlab:gitaly namespace rake task' do + before :all do + Rake.application.rake_require 'tasks/gitlab/gitaly' + end + + describe 'install' do + let(:repo) { 'https://gitlab.com/gitlab-org/gitaly.git' } + let(:clone_path) { Rails.root.join('tmp/tests/gitaly').to_s } + let(:tag) { "v#{File.read(Rails.root.join(Gitlab::GitalyClient::SERVER_VERSION_FILE)).chomp}" } + + context 'no dir given' do + it 'aborts and display a help message' do + # avoid writing task output to spec progress + allow($stderr).to receive :write + expect { run_rake_task('gitlab:gitaly:install') }.to raise_error /Please specify the directory where you want to install gitaly/ + end + end + + context 'when an underlying Git command fail' do + it 'aborts and display a help message' do + expect_any_instance_of(Object). + to receive(:checkout_or_clone_tag).and_raise 'Git error' + + expect { run_rake_task('gitlab:gitaly:install', clone_path) }.to raise_error 'Git error' + end + end + + describe 'checkout or clone' do + before do + expect(Dir).to receive(:chdir).with(clone_path) + end + + it 'calls checkout_or_clone_tag with the right arguments' do + expect_any_instance_of(Object). + to receive(:checkout_or_clone_tag).with(tag: tag, repo: repo, target_dir: clone_path) + + run_rake_task('gitlab:gitaly:install', clone_path) + end + end + + describe 'gmake/make' do + before do + FileUtils.mkdir_p(clone_path) + expect(Dir).to receive(:chdir).with(clone_path).and_call_original + end + + context 'gmake is available' do + before do + expect_any_instance_of(Object).to receive(:checkout_or_clone_tag) + allow_any_instance_of(Object).to receive(:run_command!).with(['gmake']).and_return(true) + end + + it 'calls gmake in the gitaly directory' do + expect(Gitlab::Popen).to receive(:popen).with(%w[which gmake]).and_return(['/usr/bin/gmake', 0]) + expect_any_instance_of(Object).to receive(:run_command!).with(['gmake']).and_return(true) + + run_rake_task('gitlab:gitaly:install', clone_path) + end + end + + context 'gmake is not available' do + before do + expect_any_instance_of(Object).to receive(:checkout_or_clone_tag) + allow_any_instance_of(Object).to receive(:run_command!).with(['make']).and_return(true) + end + + it 'calls make in the gitaly directory' do + expect(Gitlab::Popen).to receive(:popen).with(%w[which gmake]).and_return(['', 42]) + expect_any_instance_of(Object).to receive(:run_command!).with(['make']).and_return(true) + + run_rake_task('gitlab:gitaly:install', clone_path) + end + end + end + end +end diff --git a/spec/tasks/gitlab/workhorse_rake_spec.rb b/spec/tasks/gitlab/workhorse_rake_spec.rb index 6de66c3cf07..8a66a4aa047 100644 --- a/spec/tasks/gitlab/workhorse_rake_spec.rb +++ b/spec/tasks/gitlab/workhorse_rake_spec.rb @@ -9,9 +9,6 @@ describe 'gitlab:workhorse namespace rake task' do let(:repo) { 'https://gitlab.com/gitlab-org/gitlab-workhorse.git' } let(:clone_path) { Rails.root.join('tmp/tests/gitlab-workhorse').to_s } let(:tag) { "v#{File.read(Rails.root.join(Gitlab::Workhorse::VERSION_FILE)).chomp}" } - before do - allow(ENV).to receive(:[]) - end context 'no dir given' do it 'aborts and display a help message' do diff --git a/spec/tasks/tokens_spec.rb b/spec/tasks/tokens_spec.rb new file mode 100644 index 00000000000..19036c7677c --- /dev/null +++ b/spec/tasks/tokens_spec.rb @@ -0,0 +1,21 @@ +require 'rake_helper' + +describe 'tokens rake tasks' do + let!(:user) { create(:user) } + + before do + Rake.application.rake_require 'tasks/tokens' + end + + describe 'reset_all task' do + it 'invokes create_hooks task' do + expect { run_rake_task('tokens:reset_all_auth') }.to change { user.reload.authentication_token } + end + end + + describe 'reset_all_email task' do + it 'invokes create_hooks task' do + expect { run_rake_task('tokens:reset_all_email') }.to change { user.reload.incoming_email_token } + end + end +end diff --git a/spec/views/projects/pipelines/show.html.haml_spec.rb b/spec/views/projects/pipelines/show.html.haml_spec.rb index c101f6f164d..e4aeaeca508 100644 --- a/spec/views/projects/pipelines/show.html.haml_spec.rb +++ b/spec/views/projects/pipelines/show.html.haml_spec.rb @@ -3,8 +3,9 @@ require 'spec_helper' describe 'projects/pipelines/show' do include Devise::Test::ControllerHelpers + let(:user) { create(:user) } let(:project) { create(:project) } - let(:pipeline) { create(:ci_empty_pipeline, project: project, sha: project.commit.id) } + let(:pipeline) { create(:ci_empty_pipeline, project: project, sha: project.commit.id, user: user) } before do controller.prepend_view_path('app/views/projects') @@ -21,6 +22,7 @@ describe 'projects/pipelines/show' do assign(:project, project) assign(:pipeline, pipeline) + assign(:commit, project.commit) allow(view).to receive(:can?).and_return(true) end @@ -31,6 +33,12 @@ describe 'projects/pipelines/show' do expect(rendered).to have_css('.js-pipeline-graph') expect(rendered).to have_css('.js-grouped-pipeline-dropdown') + # header + expect(rendered).to have_text("##{pipeline.id}") + expect(rendered).to have_css('time', text: pipeline.created_at.strftime("%b %d, %Y")) + expect(rendered).to have_selector(%Q(img[alt$="#{pipeline.user.name}'s avatar"])) + expect(rendered).to have_link(pipeline.user.name, href: user_path(pipeline.user)) + # stages expect(rendered).to have_text('Build') expect(rendered).to have_text('Test') |