Skip to content

Commit

Permalink
templates api - route for listing templates in an environment
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas Strachota committed Sep 14, 2011
1 parent e1d297a commit 36c3ce4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
4 changes: 2 additions & 2 deletions cli/src/katello/client/api/template.py
Expand Up @@ -18,8 +18,8 @@
class TemplateAPI(KatelloAPI):

def templates(self, envId):
path = "/api/templates/"
tpls = self.server.GET(path, {"environment_id": envId})[1]
path = "/api//environmants/%s/templates/" % str(envId)
tpls = self.server.GET(path)[1]
return tpls


Expand Down
13 changes: 11 additions & 2 deletions src/app/controllers/api/templates_controller.rb
Expand Up @@ -15,14 +15,19 @@
class Api::TemplatesController < Api::ApiController

before_filter :find_environment, :only => [:create, :import]
before_filter :try_find_environment, :only => [:index]
before_filter :find_template, :only => [:show, :update, :update_content, :destroy, :promote, :export]

# TODO: define authorization rules
skip_before_filter :authorize

def index
templates = SystemTemplate.where(query_params)
render :json => templates.to_json
if @environment.nil?
tpls = SystemTemplate.all
else
tpls = @environment.system_templates
end
render :json => tpls.to_json
end

def show
Expand Down Expand Up @@ -158,6 +163,10 @@ def find_environment
@environment
end

def try_find_environment
find_environment if not params[:environment_id].nil?
end

def find_template
@template = SystemTemplate.find(params[:id])
raise HttpErrors::NotFound, _("Couldn't find template '#{params[:id]}'") if @template.nil?
Expand Down
1 change: 1 addition & 0 deletions src/config/routes.rb
Expand Up @@ -319,6 +319,7 @@ def matches?(request)
get :repositories, :on => :member
end
resources :activation_keys, :only => [:index, :create]
resources :templates, :only => [:index]
end

resources :activation_keys, :only => [:show, :update, :destroy]
Expand Down
23 changes: 17 additions & 6 deletions src/spec/controllers/api/templates_controller_spec.rb
Expand Up @@ -48,17 +48,28 @@

describe "index" do

it 'should get a list of templates from specified environment ID' do
SystemTemplate.should_receive(:where).with("environment_id" => @locker.id).and_return([@tpl])
before :each do
@environment2 = KTEnvironment.new(:name => 'environment2')
@environment2.id = 3

KTEnvironment.stub(:find).with(@locker.id).and_return(@locker)
KTEnvironment.stub(:find).with(@environment2.id).and_return(@environment2)
end

it 'should get a list of templates from specified environment' do
@locker.should_receive(:system_templates).and_return([@tpl])
get 'index', :environment_id => @locker.id
response.should be_success
end

it 'should not fail if no templates are found, but return an empty list' do
@environment2 = KTEnvironment.new(:name => 'environment2')
@environment2.id = 3
it 'should get a list of all templates' do
SystemTemplate.should_receive(:all).and_return([@tpl])
get 'index'
response.should be_success
end

SystemTemplate.should_receive(:where).with("environment_id" => @environment2.id).and_return([])
it 'should not fail if no templates are found, but return an empty list' do
@environment2.should_receive(:system_templates).and_return([])
get 'index', :environment_id => @environment2.id
response.should be_success
end
Expand Down

0 comments on commit 36c3ce4

Please sign in to comment.