require_relative "test_helper" class ClientTest < MiniTest::Unit::TestCase def setup FakeWeb.allow_net_connect = false @client_opts = { :account_id => "RESEARCHER_ACCOUNT_UUID" } @client = Dalia::Api::SurveyPlatform::Client.new @client_opts end def test_fetch_surveys FakeWeb.register_uri( :get, "http://daliaresearch.com/api/control_panel/control_panel_users/1/researcher_users/RESEARCHER_ACCOUNT_UUID/surveys", :body => File.read("#{FIXTURES}/fake_responses/fetch_surveys.json"), :content_type => "application/json", :status => ["200", "Success"] ) response = @client.fetch_surveys assert_equal(3, response[:surveys].length) end def test_fetch_survey FakeWeb.register_uri( :get, "http://daliaresearch.com/api/control_panel/control_panel_users/1/researcher_users/RESEARCHER_ACCOUNT_UUID/surveys/SURVEY_UUID", :body => File.read("#{FIXTURES}/fake_responses/fetch_survey.json"), :content_type => "application/json", :status => ["200", "Success"] ) response = @client.fetch_survey(:survey_id => "SURVEY_UUID") assert_equal(4, response[:survey][:questions].length) end def test_send_survey FakeWeb.register_uri( :post, "http://daliaresearch.com/api/control_panel/control_panel_users/1/researcher_users/RESEARCHER_ACCOUNT_UUID/surveys/", :body => File.read("#{FIXTURES}/fake_responses/send_survey.json"), :content_type => "application/json", :parameters => { :data => "DATA" }, :status => ["200", "Success"] ) response = @client.send_survey(:data => "DATA") assert_equal("280", response[:survey][:credits][:amount]) end def test_update_survey FakeWeb.register_uri( :put, "http://daliaresearch.com/api/control_panel/control_panel_users/1/researcher_users/RESEARCHER_ACCOUNT_UUID/surveys/SURVEY_UUID", :body => File.read("#{FIXTURES}/fake_responses/send_survey.json"), :content_type => "application/json", :parameters => { :survey_id => "SURVEY_UUID", :data => "DATA" }, :status => ["200", "Success"] ) response = @client.update_survey(:survey_id => "SURVEY_UUID", :data => "DATA") assert_equal("280", response[:survey][:credits][:amount]) end def test_fetch_completions FakeWeb.register_uri( :get, "http://daliaresearch.com/api/control_panel/control_panel_users/1/researcher_users/RESEARCHER_ACCOUNT_UUID/surveys/SURVEY_UUID/completions", :body => File.read("#{FIXTURES}/fake_responses/fetch_completions.json"), :content_type => "application/json", :status => ["200", "Success"] ) response = @client.fetch_completions(:survey_id => "SURVEY_UUID") assert_equal(3, response[:completions].length) end def test_fetch_completions_csv FakeWeb.register_uri( :get, "http://daliaresearch.com/api/control_panel/control_panel_users/1/researcher_users/RESEARCHER_ACCOUNT_UUID/surveys/SURVEY_UUID/completions/index_to_csv", :body => File.read("#{FIXTURES}/fake_responses/fetch_completions.csv"), :content_type => "text/csv", :status => ["200", "Success"] ) response = @client.fetch_completions_csv(:survey_id => "SURVEY_UUID") assert_equal(8, response.lines.length) end def test_fetch_completion FakeWeb.register_uri( :get, "http://daliaresearch.com/api/control_panel/control_panel_users/1/researcher_users/RESEARCHER_ACCOUNT_UUID/surveys/SURVEY_UUID/completions/COMPLETION_UUID", :body => File.read("#{FIXTURES}/fake_responses/fetch_completion.json"), :content_type => "application/json", :status => ["200", "Success"] ) response = @client.fetch_completion(:survey_id => "SURVEY_UUID", :completion_id => "COMPLETION_UUID") assert_equal("completed", response[:completion][:state]) end def test_fetch_survey_price FakeWeb.register_uri( :post, "http://daliaresearch.com/api/control_panel/control_panel_users/1/researcher_users/RESEARCHER_ACCOUNT_UUID/surveys/price", :body => File.read("#{FIXTURES}/fake_responses/fetch_survey_price.json"), :content_type => "application/json", :status => ["200", "Success"] ) response = @client.fetch_survey_price(:account_id => "RESEARCHER_ACCOUNT_UUID") assert_equal(1.0, response[:price][:total]) end def test_create_query FakeWeb.register_uri( :get, "http://daliaresearch.com/api/control_panel/control_panel_users/1/researcher_users/RESEARCHER_ACCOUNT_UUID/surveys/SURVEY_UUID/query?question_id=QUESTION_UUID", :body => File.read("#{FIXTURES}/fake_responses/create_query.json"), :content_type => "application/json", :status => ["200", "Success"] ) response = @client.create_query(:survey_id => "SURVEY_UUID", :question_id => "QUESTION_UUID") assert_equal("Qb5faf2", response[:question_id]) end end