Skip to content

Commit 62ec2dc

Browse files
committed
Add username and origin to user create message
1 parent 93ad9c6 commit 62ec2dc

File tree

2 files changed

+98
-4
lines changed

2 files changed

+98
-4
lines changed

app/messages/user_create_message.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,27 @@
22

33
module VCAP::CloudController
44
class UserCreateMessage < MetadataBaseMessage
5-
register_allowed_keys [:guid]
5+
register_allowed_keys %i[guid origin username]
6+
7+
8+
class UserCreateValidator < ActiveModel::Validator
9+
def validate(record)
10+
if record.guid
11+
record.errors.add(:username, message: "cannot be provided with 'guid'") if record.username
12+
record.errors.add(:origin, message: "cannot be provided with 'guid'") if record.origin
13+
elsif record.username || record.origin
14+
record.errors.add(:origin, message: "'username' is missing") unless record.username
15+
record.errors.add(:username, message: "'origin' is missing") unless record.origin
16+
else
17+
record.errors.add(:guid, message: "either 'guid' or 'username' and 'origin' must be provided")
18+
end
19+
end
20+
end
621

722
validates_with NoAdditionalKeysValidator
8-
validates :guid, guid: true
23+
validates :guid, guid: true, allow_nil: true
24+
validates :origin, string: true, allow_nil: true
25+
validates :username, string: true, allow_nil: true
26+
validates_with UserCreateValidator
927
end
1028
end

spec/unit/messages/user_create_message_spec.rb

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,35 @@ module VCAP::CloudController
2727

2828
it 'is not valid' do
2929
expect(subject).not_to be_valid
30-
expect(subject.errors[:guid]).to include('must be between 1 and 200 characters')
30+
expect(subject.errors[:guid]).to include("either 'guid' or 'username' and 'origin' must be provided")
31+
end
32+
end
33+
34+
context 'when guid and username is provided' do
35+
let(:params) do
36+
{
37+
username: 'meow',
38+
guid: 'some-user-guid'
39+
}
40+
end
41+
42+
it 'is not valid' do
43+
expect(subject).not_to be_valid
44+
expect(subject.errors[:username]).to include("cannot be provided with 'guid'")
45+
end
46+
end
47+
48+
context 'when guid and origin is provided' do
49+
let(:params) do
50+
{
51+
origin: 'meow',
52+
guid: 'some-user-guid'
53+
}
54+
end
55+
56+
it 'is not valid' do
57+
expect(subject).not_to be_valid
58+
expect(subject.errors[:origin]).to include("cannot be provided with 'guid'")
3159
end
3260
end
3361

@@ -45,7 +73,7 @@ module VCAP::CloudController
4573
end
4674
end
4775

48-
context 'guid' do
76+
describe 'guid' do
4977
context 'when not a string' do
5078
let(:params) do
5179
{ guid: 5 }
@@ -75,6 +103,54 @@ module VCAP::CloudController
75103
end
76104
end
77105
end
106+
107+
describe 'username' do
108+
context 'when not a string' do
109+
let(:params) do
110+
{ username: 5 }
111+
end
112+
113+
it 'is not valid' do
114+
expect(subject).not_to be_valid
115+
expect(subject.errors[:username]).to include('must be a string')
116+
end
117+
end
118+
119+
context 'when origin is missing' do
120+
let(:params) do
121+
{ username: 5 }
122+
end
123+
124+
it 'is not valid' do
125+
expect(subject).not_to be_valid
126+
expect(subject.errors[:username]).to include("'origin' is missing")
127+
end
128+
end
129+
end
130+
131+
describe 'origin' do
132+
context 'when not a string' do
133+
let(:params) do
134+
{ origin: 5 }
135+
end
136+
137+
it 'is not valid' do
138+
expect(subject).not_to be_valid
139+
expect(subject.errors[:origin]).to include('must be a string')
140+
end
141+
end
142+
143+
context 'when username is missing' do
144+
let(:params) do
145+
{ origin: 5 }
146+
end
147+
148+
it 'is not valid' do
149+
expect(subject).not_to be_valid
150+
expect(subject.errors[:origin]).to include("'username' is missing")
151+
end
152+
end
153+
end
78154
end
79155
end
80156
end

0 commit comments

Comments
 (0)