pull/15023/head
Alberto Romeu 5 years ago
parent 5b444e5bd9
commit 393854ddba

@ -0,0 +1,11 @@
class UrlValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.errors[attribute] << (options[:message] || "must be a valid URL") unless url_valid?(value)
end
def url_valid?(url)
url = URI.parse(url) rescue false
url.kind_of?(URI::HTTP) || url.kind_of?(URI::HTTPS)
end
end

@ -1,5 +1,7 @@
# encoding: utf-8
require_dependency 'carto/helpers/url_validator'
module Carto
class OauthApp < ActiveRecord::Base
# Multiple of 3 for pretty base64
@ -12,10 +14,11 @@ module Carto
validates :user, presence: true, if: -> { sync_with_central? || !central_enabled? }
validates :name, presence: true
validates :website_url, presence: true, url: true
validates :client_id, presence: true
validates :client_secret, presence: true
validates :redirect_uris, presence: true
validates :icon_url, presence: true
validates :icon_url, presence: true, url: true
validates :oauth_app_organizations, absence: true, unless: :restricted?
validate :validate_uris
@ -25,7 +28,8 @@ module Carto
after_update :update_central, if: :sync_with_central?
after_destroy :delete_central, if: :sync_with_central?
ALLOWED_SYNC_ATTRIBUTES = %i[id name client_id client_secret redirect_uris icon_url restricted].freeze
ALLOWED_SYNC_ATTRIBUTES = %i[id name client_id client_secret redirect_uris
icon_url restricted description website_url].freeze
attr_accessor :avoid_sync_central

@ -5,6 +5,7 @@ FactoryGirl.define do
name { unique_name('Oauth application') }
redirect_uris ['https://redirect.uri']
icon_url 'http://localhost/some_icon.png'
website_url 'http://localhost'
avoid_sync_central true
end
end

@ -37,6 +37,13 @@ module Carto
expect(app.errors[:icon_url]).to(include("can't be blank"))
end
it 'rejected if icon_url invalid' do
app = OauthApp.new
app.icon_url = 'carto.com'
expect(app).to_not(be_valid)
expect(app.errors[:icon_url]).to(include("must be a valid URL"))
end
describe 'redirection uri' do
it 'rejected if empty' do
app = OauthApp.new
@ -84,7 +91,11 @@ module Carto
end
it 'accepts if valid' do
app = OauthApp.new(user: @user, name: 'name', redirect_uris: ['https://re.dir'], icon_url: 'some.png')
app = OauthApp.new(user: @user,
name: 'name',
redirect_uris: ['https://re.dir'],
icon_url: 'http://localhost/some.png',
website_url: 'http://localhost')
expect(app).to(be_valid)
end
@ -92,7 +103,8 @@ module Carto
Cartodb::Central.stubs(:sync_data_with_cartodb_central?).returns(true)
app = OauthApp.new(name: 'name',
redirect_uris: ['https://re.dir'],
icon_url: 'some.png',
icon_url: 'http://localhost/some.png',
website_url: 'http://localhost',
avoid_sync_central: true)
expect(app).to(be_valid)
Cartodb::Central.unstub(:sync_data_with_cartodb_central?)
@ -125,7 +137,9 @@ module Carto
params = { id: '26da639b-0b8c-4e81-aeb4-33b81fd0cacb',
name: 'name1',
redirect_uris: ['https://re.dir'],
icon_url: 'some.png',
icon_url: 'http://localhost/some.png',
website_url: 'http://localhost',
description: nil,
client_id: '1234',
client_secret: '5678',
restricted: false }
@ -150,7 +164,8 @@ module Carto
expect {
@oauth_app2 = OauthApp.create!(name: 'name1',
redirect_uris: ['https://re.dir'],
icon_url: 'some.png',
icon_url: 'http://localhost/some.png',
website_url: 'http://localhost',
avoid_sync_central: true)
}.to change { OauthApp.count }.by(1)
end
@ -163,7 +178,8 @@ module Carto
@oauth_app2 = OauthApp.create!(user: @user_oauth,
name: 'name1',
redirect_uris: ['https://re.dir'],
icon_url: 'some.png')
website_url: 'http://localhost',
icon_url: 'http://localhost/some.png')
}.to change { OauthApp.count }.by(1)
end
@ -175,7 +191,8 @@ module Carto
expect {
@oauth_app2 = OauthApp.create!(name: 'name1',
redirect_uris: ['https://re.dir'],
icon_url: 'some.png')
website_url: 'http://localhost',
icon_url: 'http://localhost/some.png')
}.to raise_error
end
end
@ -193,6 +210,8 @@ module Carto
client_secret: @oauth_app.client_secret,
redirect_uris: @oauth_app.redirect_uris,
icon_url: @oauth_app.icon_url,
website_url: @oauth_app.website_url,
description: @oauth_app.description,
restricted: @oauth_app.restricted)
.returns({})
.once

Loading…
Cancel
Save