Skip to content

Commit

Permalink
Add validations and unit test to Domain model
Browse files Browse the repository at this point in the history
  • Loading branch information
suprjinx committed Sep 5, 2024
1 parent 9d6c43d commit ef1c8bd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/models/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class Domain < ApplicationRecord
serialize :groups, coder: YAML, type: Array
before_save :clean_groups

validates :fqdn, :owner, presence: true

def clean_groups
self.groups = groups.sort.uniq
end
Expand Down
40 changes: 40 additions & 0 deletions test/models/domain_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# test/models/cert_issue_request_test.rb
require "test_helper"

class DomainTest < ActiveSupport::TestCase
def setup
@attributes = {
fqdn: "example4.com",
owner: "[email protected]",

Check failure on line 8 in test/models/domain_test.rb

View workflow job for this annotation

GitHub Actions / build_test

Style/TrailingCommaInHashLiteral: Avoid comma after the last item of a hash.
}
@domain = Domain.new(@attributes)
end

test "#new should set attributes from attributes argument" do
@attributes.each do |key, value|
assert_equal value, @domain.send(key), "Attribute #{key} was not set correctly"
end
end

test "#valid? should be valid with valid attributes" do
assert @domain.valid?
end

test "#valid? should require an fqdn" do
@domain.fqdn = nil
assert_not @domain.valid?
assert_includes @domain.errors[:fqdn], "can't be blank"
end

test "#valid? should require an owner" do
@domain.owner = nil
assert_not @domain.valid?
assert_includes @domain.errors[:owner], "can't be blank"
end

test "before_save should sort and dedupe groups" do
@domain.groups = [ "two", "two", "one" ]
@domain.save
assert_equal [ "one", "two" ], @domain.groups
end
end

0 comments on commit ef1c8bd

Please sign in to comment.