diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb new file mode 100644 index 0000000..c1d58ff --- /dev/null +++ b/app/controllers/api/v1/base_controller.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module Api + module V1 + # API + class BaseController < ActionController::Base + protect_from_forgery with: :null_session + end + end +end diff --git a/app/controllers/api/v1/sites_controller.rb b/app/controllers/api/v1/sites_controller.rb new file mode 100644 index 0000000..8e79c88 --- /dev/null +++ b/app/controllers/api/v1/sites_controller.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +module Api + module V1 + # API para sitios + class SitesController < BaseController + # Detecta si se puede generar un certificado + def allowed + name = params[:domain].gsub(/\.#{Site.domain}\Z/, '') + site = Site.find_by(name: name) + + if site + head :ok + else + head :not_found + end + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index a492ba9..137ef94 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,6 +11,14 @@ Rails.application.routes.draw do # como un objeto vĂ¡lido resources :invitadxs, only: [:create] + constraints subdomain: 'api' do + scope module: 'api' do + namespace :v1 do + get 'sites/allowed', to: 'sites#allowed' + end + end + end + resources :sites, constraints: { site_id: %r{[^/]+}, id: %r{[^/]+} } do get 'public/:type/:basename', to: 'sites#send_public_file' diff --git a/test/controllers/api/v1/sites_controller_test.rb b/test/controllers/api/v1/sites_controller_test.rb new file mode 100644 index 0000000..b55273c --- /dev/null +++ b/test/controllers/api/v1/sites_controller_test.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require 'test_helper' + +module Api + module V1 + class SitesControllerTest < ActionDispatch::IntegrationTest + setup do + @rol = create :rol + @site = @rol.site + @usuarie = @rol.usuarie + + @authorization = { + Authorization: ActionController::HttpAuthentication::Basic + .encode_credentials(@usuarie.email, @usuarie.password) + } + end + + teardown do + @site.destroy + end + + test 'se puede generar un certificado' do + get v1_sites_allowed_url, params: { domain: @site.name } + assert_response :ok + + get v1_sites_allowed_url, params: { domain: SecureRandom.hex } + assert_response :not_found + end + end + end +end