spec/urbanairship_spec.rb in urbanairship-1.0.2 vs spec/urbanairship_spec.rb in urbanairship-1.0.3

- old
+ new

@@ -4,10 +4,11 @@ FakeWeb.allow_net_connect = false # register_device FakeWeb.register_uri(:put, "https://my_app_key:my_app_secret@go.urbanairship.com/api/device_tokens/new_device_token", :status => ["201", "Created"]) FakeWeb.register_uri(:put, "https://my_app_key:my_app_secret@go.urbanairship.com/api/device_tokens/existing_device_token", :status => ["200", "OK"]) + FakeWeb.register_uri(:put, "https://my_app_key:my_app_secret@go.urbanairship.com/api/device_tokens/device_token_one", :status => ["201", "Created"]) FakeWeb.register_uri(:put, /bad_key\:my_app_secret\@go\.urbanairship\.com/, :status => ["401", "Unauthorized"]) # unregister_device FakeWeb.register_uri(:delete, /my_app_key\:my_app_secret\@go\.urbanairship.com\/api\/device_tokens\/.+/, :status => ["204", "No Content"]) FakeWeb.register_uri(:delete, /bad_key\:my_app_secret\@go\.urbanairship.com\/api\/device_tokens\/.+/, :status => ["401", "Unauthorized"]) @@ -23,10 +24,15 @@ # broadcast push FakeWeb.register_uri(:post, "https://my_app_key:my_master_secret@go.urbanairship.com/api/push/broadcast/", :status => ["200", "OK"]) FakeWeb.register_uri(:post, "https://my_app_key2:my_master_secret2@go.urbanairship.com/api/push/broadcast/", :status => ["400", "Bad Request"]) + # delete_scheduled_push + FakeWeb.register_uri(:delete, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/push\/scheduled\/[0-9]+/, :status => ["204", "No Content"]) + FakeWeb.register_uri(:delete, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/push\/scheduled\/alias\/.+/, :status => ["204", "No Content"]) + FakeWeb.register_uri(:delete, /bad_key\:my_master_secret\@go\.urbanairship.com\/api\/push\/scheduled\/[0-9]+/, :status => ["401", "Unauthorized"]) + # feedback FakeWeb.register_uri(:get, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/device_tokens\/feedback/, :status => ["200", "OK"], :body => "[{\"device_token\":\"token\",\"marked_inactive_on\":\"2010-10-14T19:15:13Z\",\"alias\":\"my_alias\"}]") FakeWeb.register_uri(:get, /my_app_key2\:my_master_secret2\@go\.urbanairship.com\/api\/device_tokens\/feedback/, :status => ["500", "Internal Server Error"]) end @@ -63,10 +69,11 @@ end describe "registering a device" do before(:each) do + @valid_params = {:alias => 'one'} Urbanairship.application_key = "my_app_key" Urbanairship.application_secret = "my_app_secret" end it "raises an error if call is made without an app key and secret configured" do @@ -99,13 +106,40 @@ it "returns false when the authorization is invalid" do Urbanairship.application_key = "bad_key" Urbanairship.register_device("new_device_token").should == false end - # TODO: - # it "accepts additional parameters (EXPAND THIS)" + it "doesn't set the content-type header to application/json if options are empty" do + Urbanairship.register_device("device_token_one") + FakeWeb.last_request['content-type'].should_not == 'application/json' + end + it "accepts an alias" do + Urbanairship.register_device("device_token_one", @valid_params).should == true + end + + it "sets the content-type header to application/json when options are added" do + Urbanairship.register_device("device_token_one", @valid_params) + FakeWeb.last_request['content-type'].should == 'application/json' + end + + it "adds alias to the JSON payload" do + Urbanairship.register_device("device_token_one", @valid_params) + request_json['alias'].should == "one" + end + + it "converts alias param to string" do + Urbanairship.register_device("device_token_one", :alias => 11) + request_json['alias'].should be_a_kind_of String + end + + it "excludes invalid parameters from the JSON payload" do + @valid_params.merge!(:foo => 'bar') + Urbanairship.register_device("device_token_one", @valid_params) + request_json['foo'].should be_nil + end + end describe "unregistering a device" do before(:each) do Urbanairship.application_key = "my_app_key" @@ -141,10 +175,57 @@ Urbanairship.unregister_device("key_to_delete").should == false end end + describe "deleting a scheduled push notification" do + before(:each) do + Urbanairship.application_key = "my_app_key" + Urbanairship.master_secret = "my_master_secret" + end + + it "raises an error if call is made without an app key and master secret configured" do + Urbanairship.application_key = nil + Urbanairship.master_secret = nil + + lambda { + Urbanairship.delete_scheduled_push("123456789") + }.should raise_error(RuntimeError, "Must configure application_key, master_secret before making this request.") + end + + it "uses app key and secret to sign the request" do + Urbanairship.delete_scheduled_push("123456789") + FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}" + end + + it "sends the key that needs to be deleted" do + Urbanairship.delete_scheduled_push("123456789") + FakeWeb.last_request.path.should == "/api/push/scheduled/123456789" + end + + it "sends the key that needs to be deleted" do + Urbanairship.delete_scheduled_push(123456789) + FakeWeb.last_request.path.should == "/api/push/scheduled/123456789" + end + + it "sends the alias that needs to be deleted" do + Urbanairship.delete_scheduled_push(:alias => "alias_to_delete") + FakeWeb.last_request.path.should == "/api/push/scheduled/alias/alias_to_delete" + end + + it "returns true when the push notification is successfully deleted" do + Urbanairship.delete_scheduled_push("123456789").should == true + FakeWeb.last_request.body.should be_nil + end + + it "returns false when the authorization is invalid" do + Urbanairship.application_key = "bad_key" + Urbanairship.delete_scheduled_push("123456789").should == false + end + + end + describe "sending multiple push notifications" do before(:each) do @valid_params = {:device_tokens => ['device_token_one', 'device_token_two'], :aps => {:alert => 'foo'}} Urbanairship.application_key = "my_app_key" @@ -201,9 +282,16 @@ end it "only attempts to format schedule_for if it is a time object" do Urbanairship.push(@valid_params.merge(:schedule_for => ["2010-10-10 09:09:09 UTC"])) request_json['schedule_for'].should == ['2010-10-10T09:09:09Z'] + end + + it "adds an aliased schedule_for to the JSON payload" do + time = Time.parse("Oct 17th, 2010, 8:00 PM UTC") + alias_str = 'cafebabe' + Urbanairship.push(@valid_params.merge(:schedule_for => [{ :alias => alias_str, :scheduled_time => time }])) + request_json['schedule_for'].should == [{ 'alias' => alias_str, 'scheduled_time' => '2010-10-17T20:00:00Z' }] end it "adds exclude_tokens to the JSON payload" do Urbanairship.push(@valid_params.merge(:exclude_tokens => ["one", "two"])) request_json['exclude_tokens'].should == ["one", "two"]