triggers_spec.rb 7.49 KB
Newer Older
1 2
require 'spec_helper'

3
describe 'Triggers', :js do
4
  let(:trigger_title) { 'trigger desc' }
5
  let(:user) { create(:user) }
6 7
  let(:user2) { create(:user) }
  let(:guest_user) { create(:user) }
8 9

  before do
10
    sign_in(user)
11

12
    @project = create(:project)
13 14
    @project.add_maintainer(user)
    @project.add_maintainer(user2)
15
    @project.add_guest(guest_user)
16

17
    visit project_settings_ci_cd_path(@project)
18 19
  end

20
  describe 'create trigger workflow' do
21
    it 'prevents adding new trigger with no description' do
22 23 24 25
      fill_in 'trigger_description', with: ''
      click_button 'Add trigger'

      # See if input has error due to empty value
Clement Ho's avatar
Clement Ho committed
26
      expect(page.find('form.gl-show-field-errors .gl-field-error')).to be_visible
27 28
    end

29
    it 'adds new trigger with description' do
30 31 32 33 34 35
      fill_in 'trigger_description', with: 'trigger desc'
      click_button 'Add trigger'

      # See if "trigger creation successful" message displayed and description and owner are correct
      expect(page.find('.flash-notice')).to have_content 'Trigger was created successfully.'
      expect(page.find('.triggers-list')).to have_content 'trigger desc'
36
      expect(page.find('.triggers-list .trigger-owner')).to have_content user.name
37 38 39 40 41 42
    end
  end

  describe 'edit trigger workflow' do
    let(:new_trigger_title) { 'new trigger' }

43
    it 'click on edit trigger opens edit trigger page' do
44
      create(:ci_trigger, owner: user, project: @project, description: trigger_title)
45
      visit project_settings_ci_cd_path(@project)
46 47

      # See if edit page has correct descrption
48
      find('a[title="Edit"]').send_keys(:return)
49 50 51
      expect(page.find('#trigger_description').value).to have_content 'trigger desc'
    end

52
    it 'edit trigger and save' do
53
      create(:ci_trigger, owner: user, project: @project, description: trigger_title)
54
      visit project_settings_ci_cd_path(@project)
55 56

      # See if edit page opens, then fill in new description and save
57
      find('a[title="Edit"]').send_keys(:return)
58 59 60 61 62 63
      fill_in 'trigger_description', with: new_trigger_title
      click_button 'Save trigger'

      # See if "trigger updated successfully" message displayed and description and owner are correct
      expect(page.find('.flash-notice')).to have_content 'Trigger was successfully updated.'
      expect(page.find('.triggers-list')).to have_content new_trigger_title
64
      expect(page.find('.triggers-list .trigger-owner')).to have_content user.name
65 66
    end

67
    it 'edit "legacy" trigger and save' do
68 69
      # Create new trigger without owner association, i.e. Legacy trigger
      create(:ci_trigger, owner: nil, project: @project)
70
      visit project_settings_ci_cd_path(@project)
71 72

      # See if the trigger can be edited and description is blank
73
      find('a[title="Edit"]').send_keys(:return)
74 75 76 77 78 79 80 81 82 83 84
      expect(page.find('#trigger_description').value).to have_content ''

      # See if trigger can be updated with description and saved successfully
      fill_in 'trigger_description', with: new_trigger_title
      click_button 'Save trigger'
      expect(page.find('.flash-notice')).to have_content 'Trigger was successfully updated.'
      expect(page.find('.triggers-list')).to have_content new_trigger_title
    end
  end

  describe 'trigger "Take ownership" workflow' do
85
    before do
86
      create(:ci_trigger, owner: user2, project: @project, description: trigger_title)
87
      visit project_settings_ci_cd_path(@project)
88 89
    end

90
    it 'button "Take ownership" has correct alert' do
91 92
      expected_alert = 'By taking ownership you will bind this trigger to your user account. With this the trigger will have access to all your projects as if it was you. Are you sure?'
      expect(page.find('a.btn-trigger-take-ownership')['data-confirm']).to eq expected_alert
93 94
    end

