intercom-rails 0.2.24 → 0.2.25

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,79 +0,0 @@
1
- require 'test_setup'
2
-
3
- class ConfigTest < MiniTest::Unit::TestCase
4
-
5
- include InterTest
6
-
7
- def test_setting_app_id
8
- IntercomRails.config.app_id = "1234"
9
- assert_equal IntercomRails.config.app_id, "1234"
10
- end
11
-
12
- def test_setting_current_user
13
- current_user = Proc.new { @blah }
14
- IntercomRails.config.user.current = current_user
15
- assert_equal IntercomRails.config.user.current, current_user
16
- end
17
-
18
- def test_setting_current_user_not_to_a_proc
19
- assert_raises ArgumentError do
20
- IntercomRails.config.user.current = 1
21
- end
22
- end
23
-
24
- def test_configuring_intercom_with_block
25
- IntercomRails.config do |config|
26
- config.app_id = "4567"
27
- end
28
-
29
- assert_equal IntercomRails.config.app_id, "4567"
30
- end
31
-
32
- def test_custom_data_rejects_non_proc_or_symbol_attributes
33
- exception = assert_raises ArgumentError do
34
- IntercomRails.config.user.custom_data = {
35
- 'foo' => Proc.new {},
36
- 'bar' => 'heyheyhey!'
37
- }
38
- end
39
-
40
- assert_equal "all custom_data attributes should be either a Proc or a symbol", exception.message
41
- end
42
-
43
- def test_setting_custom_data
44
- custom_data_config = {
45
- 'foo' => Proc.new {},
46
- 'bar' => :method_name
47
- }
48
-
49
- IntercomRails.config.user.custom_data = custom_data_config
50
- assert_equal custom_data_config, IntercomRails.config.user.custom_data
51
- end
52
-
53
- def test_setting_company_custom_data
54
- custom_data_config = {
55
- 'the_local' => Proc.new { 'club 93' }
56
- }
57
-
58
- IntercomRails.config.company.custom_data = custom_data_config
59
- assert_equal custom_data_config, IntercomRails.config.company.custom_data
60
- end
61
-
62
- def test_setting_inbox_style
63
- IntercomRails.config.inbox.style = :custom
64
- assert_equal :custom, IntercomRails.config.inbox.style
65
- end
66
-
67
- def test_reset_clears_existing_config
68
- IntercomRails.config.user.custom_data = {'muffin' => :muffin}
69
- IntercomRails.config.reset!
70
- assert_equal nil, IntercomRails.config.user.custom_data
71
- end
72
-
73
- def test_reset_clears_inbox_config_too
74
- IntercomRails.config.inbox.style = :custom
75
- IntercomRails.config.reset!
76
- assert_equal nil, IntercomRails.config.inbox.style
77
- end
78
-
79
- end
@@ -1,122 +0,0 @@
1
- require 'import_test_setup'
2
- require 'sinatra/base'
3
-
4
- class MockIntercom < Sinatra::Base
5
-
6
- set :server, 'thin'
7
-
8
- before do
9
- content_type 'application/json'
10
- end
11
-
12
- get '/health_check' do
13
- content_type 'plain/text'
14
- 'hello world'
15
- end
16
-
17
- post '/all_successful' do
18
- {:failed => []}.to_json
19
- end
20
-
21
- post '/one_failure' do
22
- {:failed => ['ben@intercom.io']}.to_json
23
- end
24
-
25
- post '/bad_auth' do
26
- status 403
27
- {"error" => {"type" => "not_authenticated", "message" => "HTTP Basic: Access denied."}}.to_json
28
- end
29
-
30
- post '/500_error' do
31
- status 500
32
- {"error" => {"type" => "server_error", "message" => "Danger deploy, gone wrong?"}}.to_json
33
- end
34
-
35
- end
36
-
37
- class InterRunner < MiniTest::Unit
38
-
39
- self.runner = self.new
40
-
41
- def _run(*args)
42
- @mock_intercom_pid = start_mock_intercom
43
- super
44
- ensure
45
- Process.kill('INT', @mock_intercom_pid)
46
- Process.wait(@mock_intercom_pid) rescue SystemError
47
- end
48
-
49
- private
50
-
51
- def start_mock_intercom
52
- pid = fork do
53
- MockIntercom.run!(:port => 46837) do |server|
54
- server.silent = true
55
- end
56
- end
57
-
58
- response = nil
59
- uri = URI.parse("http://localhost:46837/health_check")
60
-
61
- begin
62
- response = Net::HTTP.get_response(uri).body until(response == 'hello world')
63
- rescue Errno::ECONNREFUSED
64
- sleep(0.5)
65
- retry
66
- end
67
-
68
- pid
69
- end
70
-
71
- end
72
-
73
- class ImportNetworkTest < InterRunner::TestCase
74
-
75
- include InterTest
76
- include ImportTest
77
-
78
- def api_path=(path)
79
- IntercomRails::Import.stub(:bulk_create_api_endpoint) {
80
- URI.parse("http://localhost:46837/#{path}")
81
- }
82
-
83
- @import = IntercomRails::Import.new
84
- end
85
-
86
- def test_empty_failed
87
- self.api_path = '/all_successful'
88
-
89
- @import.run
90
- assert_equal [], @import.failed
91
- assert_equal 2, @import.total_sent
92
- end
93
-
94
- def test_sets_failed_correctly
95
- self.api_path = '/one_failure'
96
-
97
- @import.run
98
- assert_equal ["ben@intercom.io"], @import.failed
99
- assert_equal 2, @import.total_sent
100
- end
101
-
102
- def test_raises_import_error_on_bad_auth
103
- self.api_path = '/bad_auth'
104
-
105
- exception = assert_raises(IntercomRails::ImportError) {
106
- @import.run
107
- }
108
-
109
- assert_equal "App ID or API Key are incorrect, please check them in config/initializers/intercom.rb", exception.message
110
- end
111
-
112
- def test_throws_exception_when_intercom_api_is_being_a_dick
113
- self.api_path = '/500_error'
114
-
115
- exception = assert_raises(IntercomRails::IntercomAPIError) {
116
- @import.run
117
- }
118
-
119
- assert_equal "The Intercom API request failed with the code: 500, after 3 attempts.", exception.message
120
- end
121
-
122
- end
@@ -1,125 +0,0 @@
1
- require 'import_test_setup'
2
-
3
- class ImportUnitTest < MiniTest::Unit::TestCase
4
-
5
- include InterTest
6
- include ImportTest
7
-
8
- def test_run_with_wrong_rails_env
9
- Rails.stub(:env).and_return ActiveSupport::StringInquirer.new("development")
10
-
11
- exception = assert_raises IntercomRails::ImportError do
12
- IntercomRails::Import.run
13
- end
14
-
15
- assert_equal exception.message, "You can only import your users from your production environment"
16
- end
17
-
18
- def test_run_with_no_user_class
19
- IntercomRails::Import.any_instance.stub(:user_klass).and_return(nil)
20
-
21
- exception = assert_raises IntercomRails::ImportError do
22
- IntercomRails::Import.run
23
- end
24
-
25
- assert_equal exception.message, "We couldn't find your user class, please set one in config/initializers/intercom_rails.rb"
26
- end
27
-
28
- def test_run_with_unsupported_user_class
29
- IntercomRails::Import.any_instance.stub(:user_klass).and_return(Class)
30
-
31
- exception = assert_raises IntercomRails::ImportError do
32
- IntercomRails::Import.run
33
- end
34
-
35
- assert_equal exception.message, "Only ActiveRecord and Mongoid models are supported"
36
- end
37
-
38
- def test_run_with_no_api_key
39
- IntercomRails.config.stub(:api_key).and_return(nil)
40
-
41
- exception = assert_raises IntercomRails::ImportError do
42
- IntercomRails::Import.run
43
- end
44
-
45
- assert_equal exception.message, "Please add an Intercom API Key to config/initializers/intercom.rb"
46
- end
47
-
48
- def test_mongoid
49
- klass = Class.new
50
- klass.class_eval do
51
- include Mongoid::Document
52
- end
53
- IntercomRails::Import.any_instance.stub(:user_klass).and_return(klass)
54
-
55
- @import = IntercomRails::Import.new
56
- @import.should_receive(:map_to_users_for_wire).with(klass.all).and_call_original
57
- @import.should_receive(:send_users).and_return('failed' => [])
58
- @import.run
59
- end
60
-
61
- def test_status_output
62
- @import = IntercomRails::Import.new(:status_enabled => true)
63
- @import.stub(:send_users).and_return('failed' => [1])
64
- @import.should_receive(:batches).and_yield(nil, 3)
65
-
66
- $stdout.flush
67
- @old_stdout = $stdout.dup
68
- $stdout = @output = StringIO.new
69
-
70
- @import.run
71
- expected_output = <<-output
72
- * Found user class: User
73
- * Intercom API key found
74
- * Sending users in batches of 100:
75
- ..F
76
- * Successfully created 2 users
77
- * Failed to create 1 user, this is likely due to bad data
78
- output
79
- $stdout.flush
80
-
81
- assert_equal expected_output, @output.string
82
- ensure
83
- $stdout = @old_stdout
84
- end
85
-
86
- def test_prepares_companies
87
- @import = IntercomRails::Import.new
88
-
89
- u = dummy_user
90
- u.instance_eval do
91
- def apps
92
- [dummy_company]
93
- end
94
- end
95
-
96
- User.stub(:find_in_batches).and_yield([u])
97
-
98
- IntercomRails.config.user.company_association = Proc.new { |user| user.apps }
99
-
100
- prepare_for_batch_users = nil
101
- @import.stub(:prepare_batch) { |users| prepare_for_batch_users = users }
102
- @import.stub(:send_users).and_return('failed' => [])
103
-
104
- @import.run
105
-
106
- assert_equal 1, prepare_for_batch_users[0][:companies].length
107
- User.rspec_reset
108
- end
109
-
110
- def test_max_batch_size_default
111
- @import = IntercomRails::Import.new
112
- assert_equal 100, @import.max_batch_size
113
- end
114
-
115
- def test_max_batch_size_settable
116
- @import = IntercomRails::Import.new(:max_batch_size => 50)
117
- assert_equal 50, @import.max_batch_size
118
- end
119
-
120
- def test_max_batch_size_hard_limit
121
- @import = IntercomRails::Import.new(:max_batch_size => 101)
122
- assert_equal 100, @import.max_batch_size
123
- end
124
-
125
- end
@@ -1,46 +0,0 @@
1
- require 'test_setup'
2
-
3
- class CompanyTest < MiniTest::Unit::TestCase
4
-
5
- include InterTest
6
-
7
- Company = IntercomRails::Proxy::Company
8
- DUMMY_COMPANY = dummy_company
9
-
10
- def test_finds_current_company
11
- IntercomRails.config.company.current = Proc.new { @app }
12
- object_with_app_instance_var = Object.new
13
- object_with_app_instance_var.instance_variable_set(:@app, DUMMY_COMPANY)
14
-
15
- c = Company.current_in_context(object_with_app_instance_var)
16
- assert_equal true, c.valid?
17
- expected_hash = {:id => '6', :name => 'Intercom'}
18
- assert_equal expected_hash, c.to_hash
19
- end
20
-
21
- def test_whiny_nil
22
- NilClass.class_eval do
23
- def id
24
- raise ArgumentError, "boo"
25
- end
26
- end
27
-
28
- search_object = nil
29
- assert_equal false, Company.new(search_object).valid?
30
- end
31
-
32
- def test_companies_for_user
33
- IntercomRails.config.user.company_association = Proc.new { |user| user.apps }
34
- test_user = dummy_user
35
- test_user.instance_eval do
36
- def apps
37
- [DUMMY_COMPANY, dummy_company(:name => "Prey", :id => "800")]
38
- end
39
- end
40
-
41
- companies = Company.companies_for_user(IntercomRails::Proxy::User.new(test_user))
42
- assert_equal 2, companies.length
43
- assert_equal ["Intercom", "Prey"], companies.map(&:company).map(&:name)
44
- end
45
-
46
- end
@@ -1,143 +0,0 @@
1
- require 'test_setup'
2
-
3
- class UserTest < MiniTest::Unit::TestCase
4
-
5
- User = IntercomRails::Proxy::User
6
-
7
- include InterTest
8
- include IntercomRails
9
-
10
- DUMMY_USER = dummy_user(:email => 'ciaran@intercom.io', :name => 'Ciaran Lee')
11
-
12
- def test_raises_error_when_no_user_found
13
- assert_raises(IntercomRails::NoUserFoundError) {
14
- User.current_in_context(Object.new)
15
- }
16
- end
17
-
18
- def test_finds_current_user
19
- object_with_current_user_method = Object.new
20
- object_with_current_user_method.instance_eval do
21
- def current_user
22
- DUMMY_USER
23
- end
24
- end
25
-
26
- @user_proxy = User.current_in_context(object_with_current_user_method)
27
- assert_user_found
28
- end
29
-
30
- def test_finds_user_instance_variable
31
- object_with_instance_variable = Object.new
32
- object_with_instance_variable.instance_eval do
33
- @user = DUMMY_USER
34
- end
35
-
36
- @user_proxy = User.current_in_context(object_with_instance_variable)
37
- assert_user_found
38
- end
39
-
40
- def test_finds_config_user
41
- object_from_config = Object.new
42
- object_from_config.instance_eval do
43
- def something_esoteric
44
- DUMMY_USER
45
- end
46
- end
47
-
48
- IntercomRails.config.user.current = Proc.new { something_esoteric }
49
- @user_proxy = User.current_in_context(object_from_config)
50
- assert_user_found
51
- end
52
-
53
- def test_finds_config_user_does_not_fallback_to_auto_find_users
54
- IntercomRails.config.user.current = Proc.new { something_esoteric }
55
- object_with_instance_variable = Object.new
56
- object_with_instance_variable.instance_eval do
57
- @user = DUMMY_USER
58
- end
59
-
60
- assert_raises(IntercomRails::NoUserFoundError) {
61
- User.current_in_context(object_with_instance_variable)
62
- }
63
- end
64
-
65
- def assert_user_found
66
- assert_equal DUMMY_USER, @user_proxy.user
67
- end
68
-
69
- def test_includes_custom_data
70
- plan_dummy_user = DUMMY_USER.dup
71
- plan_dummy_user.instance_eval do
72
- def plan
73
- 'pro'
74
- end
75
- end
76
-
77
- IntercomRails.config.user.custom_data = {
78
- 'plan' => :plan
79
- }
80
-
81
- @user_proxy = User.new(plan_dummy_user)
82
- assert_equal 'pro', @user_proxy.to_hash['plan']
83
- end
84
-
85
- def test_converts_dates_to_timestamps
86
- plan_dummy_user = DUMMY_USER.dup
87
- plan_dummy_user.instance_eval do
88
- def some_date
89
- Time.at(5)
90
- end
91
- end
92
-
93
- IntercomRails.config.user.custom_data = {
94
- 'some_date' => :some_date
95
- }
96
-
97
- @user_proxy = User.new(plan_dummy_user)
98
- assert_equal 5, @user_proxy.to_hash['some_date']
99
- end
100
-
101
- def test_valid_returns_true_if_user_id_or_email
102
- assert_equal true, User.new(DUMMY_USER).valid?
103
- end
104
-
105
- def test_not_valid_if_new_record?
106
- new_record_user = dummy_user(:email => 'not-saved@intercom.io', :name => 'New Record')
107
- def new_record_user.new_record?
108
- true
109
- end
110
- assert_equal false, User.new(new_record_user).valid?
111
- end
112
-
113
- def test_includes_custom_data_from_intercom_custom_data
114
- object_with_intercom_custom_data = Object.new
115
- object_with_intercom_custom_data.instance_eval do
116
- def intercom_custom_data
117
- o = Object.new
118
- o.instance_eval do
119
- def user
120
- {:ponies => :rainbows}
121
- end
122
- end
123
-
124
- o
125
- end
126
- end
127
-
128
- @user_proxy = User.new(DUMMY_USER, object_with_intercom_custom_data)
129
- assert_equal :rainbows, @user_proxy.to_hash[:ponies]
130
- end
131
-
132
- def test_whiny_nil
133
- NilClass.class_eval do
134
- def id
135
- raise ArgumentError, "boo"
136
- end
137
- end
138
-
139
- search_object = nil
140
- assert_equal false, User.new(search_object).valid?
141
- end
142
-
143
- end