keys_spec.rb 2.24 KB
Newer Older
1 2
require 'rails_helper'

3
describe 'Profile > SSH Keys' do
4 5 6
  let(:user) { create(:user) }

  before do
7
    sign_in(user)
8 9
  end

10 11 12 13 14
  describe 'User adds a key' do
    before do
      visit profile_keys_path
    end

15
    it 'auto-populates the title', :js do
16 17
      fill_in('Key', with: attributes_for(:key).fetch(:key))

18
      expect(page).to have_field("Title", with: "dummy@gitlab.com")
19
    end
20

21
    it 'saves the new key' do
22 23 24 25 26 27 28 29
      attrs = attributes_for(:key)

      fill_in('Key', with: attrs[:key])
      fill_in('Title', with: attrs[:title])
      click_button('Add key')

      expect(page).to have_content("Title: #{attrs[:title]}")
      expect(page).to have_content(attrs[:key])
30
      expect(find('.breadcrumbs-sub-title')).to have_link(attrs[:title])
31
    end
32

33 34 35 36 37 38 39 40 41 42 43 44 45 46
    it 'shows a confirmable warning if the key does not start with ssh-' do
      attrs = attributes_for(:key)

      fill_in('Key', with: 'invalid-key')
      fill_in('Title', with: attrs[:title])
      click_button('Add key')

      expect(page).to have_selector('.js-add-ssh-key-validation-warning')

      find('.js-add-ssh-key-validation-confirm-submit').click

      expect(page).to have_content('Key is invalid')
    end

47 48
    context 'when only DSA and ECDSA keys are allowed' do
      before do
49 50
        forbidden = ApplicationSetting::FORBIDDEN_KEY_VALUE
        stub_application_setting(rsa_key_restriction: forbidden, ed25519_key_restriction: forbidden)
51 52
      end

53
      it 'shows a validation error' do
54 55 56 57 58 59
        attrs = attributes_for(:key)

        fill_in('Key', with: attrs[:key])
        fill_in('Title', with: attrs[:title])
        click_button('Add key')

60
        expect(page).to have_content('Key type is forbidden. Must be DSA or ECDSA')
61 62
      end
    end
63 64
  end

65
  it 'User sees their keys' do
66 67 68 69 70 71
    key = create(:key, user: user)
    visit profile_keys_path

    expect(page).to have_content(key.title)
  end

72
  it 'User removes a key via the key index' do
73 74 75 76 77 78 79 80
    create(:key, user: user)
    visit profile_keys_path

    click_link('Remove')

    expect(page).to have_content('Your SSH keys (0)')
  end

81
  it 'User removes a key via its details page' do
82 83 84 85 86 87
    key = create(:key, user: user)
    visit profile_key_path(key)

    click_link('Remove')

    expect(page).to have_content('Your SSH keys (0)')
88 89
  end
end