Commit 1c62f762 authored by Gabriel Mazetto's avatar Gabriel Mazetto

Create / remove dependent OAuth application for a GeoNode

parent 5b90f894
......@@ -12,7 +12,7 @@
class GeoNode < ActiveRecord::Base
belongs_to :geo_node_key, dependent: :destroy
belongs_to :oauth_application, class_name: 'Doorkeeper::Application'
belongs_to :oauth_application, class_name: 'Doorkeeper::Application', dependent: :destroy
default_values schema: 'http',
host: lambda { Gitlab.config.gitlab.host },
......@@ -23,14 +23,14 @@ class GeoNode < ActiveRecord::Base
accepts_nested_attributes_for :geo_node_key
validates :host, host: true, presence: true, uniqueness: { case_sensitive: false, scope: :port }
validates :primary, uniqueness: { message: 'primary node already exists' }, if: :primary
validates :primary, uniqueness: { message: 'node already exists' }, if: :primary
validates :schema, inclusion: %w(http https)
validates :relative_url_root, length: { minimum: 0, allow_nil: false }
after_initialize :check_geo_node_key
after_initialize :build_dependents
after_save :refresh_bulk_notify_worker_status
after_destroy :refresh_bulk_notify_worker_status
before_validation :change_geo_node_key_title
before_validation :update_dependents_attributes
def uri
if relative_url_root
......@@ -66,12 +66,15 @@ class GeoNode < ActiveRecord::Base
end
end
def check_geo_node_key
def build_dependents
self.build_geo_node_key if geo_node_key.nil?
self.build_oauth_application if oauth_application.nil?
end
def change_geo_node_key_title
def update_dependents_attributes
self.geo_node_key.title = "Geo node: #{self.url}" if self.geo_node_key
self.oauth_application.name = "Geo node: #{self.url}" if self.geo_node_key
self.oauth_application.redirect_uri = oauth_callback_url
end
def validate(record)
......@@ -82,4 +85,9 @@ class GeoNode < ActiveRecord::Base
record.errors[:base] << 'Current node must be the primary node or you will be locking yourself out'
end
end
def oauth_callback_url
url_helpers = Rails.application.routes.url_helpers
URI.join(uri, "#{uri.path}/", url_helpers.oauth_geo_callback_path).to_s
end
end
......@@ -5,6 +5,7 @@ describe GeoNode, type: :model do
context 'associations' do
it { is_expected.to belong_to(:geo_node_key).dependent(:destroy) }
it { is_expected.to belong_to(:oauth_application).dependent(:destroy) }
end
context 'default values' do
......@@ -44,6 +45,45 @@ describe GeoNode, type: :model do
end
end
context 'dependent models for GeoNode' do
let(:geo_node_key_attributes) { FactoryGirl.build(:geo_node_key).attributes }
subject { GeoNode.new(schema: 'https', host: 'localhost', port: 3000, relative_url_root: 'gitlab') }
context 'on initialize' do
before(:each) do
subject.geo_node_key_attributes = geo_node_key_attributes
end
it 'initializes a corresponding key' do
expect(subject.geo_node_key).to be_present
end
it 'initializes a corresponding oauth application' do
expect(subject.oauth_application).to be_present
end
it 'is valid' do
expect(subject).to be_valid
end
end
context 'on create' do
before(:each) do
subject.geo_node_key_attributes = geo_node_key_attributes
subject.save!
end
it 'saves a corresponding key' do
expect(subject.geo_node_key).to be_persisted
end
it 'saves a corresponding oauth application' do
expect(subject.oauth_application).to be_persisted
end
end
end
describe '#uri' do
context 'when all fields are filled' do
subject { GeoNode.new(schema: 'https', host: 'localhost', port: 3000, relative_url_root: 'gitlab') }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment