all model tests now pass

This commit is contained in:
Jez Caudle 2023-05-15 15:13:01 +01:00
parent 9ac685fd15
commit 2fe142ed86
4 changed files with 64 additions and 3 deletions

View File

@ -1,2 +1,5 @@
class Credential < ApplicationRecord
validates :email, presence: true
validates :email, uniqueness: true
validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i , message: "email must be valid"}
end

View File

@ -2,6 +2,9 @@ class Virtual < ApplicationRecord
validate :domain_name_exists
validates :email, presence: true
validates :destination, presence: true
validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i , message: "email must be valid"}
validates :destination, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i , message: "destination must be valid"}
validates :destination, uniqueness: {scope: :email, message: "a redirection pair must be unique"}
def domain_name_exists
if email.present?

View File

@ -1,7 +1,20 @@
require "test_helper"
class CredentialTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
test "email must be present" do
@c = Credential.new
assert !@c.save
end
test "email must be valid" do
@c = Credential.new
@c.email = "not on your nelly"
assert !@c.save
end
test "email must be unique" do
@c = Credential.new
@c.email = "bob@example.net"
assert !@c.save
end
end

View File

@ -0,0 +1,42 @@
require "test_helper"
class VirtualTest < ActiveSupport::TestCase
test "the email cant be blank" do
@v = Virtual.new
@v.destination = "davesmith@example.com"
assert !@v.save
end
test "the destination email cant be blank" do
@v = Virtual.new
@v.email = "davesmith@example.com"
assert !@v.save
end
test "the domain name of the email must be on this server" do
@v = Virtual.new
@v.email = "davesmith@notonthisserver.com"
@v.destination = "bob@example.net"
assert !@v.save
end
test "the email must be a valid email" do
@v = Virtual.new
@v.email = "something not quiet right"
assert !@v.save
end
test "the destination must be a valid email" do
@v = Virtual.new
@v.destination = "this will never work"
assert !@v.save
end
test "the email plus destination must be unique" do
@v = Virtual.new
@v.email = "abuse@example.net"
@v.destination ="bob@example.net"
assert !@v.save
end
end