summaryrefslogtreecommitdiff
path: root/spec/controllers/profiles/keys_controller_spec.rb
blob: 258ed62262ad293049f22f52d4408c1fda553f08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Profiles::KeysController do
  let(:user) { create(:user) }

  describe 'POST #create' do
    before do
      sign_in(user)
    end

    it 'creates a new key' do
      expires_at = 3.days.from_now

      expect do
        post :create, params: { key: build(:key, expires_at: expires_at).attributes }
      end.to change { Key.count }.by(1)

      expect(Key.last.expires_at).to be_like_time(expires_at)
    end
  end

  describe "#get_keys" do
    describe "non existent user" do
      it "does not generally work" do
        get :get_keys, params: { username: 'not-existent' }

        expect(response).not_to be_successful
      end
    end

    describe "user with no keys" do
      it "does generally work" do
        get :get_keys, params: { username: user.username }

        expect(response).to be_successful
      end

      it "renders all keys separated with a new line" do
        get :get_keys, params: { username: user.username }

        expect(response.body).to eq("")
      end

      it "responds with text/plain content type" do
        get :get_keys, params: { username: user.username }
        expect(response.content_type).to eq("text/plain")
      end
    end

    describe "user with keys" do
      let!(:key) { create(:key, user: user) }
      let!(:another_key) { create(:another_key, user: user) }
      let!(:deploy_key) { create(:deploy_key, user: user) }

      it "does generally work" do
        get :get_keys, params: { username: user.username }

        expect(response).to be_successful
      end

      it "renders all non deploy keys separated with a new line" do
        get :get_keys, params: { username: user.username }

        expect(response.body).not_to eq('')
        expect(response.body).to eq(user.all_ssh_keys.join("\n"))

        expect(response.body).to include(key.key.sub(' dummy@gitlab.com', ''))
        expect(response.body).to include(another_key.key.sub(' dummy@gitlab.com', ''))

        expect(response.body).not_to include(deploy_key.key)
      end

      it "does not render the comment of the key" do
        get :get_keys, params: { username: user.username }

        expect(response.body).not_to match(/dummy@gitlab.com/)
      end

      it "responds with text/plain content type" do
        get :get_keys, params: { username: user.username }

        expect(response.content_type).to eq("text/plain")
      end
    end
  end
end