require 'rack/test' require File.join(File.dirname(__FILE__),'..','spec_helper') require File.join(File.dirname(__FILE__),'..','..','lib','rhoconnect','server.rb') describe "Rhoconnect::Controller::JsBase" do include Rack::Test::Methods include Rhoconnect it_behaves_like "SharedRhoconnectHelper", :rhoconnect_data => true do def app @app ||= Rack::URLMap.new Rhoconnect.url_map end before(:each) do Rhoconnect::Server.set :environment, :test Rhoconnect::Server.set :secret, "secure!" Rhoconnect.use_node = true Rhoconnect.bootstrap(get_testapp_path) do_post "/rc/#{Rhoconnect::API_VERSION}/app/login", "login" => @u.login, "password" => 'testpass' end after(:each) do Rhoconnect::Node.kill_process end it "should get / of js controller with route overridden" do get '/app/v1/JsSample', {}, {Rhoconnect::CLIENT_ID_HEADER => @c.id} last_response.should be_ok body = JSON.parse(last_response.body) body[2]['count'].should == 1 body.last['insert'].should == {'1' => {'name' => 'iPhone'}} end it "should get /custom_route of js controller" do get '/app/v1/JsSample/custom_route', {}, {Rhoconnect::CLIENT_ID_HEADER => @c.id} last_response.body.should == 'testuser' end it "should get /source from custom route" do get '/app/v1/JsSample/custom_route2', {}, {Rhoconnect::CLIENT_ID_HEADER => @c.id} last_response.body.should == 'JsSample' end it "should get stash_result from js model" do pending "stash_result not supported yet" get '/app/v1/JsSample/custom_route3',{}, {Rhoconnect::CLIENT_ID_HEADER => @c.id} res = JSON.parse(last_response.body) res[2]["count"].should == 1 end it "should register sync route" do get '/app/v1/Sample2', {}, {Rhoconnect::CLIENT_ID_HEADER => @c.id} res = JSON.parse(last_response.body) res[2]["count"].should == 1 end it "should post to js controller" do hsh = {'1'=>{'name'=>'testname','price'=>'$199'}} post '/app/v1/JsSample',hsh, {Rhoconnect::CLIENT_ID_HEADER => @c.id} last_response.should be_ok end it "should put to js controller" do put '/app/v1/JsSample/2', {}, {Rhoconnect::CLIENT_ID_HEADER => @c.id} last_response.body.should == '' end it "should delete to js controller" do @c1 = Client.create({:user_id => @u.id,:app_id => @a.id},{:source_name => 'JsSample'}) set_doc_state(@c1, :cd => @data) delete '/app/v1/JsSample/2', {}, {Rhoconnect::CLIENT_ID_HEADER => @c1.id} last_response.should be_ok end it "should call route that overrides default option" do get '/app/v1/JsSample/no_client_route', {} last_response.should be_ok last_response.body.should == 'no client required!' end it "should upload blob in multipart post" do file1,file2 = 'upload1.txt' @product1['txtfile-rhoblob'] = file1 @product1['_id'] = 'tempobj1' cud = {'create'=>{'1'=>{'name'=>'hello'}}, :blob_fields => ['txtfile-rhoblob']}.to_json post "/app/#{Rhoconnect::API_VERSION}/JsSample", {:cud => cud,'txtfile-rhoblob-1' => Rack::Test::UploadedFile.new(File.join(File.dirname(__FILE__),'..','testdata',file1), "application/octet-stream")}, {Rhoconnect::CLIENT_ID_HEADER => @c.id} last_response.should be_ok get "/app/#{Rhoconnect::API_VERSION}/JsSample", {}, {Rhoconnect::CLIENT_ID_HEADER => @c.id} json = JSON.parse(last_response.body) json[5]['links'].should == { "1" => { "l" => "blob_created" } } json[5]['delete'].should == { "blob_created" => { "name" => "hello", "txtfile-rhoblob" => "blob_created" } } end it "should register deprecated_route" end end