95
    it 'take trigger ownership' do
96 97
      # See if "Take ownership" on trigger works post trigger creation
      page.accept_confirm do
98
        first(:link, "Take ownership").send_keys(:return)
99
      end
100 101 102 103

      expect(page.find('.flash-notice')).to have_content 'Trigger was re-assigned.'
      expect(page.find('.triggers-list')).to have_content trigger_title
      expect(page.find('.triggers-list .trigger-owner')).to have_content user.name
104
    end
105 106 107
  end

  describe 'trigger "Revoke" workflow' do
108
    before do
109
      create(:ci_trigger, owner: user2, project: @project, description: trigger_title)
110
      visit project_settings_ci_cd_path(@project)
111 112
    end

113
    it 'button "Revoke" has correct alert' do
114 115 116 117
      expected_alert = 'By revoking a trigger you will break any processes making use of it. Are you sure?'
      expect(page.find('a.btn-trigger-revoke')['data-confirm']).to eq expected_alert
    end

118
    it 'revoke trigger' do
119 120
      # See if "Revoke" on trigger works post trigger creation
      page.accept_confirm do
121
        find('a.btn-trigger-revoke').send_keys(:return)
122
      end
123 124 125

      expect(page.find('.flash-notice')).to have_content 'Trigger removed'
      expect(page).to have_selector('p.settings-message.text-center.append-bottom-default')
126 127 128 129
    end
  end

  describe 'show triggers workflow' do
130
    it 'contains trigger description placeholder' do
131 132 133
      expect(page.find('#trigger_description')['placeholder']).to eq 'Trigger description'
    end

134
    it 'show "legacy" badge for legacy trigger' do
135
      create(:ci_trigger, owner: nil, project: @project)
136
      visit project_settings_ci_cd_path(@project)
137 138 139 140 141 142

      # See if trigger without owner (i.e. legacy) shows "legacy" badge and is editable
      expect(page.find('.triggers-list')).to have_content 'legacy'
      expect(page.find('.triggers-list')).to have_selector('a[title="Edit"]')
    end

143
    it 'show "invalid" badge for trigger with owner having insufficient permissions' do
144
      create(:ci_trigger, owner: guest_user, project: @project, description: trigger_title)
145
      visit project_settings_ci_cd_path(@project)
146 147 148 149 150 151

      # See if trigger without owner (i.e. legacy) shows "legacy" badge and is non-editable
      expect(page.find('.triggers-list')).to have_content 'invalid'
      expect(page.find('.triggers-list')).not_to have_selector('a[title="Edit"]')
    end

152
    it 'do not show "Edit" or full token for not owned trigger' do
153 154
      # Create trigger with user different from current_user
      create(:ci_trigger, owner: user2, project: @project, description: trigger_title)
155
      visit project_settings_ci_cd_path(@project)
156 157 158 159 160 161

      # See if trigger not owned by current_user shows only first few token chars and doesn't have copy-to-clipboard button
      expect(page.find('.triggers-list')).to have_content(@project.triggers.first.token[0..3])
      expect(page.find('.triggers-list')).not_to have_selector('button.btn-clipboard')

      # See if trigger owner name doesn't match with current_user and trigger is non-editable
162
      expect(page.find('.triggers-list .trigger-owner')).not_to have_content user.name
163 164 165
      expect(page.find('.triggers-list')).not_to have_selector('a[title="Edit"]')
    end

166
    it 'show "Edit" and full token for owned trigger' do
167
      create(:ci_trigger, owner: user, project: @project, description: trigger_title)
168
      visit project_settings_ci_cd_path(@project)
169 170 171 172

      # See if trigger shows full token and has copy-to-clipboard button
      expect(page.find('.triggers-list')).to have_content @project.triggers.first.token
      expect(page.find('.triggers-list')).to have_selector('button.btn-clipboard')
173

174
      # See if trigger owner name matches with current_user and is editable
175
      expect(page.find('.triggers-list .trigger-owner')).to have_content user.name
176
      expect(page.find('.triggers-list')).to have_selector('a[title="Edit"]')
177 178 179
    end
  end
end