spec/urbanairship_spec.rb in urbanairship-2.2.0 vs spec/urbanairship_spec.rb in urbanairship-2.2.1

- old
+ new

@@ -33,10 +33,45 @@ 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"]) + + #tags + FakeWeb.register_uri(:get, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/tags/, :status => ["200", "OK"], :body => "[{\"tags\":[\"tag1\",\"tag2\"]}]") + FakeWeb.register_uri(:get, /my_app_key2\:my_master_secret2\@go\.urbanairship.com\/api\/tags/, :status => ["500", "Internal Server Error"]) + + #add_tag + FakeWeb.register_uri(:put, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/tags\/new_tag/, :status => ["200", "OK"]) + FakeWeb.register_uri(:put, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/tags\/existing_tag/, :status => ["201", "OK"]) + FakeWeb.register_uri(:put, /my_app_key2\:my_master_secret2\@go\.urbanairship.com\/api\/tags\/a_tag/, :status => ["500", "Internal Server Error"]) + + #remove_tag + FakeWeb.register_uri(:delete, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/tags\/non_deleted_tag/, :status => ["204", "OK"]) + FakeWeb.register_uri(:delete, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/tags\/deleted_tag/, :status => ["404", "OK"]) + FakeWeb.register_uri(:delete, /my_app_key2\:my_master_secret2\@go\.urbanairship.com\/api\/tags\/a_tag/, :status => ["500", "Internal Server Error"]) + + #tags_for_device_tokens + FakeWeb.register_uri(:get, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/device_tokens\/valid_device_token\/tags/, :status => ["200", "OK"], :body => "[{\"tags\":[\"tag1\",\"tag2\"]}]") + FakeWeb.register_uri(:get, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/device_tokens\/invalid_device_token\/tags/, :status => ["404", "OK"]) + FakeWeb.register_uri(:get, /my_app_key2\:my_master_secret2\@go\.urbanairship.com\/api\/device_tokens\/a_device_token\/tags/, :status => ["500", "Internal Server Error"]) + + ##tag_device + FakeWeb.register_uri(:put, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/device_tokens\/valid_device_token\/tags\/new_tag/, :status => ["201", "OK"]) + FakeWeb.register_uri(:put, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/device_tokens\/valid_device_token\/tags\/existing_tag/, :status => ["200", "OK"]) + FakeWeb.register_uri(:put, /my_app_key2\:my_master_secret2\@go\.urbanairship.com\/api\/device_tokens\/a_device_token\/tags\/a_tag/, :status => ["500", "Internal Server Error"]) + + #untag_device + FakeWeb.register_uri(:delete, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/device_tokens\/valid_device_token\/tags\/existing_tag/, :status => ["204", "OK"]) + FakeWeb.register_uri(:delete, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/device_tokens\/valid_device_token\/tags\/non_existant_tag/, :status => ["404", "OK"]) + FakeWeb.register_uri(:delete, /my_app_key2\:my_master_secret2\@go\.urbanairship.com\/api\/device_tokens\/a_device_token\/tags\/a_tag/, :status => ["500", "Internal Server Error"]) + + # push to segment + FakeWeb.register_uri(:post, "https://my_app_key:my_master_secret@go.urbanairship.com/api/push/segments", :status => ["200", "OK"]) + FakeWeb.register_uri(:post, "https://my_app_key2:my_master_secret2@go.urbanairship.com/api/push/segments", :status => ["400", "Bad Request"]) + FakeWeb.register_uri(:post, /bad_key\:my_master_secret\@go\.urbanairship\.com/, :status => ["401", "Unauthorized"]) + end describe "configuration" do it "enables you to configure the application key" do subject.application_key.should be_nil @@ -54,11 +89,228 @@ subject.master_secret.should be_nil subject.master_secret = "asdf1234" subject.master_secret.should == "asdf1234" end end + + describe "::tags" do + before(:each) do + subject.application_key = "my_app_key" + subject.master_secret = "my_master_secret" + end + it "raises an error if call is made without an app key and master secret configured" do + subject.application_key = nil + subject.master_secret = nil + + lambda { + subject.tags + }.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 + subject.tags + FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}" + end + + it "returns valid tags" do + response = subject.tags + response.first.should include("tags") + response.first["tags"].should include("tag1") + response.first["tags"].should include("tag2") + end + + it "success? is false when the call doesn't return 200" do + subject.application_key = "my_app_key2" + subject.master_secret = "my_master_secret2" + subject.tags.success?.should == false + end + + end + + describe "::add_tag" do + before(:each) do + subject.application_key = "my_app_key" + subject.master_secret = "my_master_secret" + end + + it "raises an error if call is made without an app key and master secret configured" do + subject.application_key = nil + subject.master_secret = nil + + lambda { + subject.add_tag('a_tag') + }.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 + subject.add_tag('new_tag') + FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}" + end + + it "adds a new tag" do + subject.add_tag('new_tag').success?.should == true + subject.add_tag('new_tag').code.should == "200" + end + + it "adds an exisiting tag" do + subject.add_tag('existing_tag').success?.should == true + subject.add_tag('existing_tag').code.should == "201" + end + + it "success? is false when the call doesn't return 200 or 201" do + subject.application_key = "my_app_key2" + subject.master_secret = "my_master_secret2" + subject.add_tag('a_tag').success?.should == false + end + + end + + describe "::remove_tag" do + before(:each) do + subject.application_key = "my_app_key" + subject.master_secret = "my_master_secret" + end + + it "raises an error if call is made without an app key and master secret configured" do + subject.application_key = nil + subject.master_secret = nil + + lambda { + subject.remove_tag('a_tag') + }.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 + subject.remove_tag('non_deleted_tag') + FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}" + end + + it "removes a tag that has not been removed yet" do + subject.remove_tag('non_deleted_tag').success?.should == true + subject.remove_tag('non_deleted_tag').code.should == "204" + end + + it "attempts to remove an already deleted tag" do + subject.remove_tag('deleted_tag').success?.should == false + subject.remove_tag('deleted_tag').code.should == "404" + end + + it "success? is false when the call doesn't return 204" do + subject.application_key = "my_app_key2" + subject.master_secret = "my_master_secret2" + subject.add_tag('a_tag').success?.should == false + end + end + + describe "::tags_for_device" do + before(:each) do + subject.application_key = "my_app_key" + subject.master_secret = "my_master_secret" + end + + it "raises an error if call is made without an app key and master secret configured" do + subject.application_key = nil + subject.master_secret = nil + + lambda { + subject.tags_for_device('a_device_token') + }.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 + subject.tags_for_device('valid_device_token') + FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}" + end + + it "returns valid tags for a device" do + response = subject.tags_for_device('valid_device_token') + response.first.should include("tags") + response.first["tags"].should include("tag1") + response.first["tags"].should include("tag2") + response.code.should == "200" + end + + it "returns invalid response for device token that is not found or registered" do + response = subject.tags_for_device('invalid_device_token') + response.code.should == "404" + end + + it "success? is false when the call doesn't return 200" do + subject.application_key = "my_app_key2" + subject.master_secret = "my_master_secret2" + subject.tags_for_device('a_device_token').success?.should == false + end + end + + describe "::tag_device" do + before(:each) do + subject.application_key = "my_app_key" + subject.master_secret = "my_master_secret" + end + + it "raises an error if call is made without an app key and master secret configured" do + subject.application_key = nil + subject.master_secret = nil + + lambda { + subject.tag_device({:device_token => 'a_device_token', :tag => 'a_tag'}) + }.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 + subject.tag_device({:device_token => 'valid_device_token', :tag => 'new_tag'}) + FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}" + end + + it "adds a valid device token to tag" do + response = subject.tag_device({:device_token => 'valid_device_token', :tag => 'new_tag'}) + response.code.should == "201" + response.success?.should == true + end + + it "adds a valid device token to an existing tag" do + response = subject.tag_device({:device_token => 'valid_device_token', :tag => 'existing_tag'}) + response.code.should == "200" + response.success?.should == true + end + end + + describe "::untag_device" do + before(:each) do + subject.application_key = "my_app_key" + subject.master_secret = "my_master_secret" + end + + it "raises an error if call is made without an app key and master secret configured" do + subject.application_key = nil + subject.master_secret = nil + + lambda { + subject.untag_device({:device_token => 'a_device_token', :tag => 'a_tag'}) + }.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 + subject.untag_device({:device_token => 'valid_device_token', :tag => 'existing_tag'}) + FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}" + end + + it "removes a valid device token from a tag" do + response = subject.untag_device({:device_token => 'valid_device_token', :tag => 'existing_tag'}) + response.code.should == "204" + response.success?.should == true + end + + it "removes a device token from a tag that it is not associated with" do + response = subject.untag_device({:device_token => 'valid_device_token', :tag => 'non_existant_tag'}) + response.code.should == "404" + response.success?.should == false + end + end + + describe "::register_device" do before(:each) do @valid_params = {:alias => 'one'} subject.application_key = "my_app_key" subject.application_secret = "my_app_secret" @@ -266,9 +518,57 @@ it "returns false if urbanairship responds with a non-200 response" do subject.application_key = "my_app_key2" subject.master_secret = "my_master_secret2" subject.push.success?.should == false + end + end + + describe "::push_to_segment" do + before(:each) do + @valid_params = {:segments => ['segment-id'], :aps => {:alert => 'foo'}} + subject.application_key = "my_app_key" + subject.master_secret = "my_master_secret" + end + + it "raises an error if call is made without an app key and master secret configured" do + subject.application_key = nil + subject.master_secret = nil + + lambda { + subject.push(@valid_params) + }.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 + subject.push_to_segment(@valid_params) + FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}" + end + + it "returns true when it successfully pushes a notification" do + subject.push_to_segment(@valid_params).success?.should == true + end + + it "returns false when the authorization is invalid" do + subject.application_key = "bad_key" + subject.push_to_segment(@valid_params).success?.should == false + end + + it "adds schedule_for to the JSON payload" do + time = Time.parse("Oct 17th, 2010, 8:00 PM UTC") + subject.push_to_segment(@valid_params.merge(:schedule_for => [time])) + request_json['schedule_for'].should == ['2010-10-17T20:00:00Z'] + end + + it "only attempts to format schedule_for if it is a time object" do + subject.push_to_segment(@valid_params.merge(:schedule_for => ["2010-10-10 09:09:09 UTC"])) + request_json['schedule_for'].should == ['2010-10-10T09:09:09Z'] + end + + it "returns false if urbanairship responds with a non-200 response" do + subject.application_key = "my_app_key2" + subject.master_secret = "my_master_secret2" + subject.push_to_segment.success?.should == false end end describe "::batch_push" do before(:each) do