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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 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 167 168 169 170 171 172 173 174 175
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe API::Organizations, feature_category: :cell do
include WorkhorseHelpers
let(:user) { create(:user) }
shared_examples 'organization avatar upload' do
context 'when valid' do
let(:file_path) { 'spec/fixtures/banana_sample.gif' }
it 'returns avatar url in response' do
make_upload_request
organization_id = json_response['id']
avatar_url = "http://localhost/uploads/-/system/organizations/organization_detail/avatar/#{organization_id}/banana_sample.gif"
expect(json_response['avatar_url']).to eq(avatar_url)
end
end
context 'when invalid' do
shared_examples 'invalid file upload request' do
it 'returns 400', :aggregate_failures do
make_upload_request
expect(response).to have_gitlab_http_status(:bad_request)
expect(response.message).to eq('Bad Request')
expect(json_response['message'].to_s).to match(/#{message}/)
end
end
context 'when file format is not supported' do
let(:file_path) { 'spec/fixtures/doc_sample.txt' }
let(:message) { 'file format is not supported. Please try one of the following supported formats: image/png' }
it_behaves_like 'invalid file upload request'
end
context 'when file is too large' do
let(:file_path) { 'spec/fixtures/big-image.png' }
let(:message) { 'is too big' }
it_behaves_like 'invalid file upload request'
end
end
end
describe 'POST /organizations' do
let(:base_params) do
{
name: 'New Organization',
path: 'new-org',
description: 'A new organization'
}
end
let(:params) { base_params }
context 'when user is not authorized' do
it 'returns unauthorized' do
post api("/organizations"), params: params
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
context 'when feature flag is disabled' do
before do
stub_feature_flags(allow_organization_creation: false)
end
it 'returns forbidden' do
post api("/organizations", user), params: params
expect(response).to have_gitlab_http_status(:forbidden)
end
end
context 'when user is authorized' do
it_behaves_like 'organization avatar upload' do
def make_upload_request
params_with_file_upload = params.merge(avatar: fixture_file_upload(file_path))
workhorse_form_with_file(
api('/organizations', user),
method: :post,
file_key: :avatar,
params: params_with_file_upload
)
end
end
it_behaves_like 'rate limited endpoint', rate_limit_key: :create_organization_api do
let(:current_user) { user }
def request
post api("/organizations", user), params: params
end
end
shared_examples 'returns bad request' do
specify do
post api("/organizations", user), params: params
expect(response).to have_gitlab_http_status(:bad_request)
end
end
it 'creates a new organization' do
post api("/organizations", user), params: params
expect(response).to have_gitlab_http_status(:success)
expect(json_response['name']).to eq('New Organization')
expect(json_response['path']).to eq('new-org')
expect(json_response['description']).to eq('A new organization')
end
context 'when optional params are missing' do
context 'with missing description' do
let(:params) { base_params.except(:description) }
it 'creates a new organization' do
post api("/organizations", user), params: params
expect(response).to have_gitlab_http_status(:success)
expect(json_response['name']).to eq('New Organization')
expect(json_response['path']).to eq('new-org')
end
end
end
context 'when required params are missing' do
context 'with missing name' do
let(:params) { base_params.except(:name) }
it_behaves_like 'returns bad request'
end
context 'with missing path' do
let(:params) { base_params.except(:path) }
it_behaves_like 'returns bad request'
end
end
context 'when organization creation fails' do
it 'returns an error message' do
message = _('Failed to create organization')
allow_next_instance_of(::Organizations::CreateService) do |service|
allow(service).to receive(:execute).and_return(ServiceResponse.error(message: Array(message)))
end
post api("/organizations", user), params: params
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['message']).to match_array(message)
end
end
context 'when organization creation is disable by admin' do
before do
stub_application_setting(can_create_organization: false)
end
it 'returns forbidden' do
post api("/organizations", user), params: params
expect(response).to have_gitlab_http_status(:forbidden)
end
end
end
end
end
|