Project

General

Profile

« Previous | Next » 

Revision acd8589a

Added by Martin Bacovsky almost 12 years ago

  • ID acd8589abbb2283afa638d1d4b84099576acc452

api v1 - architectures controler and tests

View differences:

app/controllers/api/v1/architectures_controller.rb
include Foreman::Controller::AutoCompleteSearch
before_filter :find_by_name, :only => %w{show update destroy}
api :GET, "/architectures/", "List all architectures."
def index
@architectures = Architecture.search_for(params[:search], :order => params[:order]).paginate(:page => params[:page], :include => :operatingsystems)
end
api :GET, "/architectures/:id/", "Show an architecture."
def show
end
api :POST, "/architectures/", "Create an architecture."
param :architecture, Hash, :required => true do
param :name, String, :required => true
end
def create
respond_with Architecture.new(params[:architecture])
@architecture = Architecture.new(params[:architecture])
process_response @architecture.save
end
api :PUT, "/architectures/:id/", "Update an architecture."
param :architecture, Hash, :required => true do
param :name, String
end
def update
respond_with @architecture.update_attributes(params[:architecture])
process_response @architecture.update_attributes(params[:architecture])
end
api :DELETE, "/architecturess/:id/", "Delete an architecture."
def destroy
respond_with @architecture.destroy
process_response @architecture.destroy
end
end
end
app/views/api/v1/architectures/create.json.rabl
object @architecture
extends "api/v1/architectures/show"
app/views/api/v1/architectures/index.json.rabl
collection @architectures
extends "api/v1/architectures/show"
app/views/api/v1/architectures/show.json.rabl
object @architecture
attributes :name, :id
test/fixtures/architectures.yml
sparc:
name: sparc
#
# two:
# column: value
s390:
name: s390
test/functional/api/v1/architectures_controller_test.rb
require 'test_helper'
class Api::V1::ArchitecturesControllerTest < ActionController::TestCase
arch_i386 = {:name => 'i386'}
def user_one_as_anonymous_viewer
users(:one).roles = [Role.find_by_name('Anonymous'), Role.find_by_name('Viewer')]
end
test "should get index" do
as_user :admin do
get :index, {}
end
assert_response :success
assert_not_nil assigns(:architectures)
end
test "should show architecture" do
as_user :admin do
get :show, {:id => architectures(:x86_64).to_param}
end
assert_response :success
end
test "should create architecture" do
as_user :admin do
assert_difference('Architecture.count') do
post :create, {:architecture => arch_i386}
end
end
assert_response :success
end
test "should update architecture" do
as_user :admin do
put :update, {:id => architectures(:x86_64).to_param, :architecture => {} }
end
assert_response :success
end
test "should destroy architecture" do
as_user :admin do
assert_difference('Architecture.count', -1) do
delete :destroy, {:id => architectures(:s390).to_param}
end
end
assert_response :success
end
test "should not destroy used architecture" do
as_user :admin do
assert_difference('Architecture.count', 0) do
delete :destroy, {:id => architectures(:x86_64).to_param}
end
end
assert_response :unprocessable_entity
end
test "user with viewer rights should fail to update an architecture" do
user_one_as_anonymous_viewer
as_user :one do
put :update, {:id => architectures(:x86_64).to_param, :architecture => {} }
end
assert_response :forbidden
end
test "user with viewer rights should succeed in viewing architectures" do
user_one_as_anonymous_viewer
as_user :one do
get :index, {}
end
assert_response :success
end
end

Also available in: Unified diff