From 2fe142ed86021ca84942834fa8cf8776e91c5a7e Mon Sep 17 00:00:00 2001 From: Jez Caudle Date: Mon, 15 May 2023 15:13:01 +0100 Subject: [PATCH] all model tests now pass --- app/models/credential.rb | 3 +++ app/models/virtual.rb | 3 +++ test/models/credential_test.rb | 19 ++++++++++++--- test/models/virtual_test.rb | 42 ++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 3 deletions(-) diff --git a/app/models/credential.rb b/app/models/credential.rb index bed8801..cb9acd8 100644 --- a/app/models/credential.rb +++ b/app/models/credential.rb @@ -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 diff --git a/app/models/virtual.rb b/app/models/virtual.rb index 757f926..581dbbf 100644 --- a/app/models/virtual.rb +++ b/app/models/virtual.rb @@ -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? diff --git a/test/models/credential_test.rb b/test/models/credential_test.rb index 3396384..ab184c4 100644 --- a/test/models/credential_test.rb +++ b/test/models/credential_test.rb @@ -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 diff --git a/test/models/virtual_test.rb b/test/models/virtual_test.rb index e69de29..979e7c7 100644 --- a/test/models/virtual_test.rb +++ b/test/models/virtual_test.rb @@ -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