Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions spec/asset_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ module PlaceOS::Model
asset.save!
end
end

it "ensure associated authority" do
asset = Generator.asset
asset.authority_id = ""
asset.valid?.should be_false
asset.errors.first.field.should eq :authority_id
end
end

describe "#consumable_assets" do
Expand Down
9 changes: 9 additions & 0 deletions spec/broker_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,14 @@ module PlaceOS::Model
broker.persisted?.should be_true
Broker.find!(broker.id.as(String)).id.should eq broker.id
end

describe "validations" do
it "ensure associated authority" do
broker = Generator.broker
broker.authority_id = ""
broker.valid?.should be_false
broker.errors.first.field.should eq :authority_id
end
end
end
end
11 changes: 9 additions & 2 deletions spec/control_system_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,19 @@ module PlaceOS::Model
end
end

describe "validation" do
describe "validations" do
it "rejects invalid support URI" do
sys = Generator.control_system
sys.support_url = "string"
sys.valid?.should be_false
end

it "ensure associated authority" do
control_system = Generator.control_system
control_system.authority_id = ""
control_system.valid?.should be_false
control_system.errors.first.field.should eq :authority_id
end
end

describe "add_module" do
Expand Down Expand Up @@ -193,7 +200,7 @@ module PlaceOS::Model

cs.save!

trigger = Trigger.create!(name: "trigger test")
trigger = Trigger.create!(name: "trigger test", authority_id: "spec-authority-id")
zone = Generator.zone
trigger_id = trigger.id
if trigger_id
Expand Down
9 changes: 9 additions & 0 deletions spec/edge_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,14 @@ module PlaceOS::Model
edge = Generator.edge.save!
Edge.jwt_edge_id?(edge.api_key.not_nil!.build_jwt).should eq(edge.id)
end

describe "validations" do
it "ensure associated authority" do
edge = Generator.edge
edge.authority_id = ""
edge.valid?.should be_false
edge.errors.first.field.should eq :authority_id
end
end
end
end
13 changes: 10 additions & 3 deletions spec/generator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ module PlaceOS::Model
def self.trigger(system : ControlSystem? = nil)
trigger = Trigger.new(
name: Faker::Hacker.noun,
authority_id: "spec-authority-id",
)
trigger.control_system = system if system
trigger
Expand All @@ -96,6 +97,7 @@ module PlaceOS::Model
def self.control_system
ControlSystem.new(
name: RANDOM.base64(10),
authority_id: "spec-authority-id",
)
end

Expand All @@ -107,23 +109,25 @@ module PlaceOS::Model

mod = case driver.role
in .logic?
Module.new(custom_name: mod_name, uri: Faker::Internet.url)
Module.new(custom_name: mod_name, uri: Faker::Internet.url, authority_id: "spec-authority-id")
in .device?
Module.new(
custom_name: mod_name,
uri: Faker::Internet.url,
ip: Faker::Internet.ip_v4_address,
port: rand((1..6555)),
authority_id: "spec-authority-id",
)
in .ssh?
Module.new(
custom_name: mod_name,
uri: Faker::Internet.url,
ip: Faker::Internet.ip_v4_address,
port: rand((1..65_535)),
authority_id: "spec-authority-id",
)
in .service?, .websocket?
Module.new(custom_name: mod_name, uri: Faker::Internet.url)
Module.new(custom_name: mod_name, uri: Faker::Internet.url, authority_id: "spec-authority-id")
end

# Set driver
Expand All @@ -137,7 +141,7 @@ module PlaceOS::Model

def self.edge(user : User? = nil)
user = self.user.save! if user.nil?
Edge.for_user(user, name: "#{Faker::Address.city}_#{RANDOM.base64(5)}")
Edge.for_user(user, name: "#{Faker::Address.city}_#{RANDOM.base64(5)}", authority_id: "spec-authority-id")
end

def self.encryption_level
Expand Down Expand Up @@ -208,6 +212,7 @@ module PlaceOS::Model
def self.zone
Zone.new(
name: RANDOM.base64(10),
authority_id: "spec-authority-id",
)
end

Expand All @@ -217,6 +222,7 @@ module PlaceOS::Model
purchase_date: Time.local,
identifier: RANDOM.rand(Int32).to_s,
purchase_price: RANDOM.rand(Int32),
authority_id: "spec-authority-id",
)
end

Expand Down Expand Up @@ -322,6 +328,7 @@ module PlaceOS::Model
Broker.new(
name: Faker::Name.name,
host: Faker::Internet.domain_name,
authority_id: "spec-authority-id",
)
end

Expand Down
9 changes: 9 additions & 0 deletions spec/module_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -322,5 +322,14 @@ module PlaceOS::Model
end
end
end

describe "validations" do
it "ensure associated authority" do
mod = Generator.module
mod.authority_id = ""
mod.valid?.should be_false
mod.errors.first.field.should eq :authority_id
end
end
end
end
29 changes: 19 additions & 10 deletions spec/trigger_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,25 @@ module PlaceOS::Model
Trigger.find!(inst.id.as(String)).id.should eq inst.id
end

it "validates webhook methods" do
invalid_model = Generator.trigger
invalid_model.supported_methods = ["NUGGET", "GET"]
valid_model = Generator.trigger
valid_model.supported_methods = ["POST", "GET"]

valid_model.valid?.should be_true
invalid_model.valid?.should be_false
invalid_model.errors.size.should eq 1
invalid_model.errors.first.to_s.should end_with "supported_methods contains invalid methods: NUGGET"
describe "validations" do
it "validates webhook methods" do
invalid_model = Generator.trigger
invalid_model.supported_methods = ["NUGGET", "GET"]
valid_model = Generator.trigger
valid_model.supported_methods = ["POST", "GET"]

