require 'test_helper' require 'json' class RequestTest < Test::Unit::TestCase context "Request" do setup do stub_rooms_request({:code => 200, :[] => JSON.parse(fixture_file("rooms.json"))["rooms"]}) @campfire = Kindling::Campfire.new(TEST_API_TOKEN, TEST_SUBDOMAIN, TEST_ROOM_ID) end context "rooms" do should "should raise error if invalid subdomain" do stub_rooms_request({:code => 404}) assert_raises Kindling::InvalidSubdomain do Kindling::Request.rooms(@campfire) end end should "should raise error if invalid api_token" do stub_rooms_request({:code => 401}) assert_raises Kindling::InvalidAPIToken do Kindling::Request.rooms(@campfire) end end should "should return rooms list" do stub_rooms_request({:code => 200, :[] => JSON.parse(fixture_file("rooms.json"))["rooms"]}) assert_equal [{"name"=>"Room 1", "id"=>201712}, {"name"=>"Room 2", "id"=>298625}, {"name"=>"Room 3", "id"=>206877}], Kindling::Request.rooms(@campfire) end end context "send message" do should "send message successfully if 200" do stub_send_message_request('test message', {:code => 200}) Kindling::Request.speak("test message", @campfire) end should "send message successfully if 201" do stub_send_message_request('test message', {:code => 201}) Kindling::Request.speak("test message", @campfire) end should "raise error if invalid room id" do stub_send_message_request('test message', {:code => 404}) assert_raises Kindling::InvalidRoomID do Kindling::Request.speak("test message", @campfire) end end should "raise error if invalid API token" do stub_send_message_request('test message', {:code => 401}) assert_raises Kindling::InvalidAPIToken do Kindling::Request.speak("test message", @campfire) end end end end end