Obtener listado de sitios

This commit is contained in:
f 2019-09-06 20:40:33 -03:00
parent bdfd82c6c5
commit 7d7b265610
No known key found for this signature in database
GPG key ID: 2AE5A13E321F953D
5 changed files with 31 additions and 3 deletions

View file

@ -4,7 +4,11 @@ module Api
module V1
# API
class BaseController < ActionController::Base
http_basic_authenticate_with name: ENV['HTTP_BASIC_USER'],
password: ENV['HTTP_BASIC_PASSWORD']
protect_from_forgery with: :null_session
respond_to :json
end
end
end

View file

@ -4,6 +4,10 @@ module Api
module V1
# API para sitios
class SitesController < BaseController
def index
render json: Site.all.order(:name).pluck(:name)
end
# Detecta si se puede generar un certificado
def allowed
name = params[:domain].gsub(/\.#{Site.domain}\Z/, '')

View file

@ -0,0 +1,11 @@
# frozen_string_literal: true
Rails.application.configure do
next if ENV['RAILS_ENV'] == 'test'
domain = ENV.fetch('SUTTY', 'sutty.nl')
config.hosts << domain
config.hosts << "edit.#{domain}"
config.hosts << "api.#{domain}"
end

View file

@ -15,6 +15,7 @@ Rails.application.routes.draw do
scope module: 'api' do
namespace :v1 do
get 'sites/allowed', to: 'sites#allowed'
resources :sites, only: %i[index]
end
end
end

View file

@ -12,7 +12,8 @@ module Api
@authorization = {
Authorization: ActionController::HttpAuthentication::Basic
.encode_credentials(@usuarie.email, @usuarie.password)
.encode_credentials(ENV['HTTP_BASIC_USER'],
ENV['HTTP_BASIC_PASSWORD'])
}
end
@ -21,12 +22,19 @@ module Api
end
test 'se puede generar un certificado' do
get v1_sites_allowed_url, params: { domain: @site.name }
get v1_sites_allowed_url, headers: @authorization,
params: { domain: @site.name }
assert_response :ok
get v1_sites_allowed_url, params: { domain: SecureRandom.hex }
get v1_sites_allowed_url, headers: @authorization,
params: { domain: SecureRandom.hex }
assert_response :not_found
end
test 'se puede obtener un listado de todos' do
get v1_sites_url, headers: @authorization, as: :json
assert_equal [@site.name], JSON.parse(response.body)
end
end
end
end