valid_model.valid?.should be_true
invalid_model.valid?.should be_false
invalid_model.errors.size.should eq 1
invalid_model.errors.first.to_s.should end_with "supported_methods contains invalid methods: NUGGET"
end

it "ensure associated authority" do
trigger = Generator.trigger
trigger.authority_id = ""
trigger.valid?.should be_false
trigger.errors.first.field.should eq :authority_id
end
end

describe Trigger::Actions do
Expand Down
15 changes: 12 additions & 3 deletions spec/zone_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ module PlaceOS::Model

cs.save!

trigger = Trigger.create!(name: "trigger test")
trigger = Trigger.create!(name: "trigger test", authority_id: "spec-authority-id")

# No trigger_instances associated with zone
zone.trigger_instances.to_a.size.should eq 0
Expand Down Expand Up @@ -125,11 +125,20 @@ module PlaceOS::Model
zone.save!
end

Zone.with_tag("0").to_a.compact_map(&.id).sort!.should eq zones.compact_map(&.id).sort!
Zone.with_tag("3").to_a.compact_map(&.id).sort!.should eq zones[3..].compact_map(&.id).sort!
Zone.with_tag("0", "spec-authority-id").to_a.compact_map(&.id).sort!.should eq zones.compact_map(&.id).sort!
Zone.with_tag("3", "spec-authority-id").to_a.compact_map(&.id).sort!.should eq zones[3..].compact_map(&.id).sort!

zones.each &.destroy
end
end

describe "validations" do
it "ensure associated authority" do
zone = Generator.zone
zone.authority_id = ""
zone.valid?.should be_false
zone.errors.first.field.should eq :authority_id
end
end
end
end
5 changes: 5 additions & 0 deletions src/placeos-models/asset.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ module PlaceOS::Model
# Association
###############################################################################################

secondary_index :authority_id

belongs_to Authority

belongs_to Asset, foreign_key: "parent_id", association_name: "parent"

has_many(
Expand All @@ -53,6 +57,7 @@ module PlaceOS::Model
# Validation
###############################################################################################

validates :authority_id, presence: true
validates :quantity, numericality: {minimum: 0}
validates :in_use, numericality: {minimum: 0}

Expand Down
13 changes: 12 additions & 1 deletion src/placeos-models/broker.cr
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,25 @@ module PlaceOS::Model
# Matches will be replaced with a hmac_256(secret, match).
attribute filters : Array(String) = ->{ [] of String }

# Association
###############################################################################################

secondary_index :authority_id

belongs_to Authority

# Validation
###############################################################################################

validates :authority_id, presence: true
validates :name, presence: true
validates :host, presence: true
validates :secret, presence: true

ensure_unique :name
# Ensure unique name under authority scope
ensure_unique :name, scope: [:authority_id, :name] do |authority_id, name|
{authority_id, name}
end

validate ->Broker.validate_filters(Broker)

Expand Down
11 changes: 9 additions & 2 deletions src/placeos-models/control_system.cr
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ module PlaceOS::Model
# Associations
###############################################################################################

secondary_index :authority_id

belongs_to Authority

# Encrypted yaml settings, with metadata
has_many(
child_class: Settings,
Expand Down Expand Up @@ -83,12 +87,15 @@ module PlaceOS::Model
# Validation
###############################################################################################

validates :authority_id, presence: true

# Zones and settings are only required for confident coding
validates :name, presence: true

# TODO: Ensure unique regardless of casing
ensure_unique :name do |name|
name.strip
# Ensure unique name under authority scope
ensure_unique :name, scope: [:authority_id, :name] do |authority_id, name|
{authority_id, name.strip}
end

# Validate support URI
Expand Down
11 changes: 9 additions & 2 deletions src/placeos-models/edge.cr
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ module PlaceOS::Model
# Association
###############################################################################################

secondary_index :authority_id

belongs_to Authority

# Modules allocated to this Edge
has_many(
child_class: Module,
Expand Down Expand Up @@ -128,8 +132,11 @@ module PlaceOS::Model
# Validation
###############################################################################################

ensure_unique :name do |name|
name.strip
validates :authority_id, presence: true

# Ensure unique name under authority scope
ensure_unique :name, scope: [:authority_id, :name] do |authority_id, name|
{authority_id, name.strip}
end
end
end
6 changes: 6 additions & 0 deletions src/placeos-models/module.cr
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ module PlaceOS::Model
# Associations
###############################################################################################

secondary_index :authority_id

belongs_to Authority

# Control System the _logic_ module may be assigned to
belongs_to ControlSystem, foreign_key: "control_system_id"

Expand All @@ -66,6 +70,8 @@ module PlaceOS::Model
# Validation
###############################################################################################

validates :authority_id, presence: true

validate ->(this : Module) {
driver = this.driver
role = driver.try(&.role)
Expand Down
6 changes: 6 additions & 0 deletions src/placeos-models/trigger.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ module PlaceOS::Model
# Association
###############################################################################################

secondary_index :authority_id

belongs_to Authority

has_many(
child_class: TriggerInstance,
dependent: :destroy,
Expand All @@ -43,6 +47,8 @@ module PlaceOS::Model
# Validation
###############################################################################################

validates :authority_id, presence: true

# Validate `supported_methods`
validate ->(this : Trigger) do
invalid = this.supported_methods - METHODS
Expand Down
Loading