Skip to content

Commit 5d3e46d

Browse files
committed
Validate applications before saving
1 parent 8bd9e0c commit 5d3e46d

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

lib/3scale/backend/application.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def with_app_id_from_params(service_id, app_id, user_key)
164164
end
165165

166166
def save
167-
raise ApplicationHasNoState.new(id) if !state
167+
validate_attributes
168168

169169
storage.pipelined do |pipeline|
170170
persist_attributes(pipeline)
@@ -319,6 +319,12 @@ def has_referrer_filters?
319319

320320
private
321321

322+
def validate_attributes
323+
raise ApplicationHasNoID.new unless id
324+
raise ApplicationHasNoState.new(id) unless state
325+
raise ApplicationStateInvalid.new(id, state) unless [:live, :active, :pending, :suspended].include?(state.to_sym)
326+
end
327+
322328
def persist_attributes(client)
323329
client.set(storage_key(:state), state.to_s) if state
324330
client.set(storage_key(:plan_id), plan_id) if plan_id

lib/3scale/backend/errors.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,21 @@ def initialize
297297
end
298298
end
299299

300+
class ApplicationHasNoID < BadRequest
301+
def initialize
302+
super %(Application has no id)
303+
end
304+
end
305+
300306
class ApplicationHasNoState < BadRequest
301307
def initialize(id)
302-
super %(Application with id="#{id}" has no state )
308+
super %(Application with id="#{id}" has no state)
309+
end
310+
end
311+
312+
class ApplicationStateInvalid < BadRequest
313+
def initialize(id, state)
314+
super %(Application with id="#{id}" has an invalid state "#{state}")
303315
end
304316
end
305317

0 commit comments

Comments
 (0)