From 6c43da5e65c26b9ec6b2184b7d8be44d38d092ea Mon Sep 17 00:00:00 2001 From: Michael Kozono Date: Thu, 21 Mar 2019 15:47:44 -0700 Subject: [PATCH] Persist OAuth callback changes for Alternate URL --- ee/app/models/geo_node.rb | 2 +- ...mk-geo-node-autosave-oauth-application.yml | 5 ++ ee/spec/models/geo_node_spec.rb | 47 ++++++++++++++++++- 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 ee/changelogs/unreleased/mk-geo-node-autosave-oauth-application.yml diff --git a/ee/app/models/geo_node.rb b/ee/app/models/geo_node.rb index 7234f15f290416..5186584081936f 100644 --- a/ee/app/models/geo_node.rb +++ b/ee/app/models/geo_node.rb @@ -9,7 +9,7 @@ class GeoNode < ActiveRecord::Base # Array of repository storages to synchronize for selective sync by shards serialize :selective_sync_shards, Array # rubocop:disable Cop/ActiveRecordSerialize - belongs_to :oauth_application, class_name: 'Doorkeeper::Application', dependent: :destroy # rubocop: disable Cop/ActiveRecordDependent + belongs_to :oauth_application, class_name: 'Doorkeeper::Application', dependent: :destroy, autosave: true # rubocop: disable Cop/ActiveRecordDependent has_many :geo_node_namespace_links has_many :namespaces, through: :geo_node_namespace_links diff --git a/ee/changelogs/unreleased/mk-geo-node-autosave-oauth-application.yml b/ee/changelogs/unreleased/mk-geo-node-autosave-oauth-application.yml new file mode 100644 index 00000000000000..c356db9b3005b2 --- /dev/null +++ b/ee/changelogs/unreleased/mk-geo-node-autosave-oauth-application.yml @@ -0,0 +1,5 @@ +--- +title: 'Geo: Persist OAuth callback changes for Alternate URL' +merge_request: 10358 +author: +type: fixed diff --git a/ee/spec/models/geo_node_spec.rb b/ee/spec/models/geo_node_spec.rb index 17e8faeaefe98f..c482f057d38d45 100644 --- a/ee/spec/models/geo_node_spec.rb +++ b/ee/spec/models/geo_node_spec.rb @@ -16,7 +16,7 @@ let(:api_version) { API::API.version } context 'associations' do - it { is_expected.to belong_to(:oauth_application).dependent(:destroy) } + it { is_expected.to belong_to(:oauth_application).class_name('Doorkeeper::Application').dependent(:destroy).autosave(true) } it { is_expected.to have_many(:geo_node_namespace_links) } it { is_expected.to have_many(:namespaces).through(:geo_node_namespace_links) } @@ -196,6 +196,51 @@ end end end + + context 'when saving' do + let(:oauth_application) { node.oauth_application } + + context 'when url is changed' do + it "updates the associated OAuth application's redirect_uri" do + node.update!(url: 'http://modified-url') + + expect(oauth_application.reload.redirect_uri).to eq('http://modified-url/oauth/geo/callback') + end + end + + context 'when alternate_url is added' do + it "adds a callback URL to the associated OAuth application's redirect_uri" do + expected_redirect_uri = "#{oauth_application.redirect_uri}\nhttp://alternate-url/oauth/geo/callback" + + node.update!(alternate_url: 'http://alternate-url') + + expect(oauth_application.reload.redirect_uri).to eq(expected_redirect_uri) + end + end + + context 'when alternate_url is modified' do + it "updates the alternate callback URL in the associated OAuth application's redirect_uri" do + node.update!(alternate_url: 'http://alternate-url') + oauth_application.update!(redirect_uri: "#{node.oauth_callback_url}\nhttp://alternate-url/oauth/geo/callback") + expected_redirect_uri = "#{node.oauth_callback_url}\nhttp://modified-alternate-url/oauth/geo/callback" + + node.update!(alternate_url: 'http://modified-alternate-url') + + expect(oauth_application.reload.redirect_uri).to eq(expected_redirect_uri) + end + end + + context 'when alternate_url is cleared' do + it "removes the alternate callback URL in the associated OAuth application's redirect_uri" do + expected_redirect_uri = oauth_application.redirect_uri + oauth_application.update!(redirect_uri: "#{node.oauth_callback_url}\nhttp://alternate-url/oauth/geo/callback") + + node.update!(alternate_url: nil) + + expect(oauth_application.reload.redirect_uri).to eq(expected_redirect_uri) + end + end + end end context 'cache expiration' do -- GitLab