stackmob 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,105 +0,0 @@
1
- require 'helper'
2
-
3
- class StackmobClientTest < MiniTest::Unit::TestCase
4
-
5
- attr_reader :valid_client
6
-
7
- def app_name
8
- "test"
9
- end
10
-
11
- def app_vsn
12
- 0
13
- end
14
-
15
- def test_params
16
- {:abc => 123, :def => 456}
17
- end
18
-
19
- def stub_all_requests(method, resp_obj)
20
- OAuth::AccessToken.any_instance.stubs(method).returns(resp_obj)
21
- end
22
-
23
- def setup
24
- @valid_client = StackMob::Client.new("http://123.com/", app_name, app_vsn, "some-valid-test-key", "some-valid-test-secret")
25
- @test_hash = {"a" => 1, "b" => 2}
26
- @good_resp = mock("Net::HTTPResponse")
27
- @good_resp.stubs(:code).returns(200)
28
- @good_resp.stubs(:body).returns(Yajl::Encoder.encode(@test_hash))
29
-
30
- # hack to give test suite access to underlying oauth object
31
- class << valid_client
32
- def _oauth
33
- @oauth_client
34
- end
35
- end
36
- end
37
-
38
- def test_response_json_parsed
39
- stub_all_requests(:get, @good_resp)
40
-
41
- assert_equal @test_hash, valid_client.request(:get, :some_service, "/abc")
42
- end
43
-
44
- def test_requesting_invalid_method_raises_error
45
- assert_raises StackMob::Client::InvalidRequestMethod do
46
- valid_client.request(:invalid, :some_service, "/asd")
47
- end
48
- end
49
-
50
- def test_when_resp_code_is_not_200
51
- failed = mock("Net::HTTPResponse")
52
- failed.stubs(:code).returns(404)
53
- stub_all_requests(:get, failed)
54
-
55
-
56
- assert_raises StackMob::Client::RequestError do
57
- valid_client.request(:get, :some_service, "/asd")
58
- end
59
- end
60
-
61
- def test_when_json_parsing_will_raise_an_error
62
- resp_w_invalid_body = mock("Net::HTTPResponse")
63
- resp_w_invalid_body.stubs(:code).returns(200)
64
- resp_w_invalid_body.stubs(:body).returns("this is not valid json")
65
- stub_all_requests(:get, resp_w_invalid_body)
66
-
67
- assert_raises StackMob::Client::BadResponseBody do
68
- valid_client.request(:get, :some_service, "/asd")
69
- end
70
- end
71
-
72
- def test_get_with_request_params
73
- service = :some_service
74
- path = "user"
75
-
76
- valid_client._oauth.expects(:get).with("/#{service}/#{app_vsn}/#{app_name}/#{path}?abc=123&def=456", "").returns(@good_resp)
77
- valid_client.request(:get, service, path, test_params)
78
- end
79
-
80
- def test_post_with_request_params
81
- service = :some_service
82
- path = "user"
83
- @good_resp.stubs(:code).returns(201)
84
-
85
- valid_client._oauth.expects(:post).with("/#{service}/#{app_vsn}/#{app_name}/#{path}", Yajl::Encoder.encode(test_params), "Content-Type" => "application/json").returns(@good_resp)
86
- valid_client.request(:post, service, path, test_params)
87
- end
88
-
89
- def test_delete_with_request_params
90
- service = :a_service
91
- path = "abc"
92
-
93
- valid_client._oauth.expects(:delete).with("/#{service}/#{app_vsn}/#{app_name}/#{path}?something=123", "").returns(@good_resp)
94
- valid_client.request(:delete, service, path, something: "123")
95
- end
96
-
97
- def test_put_with_request_params
98
- service = :some_service
99
- path = "abc"
100
-
101
- valid_client._oauth.expects(:put).with("/#{service}/#{app_vsn}/#{app_name}/#{path}", Yajl::Encoder.encode(test_params), "Content-Type" => "application/json").returns(@good_resp)
102
- valid_client.request(:put, service, path, test_params)
103
- end
104
-
105
- end
@@ -1,83 +0,0 @@
1
- require 'helper'
2
-
3
- class StackMobDataStoreTest < MiniTest::Unit::TestCase
4
-
5
- def setup
6
- @mock_client = mock("StackMob::Client")
7
- @datastore = StackMob::DataStore.new(@mock_client)
8
- end
9
-
10
- def test_given_client_is_returned_client
11
- assert_equal @mock_client, @datastore.client
12
- end
13
-
14
- def test_api_schema_calls_listapi
15
- @mock_client.expects(:request).with(:get, :api, "/listapi").returns("obj" => "")
16
- @datastore.api_schema
17
- end
18
-
19
- def test_delete_call
20
- uid = "abc"
21
-
22
- @mock_client.expects(:request).with(:delete, :api, "/user", :user_id => uid).returns(nil)
23
- @datastore.delete(:user, :user_id => uid)
24
- end
25
-
26
- def test_create_call
27
- name = "test user"
28
-
29
- @mock_client.expects(:request).with(:post, :api, "/user", :name => name).returns(nil)
30
- @datastore.create(:user, :name => name)
31
- end
32
-
33
- def test_get_all_call
34
- @mock_client.expects(:request).with(:get, :api, "/user", Hash.new).returns(nil)
35
- @datastore.get(:user)
36
- end
37
-
38
- def test_update_call
39
- uid = "abc"
40
- name = "test user"
41
- params = {:name => name}
42
-
43
- @mock_client.expects(:request).with(:put, :api, "/user/#{uid}", params).returns(nil)
44
- @datastore.update(:user, uid, params)
45
- end
46
-
47
- def test_create_returns_false_on_request_error
48
- @mock_client.expects(:request).raises(StackMob::Client::RequestError)
49
- assert !@datastore.create(:user, :name => "def")
50
- end
51
-
52
- def test_create_bang_does_not_catch_error
53
- @mock_client.expects(:request).raises(StackMob::Client::RequestError)
54
- assert_raises StackMob::Client::RequestError do
55
- @datastore.create!(:user, :name => "def")
56
- end
57
- end
58
-
59
- def test_update_returns_false_on_request_error
60
- @mock_client.expects(:request).raises(StackMob::Client::RequestError)
61
- assert !@datastore.update(:user, "123", :name => "def")
62
- end
63
-
64
- def test_update_bang_does_not_catch_error
65
- @mock_client.expects(:request).raises(StackMob::Client::RequestError)
66
- assert_raises StackMob::Client::RequestError do
67
- @datastore.update!(:user, "123", :name => "def")
68
- end
69
- end
70
-
71
- def test_delete_returns_false_on_request_error
72
- @mock_client.expects(:request).raises(StackMob::Client::RequestError)
73
- assert !@datastore.delete(:user, :name => "ads")
74
- end
75
-
76
- def test_delete_bang_does_not_catch_error
77
- @mock_client.expects(:request).raises(StackMob::Client::RequestError)
78
- assert_raises StackMob::Client::RequestError do
79
- @datastore.delete!(:user, :name => "def")
80
- end
81
- end
82
-
83
- end
@@ -1,21 +0,0 @@
1
- require 'helper'
2
-
3
- class StackMobDeployerTest < MiniTest::Unit::TestCase
4
-
5
- def setup
6
- @mock_client = mock("StackMob::Client")
7
- @deployer = StackMob::Deployer.new(@mock_client)
8
- end
9
-
10
- def test_register_call
11
- hostname = "testing1234"
12
- @mock_client.expects(:request).with(:post, :api, "heroku/app", :hostname => hostname)
13
- @deployer.register(hostname)
14
- end
15
-
16
- def test_fetch_call
17
- @mock_client.expects(:request).with(:get, :api, "heroku/app")
18
- @deployer.fetch
19
- end
20
-
21
- end
@@ -1,133 +0,0 @@
1
- require 'helper'
2
-
3
- class StackMobPushTest < MiniTest::Unit::TestCase
4
-
5
- def setup
6
- @test_token = "abcdefghijklmnopqrstuv"
7
- @user_id = "123"
8
-
9
- @mock_client = mock("StackMob::Client")
10
- @push = StackMob::Push.new(@mock_client)
11
- end
12
-
13
- def test_given_client_is_returned_client
14
- assert_equal @mock_client, @push.client
15
- end
16
-
17
- def test_register_call
18
- @mock_client.expects(:request).with(:post, :push, "/device_tokens", :userId => @user_id, :token => @test_token).returns(nil)
19
- @push.register(@user_id, @test_token)
20
- end
21
-
22
- def test_broadcast_call
23
- badge = 1,
24
- sound = "testsound.mp3"
25
- alert = "testing"
26
-
27
- expected_body = {:recipients => [],
28
- :aps => {:alert => alert, :sound => sound, :badge => badge},
29
- :areRecipientsDeviceTokens => true,
30
- :exclude_tokens => []}
31
- expected_params = [:post, :push, "/push_broadcast", expected_body]
32
- @mock_client.expects(:request).with(*expected_params).returns(nil)
33
-
34
- @push.broadcast(:badge => badge, :sound => sound, :alert => alert)
35
- end
36
-
37
- def test_broadcast_missing_alert_raises_error
38
- assert_raises ArgumentError do
39
- @push.broadcast(:badge => 0, :sound => "")
40
- end
41
- end
42
-
43
- def test_broadcast_badge_defaults_to_zero
44
- sound = "test.mp3"
45
- alert = "alert message"
46
-
47
- expected_body = {:recipients => [],
48
- :aps => {:alert => alert, :sound => sound, :badge => 0},
49
- :areRecipientsDeviceTokens => true,
50
- :exclude_tokens => []}
51
- expected_params = [:post, :push, "/push_broadcast", expected_body]
52
- @mock_client.expects(:request).with(*expected_params).returns(nil)
53
- @push.broadcast(:sound => sound, :alert => alert)
54
- end
55
-
56
- def test_broadcast_sound_defaults_to_empty_string
57
- badge = 1
58
- alert = "alert message"
59
-
60
- expected_body = {:recipients => [],
61
- :aps => {:alert => alert, :sound => "", :badge => badge},
62
- :areRecipientsDeviceTokens => true,
63
- :exclude_tokens => []}
64
- expected_params = [:post, :push, "/push_broadcast", expected_body]
65
- @mock_client.expects(:request).with(*expected_params).returns(nil)
66
- @push.broadcast(:badge => badge, :alert => alert)
67
- end
68
-
69
- def test_send_messsage_to_one_call
70
- sound = "testsound.mp3"
71
- alert = "Single Message"
72
- badge = 2
73
- token = "abc"
74
-
75
- expected_body = {:recipients => [token],
76
- :aps => {:badge => badge, :sound => sound, :alert => alert},
77
- :areRecipientsDeviceTokens => true,
78
- :exclude_tokens => []}
79
- expected_params = [:post, :push, "/push", expected_body]
80
- @mock_client.expects(:request).with(*expected_params).returns(nil)
81
- @push.send_message(token, :alert => alert, :sound => sound, :badge => badge)
82
- end
83
-
84
- def test_send_message_without_alert_raises_error
85
- assert_raises ArgumentError do
86
- @push.send_message("avc", :sound => "somefile.mp3", :badge => 1)
87
- end
88
- end
89
-
90
- def test_send_message_defaults_badge_to_zero
91
- sound = "testsound.mp3"
92
- alert = "Single Message"
93
- token = "abc"
94
-
95
- expected_body = {:recipients => [token],
96
- :aps => {:badge => 0, :sound => sound, :alert => alert},
97
- :areRecipientsDeviceTokens => true,
98
- :exclude_tokens => []}
99
- expected_params = [:post, :push, "/push", expected_body]
100
- @mock_client.expects(:request).with(*expected_params).returns(nil)
101
- @push.send_message(token, :alert => alert, :sound => sound)
102
- end
103
-
104
- def test_send_message_defaults_sound_to_empty_string
105
- alert = "Single Message"
106
- badge = 2
107
- token = "abc"
108
-
109
- expected_body = {:recipients => [token],
110
- :aps => {:badge => badge, :sound => "", :alert => alert},
111
- :areRecipientsDeviceTokens => true,
112
- :exclude_tokens => []}
113
- expected_params = [:post, :push, "/push", expected_body]
114
- @mock_client.expects(:request).with(*expected_params).returns(nil)
115
- @push.send_message(token, :alert => alert, :badge => badge)
116
- end
117
-
118
- def test_send_message_with_user_ids
119
- sound = "testsound.mp3"
120
- alert = "Single Message"
121
- badge = 2
122
- token = "abc"
123
-
124
- expected_body = {:recipients => [token],
125
- :aps => {:badge => badge, :sound => sound, :alert => alert},
126
- :areRecipientsDeviceTokens => false,
127
- :exclude_tokens => []}
128
- expected_params = [:post, :push, "/push", expected_body]
129
- @mock_client.expects(:request).with(*expected_params).returns(nil)
130
- @push.send_message(token, :alert => alert, :sound => sound, :badge => badge, :recipients_are_users => true)
131
- end
132
-
133
- end
@@ -1,129 +0,0 @@
1
- require 'helper'
2
-
3
- class StackMobTestCase < MiniTest::Unit::TestCase
4
-
5
- def setup
6
- @config_dev_key = "config_dev_key"
7
- @config_dev_secret = "config_dev_secret"
8
- @config_prod_key = "config_prod_key"
9
- @config_prod_secret = "config_prod_secret"
10
- @config_app_name = "config_app_name"
11
- @config_client_name = "config_client_name"
12
-
13
- @env_dev_key = "env_dev_key"
14
- @env_dev_secret = "env_dev_secret"
15
- @env_prod_key = "env_prod_key"
16
- @env_prod_secret = "env_prod_secret"
17
- @env_app_name = "env_app_name"
18
- @env_client_name = "env_client_name"
19
-
20
- StackMob.stubs(:config).returns("sm_client_name" => @config_client_name, "sm_app_name" => @config_app_name,
21
- "development" => {"key" => @config_dev_key, "secret" => @config_dev_secret}, "production" => {"key" => @config_prod_key, "secret" => @config_prod_secret})
22
- end
23
-
24
- def test_fetch_app_name_when_no_env_key_exists
25
- assert_equal @config_app_name, StackMob.app_name
26
- end
27
-
28
- def test_fetch_app_name_when_env_key_exists
29
- ENV["STACKMOB_APP_NAME"] = @env_app_name
30
-
31
- assert_equal @env_app_name, StackMob.app_name
32
-
33
- ENV["STACKMOB_APP_NAME"] = nil
34
- end
35
-
36
- def test_fetch_client_name_when_no_env_key_exists
37
- assert_equal @config_client_name, StackMob.client_name
38
- end
39
-
40
- def test_fetch_client_name_when_env_key_exists
41
- ENV["STACKMOB_CLIENT_NAME"] = @env_client_name
42
-
43
- assert_equal @env_client_name, StackMob.client_name
44
-
45
- ENV["STACKMOB_CLIENT_NAME"] = nil
46
- end
47
-
48
- def test_fetch_dev_key_when_no_env_key_exists
49
- previous_rack_env = ENV["RACK_ENV"]
50
- ENV["RACK_ENV"] = "development"
51
-
52
- assert_equal @config_dev_key, StackMob.key
53
-
54
- ENV["RACK_ENV"] = previous_rack_env
55
- end
56
-
57
- def test_fetch_dev_secret_when_no_env_secret_exists
58
- previous_rack_env = ENV["RACK_ENV"]
59
- ENV["RACK_ENV"] = "development"
60
-
61
- assert_equal @config_dev_secret, StackMob.secret
62
-
63
- ENV["RACK_ENV"] = previous_rack_env
64
- end
65
-
66
- def test_fetch_prod_key_when_no_env_key_exists
67
- previous_rack_env = ENV["RACK_ENV"]
68
- ENV["RACK_ENV"] = "production"
69
-
70
- assert_equal @config_prod_key, StackMob.key
71
-
72
- ENV["RACK_ENV"] = previous_rack_env
73
- end
74
-
75
- def test_fetch_prod_secret_when_no_env_secret_exists
76
- previous_rack_env = ENV["RACK_ENV"]
77
- ENV["RACK_ENV"] = "production"
78
-
79
- assert_equal @config_prod_secret, StackMob.secret
80
-
81
- ENV["RACK_ENV"] = previous_rack_env
82
- end
83
-
84
- def test_fetches_dev_key_from_env_when_exists
85
- previous_rack_env = ENV["RACK_ENV"]
86
- ENV["RACK_ENV"] = "development"
87
- ENV["STACKMOB_SAND_PUBLIC_KEY"] = @env_dev_key
88
-
89
- assert_equal @env_dev_key, StackMob.key
90
-
91
- ENV["STACKMOB_SAND_PUBLIC_KEY"] = nil
92
- ENV["RACK_ENV"] = previous_rack_env
93
- end
94
-
95
- def test_fetches_dev_secret_from_env_when_exists
96
- previous_rack_env = ENV["RACK_ENV"]
97
- ENV["RACK_ENV"] = "development"
98
- ENV["STACKMOB_SAND_PRIVATE_KEY"] = @env_dev_secret
99
-
100
- assert_equal @env_dev_secret, StackMob.secret
101
-
102
- ENV["STACKMOB_SAND_PRIVATE_KEY"] = nil
103
- ENV["RACK_ENV"] = previous_rack_env
104
- end
105
-
106
- def test_fetches_prod_key_from_env_when_exists
107
- previous_rack_env = ENV["RACK_ENV"]
108
- ENV["RACK_ENV"] = "production"
109
- ENV["STACKMOB_PROD_PUBLIC_KEY"] = @env_prod_key
110
-
111
- assert_equal @env_prod_key, StackMob.key
112
-
113
- ENV["RACK_ENV"] = previous_rack_env
114
- ENV["STACKMOB_PROD_PUBLIC_KEY"] = nil
115
- end
116
-
117
- def test_fetches_prod_secret_from_env_when_exists
118
- previous_rack_env = ENV["RACK_ENV"]
119
- ENV["RACK_ENV"] = "production"
120
- ENV["STACKMOB_PROD_PRIVATE_KEY"] = @env_prod_secret
121
-
122
- assert_equal @env_prod_secret, StackMob.secret
123
-
124
- ENV["RACK_ENV"] = previous_rack_env
125
- ENV["STACKMOB_PROD_PRIVATE_KEY"] = nil
126
- end
127
-
128
-
129
- end