environments_spec.rb 4.19 KB
Newer Older
1 2 3 4 5 6 7
require 'spec_helper'

describe API::API, api: true  do
  include ApiHelpers

  let(:user)          { create(:user) }
  let(:non_member)    { create(:user) }
Z.J. van de Weg's avatar
Z.J. van de Weg committed
8
  let(:project)       { create(:project, :private, namespace: user.namespace) }
9 10 11 12 13 14 15 16
  let!(:environment)  { create(:environment, project: project) }

  before do
    project.team << [user, :master]
  end

  describe 'GET /projects/:id/environments' do
    context 'as member of the project' do
17 18 19 20
      it_behaves_like 'a paginated resources' do
        let(:request) { get api("/projects/#{project.id}/environments", user) }
      end

21
      it 'returns project environments' do
22 23 24 25 26 27 28
        get api("/projects/#{project.id}/environments", user)

        expect(response).to have_http_status(200)
        expect(json_response).to be_an Array
        expect(json_response.size).to eq(1)
        expect(json_response.first['name']).to eq(environment.name)
        expect(json_response.first['external_url']).to eq(environment.external_url)
29
        expect(json_response.first['project']['id']).to eq(project.id)
30 31 32 33
      end
    end

    context 'as non member' do
34
      it 'returns a 404 status code' do
35 36 37 38 39 40 41
        get api("/projects/#{project.id}/environments", non_member)

        expect(response).to have_http_status(404)
      end
    end
  end

Z.J. van de Weg's avatar
Z.J. van de Weg committed
42
  describe 'POST /projects/:id/environments' do
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    context 'as a member' do
      it 'creates a environment with valid params' do
        post api("/projects/#{project.id}/environments", user), name: "mepmep"

        expect(response).to have_http_status(201)
        expect(json_response['name']).to eq('mepmep')
        expect(json_response['external']).to be nil
      end

      it 'requires name to be passed' do
        post api("/projects/#{project.id}/environments", user), external_url: 'test.gitlab.com'

        expect(response).to have_http_status(400)
      end

58
      it 'returns a 400 if environment already exists' do
59 60
        post api("/projects/#{project.id}/environments", user), name: environment.name

Z.J. van de Weg's avatar
Z.J. van de Weg committed
61
        expect(response).to have_http_status(400)
62 63 64 65 66
      end
    end

    context 'a non member' do
      it 'rejects the request' do
67
        post api("/projects/#{project.id}/environments", non_member), name: 'gitlab.com'
68

69 70 71 72 73
        expect(response).to have_http_status(404)
      end

      it 'returns a 400 when the required params are missing' do
        post api("/projects/12345/environments", non_member), external_url: 'http://env.git.com'
74 75 76 77
      end
    end
  end

78 79 80 81 82
  describe 'PUT /projects/:id/environments/:environment_id' do
    it 'returns a 200 if name and external_url are changed' do
      url = 'https://mepmep.whatever.ninja'
      put api("/projects/#{project.id}/environments/#{environment.id}", user),
          name: 'Mepmep', external_url: url
83

84 85 86
      expect(response).to have_http_status(200)
      expect(json_response['name']).to eq('Mepmep')
      expect(json_response['external_url']).to eq(url)
87 88
    end

89 90
    it "won't update the external_url if only the name is passed" do
      url = environment.external_url
91
      put api("/projects/#{project.id}/environments/#{environment.id}", user),
92
          name: 'Mepmep'
93 94 95

      expect(response).to have_http_status(200)
      expect(json_response['name']).to eq('Mepmep')
96
      expect(json_response['external_url']).to eq(url)
97 98
    end

99
    it 'returns a 404 if the environment does not exist' do
100 101 102 103 104
      put api("/projects/#{project.id}/environments/12345", user)

      expect(response).to have_http_status(404)
    end
  end
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

  describe 'DELETE /projects/:id/environments/:environment_id' do
    context 'as a master' do
      it 'returns a 200 for an existing environment' do
        delete api("/projects/#{project.id}/environments/#{environment.id}", user)

        expect(response).to have_http_status(200)
      end

      it 'returns a 404 for non existing id' do
        delete api("/projects/#{project.id}/environments/12345", user)

        expect(response).to have_http_status(404)
        expect(json_response['message']).to eq('404 Not found')
      end
    end
121 122 123 124 125 126 127 128

    context 'a non member' do
      it 'rejects the request' do
        delete api("/projects/#{project.id}/environments/#{environment.id}", non_member)

        expect(response).to have_http_status(404)
      end
    end
129
  end
130
end