Skip to content

Commit 85891a0

Browse files
committed
Additional validation and tests for v3/stacks API
Signed-off-by: Rashed Kamal <[email protected]>
1 parent b37be77 commit 85891a0

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

app/messages/stack_create_message.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ class StackCreateMessage < MetadataBaseMessage
77

88
validates :name, presence: true, length: { maximum: 250 }
99
validates :description, length: { maximum: 250 }
10-
validates :state, inclusion: { in: StackStates::VALID_STATES, message: "must be one of #{StackStates::VALID_STATES.join(', ')}" }, allow_nil: true
10+
validates :state, inclusion: { in: StackStates::VALID_STATES, message: "must be one of #{StackStates::VALID_STATES.join(', ')}" }, allow_nil: false, if: :state_requested?
11+
12+
def state_requested?
13+
requested?(:state)
14+
end
1115

1216
def state
1317
return @state if defined?(@state)

app/messages/stack_update_message.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ class StackUpdateMessage < MetadataBaseMessage
66
register_allowed_keys [:state]
77

88
validates_with NoAdditionalKeysValidator
9-
validates :state, inclusion: { in: StackStates::VALID_STATES, message: "must be one of #{StackStates::VALID_STATES.join(', ')}" }, allow_nil: true
9+
validates :state, inclusion: { in: StackStates::VALID_STATES, message: "must be one of #{StackStates::VALID_STATES.join(', ')}" }, allow_nil: false, if: :state_requested?
10+
11+
def state_requested?
12+
requested?(:state)
13+
end
1014
end
1115
end

spec/request/stacks_state_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@
5555
end
5656
end
5757

58+
context 'when creating stack with null state' do
59+
it 'returns validation error' do
60+
request_body = {
61+
name: 'stack-null-state',
62+
state: nil
63+
}.to_json
64+
65+
post '/v3/stacks', request_body, headers
66+
67+
expect(last_response.status).to eq(422)
68+
expect(parsed_response['errors'].first['detail']).to include('must be one of ACTIVE, RESTRICTED, DEPRECATED, DISABLED')
69+
end
70+
end
71+
5872
context 'as non-admin user' do
5973
let(:non_admin_user) { make_user }
6074
let(:non_admin_headers) { headers_for(non_admin_user) }

spec/unit/messages/stack_create_message_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@
112112
expect(subject.errors[:state]).to include('must be one of ACTIVE, RESTRICTED, DEPRECATED, DISABLED')
113113
end
114114
end
115+
116+
context 'when it is explicitly null' do
117+
let(:params) { valid_params.merge({ state: nil }) }
118+
119+
it 'returns an error' do
120+
expect(subject).not_to be_valid
121+
expect(subject.errors[:state]).to include('must be one of ACTIVE, RESTRICTED, DEPRECATED, DISABLED')
122+
end
123+
end
115124
end
116125
end
117126
end

spec/unit/messages/stack_update_message_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@
5252
expect(subject).to be_valid
5353
end
5454
end
55+
56+
context 'when it is explicitly null' do
57+
let(:params) { valid_params.merge({ state: nil }) }
58+
59+
it 'returns an error' do
60+
expect(subject).not_to be_valid
61+
expect(subject.errors[:state]).to include('must be one of ACTIVE, RESTRICTED, DEPRECATED, DISABLED')
62+
end
63+
end
5564
end
5665
end
5766
end

0 commit comments

Comments
 (0)