Commit eb1636f

mo <mo@mokhan.ca>
2018-10-13 17:03:24
add client validations
1 parent ae05433
Changed files (3)
app/models/application_record.rb
@@ -1,6 +1,7 @@
 # frozen_string_literal: true
 
 class ApplicationRecord < ActiveRecord::Base
+  UUID = /\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/
   include Flippable
   self.abstract_class = true
 end
app/models/client.rb
@@ -6,6 +6,10 @@ class Client < ApplicationRecord
   has_secure_token :secret
   has_many :authorizations
 
+  validates :name, presence: true
+  validates :redirect_uri, presence: true, format: { with: /\A#{URI::regexp(['http', 'https'])}\z/ }
+  validates :uuid, presence: true, format: { with: ApplicationRecord::UUID }
+
   after_initialize do
     self.uuid = SecureRandom.uuid unless uuid
     self.secret = self.class.generate_unique_secure_token unless secret
spec/models/client_spec.rb
@@ -0,0 +1,13 @@
+require 'rails_helper'
+
+RSpec.describe Client do
+  describe "#validation" do
+    specify { expect(build(:client)).to be_valid }
+    specify { expect(build(:client, redirect_uri: nil)).to be_invalid }
+    specify { expect(build(:client, redirect_uri: '<script>alert("hi")</script>')).to be_invalid }
+    specify { expect(build(:client, redirect_uri: 'invalid')).to be_invalid }
+    specify { expect(build(:client, uuid: nil)).to be_invalid }
+    specify { expect(build(:client, uuid: 'invalid')).to be_invalid }
+    specify { expect(build(:client, name: nil)).to be_invalid }
+  end
+end