BigW Consortium Gitlab

templates_spec.rb 6.16 KB
Newer Older
1 2
require 'spec_helper'

3
describe API::Templates do
4 5
  context 'the Template Entity' do
    before { get api('/templates/gitignores/Ruby') }
6

7 8 9
    it { expect(json_response['name']).to eq('Ruby') }
    it { expect(json_response['content']).to include('*.gem') }
  end
10 11 12

  context 'the TemplateList Entity' do
    before { get api('/templates/gitignores') }
13 14 15 16 17

    it { expect(json_response.first['name']).not_to be_nil }
    it { expect(json_response.first['content']).to be_nil }
  end

18
  context 'requesting gitignores' do
19
    it 'returns a list of available gitignore templates' do
20
      get api('/templates/gitignores')
21 22

      expect(response).to have_http_status(200)
23
      expect(response).to include_pagination_headers
24 25
      expect(json_response).to be_an Array
      expect(json_response.size).to be > 15
26
    end
27
  end
28

29
  context 'requesting gitlab-ci-ymls' do
30
    it 'returns a list of available gitlab_ci_ymls' do
31
      get api('/templates/gitlab_ci_ymls')
32

33
      expect(response).to have_http_status(200)
34
      expect(response).to include_pagination_headers
35 36
      expect(json_response).to be_an Array
      expect(json_response.first['name']).not_to be_nil
37
    end
38 39
  end

40
  context 'requesting gitlab-ci-yml for Ruby' do
41
    it 'adds a disclaimer on the top' do
42
      get api('/templates/gitlab_ci_ymls/Ruby')
43 44 45 46 47 48

      expect(response).to have_http_status(200)
      expect(json_response['content']).to start_with("# This file is a template,")
    end
  end

49 50
  context 'the License Template Entity' do
    before { get api('/templates/licenses/mit') }
51 52 53 54 55 56 57 58

    it 'returns a license template' do
      expect(json_response['key']).to eq('mit')
      expect(json_response['name']).to eq('MIT License')
      expect(json_response['nickname']).to be_nil
      expect(json_response['popular']).to be true
      expect(json_response['html_url']).to eq('http://choosealicense.com/licenses/mit/')
      expect(json_response['source_url']).to eq('https://opensource.org/licenses/MIT')
59
      expect(json_response['description']).to include('A short and simple permissive license with conditions')
60 61 62
      expect(json_response['conditions']).to eq(%w[include-copyright])
      expect(json_response['permissions']).to eq(%w[commercial-use modifications distribution private-use])
      expect(json_response['limitations']).to eq(%w[no-liability])
63
      expect(json_response['content']).to include('MIT License')
64 65
    end
  end
66

67
  context 'GET templates/licenses' do
68
    it 'returns a list of available license templates' do
69
      get api('/templates/licenses')
70

71
      expect(response).to have_http_status(200)
72
      expect(response).to include_pagination_headers
73
      expect(json_response).to be_an Array
74
      expect(json_response.size).to eq(12)
75 76 77 78 79 80
      expect(json_response.map { |l| l['key'] }).to include('agpl-3.0')
    end

    describe 'the popular parameter' do
      context 'with popular=1' do
        it 'returns a list of available popular license templates' do
81
          get api('/templates/licenses?popular=1')
82 83

          expect(response).to have_http_status(200)
84
          expect(response).to include_pagination_headers
85
          expect(json_response).to be_an Array
86 87
          expect(json_response.size).to eq(3)
          expect(json_response.map { |l| l['key'] }).to include('apache-2.0')
88
        end
89 90
      end
    end
91
  end
92

93
  context 'GET templates/licenses/:name' do
94 95
    context 'with :project and :fullname given' do
      before do
96
        get api("/templates/licenses/#{license_type}?project=My+Awesome+Project&fullname=Anton+#{license_type.upcase}")
97
      end
98

99 100 101 102
      context 'for the mit license' do
        let(:license_type) { 'mit' }

        it 'returns the license text' do
103
          expect(json_response['content']).to include('MIT License')
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
        end

        it 'replaces placeholder values' do
          expect(json_response['content']).to include("Copyright (c) #{Time.now.year} Anton")
        end
      end

      context 'for the agpl-3.0 license' do
        let(:license_type) { 'agpl-3.0' }

        it 'returns the license text' do
          expect(json_response['content']).to include('GNU AFFERO GENERAL PUBLIC LICENSE')
        end

        it 'replaces placeholder values' do
          expect(json_response['content']).to include('My Awesome Project')
          expect(json_response['content']).to include("Copyright (C) #{Time.now.year}  Anton")
        end
      end

      context 'for the gpl-3.0 license' do
        let(:license_type) { 'gpl-3.0' }

        it 'returns the license text' do
          expect(json_response['content']).to include('GNU GENERAL PUBLIC LICENSE')
        end

        it 'replaces placeholder values' do
          expect(json_response['content']).to include('My Awesome Project')
          expect(json_response['content']).to include("Copyright (C) #{Time.now.year}  Anton")
        end
      end

      context 'for the gpl-2.0 license' do
        let(:license_type) { 'gpl-2.0' }

        it 'returns the license text' do
          expect(json_response['content']).to include('GNU GENERAL PUBLIC LICENSE')
        end

        it 'replaces placeholder values' do
          expect(json_response['content']).to include('My Awesome Project')
          expect(json_response['content']).to include("Copyright (C) #{Time.now.year}  Anton")
        end
      end

      context 'for the apache-2.0 license' do
        let(:license_type) { 'apache-2.0' }

        it 'returns the license text' do
          expect(json_response['content']).to include('Apache License')
        end

        it 'replaces placeholder values' do
          expect(json_response['content']).to include("Copyright #{Time.now.year} Anton")
        end
      end

      context 'for an uknown license' do
        let(:license_type) { 'muth-over9000' }

        it 'returns a 404' do
          expect(response).to have_http_status(404)
167
        end
168 169
      end
    end
Z.J. van de Weg committed
170

171 172 173 174 175 176
    context 'with no :fullname given' do
      context 'with an authenticated user' do
        let(:user) { create(:user) }

        it 'replaces the copyright owner placeholder with the name of the current user' do
          get api('/templates/licenses/mit', user)
Z.J. van de Weg committed
177

178 179
          expect(json_response['content']).to include("Copyright (c) #{Time.now.year} #{user.name}")
        end
180
      end
Z.J. van de Weg committed
181 182
    end
  end
183
end