Skip to content

Commit d05318e

Browse files
authored
(#795) Validate organization CTID (#822)
1 parent 176b40f commit d05318e

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

app/models/organization.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Organization < ActiveRecord::Base
1616

1717
validates :name, presence: true
1818
validates :admin, presence: true
19+
validate :ctid_format, if: :_ctid?
1920

2021
normalize_attribute :name, with: :squish
2122

@@ -33,8 +34,14 @@ def create_key_pair
3334
key_pairs.create!
3435
end
3536

37+
def ctid_format
38+
return if _ctid.starts_with?('ce-') && UUID.validate(_ctid[3.._ctid.size - 1])
39+
40+
errors.add(:_ctid, :invalid)
41+
end
42+
3643
def ensure_ctid
37-
self._ctid ||= SecureRandom.uuid
44+
self._ctid ||= "ce-#{SecureRandom.uuid}"
3845
end
3946

4047
def remove_deleted_envelopes

spec/api/v1/organizations_spec.rb

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,15 @@
146146
# rubocop:todo RSpec/MultipleMemoizedHelpers
147147
context 'as admin' do # rubocop:todo RSpec/ContextWording, RSpec/MultipleMemoizedHelpers
148148
let(:admin) { token.admin }
149+
let(:ctid) { "ce-#{SecureRandom.uuid}" }
149150
let(:description) { Faker::Lorem.sentence }
150151
let(:name) { Faker::Company.name }
151152
let(:token) { create(:auth_token, :admin) }
152153

153154
before do
154155
post '/metadata/organizations',
155156
{
157+
_ctid: ctid,
156158
name: name,
157159
description: description
158160
},
@@ -172,19 +174,48 @@
172174
end
173175
# rubocop:enable RSpec/MultipleMemoizedHelpers
174176

177+
context 'invalid CTID' do # rubocop:todo RSpec/ContextWording, RSpec/MultipleMemoizedHelpers, RSpec/NestedGroups
178+
let(:ctid) { SecureRandom.uuid }
179+
180+
it 'returns 422' do
181+
expect_status(:unprocessable_entity)
182+
expect_json('error', 'Ctid is invalid')
183+
end
184+
end
185+
175186
# rubocop:todo RSpec/MultipleMemoizedHelpers
176187
# rubocop:todo RSpec/NestedGroups
177188
context 'valid params' do # rubocop:todo RSpec/ContextWording, RSpec/MultipleMemoizedHelpers, RSpec/NestedGroups
178-
# rubocop:enable RSpec/NestedGroups
179-
it do
180-
organization = Organization.order(:created_at).last
181-
expect(organization.admin).to eq(admin)
182-
expect(organization.description).to eq(description)
183-
expect(organization.name).to eq(name)
184-
expect_status(:created)
185-
expect_json('id', organization.id)
186-
expect_json('description', organization.description)
187-
expect_json('name', organization.name)
189+
context 'without CTID' do
190+
let(:ctid) { nil }
191+
# rubocop:enable RSpec/NestedGroups
192+
193+
it 'creates an organization with a generated CTID' do
194+
organization = Organization.order(:created_at).last
195+
expect(organization.admin).to eq(admin)
196+
expect(organization.description).to eq(description)
197+
expect(organization.name).to eq(name)
198+
expect_status(:created)
199+
expect_json('_ctid', organization._ctid)
200+
expect_json('id', organization.id)
201+
expect_json('description', organization.description)
202+
expect_json('name', organization.name)
203+
end
204+
end
205+
206+
context 'with CTID' do # rubocop:todo RSpec/NestedGroups
207+
it 'creates an organization' do # rubocop:todo RSpec/MultipleExpectations
208+
organization = Organization.order(:created_at).last
209+
expect(organization._ctid).to eq(ctid)
210+
expect(organization.admin).to eq(admin)
211+
expect(organization.description).to eq(description)
212+
expect(organization.name).to eq(name)
213+
expect_status(:created)
214+
expect_json('_ctid', organization._ctid)
215+
expect_json('id', organization.id)
216+
expect_json('description', organization.description)
217+
expect_json('name', organization.name)
218+
end
188219
end
189220
end
190221
# rubocop:enable RSpec/MultipleMemoizedHelpers

spec/factories/organizations.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@
33
admin
44
description { Faker::Lorem.sentence }
55
name { Faker::Company.name }
6-
_ctid { SecureRandom.uuid }
76
end
87
end

0 commit comments

Comments
 (0)