From 9c266d49354e9f7f86c76b00fbe800912f475153 Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Wed, 16 Dec 2015 16:29:31 -0200 Subject: Add more descriptive error message when create branch with invalid name --- app/services/create_branch_service.rb | 2 +- app/views/projects/branches/new.html.haml | 1 + features/steps/project/commits/branches.rb | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/services/create_branch_service.rb b/app/services/create_branch_service.rb index de18f3bc556..6a77f51628e 100644 --- a/app/services/create_branch_service.rb +++ b/app/services/create_branch_service.rb @@ -4,7 +4,7 @@ class CreateBranchService < BaseService def execute(branch_name, ref) valid_branch = Gitlab::GitRefValidator.validate(branch_name) if valid_branch == false - return error('Branch name invalid') + return error("Branch name can't contains space, '~', '^', ':', '?', '*', '[', '\', '..', '@{', and consecutive slashes, start with '/' or '.' or end in '/' or '.' or '.lock'") end repository = project.repository diff --git a/app/views/projects/branches/new.html.haml b/app/views/projects/branches/new.html.haml index 31943a2407a..4e94f2a5ea2 100644 --- a/app/views/projects/branches/new.html.haml +++ b/app/views/projects/branches/new.html.haml @@ -14,6 +14,7 @@ = label_tag :branch_name, nil, class: 'control-label' .col-sm-10 = text_field_tag :branch_name, params[:branch_name], required: true, tabindex: 1, autofocus: true, class: 'form-control' + .help-block Can't contains space, '~', '^', ':', '?', '*', '[', '\', '..', '@{', and consecutive slashes, start with '/' or '.' or end in '/' or '.' or '.lock' .form-group = label_tag :ref, 'Create from', class: 'control-label' .col-sm-10 diff --git a/features/steps/project/commits/branches.rb b/features/steps/project/commits/branches.rb index 338f5e8d3ee..e9312e87637 100644 --- a/features/steps/project/commits/branches.rb +++ b/features/steps/project/commits/branches.rb @@ -61,7 +61,7 @@ class Spinach::Features::ProjectCommitsBranches < Spinach::FeatureSteps end step 'I should see new an error that branch is invalid' do - expect(page).to have_content 'Branch name invalid' + expect(page).to have_content "Branch name can't contains space, '~', '^', ':', '?', '*', '[', '\', '..', '@{', and consecutive slashes, start with '/' or '.' or end in '/' or '.' or '.lock'" end step 'I should see new an error that ref is invalid' do -- cgit v1.2.1 From c91cf1f66b3ed4329b313b7b75fca816fc5c6076 Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Wed, 16 Dec 2015 16:48:14 -0200 Subject: Remove the hint on the branch name input --- app/views/projects/branches/new.html.haml | 1 - 1 file changed, 1 deletion(-) diff --git a/app/views/projects/branches/new.html.haml b/app/views/projects/branches/new.html.haml index 4e94f2a5ea2..31943a2407a 100644 --- a/app/views/projects/branches/new.html.haml +++ b/app/views/projects/branches/new.html.haml @@ -14,7 +14,6 @@ = label_tag :branch_name, nil, class: 'control-label' .col-sm-10 = text_field_tag :branch_name, params[:branch_name], required: true, tabindex: 1, autofocus: true, class: 'form-control' - .help-block Can't contains space, '~', '^', ':', '?', '*', '[', '\', '..', '@{', and consecutive slashes, start with '/' or '.' or end in '/' or '.' or '.lock' .form-group = label_tag :ref, 'Create from', class: 'control-label' .col-sm-10 -- cgit v1.2.1 From 1757e6ef65a1cedbfe5d5b5da894d6d0d3d5ef16 Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Thu, 17 Dec 2015 16:59:15 -0200 Subject: Add JS validation for invalid characters in branch name More info about valid ref names: https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.ht ml --- app/assets/javascripts/new_branch_form.js.coffee | 73 ++++++++++++++++++++++++ app/services/create_branch_service.rb | 2 +- app/views/projects/branches/new.html.haml | 10 ++-- features/project/commits/branches.feature | 1 + features/steps/project/commits/branches.rb | 3 +- 5 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 app/assets/javascripts/new_branch_form.js.coffee diff --git a/app/assets/javascripts/new_branch_form.js.coffee b/app/assets/javascripts/new_branch_form.js.coffee new file mode 100644 index 00000000000..af531b3bf9f --- /dev/null +++ b/app/assets/javascripts/new_branch_form.js.coffee @@ -0,0 +1,73 @@ +class @NewBranchForm + constructor: (form, availableRefs) -> + @branchNameError = form.find('.js-branch-name-error') + @name = form.find('.js-branch-name') + @ref = form.find('#ref') + + @setupAvailableRefs(availableRefs) + @setupRestrictions() + @addBinding() + @init() + + addBinding: -> + @name.on 'blur', @validate + + init: -> + @name.trigger 'blur'if @name.val().length > 0 + + setupAvailableRefs: (availableRefs) -> + @ref.autocomplete { + source: availableRefs, + minLength: 1 + } + + setupRestrictions: -> + startsWith = { + pattern: /^(\/|\.)/g, + prefix: "can't start with ", + conjunction: "or" + } + + endsWith = { + pattern: /(\/|\.|\.lock)$/g, + prefix: "can't end in ", + conjunction: "or" + } + + characters = { + pattern: /(\s|~|\^|:|\?|\*|\[|\\|\.\.|@\{|\/{2,}){1}/g + prefix: "can't contains ", + conjunction: ", " + } + + @restrictions = [startsWith, characters, endsWith] + + validate: => + @branchNameError.empty() + + unique = (values, value) -> + values.push(value) unless value in values + values + + formatter = (values, restriction) -> + formatted = values.map (value) -> + switch + when /\s/.test value then 'spaces' + when /\/{2,}/g.test value then 'consecutive slashes' + else "'#{value}'" + + "#{restriction.prefix} #{formatted.join(restriction.conjunction)}" + + validator = (errors, restriction) => + matched = @name.val().match(restriction.pattern) + + if matched + errors.concat formatter(matched.reduce(unique, []), restriction) + else + errors + + errors = @restrictions.reduce validator, [] + + if errors.length > 0 + errorMessage = $("").text(errors.join(', ')) + @branchNameError.append(errorMessage) diff --git a/app/services/create_branch_service.rb b/app/services/create_branch_service.rb index 6a77f51628e..a6844985c4e 100644 --- a/app/services/create_branch_service.rb +++ b/app/services/create_branch_service.rb @@ -4,7 +4,7 @@ class CreateBranchService < BaseService def execute(branch_name, ref) valid_branch = Gitlab::GitRefValidator.validate(branch_name) if valid_branch == false - return error("Branch name can't contains space, '~', '^', ':', '?', '*', '[', '\', '..', '@{', and consecutive slashes, start with '/' or '.' or end in '/' or '.' or '.lock'") + return error('Branch name is invalid') end repository = project.repository diff --git a/app/views/projects/branches/new.html.haml b/app/views/projects/branches/new.html.haml index 31943a2407a..c659af6338c 100644 --- a/app/views/projects/branches/new.html.haml +++ b/app/views/projects/branches/new.html.haml @@ -9,11 +9,12 @@ New Branch %hr -= form_tag namespace_project_branches_path, method: :post, id: "new-branch-form", class: "form-horizontal js-requires-input" do += form_tag namespace_project_branches_path, method: :post, id: "new-branch-form", class: "form-horizontal js-create-branch-form js-requires-input" do .form-group = label_tag :branch_name, nil, class: 'control-label' .col-sm-10 - = text_field_tag :branch_name, params[:branch_name], required: true, tabindex: 1, autofocus: true, class: 'form-control' + = text_field_tag :branch_name, params[:branch_name], required: true, tabindex: 1, autofocus: true, class: 'form-control js-branch-name' + .help-block.text-danger.js-branch-name-error .form-group = label_tag :ref, 'Create from', class: 'control-label' .col-sm-10 @@ -26,7 +27,4 @@ :javascript var availableRefs = #{@project.repository.ref_names.to_json}; - $("#ref").autocomplete({ - source: availableRefs, - minLength: 1 - }); + new NewBranchForm($('.js-create-branch-form'), availableRefs) diff --git a/features/project/commits/branches.feature b/features/project/commits/branches.feature index 5103ca12947..2c17d32154a 100644 --- a/features/project/commits/branches.feature +++ b/features/project/commits/branches.feature @@ -25,6 +25,7 @@ Feature: Project Commits Branches And I click branch 'improve/awesome' delete link Then I should not see branch 'improve/awesome' + @javascript Scenario: I create a branch with invalid name Given I visit project branches page And I click new branch link diff --git a/features/steps/project/commits/branches.rb b/features/steps/project/commits/branches.rb index e9312e87637..109d031d446 100644 --- a/features/steps/project/commits/branches.rb +++ b/features/steps/project/commits/branches.rb @@ -61,7 +61,8 @@ class Spinach::Features::ProjectCommitsBranches < Spinach::FeatureSteps end step 'I should see new an error that branch is invalid' do - expect(page).to have_content "Branch name can't contains space, '~', '^', ':', '?', '*', '[', '\', '..', '@{', and consecutive slashes, start with '/' or '.' or end in '/' or '.' or '.lock'" + expect(page).to have_content 'Branch name is invalid' + expect(page).to have_content "can't contains spaces" end step 'I should see new an error that ref is invalid' do -- cgit v1.2.1 From e26d1b47575646f15f1ab3a91878bbb695d3bc65 Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Thu, 17 Dec 2015 19:44:28 -0200 Subject: Remove extra spaces in the error messages --- app/assets/javascripts/new_branch_form.js.coffee | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/new_branch_form.js.coffee b/app/assets/javascripts/new_branch_form.js.coffee index af531b3bf9f..6b01bb0ce21 100644 --- a/app/assets/javascripts/new_branch_form.js.coffee +++ b/app/assets/javascripts/new_branch_form.js.coffee @@ -24,19 +24,19 @@ class @NewBranchForm setupRestrictions: -> startsWith = { pattern: /^(\/|\.)/g, - prefix: "can't start with ", + prefix: "can't start with", conjunction: "or" } endsWith = { pattern: /(\/|\.|\.lock)$/g, - prefix: "can't end in ", + prefix: "can't end in", conjunction: "or" } characters = { pattern: /(\s|~|\^|:|\?|\*|\[|\\|\.\.|@\{|\/{2,}){1}/g - prefix: "can't contains ", + prefix: "can't contains", conjunction: ", " } -- cgit v1.2.1 From 05737f6c85619863f1ed15a8f6a6d0f1d72c1f22 Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Thu, 17 Dec 2015 19:45:11 -0200 Subject: Add restriction for single characters in branch name --- app/assets/javascripts/new_branch_form.js.coffee | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/new_branch_form.js.coffee b/app/assets/javascripts/new_branch_form.js.coffee index 6b01bb0ce21..23a5b333b8a 100644 --- a/app/assets/javascripts/new_branch_form.js.coffee +++ b/app/assets/javascripts/new_branch_form.js.coffee @@ -34,13 +34,19 @@ class @NewBranchForm conjunction: "or" } - characters = { + invalid = { pattern: /(\s|~|\^|:|\?|\*|\[|\\|\.\.|@\{|\/{2,}){1}/g prefix: "can't contains", conjunction: ", " } - @restrictions = [startsWith, characters, endsWith] + single = { + pattern: /^@+$/g + prefix: "can't be", + conjunction: "or" + } + + @restrictions = [startsWith, invalid, endsWith, single] validate: => @branchNameError.empty() -- cgit v1.2.1 From 4163eb56d91f75f62d562034140b0b4debecb315 Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Thu, 17 Dec 2015 19:45:43 -0200 Subject: Add specs for JS validation for invalid characters in branch name --- spec/javascripts/fixtures/new_branch.html.haml | 4 + spec/javascripts/new_branch_spec.js.coffee | 155 +++++++++++++++++++++++++ 2 files changed, 159 insertions(+) create mode 100644 spec/javascripts/fixtures/new_branch.html.haml create mode 100644 spec/javascripts/new_branch_spec.js.coffee diff --git a/spec/javascripts/fixtures/new_branch.html.haml b/spec/javascripts/fixtures/new_branch.html.haml new file mode 100644 index 00000000000..f06629e5ecc --- /dev/null +++ b/spec/javascripts/fixtures/new_branch.html.haml @@ -0,0 +1,4 @@ +%form.js-create-branch-form + %input.js-branch-name + .js-branch-name-error + %input{id: "ref"} diff --git a/spec/javascripts/new_branch_spec.js.coffee b/spec/javascripts/new_branch_spec.js.coffee new file mode 100644 index 00000000000..1cba9546655 --- /dev/null +++ b/spec/javascripts/new_branch_spec.js.coffee @@ -0,0 +1,155 @@ +#= require jquery.ui.all +#= require new_branch_form + +describe 'Branch', -> + describe 'create a new branch', -> + fixture.preload('new_branch.html') + + beforeEach -> + fixture.load('new_branch.html') + $('form').on 'submit', (e) -> e.preventDefault() + + @form = new NewBranchForm($('.js-create-branch-form'), []) + @name = $('.js-branch-name') + + it "can't start with a dot", -> + @name.val('.foo').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't start with '.'") + + it "can't start with a slash", -> + @name.val('/foo').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't start with '/'") + + it "can't have two consecutive dots", -> + @name.val('foo..bar').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '..'") + + it "can't have spaces anywhere", -> + @name.val(' foo').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains spaces") + @name.val('foo bar').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains spaces") + @name.val('foo ').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains spaces") + + it "can't have ~ anywhere", -> + @name.val('~foo').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '~'") + @name.val('foo~bar').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '~'") + @name.val('foo~').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '~'") + + it "can't have tilde anwhere", -> + @name.val('~foo').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '~'") + @name.val('foo~bar').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '~'") + @name.val('foo~').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '~'") + + it "can't have caret anywhere", -> + @name.val('^foo').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '^'") + @name.val('foo^bar').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '^'") + @name.val('foo^').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '^'") + + it "can't have : anywhere", -> + @name.val(':foo').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains ':'") + @name.val('foo:bar').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains ':'") + @name.val(':foo').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains ':'") + + it "can't have question mark anywhere", -> + @name.val('?foo').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '?'") + @name.val('foo?bar').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '?'") + @name.val('foo?').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '?'") + + it "can't have asterisk anywhere", -> + @name.val('*foo').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '*'") + @name.val('foo*bar').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '*'") + @name.val('foo*').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '*'") + + it "can't have open bracket anywhere", -> + @name.val('[foo').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '['") + @name.val('foo[bar').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '['") + @name.val('foo[').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '['") + + it "can't have a backslash anywhere", -> + @name.val('\\foo').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '\\'") + @name.val('foo\\bar').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '\\'") + @name.val('foo\\').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '\\'") + + it "can't contain a sequence @{ anywhere", -> + @name.val('@{foo').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '@{'") + @name.val('foo@{bar').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '@{'") + @name.val('foo@{').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '@{'") + + it "can't have consecutive slashes", -> + @name.val('foo//bar').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains consecutive slashes") + + it "can't end with a slash", -> + @name.val('foo/').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't end in '/'") + + it "can't end with a dot", -> + @name.val('foo.').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't end in '.'") + + it "can't end with .lock", -> + @name.val('foo.lock').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't end in '.lock'") + + it "can't be the single character @", -> + @name.val('@').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't be '@'") + + it "concatenates all error messages", -> + @name.val('/foo bar?~.').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't start with '/', can't contains spaces, '?', '~', can't end in '.'") + + it "doesn't duplicate error messages", -> + @name.val('?foo?bar?zoo?').trigger('blur') + expect($('.js-branch-name-error span').text()).toEqual("can't contains '?'") + + it "removes the error message when is a valid name", -> + @name.val('foo?bar').trigger('blur') + expect($('.js-branch-name-error span').length).toEqual(1) + @name.val('foobar').trigger('blur') + expect($('.js-branch-name-error span').length).toEqual(0) + + it "can have dashes anywhere", -> + @name.val('-foo-bar-zoo-').trigger('blur') + expect($('.js-branch-name-error span').length).toEqual(0) + + it "can have underscores anywhere", -> + @name.val('_foo_bar_zoo_').trigger('blur') + expect($('.js-branch-name-error span').length).toEqual(0) + + it "can have numbers anywhere", -> + @name.val('1foo2bar3zoo4').trigger('blur') + expect($('.js-branch-name-error span').length).toEqual(0) + + it "can be only letters", -> + @name.val('foo').trigger('blur') + expect($('.js-branch-name-error span').length).toEqual(0) -- cgit v1.2.1 From daa28d9fbd78415fd9e1d33deafdcaed56b10981 Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Fri, 18 Dec 2015 12:43:53 -0200 Subject: Fix spec --- spec/requests/api/branches_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/requests/api/branches_spec.rb b/spec/requests/api/branches_spec.rb index 5c1b58535cc..36461e84c3a 100644 --- a/spec/requests/api/branches_spec.rb +++ b/spec/requests/api/branches_spec.rb @@ -118,7 +118,7 @@ describe API::API, api: true do branch_name: 'new design', ref: branch_sha expect(response.status).to eq(400) - expect(json_response['message']).to eq('Branch name invalid') + expect(json_response['message']).to eq('Branch name is invalid') end it 'should return 400 if branch already exists' do -- cgit v1.2.1 From 23b436dc9346275e9fbd6201317e09da63befc46 Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Mon, 21 Dec 2015 21:44:34 -0200 Subject: Fix minor stylistic complaints --- app/assets/javascripts/new_branch_form.js.coffee | 7 ++- features/steps/project/commits/branches.rb | 2 +- spec/javascripts/new_branch_spec.js.coffee | 68 ++++++++++++------------ 3 files changed, 38 insertions(+), 39 deletions(-) diff --git a/app/assets/javascripts/new_branch_form.js.coffee b/app/assets/javascripts/new_branch_form.js.coffee index 23a5b333b8a..4b350854f78 100644 --- a/app/assets/javascripts/new_branch_form.js.coffee +++ b/app/assets/javascripts/new_branch_form.js.coffee @@ -13,13 +13,12 @@ class @NewBranchForm @name.on 'blur', @validate init: -> - @name.trigger 'blur'if @name.val().length > 0 + @name.trigger 'blur' if @name.val().length > 0 setupAvailableRefs: (availableRefs) -> - @ref.autocomplete { + @ref.autocomplete source: availableRefs, minLength: 1 - } setupRestrictions: -> startsWith = { @@ -36,7 +35,7 @@ class @NewBranchForm invalid = { pattern: /(\s|~|\^|:|\?|\*|\[|\\|\.\.|@\{|\/{2,}){1}/g - prefix: "can't contains", + prefix: "can't contain", conjunction: ", " } diff --git a/features/steps/project/commits/branches.rb b/features/steps/project/commits/branches.rb index 109d031d446..0a42931147d 100644 --- a/features/steps/project/commits/branches.rb +++ b/features/steps/project/commits/branches.rb @@ -62,7 +62,7 @@ class Spinach::Features::ProjectCommitsBranches < Spinach::FeatureSteps step 'I should see new an error that branch is invalid' do expect(page).to have_content 'Branch name is invalid' - expect(page).to have_content "can't contains spaces" + expect(page).to have_content "can't contain spaces" end step 'I should see new an error that ref is invalid' do diff --git a/spec/javascripts/new_branch_spec.js.coffee b/spec/javascripts/new_branch_spec.js.coffee index 1cba9546655..ea479239957 100644 --- a/spec/javascripts/new_branch_spec.js.coffee +++ b/spec/javascripts/new_branch_spec.js.coffee @@ -22,91 +22,91 @@ describe 'Branch', -> it "can't have two consecutive dots", -> @name.val('foo..bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '..'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '..'") it "can't have spaces anywhere", -> @name.val(' foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains spaces") + expect($('.js-branch-name-error span').text()).toEqual("can't contain spaces") @name.val('foo bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains spaces") + expect($('.js-branch-name-error span').text()).toEqual("can't contain spaces") @name.val('foo ').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains spaces") + expect($('.js-branch-name-error span').text()).toEqual("can't contain spaces") it "can't have ~ anywhere", -> @name.val('~foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '~'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '~'") @name.val('foo~bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '~'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '~'") @name.val('foo~').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '~'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '~'") it "can't have tilde anwhere", -> @name.val('~foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '~'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '~'") @name.val('foo~bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '~'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '~'") @name.val('foo~').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '~'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '~'") it "can't have caret anywhere", -> @name.val('^foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '^'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '^'") @name.val('foo^bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '^'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '^'") @name.val('foo^').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '^'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '^'") it "can't have : anywhere", -> @name.val(':foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains ':'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain ':'") @name.val('foo:bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains ':'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain ':'") @name.val(':foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains ':'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain ':'") it "can't have question mark anywhere", -> @name.val('?foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '?'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '?'") @name.val('foo?bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '?'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '?'") @name.val('foo?').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '?'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '?'") it "can't have asterisk anywhere", -> @name.val('*foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '*'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '*'") @name.val('foo*bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '*'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '*'") @name.val('foo*').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '*'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '*'") it "can't have open bracket anywhere", -> @name.val('[foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '['") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '['") @name.val('foo[bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '['") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '['") @name.val('foo[').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '['") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '['") it "can't have a backslash anywhere", -> @name.val('\\foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '\\'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '\\'") @name.val('foo\\bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '\\'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '\\'") @name.val('foo\\').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '\\'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '\\'") it "can't contain a sequence @{ anywhere", -> @name.val('@{foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '@{'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '@{'") @name.val('foo@{bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '@{'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '@{'") @name.val('foo@{').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '@{'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '@{'") it "can't have consecutive slashes", -> @name.val('foo//bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains consecutive slashes") + expect($('.js-branch-name-error span').text()).toEqual("can't contain consecutive slashes") it "can't end with a slash", -> @name.val('foo/').trigger('blur') @@ -126,11 +126,11 @@ describe 'Branch', -> it "concatenates all error messages", -> @name.val('/foo bar?~.').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't start with '/', can't contains spaces, '?', '~', can't end in '.'") + expect($('.js-branch-name-error span').text()).toEqual("can't start with '/', can't contain spaces, '?', '~', can't end in '.'") it "doesn't duplicate error messages", -> @name.val('?foo?bar?zoo?').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contains '?'") + expect($('.js-branch-name-error span').text()).toEqual("can't contain '?'") it "removes the error message when is a valid name", -> @name.val('foo?bar').trigger('blur') -- cgit v1.2.1 From 3657eb76c265f4cf6c35c13c2e32bd2536df89ed Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Tue, 22 Dec 2015 15:32:57 -0200 Subject: Add helper methods to JS validation spec for invalid branch names --- spec/javascripts/new_branch_spec.js.coffee | 179 +++++++++++++++-------------- 1 file changed, 92 insertions(+), 87 deletions(-) diff --git a/spec/javascripts/new_branch_spec.js.coffee b/spec/javascripts/new_branch_spec.js.coffee index ea479239957..8889ce2e9b3 100644 --- a/spec/javascripts/new_branch_spec.js.coffee +++ b/spec/javascripts/new_branch_spec.js.coffee @@ -5,151 +5,156 @@ describe 'Branch', -> describe 'create a new branch', -> fixture.preload('new_branch.html') + fillNameWith = (value) -> + $('.js-branch-name').val(value).trigger('blur') + + expectToHaveError = (error) -> + expect($('.js-branch-name-error span').text()).toEqual(error) + beforeEach -> fixture.load('new_branch.html') $('form').on 'submit', (e) -> e.preventDefault() @form = new NewBranchForm($('.js-create-branch-form'), []) - @name = $('.js-branch-name') it "can't start with a dot", -> - @name.val('.foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't start with '.'") + fillNameWith '.foo' + expectToHaveError "can't start with '.'" it "can't start with a slash", -> - @name.val('/foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't start with '/'") + fillNameWith '/foo' + expectToHaveError "can't start with '/'" it "can't have two consecutive dots", -> - @name.val('foo..bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '..'") + fillNameWith 'foo..bar' + expectToHaveError "can't contain '..'" it "can't have spaces anywhere", -> - @name.val(' foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain spaces") - @name.val('foo bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain spaces") - @name.val('foo ').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain spaces") + fillNameWith ' foo' + expectToHaveError "can't contain spaces" + fillNameWith 'foo bar' + expectToHaveError "can't contain spaces" + fillNameWith 'foo ' + expectToHaveError "can't contain spaces" it "can't have ~ anywhere", -> - @name.val('~foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '~'") - @name.val('foo~bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '~'") - @name.val('foo~').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '~'") + fillNameWith '~foo' + expectToHaveError "can't contain '~'" + fillNameWith 'foo~bar' + expectToHaveError "can't contain '~'" + fillNameWith 'foo~' + expectToHaveError "can't contain '~'" it "can't have tilde anwhere", -> - @name.val('~foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '~'") - @name.val('foo~bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '~'") - @name.val('foo~').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '~'") + fillNameWith '~foo' + expectToHaveError "can't contain '~'" + fillNameWith 'foo~bar' + expectToHaveError "can't contain '~'" + fillNameWith 'foo~' + expectToHaveError "can't contain '~'" it "can't have caret anywhere", -> - @name.val('^foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '^'") - @name.val('foo^bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '^'") - @name.val('foo^').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '^'") + fillNameWith '^foo' + expectToHaveError "can't contain '^'" + fillNameWith 'foo^bar' + expectToHaveError "can't contain '^'" + fillNameWith 'foo^' + expectToHaveError "can't contain '^'" it "can't have : anywhere", -> - @name.val(':foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain ':'") - @name.val('foo:bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain ':'") - @name.val(':foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain ':'") + fillNameWith ':foo' + expectToHaveError "can't contain ':'" + fillNameWith 'foo:bar' + expectToHaveError "can't contain ':'" + fillNameWith ':foo' + expectToHaveError "can't contain ':'" it "can't have question mark anywhere", -> - @name.val('?foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '?'") - @name.val('foo?bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '?'") - @name.val('foo?').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '?'") + fillNameWith '?foo' + expectToHaveError "can't contain '?'" + fillNameWith 'foo?bar' + expectToHaveError "can't contain '?'" + fillNameWith 'foo?' + expectToHaveError "can't contain '?'" it "can't have asterisk anywhere", -> - @name.val('*foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '*'") - @name.val('foo*bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '*'") - @name.val('foo*').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '*'") + fillNameWith '*foo' + expectToHaveError "can't contain '*'" + fillNameWith 'foo*bar' + expectToHaveError "can't contain '*'" + fillNameWith 'foo*' + expectToHaveError "can't contain '*'" it "can't have open bracket anywhere", -> - @name.val('[foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '['") - @name.val('foo[bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '['") - @name.val('foo[').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '['") + fillNameWith '[foo' + expectToHaveError "can't contain '['" + fillNameWith 'foo[bar' + expectToHaveError "can't contain '['" + fillNameWith 'foo[' + expectToHaveError "can't contain '['" it "can't have a backslash anywhere", -> - @name.val('\\foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '\\'") - @name.val('foo\\bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '\\'") - @name.val('foo\\').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '\\'") + fillNameWith '\\foo' + expectToHaveError "can't contain '\\'" + fillNameWith 'foo\\bar' + expectToHaveError "can't contain '\\'" + fillNameWith 'foo\\' + expectToHaveError "can't contain '\\'" it "can't contain a sequence @{ anywhere", -> - @name.val('@{foo').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '@{'") - @name.val('foo@{bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '@{'") - @name.val('foo@{').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '@{'") + fillNameWith '@{foo' + expectToHaveError "can't contain '@{'" + fillNameWith 'foo@{bar' + expectToHaveError "can't contain '@{'" + fillNameWith 'foo@{' + expectToHaveError "can't contain '@{'" it "can't have consecutive slashes", -> - @name.val('foo//bar').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain consecutive slashes") + fillNameWith 'foo//bar' + expectToHaveError "can't contain consecutive slashes" it "can't end with a slash", -> - @name.val('foo/').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't end in '/'") + fillNameWith 'foo/' + expectToHaveError "can't end in '/'" it "can't end with a dot", -> - @name.val('foo.').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't end in '.'") + fillNameWith 'foo.' + expectToHaveError "can't end in '.'" it "can't end with .lock", -> - @name.val('foo.lock').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't end in '.lock'") + fillNameWith 'foo.lock' + expectToHaveError "can't end in '.lock'" it "can't be the single character @", -> - @name.val('@').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't be '@'") + fillNameWith '@' + expectToHaveError "can't be '@'" it "concatenates all error messages", -> - @name.val('/foo bar?~.').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't start with '/', can't contain spaces, '?', '~', can't end in '.'") + fillNameWith '/foo bar?~.' + expectToHaveError "can't start with '/', can't contain spaces, '?', '~', can't end in '.'" it "doesn't duplicate error messages", -> - @name.val('?foo?bar?zoo?').trigger('blur') - expect($('.js-branch-name-error span').text()).toEqual("can't contain '?'") + fillNameWith '?foo?bar?zoo?' + expectToHaveError "can't contain '?'" it "removes the error message when is a valid name", -> - @name.val('foo?bar').trigger('blur') + fillNameWith 'foo?bar' expect($('.js-branch-name-error span').length).toEqual(1) - @name.val('foobar').trigger('blur') + fillNameWith 'foobar' expect($('.js-branch-name-error span').length).toEqual(0) it "can have dashes anywhere", -> - @name.val('-foo-bar-zoo-').trigger('blur') + fillNameWith '-foo-bar-zoo-' expect($('.js-branch-name-error span').length).toEqual(0) it "can have underscores anywhere", -> - @name.val('_foo_bar_zoo_').trigger('blur') + fillNameWith '_foo_bar_zoo_' expect($('.js-branch-name-error span').length).toEqual(0) it "can have numbers anywhere", -> - @name.val('1foo2bar3zoo4').trigger('blur') + fillNameWith '1foo2bar3zoo4' expect($('.js-branch-name-error span').length).toEqual(0) it "can be only letters", -> - @name.val('foo').trigger('blur') + fillNameWith 'foo' expect($('.js-branch-name-error span').length).toEqual(0) -- cgit v1.2.1