diff options
Diffstat (limited to 'spec')
34 files changed, 721 insertions, 276 deletions
diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb index 4e91068ab88..efc3ce74627 100644 --- a/spec/controllers/application_controller_spec.rb +++ b/spec/controllers/application_controller_spec.rb @@ -650,7 +650,7 @@ describe ApplicationController do describe '#access_denied' do controller(described_class) do def index - access_denied!(params[:message]) + access_denied!(params[:message], params[:status]) end end @@ -669,6 +669,12 @@ describe ApplicationController do expect(response).to have_gitlab_http_status(403) end + + it 'renders a status passed to access denied' do + get :index, status: 401 + + expect(response).to have_gitlab_http_status(401) + end end context 'when invalid UTF-8 parameters are received' do diff --git a/spec/factories/clusters/kubernetes_namespaces.rb b/spec/factories/clusters/kubernetes_namespaces.rb index 3a4f5193550..6ad93fb0f45 100644 --- a/spec/factories/clusters/kubernetes_namespaces.rb +++ b/spec/factories/clusters/kubernetes_namespaces.rb @@ -3,7 +3,6 @@ FactoryBot.define do factory :cluster_kubernetes_namespace, class: Clusters::KubernetesNamespace do association :cluster, :project, :provided_by_gcp - namespace { |n| "environment#{n}" } after(:build) do |kubernetes_namespace| cluster_project = kubernetes_namespace.cluster.cluster_project diff --git a/spec/features/calendar_spec.rb b/spec/features/calendar_spec.rb index a1f93bd3fbd..8cb9b57a049 100644 --- a/spec/features/calendar_spec.rb +++ b/spec/features/calendar_spec.rb @@ -64,7 +64,7 @@ describe 'Contributions Calendar', :js do end def selected_day_activities(visible: true) - find('.tab-pane#activity .user-calendar-activities', visible: visible).text + find('#js-overview .user-calendar-activities', visible: visible).text end before do @@ -74,16 +74,16 @@ describe 'Contributions Calendar', :js do describe 'calendar day selection' do before do visit user.username - page.find('.js-activity-tab a').click + page.find('.js-overview-tab a').click wait_for_requests end it 'displays calendar' do - expect(find('.tab-pane#activity')).to have_css('.js-contrib-calendar') + expect(find('#js-overview')).to have_css('.js-contrib-calendar') end describe 'select calendar day' do - let(:cells) { page.all('.tab-pane#activity .user-contrib-cell') } + let(:cells) { page.all('#js-overview .user-contrib-cell') } before do cells[0].click @@ -109,7 +109,7 @@ describe 'Contributions Calendar', :js do describe 'deselect calendar day' do before do cells[0].click - page.find('.js-activity-tab a').click + page.find('.js-overview-tab a').click wait_for_requests end @@ -124,7 +124,7 @@ describe 'Contributions Calendar', :js do shared_context 'visit user page' do before do visit user.username - page.find('.js-activity-tab a').click + page.find('.js-overview-tab a').click wait_for_requests end end @@ -133,12 +133,12 @@ describe 'Contributions Calendar', :js do include_context 'visit user page' it 'displays calendar activity square color for 1 contribution' do - expect(find('.tab-pane#activity')).to have_selector(get_cell_color_selector(contribution_count), count: 1) + expect(find('#js-overview')).to have_selector(get_cell_color_selector(contribution_count), count: 1) end it 'displays calendar activity square on the correct date' do today = Date.today.strftime(date_format) - expect(find('.tab-pane#activity')).to have_selector(get_cell_date_selector(contribution_count, today), count: 1) + expect(find('#js-overview')).to have_selector(get_cell_date_selector(contribution_count, today), count: 1) end end @@ -153,7 +153,7 @@ describe 'Contributions Calendar', :js do include_context 'visit user page' it 'displays calendar activity log' do - expect(find('.tab-pane#activity .content_list .event-target-title')).to have_content issue_title + expect(find('#js-overview .overview-content-list .event-target-title')).to have_content issue_title end end end @@ -185,17 +185,17 @@ describe 'Contributions Calendar', :js do include_context 'visit user page' it 'displays calendar activity squares for both days' do - expect(find('.tab-pane#activity')).to have_selector(get_cell_color_selector(1), count: 2) + expect(find('#js-overview')).to have_selector(get_cell_color_selector(1), count: 2) end it 'displays calendar activity square for yesterday' do yesterday = Date.yesterday.strftime(date_format) - expect(find('.tab-pane#activity')).to have_selector(get_cell_date_selector(1, yesterday), count: 1) + expect(find('#js-overview')).to have_selector(get_cell_date_selector(1, yesterday), count: 1) end it 'displays calendar activity square for today' do today = Date.today.strftime(date_format) - expect(find('.tab-pane#activity')).to have_selector(get_cell_date_selector(1, today), count: 1) + expect(find('#js-overview')).to have_selector(get_cell_date_selector(1, today), count: 1) end end end diff --git a/spec/features/issues/gfm_autocomplete_spec.rb b/spec/features/issues/gfm_autocomplete_spec.rb index 605860b90cd..7c591dacce5 100644 --- a/spec/features/issues/gfm_autocomplete_spec.rb +++ b/spec/features/issues/gfm_autocomplete_spec.rb @@ -1,6 +1,10 @@ require 'rails_helper' describe 'GFM autocomplete', :js do + let(:issue_xss_title) { 'This will execute alert<img src=x onerror=alert(2)<img src=x onerror=alert(1)>' } + let(:user_xss_title) { 'eve <img src=x onerror=alert(2)<img src=x onerror=alert(1)>' } + + let(:user_xss) { create(:user, name: user_xss_title, username: 'xss.user') } let(:user) { create(:user, name: '💃speciąl someone💃', username: 'someone.special') } let(:project) { create(:project) } let(:label) { create(:label, project: project, title: 'special+') } @@ -9,6 +13,8 @@ describe 'GFM autocomplete', :js do before do project.add_maintainer(user) + project.add_maintainer(user_xss) + sign_in(user) visit project_issue_path(project, issue) @@ -35,9 +41,8 @@ describe 'GFM autocomplete', :js do expect(page).to have_selector('.atwho-container') end - it 'opens autocomplete menu when field starts with text with item escaping HTML characters' do - alert_title = 'This will execute alert<img src=x onerror=alert(2)<img src=x onerror=alert(1)>' - create(:issue, project: project, title: alert_title) + it 'opens autocomplete menu for Issues when field starts with text with item escaping HTML characters' do + create(:issue, project: project, title: issue_xss_title) page.within '.timeline-content-form' do find('#note-body').native.send_keys('#') @@ -46,7 +51,19 @@ describe 'GFM autocomplete', :js do expect(page).to have_selector('.atwho-container') page.within '.atwho-container #at-view-issues' do - expect(page.all('li').first.text).to include(alert_title) + expect(page.all('li').first.text).to include(issue_xss_title) + end + end + + it 'opens autocomplete menu for Username when field starts with text with item escaping HTML characters' do + page.within '.timeline-content-form' do + find('#note-body').native.send_keys('@ev') + end + + expect(page).to have_selector('.atwho-container') + + page.within '.atwho-container #at-view-users' do + expect(find('li').text).to have_content(user_xss.username) end end @@ -107,7 +124,7 @@ describe 'GFM autocomplete', :js do wait_for_requests - expect(find('#at-view-64')).to have_selector('.cur:first-of-type') + expect(find('#at-view-users')).to have_selector('.cur:first-of-type') end it 'includes items for assignee dropdowns with non-ASCII characters in name' do @@ -120,7 +137,7 @@ describe 'GFM autocomplete', :js do wait_for_requests - expect(find('#at-view-64')).to have_content(user.name) + expect(find('#at-view-users')).to have_content(user.name) end it 'selects the first item for non-assignee dropdowns if a query is entered' do diff --git a/spec/features/issues/user_creates_branch_and_merge_request_spec.rb b/spec/features/issues/user_creates_branch_and_merge_request_spec.rb index 297cd808460..32bc851f00f 100644 --- a/spec/features/issues/user_creates_branch_and_merge_request_spec.rb +++ b/spec/features/issues/user_creates_branch_and_merge_request_spec.rb @@ -76,7 +76,7 @@ describe 'User creates branch and merge request on issue page', :js do visit project_issue_path(project, issue) - expect(page).to have_content('created branch 1-cherry-coloured-funk') + expect(page).to have_content("created merge request !1 to address this issue") expect(page).to have_content('mentioned in merge request !1') end @@ -106,7 +106,7 @@ describe 'User creates branch and merge request on issue page', :js do visit project_issue_path(project, issue) - expect(page).to have_content('created branch custom-branch-name') + expect(page).to have_content("created merge request !1 to address this issue") expect(page).to have_content('mentioned in merge request !1') end diff --git a/spec/features/issues/user_uses_quick_actions_spec.rb b/spec/features/issues/user_uses_quick_actions_spec.rb index 5926e442f24..27cffdc5f8b 100644 --- a/spec/features/issues/user_uses_quick_actions_spec.rb +++ b/spec/features/issues/user_uses_quick_actions_spec.rb @@ -303,5 +303,63 @@ describe 'Issues > User uses quick actions', :js do end end end + + describe 'create a merge request starting from an issue' do + let(:project) { create(:project, :public, :repository) } + let(:issue) { create(:issue, project: project) } + + def expect_mr_quickaction(success) + expect(page).to have_content 'Commands applied' + + if success + expect(page).to have_content 'created merge request' + else + expect(page).not_to have_content 'created merge request' + end + end + + it "doesn't create a merge request when the branch name is invalid" do + add_note("/create_merge_request invalid branch name") + + wait_for_requests + + expect_mr_quickaction(false) + end + + it "doesn't create a merge request when a branch with that name already exists" do + add_note("/create_merge_request feature") + + wait_for_requests + + expect_mr_quickaction(false) + end + + it 'creates a new merge request using issue iid and title as branch name when the branch name is empty' do + add_note("/create_merge_request") + + wait_for_requests + + expect_mr_quickaction(true) + + created_mr = project.merge_requests.last + expect(created_mr.source_branch).to eq(issue.to_branch_name) + + visit project_merge_request_path(project, created_mr) + expect(page).to have_content %{WIP: Resolve "#{issue.title}"} + end + + it 'creates a merge request using the given branch name' do + branch_name = '1-feature' + add_note("/create_merge_request #{branch_name}") + + expect_mr_quickaction(true) + + created_mr = project.merge_requests.last + expect(created_mr.source_branch).to eq(branch_name) + + visit project_merge_request_path(project, created_mr) + expect(page).to have_content %{WIP: Resolve "#{issue.title}"} + end + end end end diff --git a/spec/features/users/login_spec.rb b/spec/features/users/login_spec.rb index 44758f862a8..ad856bd062e 100644 --- a/spec/features/users/login_spec.rb +++ b/spec/features/users/login_spec.rb @@ -2,6 +2,7 @@ require 'spec_helper' describe 'Login' do include TermsHelper + include UserLoginHelper before do stub_authentication_activity_metrics(debug: true) @@ -546,29 +547,6 @@ describe 'Login' do ensure_tab_pane_correctness(false) end end - - def ensure_tab_pane_correctness(visit_path = true) - if visit_path - visit new_user_session_path - end - - ensure_tab_pane_counts - ensure_one_active_tab - ensure_one_active_pane - end - - def ensure_tab_pane_counts - tabs_count = page.all('[role="tab"]').size - expect(page).to have_selector('[role="tabpanel"]', count: tabs_count) - end - - def ensure_one_active_tab - expect(page).to have_selector('ul.new-session-tabs > li > a.active', count: 1) - end - - def ensure_one_active_pane - expect(page).to have_selector('.tab-pane.active', count: 1) - end end context 'when terms are enforced' do diff --git a/spec/features/users/overview_spec.rb b/spec/features/users/overview_spec.rb index b0ff53f9ccb..34ed771340f 100644 --- a/spec/features/users/overview_spec.rb +++ b/spec/features/users/overview_spec.rb @@ -54,15 +54,15 @@ describe 'Overview tab on a user profile', :js do end end - describe 'user has 10 activities' do + describe 'user has 11 activities' do before do - 10.times { push_code_contribution } + 11.times { push_code_contribution } end include_context 'visit overview tab' - it 'displays 5 entries in the list of activities' do - expect(find('#js-overview')).to have_selector('.event-item', count: 5) + it 'displays 10 entries in the list of activities' do + expect(find('#js-overview')).to have_selector('.event-item', count: 10) end it 'shows a link to the activity list' do diff --git a/spec/finders/group_descendants_finder_spec.rb b/spec/finders/group_descendants_finder_spec.rb index c64abdc3619..c28fd7cad11 100644 --- a/spec/finders/group_descendants_finder_spec.rb +++ b/spec/finders/group_descendants_finder_spec.rb @@ -74,6 +74,13 @@ describe GroupDescendantsFinder do end end + it 'sorts elements by latest created as default' do + project1 = create(:project, namespace: group, created_at: 1.hour.ago) + project2 = create(:project, namespace: group) + + expect(subject.execute).to eq([project2, project1]) + end + context 'sorting by name' do let!(:project1) { create(:project, namespace: group, name: 'a', path: 'project-a') } let!(:project2) { create(:project, namespace: group, name: 'z', path: 'project-z') } diff --git a/spec/finders/pending_todos_finder_spec.rb b/spec/finders/pending_todos_finder_spec.rb index 32fad5e225f..b41b1b46a93 100644 --- a/spec/finders/pending_todos_finder_spec.rb +++ b/spec/finders/pending_todos_finder_spec.rb @@ -46,7 +46,7 @@ describe PendingTodosFinder do create(:todo, :pending, user: user, target: note) - todos = described_class.new(user, target_type: issue.class).execute + todos = described_class.new(user, target_type: issue.class.name).execute expect(todos).to eq([todo]) end diff --git a/spec/fixtures/authentication/saml2_response.xml b/spec/fixtures/authentication/saml2_response.xml new file mode 100644 index 00000000000..67dea7209e9 --- /dev/null +++ b/spec/fixtures/authentication/saml2_response.xml @@ -0,0 +1,56 @@ +<?xml version="1.0" encoding="UTF-8"?> +<saml2p:Response xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:xs="http://www.w3.org/2001/XMLSchema" Destination="https://example.hello.com/access/saml" ID="jVFQbyEpSfUwqhZtJtarIaGoshwuAQMDwLoiMhzJXsv" InResponseTo="cfeooghajnhofcmogakmlhpkohnmikicnfhdnjlc" IssueInstant="2011-06-21T13:54:38.661Z" Version="2.0"> + <saml2:Issuer xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">https://idm.orademo.com</saml2:Issuer> + <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"> + <ds:SignedInfo> + <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> + <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/> + <ds:Reference URI="#jVFQbyEpSfUwqhZtJtarIaGoshwuAQMDwLoiMhzJXsv"> + <ds:Transforms> + <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/> + <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"> + <ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="xs"/> + </ds:Transform> + </ds:Transforms> + <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/> + <ds:DigestValue>uHuSry39P16Yh7srS32xESmj4Lw=</ds:DigestValue> + </ds:Reference> + </ds:SignedInfo> + <ds:SignatureValue>fdghdfggfd=</ds:SignatureValue> + <ds:KeyInfo> + <ds:X509Data> + <ds:X509Certificate>dfghjkl</ds:X509Certificate> + </ds:X509Data> + </ds:KeyInfo> + </ds:Signature> + <saml2p:Status> + <saml2p:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/> + </saml2p:Status> + <saml2:Assertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion" ID="emmCjammnYdAbMWDuMAJeZvQIMBayeeYqqwvQoDclKE" IssueInstant="2011-06-21T13:54:38.676Z" Version="2.0"> + <saml2:Issuer>https://idm.orademo.com</saml2:Issuer> + <saml2:Subject> + <saml2:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress" NameQualifier="idp.example.org">someone@example.org</saml2:NameID> + <saml2:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer"> + <saml2:SubjectConfirmationData InResponseTo="cfeooghajnhofcmogakmlhpkohnmikicnfhdnjlc" NotOnOrAfter="2011-06-21T14:09:38.676Z" Recipient="https://example.hello.com/access/saml"/> + </saml2:SubjectConfirmation> + </saml2:Subject> + <saml2:Conditions NotBefore="2011-06-21T13:54:38.683Z" NotOnOrAfter="2011-06-21T14:09:38.683Z"> + <saml2:AudienceRestriction> + <saml2:Audience>hello.com</saml2:Audience> + </saml2:AudienceRestriction> + </saml2:Conditions> + <saml2:AuthnStatement AuthnInstant="2011-06-21T13:54:38.685Z" SessionIndex="perdkjfskdjfksdiertusfsdfsddeurtherukjdfgkdffg"> + <saml2:AuthnContext> + <saml2:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport</saml2:AuthnContextClassRef> + </saml2:AuthnContext> + </saml2:AuthnStatement> + <saml2:AttributeStatement> + <saml2:Attribute Name="FirstName"> + <saml2:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">Someone</saml2:AttributeValue> + </saml2:Attribute> + <saml2:Attribute Name="LastName"> + <saml2:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">Special</saml2:AttributeValue> + </saml2:Attribute> + </saml2:AttributeStatement> + </saml2:Assertion> +</saml2p:Response> diff --git a/spec/helpers/auth_helper_spec.rb b/spec/helpers/auth_helper_spec.rb index 120b23e66ac..f0c2e4768ec 100644 --- a/spec/helpers/auth_helper_spec.rb +++ b/spec/helpers/auth_helper_spec.rb @@ -42,6 +42,16 @@ describe AuthHelper do end end + describe 'form_based_auth_provider_has_active_class?' do + it 'selects main LDAP server' do + allow(helper).to receive(:auth_providers) { [:twitter, :ldapprimary, :ldapsecondary, :kerberos] } + expect(helper.form_based_auth_provider_has_active_class?(:twitter)).to be(false) + expect(helper.form_based_auth_provider_has_active_class?(:ldapprimary)).to be(true) + expect(helper.form_based_auth_provider_has_active_class?(:ldapsecondary)).to be(false) + expect(helper.form_based_auth_provider_has_active_class?(:kerberos)).to be(false) + end + end + describe 'enabled_button_based_providers' do before do allow(helper).to receive(:auth_providers) { [:twitter, :github] } diff --git a/spec/javascripts/lib/utils/text_markdown_spec.js b/spec/javascripts/lib/utils/text_markdown_spec.js index b9e805628f8..f71d27eb4e4 100644 --- a/spec/javascripts/lib/utils/text_markdown_spec.js +++ b/spec/javascripts/lib/utils/text_markdown_spec.js @@ -86,6 +86,29 @@ describe('init markdown', () => { expect(textArea.value).toEqual(`${initialValue}* `); }); + + it('places the cursor inside the tags', () => { + const start = 'lorem '; + const end = ' ipsum'; + const tag = '*'; + + textArea.value = `${start}${end}`; + textArea.setSelectionRange(start.length, start.length); + + insertMarkdownText({ + textArea, + text: textArea.value, + tag, + blockTag: null, + selected: '', + wrap: true, + }); + + expect(textArea.value).toEqual(`${start}**${end}`); + + // cursor placement should be between tags + expect(textArea.selectionStart).toBe(start.length + tag.length); + }); }); describe('with selection', () => { @@ -98,16 +121,22 @@ describe('init markdown', () => { }); it('applies the tag to the selected value', () => { + const selectedIndex = text.indexOf(selected); + const tag = '*'; + insertMarkdownText({ textArea, text: textArea.value, - tag: '*', + tag, blockTag: null, selected, wrap: true, }); expect(textArea.value).toEqual(text.replace(selected, `*${selected}*`)); + + // cursor placement should be after selection + 2 tag lengths + expect(textArea.selectionStart).toBe(selectedIndex + selected.length + 2 * tag.length); }); it('replaces the placeholder in the tag', () => { diff --git a/spec/javascripts/vue_shared/components/expand_button_spec.js b/spec/javascripts/vue_shared/components/expand_button_spec.js index 98fee9a74a5..2af4abc299a 100644 --- a/spec/javascripts/vue_shared/components/expand_button_spec.js +++ b/spec/javascripts/vue_shared/components/expand_button_spec.js @@ -18,7 +18,7 @@ describe('expand button', () => { vm.$destroy(); }); - it('renders a collpased button', () => { + it('renders a collapsed button', () => { expect(vm.$children[0].iconTestClass).toEqual('ic-ellipsis_h'); }); diff --git a/spec/lib/gitlab/auth/saml/auth_hash_spec.rb b/spec/lib/gitlab/auth/saml/auth_hash_spec.rb index 76f49e778fb..3620e1afe25 100644 --- a/spec/lib/gitlab/auth/saml/auth_hash_spec.rb +++ b/spec/lib/gitlab/auth/saml/auth_hash_spec.rb @@ -82,6 +82,17 @@ describe Gitlab::Auth::Saml::AuthHash do end end + context 'with SAML 2.0 response_object' do + before do + auth_hash_data[:extra][:response_object] = { document: + saml_xml(File.read('spec/fixtures/authentication/saml2_response.xml')) } + end + + it 'can extract authn_context' do + expect(saml_auth_hash.authn_context).to eq 'urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport' + end + end + context 'without response_object' do it 'returns an empty string' do expect(saml_auth_hash.authn_context).to be_nil diff --git a/spec/lib/gitlab/ci/variables/collection/item_spec.rb b/spec/lib/gitlab/ci/variables/collection/item_spec.rb index 46874662edd..e1e0582cd11 100644 --- a/spec/lib/gitlab/ci/variables/collection/item_spec.rb +++ b/spec/lib/gitlab/ci/variables/collection/item_spec.rb @@ -36,7 +36,7 @@ describe Gitlab::Ci::Variables::Collection::Item do shared_examples 'raises error for invalid type' do it do expect { described_class.new(key: variable_key, value: variable_value) } - .to raise_error ArgumentError, /`value` must be of type String, while it was:/ + .to raise_error ArgumentError, /`#{variable_key}` must be of type String, while it was:/ end end @@ -46,7 +46,7 @@ describe Gitlab::Ci::Variables::Collection::Item do let(:variable_value) { nil } let(:expected_value) { nil } - it_behaves_like 'creates variable' + it_behaves_like 'raises error for invalid type' end context "when it's an empty string" do diff --git a/spec/lib/gitlab/import_export/project.json b/spec/lib/gitlab/import_export/project.json index 3f2281f213f..58949f76bd6 100644 --- a/spec/lib/gitlab/import_export/project.json +++ b/spec/lib/gitlab/import_export/project.json @@ -2556,7 +2556,7 @@ "merge_request_diff_id": 27, "relative_order": 0, "sha": "bb5206fee213d983da88c47f9cf4cc6caf9c66dc", - "message": "Feature conflcit added\n\nSigned-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>\n", + "message": "Feature conflict added\n\nSigned-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>\n", "authored_date": "2014-08-06T08:35:52.000+02:00", "author_name": "Dmitriy Zaporozhets", "author_email": "dmitriy.zaporozhets@gmail.com", @@ -3605,7 +3605,7 @@ "merge_request_diff_id": 14, "relative_order": 8, "sha": "08f22f255f082689c0d7d39d19205085311542bc", - "message": "remove emtpy file.(beacase git ignore empty file)\nadd whitespace test file.\n", + "message": "remove empty file.(beacase git ignore empty file)\nadd whitespace test file.\n", "authored_date": "2015-11-13T06:00:16.000+01:00", "author_name": "윤민식", "author_email": "minsik.yoon@samsung.com", @@ -4290,7 +4290,7 @@ "merge_request_diff_id": 13, "relative_order": 8, "sha": "08f22f255f082689c0d7d39d19205085311542bc", - "message": "remove emtpy file.(beacase git ignore empty file)\nadd whitespace test file.\n", + "message": "remove empty file.(beacase git ignore empty file)\nadd whitespace test file.\n", "authored_date": "2015-11-13T06:00:16.000+01:00", "author_name": "윤민식", "author_email": "minsik.yoon@samsung.com", @@ -4799,7 +4799,7 @@ "merge_request_diff_id": 12, "relative_order": 8, "sha": "08f22f255f082689c0d7d39d19205085311542bc", - "message": "remove emtpy file.(beacase git ignore empty file)\nadd whitespace test file.\n", + "message": "remove empty file.(beacase git ignore empty file)\nadd whitespace test file.\n", "authored_date": "2015-11-13T06:00:16.000+01:00", "author_name": "윤민식", "author_email": "minsik.yoon@samsung.com", @@ -5507,7 +5507,7 @@ "merge_request_diff_id": 10, "relative_order": 8, "sha": "08f22f255f082689c0d7d39d19205085311542bc", - "message": "remove emtpy file.(beacase git ignore empty file)\nadd whitespace test file.\n", + "message": "remove empty file.(beacase git ignore empty file)\nadd whitespace test file.\n", "authored_date": "2015-11-13T06:00:16.000+01:00", "author_name": "윤민식", "author_email": "minsik.yoon@samsung.com", diff --git a/spec/lib/gitlab/import_export/project.light.json b/spec/lib/gitlab/import_export/project.light.json index ba2248073f5..2971ca0f0f8 100644 --- a/spec/lib/gitlab/import_export/project.light.json +++ b/spec/lib/gitlab/import_export/project.light.json @@ -101,6 +101,28 @@ ] } ], + "services": [ + { + "id": 100, + "title": "JetBrains TeamCity CI", + "project_id": 5, + "created_at": "2016-06-14T15:01:51.315Z", + "updated_at": "2016-06-14T15:01:51.315Z", + "active": false, + "properties": {}, + "template": true, + "push_events": true, + "issues_events": true, + "merge_requests_events": true, + "tag_push_events": true, + "note_events": true, + "job_events": true, + "type": "TeamcityService", + "category": "ci", + "default": false, + "wiki_page_events": true + } + ], "snippets": [], "hooks": [] } diff --git a/spec/lib/gitlab/import_export/project_tree_restorer_spec.rb b/spec/lib/gitlab/import_export/project_tree_restorer_spec.rb index 365bfae0d88..7171e12a849 100644 --- a/spec/lib/gitlab/import_export/project_tree_restorer_spec.rb +++ b/spec/lib/gitlab/import_export/project_tree_restorer_spec.rb @@ -297,7 +297,8 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do issues: 1, labels: 1, milestones: 1, - first_issue_labels: 1 + first_issue_labels: 1, + services: 1 context 'project.json file access check' do it 'does not read a symlink' do @@ -382,6 +383,12 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do project_tree_restorer.instance_variable_set(:@path, "spec/lib/gitlab/import_export/project.light.json") end + it 'does not import any templated services' do + restored_project_json + + expect(project.services.where(template: true).count).to eq(0) + end + it 'imports labels' do create(:group_label, name: 'Another label', group: project.group) diff --git a/spec/lib/gitlab/private_commit_email_spec.rb b/spec/lib/gitlab/private_commit_email_spec.rb index bc86cd3842a..10bf624bbdd 100644 --- a/spec/lib/gitlab/private_commit_email_spec.rb +++ b/spec/lib/gitlab/private_commit_email_spec.rb @@ -4,6 +4,9 @@ require 'spec_helper' describe Gitlab::PrivateCommitEmail do let(:hostname) { Gitlab::CurrentSettings.current_application_settings.commit_email_hostname } + let(:id) { 1 } + let(:valid_email) { "#{id}-foo@#{hostname}" } + let(:invalid_email) { "#{id}-foo@users.noreply.bar.com" } context '.regex' do subject { described_class.regex } @@ -16,18 +19,25 @@ describe Gitlab::PrivateCommitEmail do end context '.user_id_for_email' do - let(:id) { 1 } - it 'parses user id from email' do - email = "#{id}-foo@#{hostname}" - - expect(described_class.user_id_for_email(email)).to eq(id) + expect(described_class.user_id_for_email(valid_email)).to eq(id) end it 'returns nil on invalid commit email' do - email = "#{id}-foo@users.noreply.bar.com" + expect(described_class.user_id_for_email(invalid_email)).to be_nil + end + end + + context '.user_ids_for_email' do + it 'returns deduplicated user IDs for each valid email' do + result = described_class.user_ids_for_emails([valid_email, valid_email, invalid_email]) + + expect(result).to eq([id]) + end - expect(described_class.user_id_for_email(email)).to be_nil + it 'returns an empty array with no valid emails' do + result = described_class.user_ids_for_emails([invalid_email]) + expect(result).to eq([]) end end diff --git a/spec/lib/gitlab/repository_cache_spec.rb b/spec/lib/gitlab/repository_cache_spec.rb index 741ee12633f..1b9a8b4ab0d 100644 --- a/spec/lib/gitlab/repository_cache_spec.rb +++ b/spec/lib/gitlab/repository_cache_spec.rb @@ -4,14 +4,14 @@ describe Gitlab::RepositoryCache do let(:backend) { double('backend').as_null_object } let(:project) { create(:project) } let(:repository) { project.repository } - let(:namespace) { "#{repository.full_path}:#{project.id}" } + let(:namespace) { "project:#{project.id}" } let(:cache) { described_class.new(repository, backend: backend) } describe '#cache_key' do subject { cache.cache_key(:foo) } it 'includes the namespace' do - expect(subject).to eq "foo:#{namespace}" + expect(subject).to eq "#{namespace}:foo" end context 'with a given namespace' do @@ -22,7 +22,7 @@ describe Gitlab::RepositoryCache do end it 'includes the full namespace' do - expect(subject).to eq "foo:#{namespace}:#{extra_namespace}" + expect(subject).to eq "#{namespace}:#{extra_namespace}:foo" end end end @@ -30,21 +30,21 @@ describe Gitlab::RepositoryCache do describe '#expire' do it 'expires the given key from the cache' do cache.expire(:foo) - expect(backend).to have_received(:delete).with("foo:#{namespace}") + expect(backend).to have_received(:delete).with("#{namespace}:foo") end end describe '#fetch' do it 'fetches the given key from the cache' do cache.fetch(:bar) - expect(backend).to have_received(:fetch).with("bar:#{namespace}") + expect(backend).to have_received(:fetch).with("#{namespace}:bar") end it 'accepts a block' do p = -> {} cache.fetch(:baz, &p) - expect(backend).to have_received(:fetch).with("baz:#{namespace}", &p) + expect(backend).to have_received(:fetch).with("#{namespace}:baz", &p) end end @@ -67,7 +67,7 @@ describe Gitlab::RepositoryCache do end it 'caches the value' do - expect(backend).to receive(:write).with("#{key}:#{namespace}", true) + expect(backend).to receive(:write).with("#{namespace}:#{key}", true) cache.fetch_without_caching_false(key) { true } end @@ -83,7 +83,7 @@ describe Gitlab::RepositoryCache do end it 'does not cache the value' do - expect(backend).not_to receive(:write).with("#{key}:#{namespace}", true) + expect(backend).not_to receive(:write).with("#{namespace}:#{key}", true) cache.fetch_without_caching_false(key, &p) end @@ -92,7 +92,7 @@ describe Gitlab::RepositoryCache do context 'when the cached value is truthy' do before do - backend.write("#{key}:#{namespace}", true) + backend.write("#{namespace}:#{key}", true) end it 'returns the cached value' do @@ -116,7 +116,7 @@ describe Gitlab::RepositoryCache do context 'when the cached value is falsey' do before do - backend.write("#{key}:#{namespace}", false) + backend.write("#{namespace}:#{key}", false) end it 'returns the result of the block' do @@ -126,7 +126,7 @@ describe Gitlab::RepositoryCache do end it 'writes the truthy value to the cache' do - expect(backend).to receive(:write).with("#{key}:#{namespace}", 'block result') + expect(backend).to receive(:write).with("#{namespace}:#{key}", 'block result') cache.fetch_without_caching_false(key) { 'block result' } end diff --git a/spec/models/ci/build_spec.rb b/spec/models/ci/build_spec.rb index 6849bc6db7a..d02c3a5765f 100644 --- a/spec/models/ci/build_spec.rb +++ b/spec/models/ci/build_spec.rb @@ -1928,12 +1928,26 @@ describe Ci::Build do describe '#repo_url' do subject { build.repo_url } - it { is_expected.to be_a(String) } - it { is_expected.to end_with(".git") } - it { is_expected.to start_with(project.web_url[0..6]) } - it { is_expected.to include(build.token) } - it { is_expected.to include('gitlab-ci-token') } - it { is_expected.to include(project.web_url[7..-1]) } + context 'when token is set' do + before do + build.ensure_token + end + + it { is_expected.to be_a(String) } + it { is_expected.to end_with(".git") } + it { is_expected.to start_with(project.web_url[0..6]) } + it { is_expected.to include(build.token) } + it { is_expected.to include('gitlab-ci-token') } + it { is_expected.to include(project.web_url[7..-1]) } + end + + context 'when token is empty' do + before do + build.token = nil + end + + it { is_expected.to be_nil} + end end describe '#stuck?' do @@ -2043,7 +2057,8 @@ describe Ci::Build do end context 'use from gitlab-ci.yml' do - let(:pipeline) { create(:ci_pipeline) } + let(:project) { create(:project, :repository) } + let(:pipeline) { create(:ci_pipeline, project: project) } before do stub_ci_pipeline_yaml_file(config) @@ -2085,56 +2100,6 @@ describe Ci::Build do describe '#variables' do let(:container_registry_enabled) { false } - let(:gitlab_version_info) { Gitlab::VersionInfo.parse(Gitlab::VERSION) } - let(:predefined_variables) do - [ - { key: 'CI_PIPELINE_ID', value: pipeline.id.to_s, public: true }, - { key: 'CI_PIPELINE_URL', value: project.web_url + "/pipelines/#{pipeline.id}", public: true }, - { key: 'CI_JOB_ID', value: build.id.to_s, public: true }, - { key: 'CI_JOB_URL', value: project.web_url + "/-/jobs/#{build.id}", public: true }, - { key: 'CI_JOB_TOKEN', value: build.token, public: false }, - { key: 'CI_BUILD_ID', value: build.id.to_s, public: true }, - { key: 'CI_BUILD_TOKEN', value: build.token, public: false }, - { key: 'CI_REGISTRY_USER', value: 'gitlab-ci-token', public: true }, - { key: 'CI_REGISTRY_PASSWORD', value: build.token, public: false }, - { key: 'CI_REPOSITORY_URL', value: build.repo_url, public: false }, - { key: 'CI', value: 'true', public: true }, - { key: 'GITLAB_CI', value: 'true', public: true }, - { key: 'GITLAB_FEATURES', value: project.licensed_features.join(','), public: true }, - { key: 'CI_SERVER_NAME', value: 'GitLab', public: true }, - { key: 'CI_SERVER_VERSION', value: Gitlab::VERSION, public: true }, - { key: 'CI_SERVER_VERSION_MAJOR', value: gitlab_version_info.major.to_s, public: true }, - { key: 'CI_SERVER_VERSION_MINOR', value: gitlab_version_info.minor.to_s, public: true }, - { key: 'CI_SERVER_VERSION_PATCH', value: gitlab_version_info.patch.to_s, public: true }, - { key: 'CI_SERVER_REVISION', value: Gitlab.revision, public: true }, - { key: 'CI_JOB_NAME', value: 'test', public: true }, - { key: 'CI_JOB_STAGE', value: 'test', public: true }, - { key: 'CI_COMMIT_SHA', value: build.sha, public: true }, - { key: 'CI_COMMIT_BEFORE_SHA', value: build.before_sha, public: true }, - { key: 'CI_COMMIT_REF_NAME', value: build.ref, public: true }, - { key: 'CI_COMMIT_REF_SLUG', value: build.ref_slug, public: true }, - { key: 'CI_NODE_TOTAL', value: '1', public: true }, - { key: 'CI_BUILD_REF', value: build.sha, public: true }, - { key: 'CI_BUILD_BEFORE_SHA', value: build.before_sha, public: true }, - { key: 'CI_BUILD_REF_NAME', value: build.ref, public: true }, - { key: 'CI_BUILD_REF_SLUG', value: build.ref_slug, public: true }, - { key: 'CI_BUILD_NAME', value: 'test', public: true }, - { key: 'CI_BUILD_STAGE', value: 'test', public: true }, - { key: 'CI_PROJECT_ID', value: project.id.to_s, public: true }, - { key: 'CI_PROJECT_NAME', value: project.path, public: true }, - { key: 'CI_PROJECT_PATH', value: project.full_path, public: true }, - { key: 'CI_PROJECT_PATH_SLUG', value: project.full_path_slug, public: true }, - { key: 'CI_PROJECT_NAMESPACE', value: project.namespace.full_path, public: true }, - { key: 'CI_PROJECT_URL', value: project.web_url, public: true }, - { key: 'CI_PROJECT_VISIBILITY', value: 'private', public: true }, - { key: 'CI_PIPELINE_IID', value: pipeline.iid.to_s, public: true }, - { key: 'CI_CONFIG_PATH', value: pipeline.ci_yaml_file_path, public: true }, - { key: 'CI_PIPELINE_SOURCE', value: pipeline.source, public: true }, - { key: 'CI_COMMIT_MESSAGE', value: pipeline.git_commit_message, public: true }, - { key: 'CI_COMMIT_TITLE', value: pipeline.git_commit_title, public: true }, - { key: 'CI_COMMIT_DESCRIPTION', value: pipeline.git_commit_description, public: true } - ] - end before do stub_container_registry_config(enabled: container_registry_enabled, host_port: 'registry.example.com') @@ -2143,11 +2108,174 @@ describe Ci::Build do subject { build.variables } context 'returns variables' do + let(:predefined_variables) do + [ + { key: 'CI_PIPELINE_ID', value: pipeline.id.to_s, public: true }, + { key: 'CI_PIPELINE_URL', value: project.web_url + "/pipelines/#{pipeline.id}", public: true }, + { key: 'CI_JOB_ID', value: build.id.to_s, public: true }, + { key: 'CI_JOB_URL', value: project.web_url + "/-/jobs/#{build.id}", public: true }, + { key: 'CI_JOB_TOKEN', value: 'my-token', public: false }, + { key: 'CI_BUILD_ID', value: build.id.to_s, public: true }, + { key: 'CI_BUILD_TOKEN', value: 'my-token', public: false }, + { key: 'CI_REGISTRY_USER', value: 'gitlab-ci-token', public: true }, + { key: 'CI_REGISTRY_PASSWORD', value: 'my-token', public: false }, + { key: 'CI_REPOSITORY_URL', value: build.repo_url, public: false }, + { key: 'CI', value: 'true', public: true }, + { key: 'GITLAB_CI', value: 'true', public: true }, + { key: 'GITLAB_FEATURES', value: project.licensed_features.join(','), public: true }, + { key: 'CI_SERVER_NAME', value: 'GitLab', public: true }, + { key: 'CI_SERVER_VERSION', value: Gitlab::VERSION, public: true }, + { key: 'CI_SERVER_VERSION_MAJOR', value: Gitlab.version_info.major.to_s, public: true }, + { key: 'CI_SERVER_VERSION_MINOR', value: Gitlab.version_info.minor.to_s, public: true }, + { key: 'CI_SERVER_VERSION_PATCH', value: Gitlab.version_info.patch.to_s, public: true }, + { key: 'CI_SERVER_REVISION', value: Gitlab.revision, public: true }, + { key: 'CI_JOB_NAME', value: 'test', public: true }, + { key: 'CI_JOB_STAGE', value: 'test', public: true }, + { key: 'CI_COMMIT_SHA', value: build.sha, public: true }, + { key: 'CI_COMMIT_BEFORE_SHA', value: build.before_sha, public: true }, + { key: 'CI_COMMIT_REF_NAME', value: build.ref, public: true }, + { key: 'CI_COMMIT_REF_SLUG', value: build.ref_slug, public: true }, + { key: 'CI_NODE_TOTAL', value: '1', public: true }, + { key: 'CI_BUILD_REF', value: build.sha, public: true }, + { key: 'CI_BUILD_BEFORE_SHA', value: build.before_sha, public: true }, + { key: 'CI_BUILD_REF_NAME', value: build.ref, public: true }, + { key: 'CI_BUILD_REF_SLUG', value: build.ref_slug, public: true }, + { key: 'CI_BUILD_NAME', value: 'test', public: true }, + { key: 'CI_BUILD_STAGE', value: 'test', public: true }, + { key: 'CI_PROJECT_ID', value: project.id.to_s, public: true }, + { key: 'CI_PROJECT_NAME', value: project.path, public: true }, + { key: 'CI_PROJECT_PATH', value: project.full_path, public: true }, + { key: 'CI_PROJECT_PATH_SLUG', value: project.full_path_slug, public: true }, + { key: 'CI_PROJECT_NAMESPACE', value: project.namespace.full_path, public: true }, + { key: 'CI_PROJECT_URL', value: project.web_url, public: true }, + { key: 'CI_PROJECT_VISIBILITY', value: 'private', public: true }, + { key: 'CI_PIPELINE_IID', value: pipeline.iid.to_s, public: true }, + { key: 'CI_CONFIG_PATH', value: pipeline.ci_yaml_file_path, public: true }, + { key: 'CI_PIPELINE_SOURCE', value: pipeline.source, public: true }, + { key: 'CI_COMMIT_MESSAGE', value: pipeline.git_commit_message, public: true }, + { key: 'CI_COMMIT_TITLE', value: pipeline.git_commit_title, public: true }, + { key: 'CI_COMMIT_DESCRIPTION', value: pipeline.git_commit_description, public: true } + ] + end + before do + build.token = 'my-token' build.yaml_variables = [] end it { is_expected.to include(*predefined_variables) } + + context 'when yaml variables are undefined' do + let(:pipeline) do + create(:ci_pipeline, project: project, + sha: project.commit.id, + ref: project.default_branch) + end + + before do + build.yaml_variables = nil + end + + context 'use from gitlab-ci.yml' do + before do + stub_ci_pipeline_yaml_file(config) + end + + context 'when config is not found' do + let(:config) { nil } + + it { is_expected.to include(*predefined_variables) } + end + + context 'when config does not have a questioned job' do + let(:config) do + YAML.dump({ + test_other: { + script: 'Hello World' + } + }) + end + + it { is_expected.to include(*predefined_variables) } + end + + context 'when config has variables' do + let(:config) do + YAML.dump({ + test: { + script: 'Hello World', + variables: { + KEY: 'value' + } + } + }) + end + + let(:variables) do + [{ key: 'KEY', value: 'value', public: true }] + end + + it { is_expected.to include(*predefined_variables) } + it { is_expected.to include(*variables) } + end + end + end + + describe 'variables ordering' do + context 'when variables hierarchy is stubbed' do + let(:build_pre_var) { { key: 'build', value: 'value', public: true } } + let(:project_pre_var) { { key: 'project', value: 'value', public: true } } + let(:pipeline_pre_var) { { key: 'pipeline', value: 'value', public: true } } + let(:build_yaml_var) { { key: 'yaml', value: 'value', public: true } } + + before do + allow(build).to receive(:predefined_variables) { [build_pre_var] } + allow(build).to receive(:yaml_variables) { [build_yaml_var] } + allow(build).to receive(:persisted_variables) { [] } + + allow_any_instance_of(Project) + .to receive(:predefined_variables) { [project_pre_var] } + + project.variables.create!(key: 'secret', value: 'value') + + allow_any_instance_of(Ci::Pipeline) + .to receive(:predefined_variables) { [pipeline_pre_var] } + end + + it 'returns variables in order depending on resource hierarchy' do + is_expected.to eq( + [build_pre_var, + project_pre_var, + pipeline_pre_var, + build_yaml_var, + { key: 'secret', value: 'value', public: false }]) + end + end + + context 'when build has environment and user-provided variables' do + let(:expected_variables) do + predefined_variables.map { |variable| variable.fetch(:key) } + + %w[YAML_VARIABLE CI_ENVIRONMENT_NAME CI_ENVIRONMENT_SLUG + CI_ENVIRONMENT_URL] + end + + before do + create(:environment, project: build.project, + name: 'staging') + + build.yaml_variables = [{ key: 'YAML_VARIABLE', + value: 'var', + public: true }] + build.environment = 'staging' + end + + it 'matches explicit variables ordering' do + received_variables = subject.map { |variable| variable.fetch(:key) } + + expect(received_variables).to eq expected_variables + end + end + end end context 'when build has user' do @@ -2409,75 +2537,20 @@ describe Ci::Build do end before do - pipeline_schedule.pipelines << pipeline + pipeline_schedule.pipelines << pipeline.reload pipeline_schedule.reload end it { is_expected.to include(pipeline_schedule_variable.to_runner_variable) } end - context 'when yaml_variables are undefined' do - let(:pipeline) do - create(:ci_pipeline, project: project, - sha: project.commit.id, - ref: project.default_branch) - end - - before do - build.yaml_variables = nil - end - - context 'use from gitlab-ci.yml' do - before do - stub_ci_pipeline_yaml_file(config) - end - - context 'when config is not found' do - let(:config) { nil } - - it { is_expected.to include(*predefined_variables) } - end - - context 'when config does not have a questioned job' do - let(:config) do - YAML.dump({ - test_other: { - script: 'Hello World' - } - }) - end - - it { is_expected.to include(*predefined_variables) } - end - - context 'when config has variables' do - let(:config) do - YAML.dump({ - test: { - script: 'Hello World', - variables: { - KEY: 'value' - } - } - }) - end - let(:variables) do - [{ key: 'KEY', value: 'value', public: true }] - end - - it { is_expected.to include(*predefined_variables) } - it { is_expected.to include(*variables) } - end - end - end - context 'when container registry is enabled' do let(:container_registry_enabled) { true } let(:ci_registry) do - { key: 'CI_REGISTRY', value: 'registry.example.com', public: true } + { key: 'CI_REGISTRY', value: 'registry.example.com', public: true } end let(:ci_registry_image) do - { key: 'CI_REGISTRY_IMAGE', value: project.container_registry_url, public: true } + { key: 'CI_REGISTRY_IMAGE', value: project.container_registry_url, public: true } end context 'and is disabled for project' do @@ -2598,66 +2671,6 @@ describe Ci::Build do end end - describe 'variables ordering' do - context 'when variables hierarchy is stubbed' do - let(:build_pre_var) { { key: 'build', value: 'value', public: true } } - let(:project_pre_var) { { key: 'project', value: 'value', public: true } } - let(:pipeline_pre_var) { { key: 'pipeline', value: 'value', public: true } } - let(:build_yaml_var) { { key: 'yaml', value: 'value', public: true } } - - before do - allow(build).to receive(:predefined_variables) { [build_pre_var] } - allow(build).to receive(:yaml_variables) { [build_yaml_var] } - allow(build).to receive(:persisted_variables) { [] } - - allow_any_instance_of(Project) - .to receive(:predefined_variables) { [project_pre_var] } - - allow_any_instance_of(Project) - .to receive(:ci_variables_for) - .with(ref: 'master', environment: nil) do - [create(:ci_variable, key: 'secret', value: 'value')] - end - - allow_any_instance_of(Ci::Pipeline) - .to receive(:predefined_variables) { [pipeline_pre_var] } - end - - it 'returns variables in order depending on resource hierarchy' do - is_expected.to eq( - [build_pre_var, - project_pre_var, - pipeline_pre_var, - build_yaml_var, - { key: 'secret', value: 'value', public: false }]) - end - end - - context 'when build has environment and user-provided variables' do - let(:expected_variables) do - predefined_variables.map { |variable| variable.fetch(:key) } + - %w[YAML_VARIABLE CI_ENVIRONMENT_NAME CI_ENVIRONMENT_SLUG - CI_ENVIRONMENT_URL] - end - - before do - create(:environment, project: build.project, - name: 'staging') - - build.yaml_variables = [{ key: 'YAML_VARIABLE', - value: 'var', - public: true }] - build.environment = 'staging' - end - - it 'matches explicit variables ordering' do - received_variables = subject.map { |variable| variable.fetch(:key) } - - expect(received_variables).to eq expected_variables - end - end - end - context 'when build has not been persisted yet' do let(:build) do described_class.new( diff --git a/spec/models/clusters/kubernetes_namespace_spec.rb b/spec/models/clusters/kubernetes_namespace_spec.rb index c068c4d7739..56c98d016c9 100644 --- a/spec/models/clusters/kubernetes_namespace_spec.rb +++ b/spec/models/clusters/kubernetes_namespace_spec.rb @@ -45,14 +45,14 @@ RSpec.describe Clusters::KubernetesNamespace, type: :model do end end - describe '#configure_predefined_variables' do + describe '#set_defaults' do let(:kubernetes_namespace) { build(:cluster_kubernetes_namespace) } let(:cluster) { kubernetes_namespace.cluster } let(:platform) { kubernetes_namespace.platform_kubernetes } - subject { kubernetes_namespace.configure_predefined_credentials } + subject { kubernetes_namespace.set_defaults } - describe 'namespace' do + describe '#namespace' do before do platform.update_column(:namespace, namespace) end @@ -80,7 +80,7 @@ RSpec.describe Clusters::KubernetesNamespace, type: :model do end end - describe 'service_account_name' do + describe '#service_account_name' do let(:service_account_name) { "#{kubernetes_namespace.namespace}-service-account" } it 'should set a service account name based on namespace' do diff --git a/spec/models/commit_spec.rb b/spec/models/commit_spec.rb index ed41ff7a0fa..2a0039a0635 100644 --- a/spec/models/commit_spec.rb +++ b/spec/models/commit_spec.rb @@ -72,6 +72,7 @@ describe Commit do context 'using eager loading' do let!(:alice) { create(:user, email: 'alice@example.com') } let!(:bob) { create(:user, email: 'hunter2@example.com') } + let!(:jeff) { create(:user) } let(:alice_commit) do described_class.new(RepoHelpers.sample_commit, project).tap do |c| @@ -93,7 +94,14 @@ describe Commit do end end - let!(:commits) { [alice_commit, bob_commit, eve_commit] } + let(:jeff_commit) do + # The commit for Jeff uses his private commit email + described_class.new(RepoHelpers.sample_commit, project).tap do |c| + c.author_email = jeff.private_commit_email + end + end + + let!(:commits) { [alice_commit, bob_commit, eve_commit, jeff_commit] } before do create(:email, user: bob, email: 'bob@example.com') @@ -125,6 +133,20 @@ describe Commit do expect(bob_commit.author).to eq(bob) end + it "preloads the authors for Commits using a User's private commit Email" do + commits.each(&:lazy_author) + + expect(jeff_commit.author).to eq(jeff) + end + + it "preloads the authors for Commits using a User's outdated private commit Email" do + jeff.update!(username: 'new-username') + + commits.each(&:lazy_author) + + expect(jeff_commit.author).to eq(jeff) + end + it 'sets the author to Nil if an author could not be found for a Commit' do commits.each(&:lazy_author) diff --git a/spec/models/project_services/chat_message/push_message_spec.rb b/spec/models/project_services/chat_message/push_message_spec.rb index 19c2862264f..973d6bdb2a0 100644 --- a/spec/models/project_services/chat_message/push_message_spec.rb +++ b/spec/models/project_services/chat_message/push_message_spec.rb @@ -48,12 +48,12 @@ describe ChatMessage::PushMessage do 'test.user pushed to branch [master](http://url.com/commits/master) of [project_name](http://url.com) ([Compare changes](http://url.com/compare/before...after))') expect(subject.attachments).to eq( "[abcdefgh](http://url1.com): message1 - author1\n\n[12345678](http://url2.com): message2 - author2") - expect(subject.activity).to eq({ - title: 'test.user pushed to branch', + expect(subject.activity).to eq( + title: 'test.user pushed to branch [master](http://url.com/commits/master)', subtitle: 'in [project_name](http://url.com)', text: '[Compare changes](http://url.com/compare/before...after)', image: 'http://someavatar.com' - }) + ) end end end @@ -89,12 +89,53 @@ describe ChatMessage::PushMessage do expect(subject.pretext).to eq( 'test.user pushed new tag [new_tag](http://url.com/commits/new_tag) to [project_name](http://url.com)') expect(subject.attachments).to be_empty - expect(subject.activity).to eq({ - title: 'test.user created tag', + expect(subject.activity).to eq( + title: 'test.user pushed new tag [new_tag](http://url.com/commits/new_tag)', subtitle: 'in [project_name](http://url.com)', text: '[Compare changes](http://url.com/compare/0000000000000000000000000000000000000000...after)', image: 'http://someavatar.com' - }) + ) + end + end + end + + context 'removed tag' do + let(:args) do + { + after: Gitlab::Git::BLANK_SHA, + before: 'before', + project_name: 'project_name', + ref: 'refs/tags/new_tag', + user_name: 'test.user', + user_avatar: 'http://someavatar.com', + project_url: 'http://url.com' + } + end + + context 'without markdown' do + it 'returns a message regarding removal of tags' do + expect(subject.pretext).to eq('test.user removed tag ' \ + 'new_tag from ' \ + '<http://url.com|project_name>') + expect(subject.attachments).to be_empty + end + end + + context 'with markdown' do + before do + args[:markdown] = true + end + + it 'returns a message regarding removal of tags' do + expect(subject.pretext).to eq( + 'test.user removed tag new_tag from [project_name](http://url.com)') + expect(subject.attachments).to be_empty + expect(subject.activity).to eq( + title: 'test.user removed tag new_tag', + subtitle: 'in [project_name](http://url.com)', + text: '[Compare changes](http://url.com/compare/before...0000000000000000000000000000000000000000)', + image: 'http://someavatar.com' + ) end end end @@ -122,12 +163,12 @@ describe ChatMessage::PushMessage do expect(subject.pretext).to eq( 'test.user pushed new branch [master](http://url.com/commits/master) to [project_name](http://url.com)') expect(subject.attachments).to be_empty - expect(subject.activity).to eq({ - title: 'test.user created branch', + expect(subject.activity).to eq( + title: 'test.user pushed new branch [master](http://url.com/commits/master)', subtitle: 'in [project_name](http://url.com)', text: '[Compare changes](http://url.com/compare/0000000000000000000000000000000000000000...after)', image: 'http://someavatar.com' - }) + ) end end end @@ -154,12 +195,12 @@ describe ChatMessage::PushMessage do expect(subject.pretext).to eq( 'test.user removed branch master from [project_name](http://url.com)') expect(subject.attachments).to be_empty - expect(subject.activity).to eq({ - title: 'test.user removed branch', + expect(subject.activity).to eq( + title: 'test.user removed branch master', subtitle: 'in [project_name](http://url.com)', text: '[Compare changes](http://url.com/compare/before...0000000000000000000000000000000000000000)', image: 'http://someavatar.com' - }) + ) end end end diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index 799a60ac62f..56edb0fd6da 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -2403,4 +2403,22 @@ describe Repository do repository.merge_base('master', 'fix') end end + + describe '#cache' do + subject(:cache) { repository.send(:cache) } + + it 'returns a RepositoryCache' do + expect(subject).to be_kind_of Gitlab::RepositoryCache + end + + it 'when is_wiki it includes wiki as part of key' do + allow(repository).to receive(:is_wiki) { true } + + expect(subject.namespace).to include('wiki') + end + + it 'when is_wiki is false extra_namespace is nil' do + expect(subject.namespace).not_to include('wiki') + end + end end diff --git a/spec/models/todo_spec.rb b/spec/models/todo_spec.rb index 2c01578aaca..82ff2a002e0 100644 --- a/spec/models/todo_spec.rb +++ b/spec/models/todo_spec.rb @@ -226,7 +226,7 @@ describe Todo do create(:todo, target: create(:merge_request)) - expect(described_class.for_type(Issue)).to eq([todo]) + expect(described_class.for_type(Issue.name)).to eq([todo]) end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 0ac5bd666ae..733c1c49f08 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1174,6 +1174,22 @@ describe User do expect(described_class.by_any_email(user.email, confirmed: true)).to eq([user]) end + + it 'finds user through a private commit email' do + user = create(:user) + private_email = user.private_commit_email + + expect(described_class.by_any_email(private_email)).to eq([user]) + expect(described_class.by_any_email(private_email, confirmed: true)).to eq([user]) + end + + it 'finds user through a private commit email in an array' do + user = create(:user) + private_email = user.private_commit_email + + expect(described_class.by_any_email([private_email])).to eq([user]) + expect(described_class.by_any_email([private_email], confirmed: true)).to eq([user]) + end end describe '.search' do @@ -1501,7 +1517,12 @@ describe User do email_unconfirmed = create :email, user: user user.reload - expect(user.all_emails).to match_array([user.email, email_unconfirmed.email, email_confirmed.email]) + expect(user.all_emails).to contain_exactly( + user.email, + user.private_commit_email, + email_unconfirmed.email, + email_confirmed.email + ) end end @@ -1512,7 +1533,11 @@ describe User do email_confirmed = create :email, user: user, confirmed_at: Time.now create :email, user: user - expect(user.verified_emails).to match_array([user.email, user.private_commit_email, email_confirmed.email]) + expect(user.verified_emails).to contain_exactly( + user.email, + user.private_commit_email, + email_confirmed.email + ) end end @@ -1532,6 +1557,14 @@ describe User do expect(user.verified_email?(user.private_commit_email)).to be_truthy end + it 'returns true for an outdated private commit email' do + old_email = user.private_commit_email + + user.update!(username: 'changed-username') + + expect(user.verified_email?(old_email)).to be_truthy + end + it 'returns false when the email is not verified/confirmed' do email_unconfirmed = create :email, user: user user.reload diff --git a/spec/services/clusters/gcp/kubernetes/create_or_update_namespace_service_spec.rb b/spec/services/clusters/gcp/kubernetes/create_or_update_namespace_service_spec.rb index fc922218ad0..661364ac765 100644 --- a/spec/services/clusters/gcp/kubernetes/create_or_update_namespace_service_spec.rb +++ b/spec/services/clusters/gcp/kubernetes/create_or_update_namespace_service_spec.rb @@ -44,7 +44,7 @@ describe Clusters::Gcp::Kubernetes::CreateOrUpdateNamespaceService, '#execute' d let(:namespace) { "#{project.path}-#{project.id}" } let(:kubernetes_namespace) do - build(:cluster_kubernetes_namespace, + create(:cluster_kubernetes_namespace, cluster: cluster, project: cluster_project.project, cluster_project: cluster_project) diff --git a/spec/services/merge_requests/create_from_issue_service_spec.rb b/spec/services/merge_requests/create_from_issue_service_spec.rb index b1882df732d..393299cce00 100644 --- a/spec/services/merge_requests/create_from_issue_service_spec.rb +++ b/spec/services/merge_requests/create_from_issue_service_spec.rb @@ -61,7 +61,15 @@ describe MergeRequests::CreateFromIssueService do expect(project.repository.branch_exists?(custom_source_branch)).to be_truthy end - it 'creates a system note' do + it 'creates the new_merge_request system note' do + expect(SystemNoteService).to receive(:new_merge_request).with(issue, project, user, instance_of(MergeRequest)) + + service.execute + end + + it 'creates the new_issue_branch system note when the branch could be created but the merge_request cannot be created' do + project.project_feature.update!(merge_requests_access_level: ProjectFeature::DISABLED) + expect(SystemNoteService).to receive(:new_issue_branch).with(issue, project, user, issue.to_branch_name) service.execute diff --git a/spec/services/quick_actions/interpret_service_spec.rb b/spec/services/quick_actions/interpret_service_spec.rb index 5a7cafcb60f..938764f40b0 100644 --- a/spec/services/quick_actions/interpret_service_spec.rb +++ b/spec/services/quick_actions/interpret_service_spec.rb @@ -1222,6 +1222,37 @@ describe QuickActions::InterpretService do expect(commands).to be_empty expect(text).to eq("#{described_class::SHRUG}\n/close") end + + context '/create_merge_request command' do + let(:branch_name) { '1-feature' } + let(:content) { "/create_merge_request #{branch_name}" } + let(:issuable) { issue } + + context 'if issuable is not an Issue' do + let(:issuable) { merge_request } + + it_behaves_like 'empty command' + end + + context "when logged user cannot create_merge_requests in the project" do + let(:project) { create(:project, :archived) } + + it_behaves_like 'empty command' + end + + context 'when logged user cannot push code to the project' do + let(:project) { create(:project, :private) } + let(:service) { described_class.new(project, create(:user)) } + + it_behaves_like 'empty command' + end + + it 'populates create_merge_request with branch_name and issue iid' do + _, updates = service.execute(content, issuable) + + expect(updates).to eq(create_merge_request: { branch_name: branch_name, issue_iid: issuable.iid }) + end + end end describe '#explain' do @@ -1473,5 +1504,27 @@ describe QuickActions::InterpretService do end end end + + describe 'create a merge request' do + context 'with no branch name' do + let(:content) { '/create_merge_request' } + + it 'uses the default branch name' do + _, explanations = service.explain(content, issue) + + expect(explanations).to eq(['Creates a branch and a merge request to resolve this issue']) + end + end + + context 'with a branch name' do + let(:content) { '/create_merge_request foo' } + + it 'uses the given branch name' do + _, explanations = service.explain(content, issue) + + expect(explanations).to eq(["Creates branch 'foo' and a merge request to resolve this issue"]) + end + end + end end end diff --git a/spec/services/system_note_service_spec.rb b/spec/services/system_note_service_spec.rb index a18126ee339..0fbfcb34e50 100644 --- a/spec/services/system_note_service_spec.rb +++ b/spec/services/system_note_service_spec.rb @@ -432,6 +432,20 @@ describe SystemNoteService do end end + describe '.new_merge_request' do + subject { described_class.new_merge_request(noteable, project, author, merge_request) } + + let(:merge_request) { create(:merge_request, source_project: project, target_project: project) } + + it_behaves_like 'a system note' do + let(:action) { 'merge' } + end + + it 'sets the new merge request note text' do + expect(subject.note).to eq("created merge request #{merge_request.to_reference} to address this issue") + end + end + describe '.cross_reference' do subject { described_class.cross_reference(noteable, mentioner, author) } diff --git a/spec/support/helpers/devise_helpers.rb b/spec/support/helpers/devise_helpers.rb index 66874e10f38..d32bc2424c0 100644 --- a/spec/support/helpers/devise_helpers.rb +++ b/spec/support/helpers/devise_helpers.rb @@ -8,8 +8,15 @@ module DeviseHelpers end def env_from_context(context) + # When we modify env_config, that is on the global + # Rails.application, and we need to stub it and allow it to be + # modified in-place, without polluting later tests. if context.respond_to?(:env_config) - context.env_config + context.env_config.deep_dup.tap do |env| + allow(context).to receive(:env_config).and_return(env) + end + # When we modify env, then the context is a request, or something + # else that only lives for a single spec. elsif context.respond_to?(:env) context.env end diff --git a/spec/support/helpers/user_login_helper.rb b/spec/support/helpers/user_login_helper.rb new file mode 100644 index 00000000000..36c002f53af --- /dev/null +++ b/spec/support/helpers/user_login_helper.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +module UserLoginHelper + def ensure_tab_pane_correctness(visit_path = true) + if visit_path + visit new_user_session_path + end + + ensure_tab_pane_counts + ensure_one_active_tab + ensure_one_active_pane + end + + def ensure_tab_pane_counts + tabs_count = page.all('[role="tab"]').size + expect(page).to have_selector('[role="tabpanel"]', count: tabs_count) + end + + def ensure_one_active_tab + expect(page).to have_selector('ul.new-session-tabs > li > a.active', count: 1) + end + + def ensure_one_active_pane + expect(page).to have_selector('.tab-pane.active', count: 1) + end +end |