notifiable-rails 0.19.9 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +1 -11
  3. data/app/controllers/notifiable/device_tokens_controller.rb +1 -1
  4. data/{lib/generators/notifiable/install/templates/create_notifiable_apps.rb → db/migrate/20131210115648_create_notifiable_apps.rb} +0 -0
  5. data/{lib/generators/notifiable/install/templates/create_notifiable_device_tokens.rb → db/migrate/20131210115649_create_notifiable_device_tokens.rb} +0 -0
  6. data/{lib/generators/notifiable/install/templates/create_notifiable_notifications.rb → db/migrate/20131210115650_create_notifiable_notifications.rb} +0 -0
  7. data/{lib/generators/notifiable/install/templates/create_notifiable_statuses.rb → db/migrate/20131210115651_create_notifiable_statuses.rb} +0 -0
  8. data/lib/notifiable/engine.rb +10 -0
  9. data/lib/notifiable/version.rb +1 -1
  10. data/spec/controllers/device_tokens_controller_spec.rb +114 -105
  11. data/spec/controllers/notifications_controller_spec.rb +45 -45
  12. data/spec/model/notifiable_app_spec.rb +25 -0
  13. data/spec/model/notifiable_spec.rb +25 -0
  14. data/spec/model/notification_spec.rb +105 -0
  15. data/spec/model/notifier_base_spec.rb +18 -0
  16. data/spec/spec_helper.rb +15 -1
  17. data/spec/test_app/app/controllers/application_controller.rb +2 -0
  18. data/spec/test_app/config/database.yml +6 -0
  19. data/spec/test_app/db/development.sqlite3 +0 -0
  20. data/spec/test_app/db/migrate/{20131210115648_create_users.rb → 20131210115652_create_users.rb} +0 -0
  21. data/spec/test_app/db/schema.rb +2 -2
  22. data/spec/test_app/db/test.sqlite3 +0 -0
  23. data/spec/test_app/log/development.log +186 -0
  24. data/spec/test_app/log/test.log +33197 -0
  25. metadata +48 -70
  26. data/spec/notifiable_app_spec.rb +0 -24
  27. data/spec/notifiable_spec.rb +0 -20
  28. data/spec/notification_spec.rb +0 -119
  29. data/spec/notifier_base_spec.rb +0 -27
  30. data/spec/test_app/db/migrate/20131210115649_create_notifiable_apps.rb +0 -10
  31. data/spec/test_app/db/migrate/20131228225139_create_notifiable_device_tokens.rb +0 -18
  32. data/spec/test_app/db/migrate/20131228225140_create_notifiable_notifications.rb +0 -26
  33. data/spec/test_app/db/migrate/20131229104039_create_notifiable_statuses.rb +0 -12
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d72a8ab45299096c3999b1f5b4636711f3abc436
4
- data.tar.gz: a5c5d447002f2b6467142e0a26ee44e95f99033f
3
+ metadata.gz: ab6756bd2340817f2d17d834d70f8e93c1ed4099
4
+ data.tar.gz: 45df3fe71bb3477e904ec77b97dfebbd3df82263
5
5
  SHA512:
6
- metadata.gz: 53bdb4ced5e11d073810bff0d408d69e7ef46ca0ae7c2fbc7295f48687d7a2eb3089aca448434680595e857862533d802907b8293cdbbe9786b0f9eec8fa1b34
7
- data.tar.gz: 06bba839f81ee6e931fce417febaf9c55c63e87abba3fae279ab232b5fd05f43ac716d937fbe1aa821c6b781af3c9a3ac6cacb558da229557338376dce8c3989
6
+ metadata.gz: 315d467276e714839d3f7d82cd19698e3ca1ed7e3b91da2d86a05476bb706ef37c56b8d728f7649de9cce92ec6c6d97f169cf6472627355fca84ff6ea86330ba
7
+ data.tar.gz: 5904e1345423e81be5e682ee117e51122ca6263452421d2b66efcb0e4207f4d4b5e473aa2efd009d01e1fa642e171ddad4045296049c51e59f918b0f147a0911
data/Rakefile CHANGED
@@ -4,21 +4,11 @@ rescue LoadError
4
4
  puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
5
  end
6
6
 
7
- require 'rdoc/task'
8
-
9
- RDoc::Task.new(:rdoc) do |rdoc|
10
- rdoc.rdoc_dir = 'rdoc'
11
- rdoc.title = 'Notifiable'
12
- rdoc.options << '--line-numbers'
13
- rdoc.rdoc_files.include('README.rdoc')
14
- rdoc.rdoc_files.include('lib/**/*.rb')
15
- end
7
+ Bundler::GemHelper.install_tasks
16
8
 
17
9
  APP_RAKEFILE = File.expand_path("../spec/test_app/Rakefile", __FILE__)
18
10
  load 'rails/tasks/engine.rake'
19
11
 
20
- Bundler::GemHelper.install_tasks
21
-
22
12
  Dir[File.join(File.dirname(__FILE__), 'lib/tasks/**/*.rake')].each {|f| load f }
23
13
 
24
14
  require 'rspec/core'
@@ -49,7 +49,7 @@ module Notifiable
49
49
  end
50
50
 
51
51
  def ensure_authorized!
52
- head :status => :unauthorized if @device_token.user && current_notifiable_user && !@device_token.user.eql?(current_notifiable_user)
52
+ head :status => :unauthorized unless !@device_token.user || @device_token.user.eql?(current_notifiable_user)
53
53
  end
54
54
 
55
55
  def find_device_token
@@ -3,6 +3,7 @@ module Notifiable
3
3
  class Engine < ::Rails::Engine
4
4
  isolate_namespace Notifiable
5
5
 
6
+ # use rspec for testing
6
7
  config.generators do |g|
7
8
  g.test_framework :rspec, :fixture => false
8
9
  g.fixture_replacement :factory_girl, :dir => 'spec/factories'
@@ -16,6 +17,15 @@ module Notifiable
16
17
  require_dependency(c)
17
18
  end
18
19
  end
20
+
21
+ # append migrations
22
+ initializer :append_migrations do |app|
23
+ unless app.root.to_s.match root.to_s
24
+ config.paths["db/migrate"].expanded.each do |expanded_path|
25
+ app.config.paths["db/migrate"] << expanded_path
26
+ end
27
+ end
28
+ end
19
29
  end
20
30
 
21
31
  end
@@ -1,3 +1,3 @@
1
1
  module Notifiable
2
- VERSION = "0.19.9"
2
+ VERSION = "0.20.0"
3
3
  end
@@ -2,137 +2,146 @@ require 'spec_helper'
2
2
 
3
3
  describe Notifiable::DeviceTokensController do
4
4
 
5
- let(:user1) { FactoryGirl.create(:user) }
6
- let(:user2) { FactoryGirl.create(:user_with_mock_token) }
7
- let(:user2_device_token) { user2.device_tokens.first }
8
- let(:app) { FactoryGirl.create(:app) }
9
- let(:invalid_device_token) { FactoryGirl.create(:invalid_mock_token) }
10
-
11
5
  before(:each) do
12
6
  @request.env["HTTP_ACCEPT"] = "application/json"
13
7
  @request.env["CONTENT_TYPE"] = "application/json"
14
8
  end
15
-
16
- it "creates a new device token for an existing user" do
17
- post :create, :token => "ABC123", :user_email => user1.email, :provider => :apns, :app_id => app.id
18
-
19
- response.status.should == 200
20
- Notifiable.api_device_token_params.push('id').each{|p| json.should have_key(p.to_s)}
21
-
22
- Notifiable::DeviceToken.count.should == 1
23
- user1.device_tokens.count.should == 1
24
- dt = user1.device_tokens.first
25
- dt.token.should.eql? "ABC123"
26
- dt.provider.should.eql? :apns
27
- dt.app.should.eql? app
28
- end
29
-
30
- it "creates a new device token for an anonymous user" do
31
- post :create, :token => "ABC123", :provider => :apns, :app_id => app.id
32
-
33
- response.status.should == 200
34
- Notifiable.api_device_token_params.push('id').each{|p| json.should have_key(p.to_s)}
35
-
36
- Notifiable::DeviceToken.count.should == 1
37
- dt = Notifiable::DeviceToken.first
38
- dt.token.should.eql? "ABC123"
39
- dt.provider.should.eql? :apns
40
- dt.app.should.eql? app
41
- User.count.should == 0
42
- end
43
-
44
- it "uses an existing token" do
45
- post :create, :token => user2_device_token.token, :provider => user2_device_token.provider, :app_id => app.id, :user_email => user2.email
46
-
47
- response.status.should == 200
48
- Notifiable.api_device_token_params.push('id').each{|p| json.should have_key(p.to_s)}
49
-
50
- Notifiable::DeviceToken.count.should == 1
51
- user2.device_tokens.count.should == 1
52
- dt = Notifiable::DeviceToken.first
53
- dt.token.should eql user2_device_token.token
54
- dt.provider.should eql user2_device_token.provider
55
- dt.app.should.eql? app
56
- end
57
-
58
- it "uses an existing token unless its invalid" do
59
- post :create, :token => invalid_device_token.token, :provider => invalid_device_token.provider, :app_id => app.id, :user_email => user1.email
60
- response.status.should == 200
61
- Notifiable.api_device_token_params.push('id').each{|p| json.should have_key(p.to_s)}
62
-
63
- Notifiable::DeviceToken.count.should == 2
64
- user1.device_tokens.count.should == 1
65
- dt = Notifiable::DeviceToken.last
66
- dt.token.should.eql? invalid_device_token.token
67
- dt.is_valid.should be_true
68
- end
69
9
 
70
- it "doesn't create a token if no app is specified" do
71
- post :create, :token => "ABC123", :user_email => user1.email, :provider => :mpns
10
+ describe "#create" do
11
+ context "for existing user" do
12
+ let(:user1) { create(:user) }
13
+ let(:app) { create(:app) }
14
+
15
+ before(:each) { post :create, :token => "ABC123", :user_email => user1.email, :provider => :apns, :app_id => app.id }
16
+
17
+ it { expect(response.status).to eq 200 }
18
+ it { expect(json['id']).to_not be_nil }
19
+ it { expect(Notifiable::DeviceToken.count).to eq 1 }
20
+ it { expect(Notifiable::DeviceToken.first.token).to eq "ABC123" }
21
+ it { expect(Notifiable::DeviceToken.first.provider).to eq "apns" }
22
+ it { expect(Notifiable::DeviceToken.first.app).to eq app }
23
+ it { expect(User.first.device_tokens.count).to eq 1 }
24
+ end
25
+
26
+ context "anonymously" do
27
+ let(:app) { create(:app) }
28
+
29
+ before(:each) { post :create, :token => "ABC123", :provider => :apns, :app_id => app.id }
30
+
31
+ it { expect(response.status).to eq 200 }
32
+ it { expect(json['id']).to_not be_nil }
33
+ it { expect(Notifiable::DeviceToken.count).to eq 1 }
34
+ it { expect(Notifiable::DeviceToken.first.token).to eq "ABC123" }
35
+ it { expect(Notifiable::DeviceToken.first.provider).to eq "apns" }
36
+ it { expect(Notifiable::DeviceToken.first.app).to eq app }
37
+ end
38
+
39
+ context "for an existing valid token" do
40
+ let(:app) { create(:app) }
41
+ let!(:token) { create(:mock_token, :token => "DEF456", :provider => :apns, :app => app) }
42
+
43
+ before(:each) { post :create, :token => "DEF456", :provider => :apns, :app_id => app.id }
44
+
45
+ it { expect(response.status).to eq 200 }
46
+ it { expect(json['id']).to eq token.id }
47
+ it { expect(Notifiable::DeviceToken.count).to eq 1 }
48
+ it { expect(Notifiable::DeviceToken.first.token).to eq "DEF456" }
49
+ it { expect(Notifiable::DeviceToken.first.provider).to eq "apns" }
50
+ it { expect(Notifiable::DeviceToken.first.app).to eq app }
51
+ end
52
+
53
+ context "for an existing invalid token" do
54
+ let(:app) { create(:app) }
55
+ let!(:token) { create(:mock_token, :token => "DEF456", :provider => :apns, :app => app, :is_valid => false) }
56
+
57
+ before(:each) { post :create, :token => "DEF456", :provider => :apns, :app_id => app.id }
58
+
59
+ it { expect(response.status).to eq 200 }
60
+ it { expect(json['id']).to eq token.id }
61
+ it { expect(Notifiable::DeviceToken.count).to eq 1 }
62
+ it { expect(Notifiable::DeviceToken.first.token).to eq "DEF456" }
63
+ it { expect(Notifiable::DeviceToken.first.provider).to eq "apns" }
64
+ it { expect(Notifiable::DeviceToken.first.app).to eq app }
65
+ it { expect(Notifiable::DeviceToken.first.is_valid).to eq true }
66
+ end
67
+
68
+ context "unless an App is specified" do
69
+ before(:each) { post :create, :token => "ABC123", :provider => :apns }
70
+
71
+ it { expect(response.status).to eq 422 }
72
+ it { expect(Notifiable::DeviceToken.count).to eq 0 }
73
+ end
72
74
 
73
- response.status.should == 422
74
- Notifiable::DeviceToken.count.should == 0
75
75
  end
76
76
 
77
- it "anonymises an existing token" do
77
+ context "old" do
78
+ let(:user1) { FactoryGirl.create(:user) }
79
+ let(:user2) { FactoryGirl.create(:user_with_mock_token) }
80
+ let(:user2_device_token) { user2.device_tokens.first }
81
+ let(:app) { FactoryGirl.create(:app) }
82
+ let(:invalid_device_token) { FactoryGirl.create(:invalid_mock_token) }
83
+
84
+ it "anonymises an existing token" do
78
85
 
79
- post :create, :token => user2_device_token.token, :provider => :mpns
86
+ post :create, :token => user2_device_token.token, :provider => :mpns
80
87
 
81
- response.status.should == 200
82
- Notifiable.api_device_token_params.push('id').each{|p| json.should have_key(p.to_s)}
88
+ response.status.should == 200
89
+ Notifiable.api_device_token_params.push('id').each{|p| json.should have_key(p.to_s)}
83
90
 
84
- Notifiable::DeviceToken.count.should == 1
85
- dt = Notifiable::DeviceToken.first
86
- dt.token.should eql user2_device_token.token
87
- dt.user.should be_nil
88
- end
91
+ Notifiable::DeviceToken.count.should == 1
92
+ dt = Notifiable::DeviceToken.first
93
+ dt.token.should eql user2_device_token.token
94
+ dt.user.should be_nil
95
+ end
89
96
 
90
- it "updates token for an existing device token" do
91
- put :update, :id => user2_device_token.id, :token => "DEF456", :user_email => user2.email
97
+ it "updates token for an existing device token" do
98
+ put :update, :id => user2_device_token.id, :token => "DEF456", :user_email => user2.email
92
99
 
93
- response.status.should == 200
94
- Notifiable.api_device_token_params.each{|p| json.should have_key(p.to_s)}
100
+ response.status.should == 200
101
+ Notifiable.api_device_token_params.each{|p| json.should have_key(p.to_s)}
95
102
 
96
- Notifiable::DeviceToken.count.should == 1
97
- dt = Notifiable::DeviceToken.first
98
- dt.token.should eql "DEF456"
99
- end
103
+ Notifiable::DeviceToken.count.should == 1
104
+ dt = Notifiable::DeviceToken.first
105
+ dt.token.should eql "DEF456"
106
+ end
100
107
 
101
- it "deletes tokens" do
102
- delete :destroy, :id => user2_device_token.id, :user_email => user2.email
108
+ it "deletes tokens" do
109
+ delete :destroy, :id => user2_device_token.id, :user_email => user2.email
103
110
 
104
- expect(response).to be_success
105
- Notifiable::DeviceToken.count.should == 0
106
- end
111
+ expect(response).to be_success
112
+ Notifiable::DeviceToken.count.should == 0
113
+ end
107
114
 
108
- it "doesn't delete token unless authorised" do
109
- delete :destroy, :id => user2_device_token.id
115
+ it "doesn't delete token unless authorised" do
116
+ delete :destroy, :id => user2_device_token.id
110
117
 
111
- response.status.should == 401
118
+ response.status.should == 401
112
119
 
113
- Notifiable::DeviceToken.where(:token => user2_device_token.token).count.should == 1
114
- end
120
+ Notifiable::DeviceToken.where(:token => user2_device_token.token).count.should == 1
121
+ end
115
122
 
116
- it "doesn't delete tokens of other users" do
117
- delete :destroy, :id => user2_device_token.id, :user_email => user1.email
123
+ it "doesn't delete tokens of other users" do
124
+ delete :destroy, :id => user2_device_token.id, :user_email => user1.email
118
125
 
119
- response.status.should == 401
126
+ response.status.should == 401
120
127
 
121
- Notifiable::DeviceToken.where(:token => user2_device_token.token).count.should == 1
122
- end
128
+ Notifiable::DeviceToken.where(:token => user2_device_token.token).count.should == 1
129
+ end
123
130
 
124
- it "doesnt delete tokens if they don't exist" do
125
- delete :destroy, :id => 59, :user_email => user1.email
131
+ it "doesnt delete tokens if they don't exist" do
132
+ delete :destroy, :id => 59, :user_email => user1.email
126
133
 
127
- response.status.should == 404
128
- Notifiable::DeviceToken.where(:token => user2_device_token.token).count.should == 1
129
- end
134
+ response.status.should == 404
135
+ Notifiable::DeviceToken.where(:token => user2_device_token.token).count.should == 1
136
+ end
130
137
 
131
- it "doesn't update token unless authorised" do
132
- put :update, :id => user2_device_token.id, :user_email => user1.email, :token => "ZXY987"
138
+ it "doesn't update token unless authorised" do
139
+ put :update, :id => user2_device_token.id, :user_email => user1.email, :token => "ZXY987"
133
140
 
134
- response.status.should == 401
135
- user2_device_token.token.should.eql? "ZXY987"
141
+ response.status.should == 401
142
+ user2_device_token.token.should.eql? "ZXY987"
143
+ end
144
+
136
145
  end
137
146
 
138
147
  end
@@ -2,64 +2,64 @@ require 'spec_helper'
2
2
 
3
3
  describe Notifiable::NotificationsController do
4
4
 
5
- let(:user1) { FactoryGirl.create(:user_with_mock_token) }
6
5
  let(:user1_device_token) { user1.device_tokens.first }
7
6
  let(:user2) { FactoryGirl.create(:user) }
8
- let(:app) { user1_device_token.app }
9
- let(:notification) { FactoryGirl.create(:notification, :app => app)}
10
7
  let(:anonymous_device_token) { FactoryGirl.create(:mock_token) }
11
8
 
12
9
  before(:each) do
13
10
  @request.env["HTTP_ACCEPT"] = "application/json"
14
11
  @request.env["CONTENT_TYPE"] = "application/json"
15
12
  end
16
-
17
- it "marks a status as opened" do
18
- FactoryGirl.create(:notification_status, :notification => notification, :device_token => user1_device_token)
19
-
20
- put :opened, {:notification_id => notification.id, :device_token_id => user1_device_token.id, :user_email => user1.email}
21
-
22
- response.status.should == 200
23
-
24
- Notifiable::NotificationStatus.count.should == 1
25
- saved_status = Notifiable::NotificationStatus.first
26
- saved_status.opened?.should be_true
27
-
28
- Notifiable::Notification.count.should == 1
29
- saved_notification = Notifiable::Notification.first
30
- saved_notification.opened_count.should == 1
31
- end
32
13
 
33
- it "marks a status as opened anonymously" do
34
- FactoryGirl.create(:notification_status, :notification => notification, :device_token => anonymous_device_token)
35
-
36
- put :opened, {:notification_id => notification.id, :device_token_id => anonymous_device_token.id}
14
+ describe "#opened" do
37
15
 
38
- response.status.should == 200
39
-
40
- Notifiable::NotificationStatus.count.should == 1
41
- saved_status = Notifiable::NotificationStatus.first
42
- saved_status.opened?.should be_true
43
-
44
- Notifiable::Notification.count.should == 1
45
- saved_notification = Notifiable::Notification.first
46
- saved_notification.opened_count.should == 1
47
- end
48
-
49
- it "returns an error if the user is incorrect" do
50
- FactoryGirl.create(:notification_status, :notification => notification, :device_token => user1_device_token)
51
-
52
- post :opened, {:notification_id => notification.id, :device_token_id => user1_device_token.id, :user_email => user2.email}
16
+ describe "by a user" do
17
+
18
+ context "authorised" do
19
+ let(:notifiable_app) { create(:app) }
20
+ let(:user) { create(:user) }
21
+ let(:token) { create(:mock_token, :app => notifiable_app, :user_id => user.id) }
22
+ subject(:n) { create(:notification, :app => notifiable_app)}
23
+ let!(:status) { create(:notification_status, :notification => n, :device_token => token) }
24
+
25
+ before(:each) { put :opened, {:notification_id => n.id, :device_token_id => token.id, :user_email => user.email} }
26
+
27
+ it { expect(Notifiable::NotificationStatus.count).to eq 1 }
28
+ it { expect(Notifiable::NotificationStatus.first.opened?).to eq true }
29
+ it { expect(Notifiable::Notification.first.opened_count).to eq 1 }
30
+ end
31
+
32
+ context "unauthorised" do
33
+ let(:notifiable_app) { create(:app) }
34
+ let(:user1) { create(:user) }
35
+ let(:user2) { create(:user) }
36
+ let(:token) { create(:mock_token, :app => notifiable_app, :user_id => user1.id) }
37
+ subject(:n) { create(:notification, :app => notifiable_app)}
38
+ let!(:status) { create(:notification_status, :notification => n, :device_token => token) }
39
+
40
+ before(:each) { put :opened, {:notification_id => n.id, :device_token_id => token.id, :user_email => user2.email} }
41
+
42
+ it { expect(response.status).to eq 401 }
43
+ it { expect(Notifiable::NotificationStatus.count).to eq 1 }
44
+ it { expect(Notifiable::NotificationStatus.first.opened?).to eq false }
45
+ it { expect(Notifiable::Notification.first.opened_count).to eq 0 }
46
+ end
47
+ end
53
48
 
54
- response.status.should == 401
49
+ describe "anonymously" do
50
+ let(:notifiable_app) { create(:app) }
51
+ let(:token) { create(:mock_token, :app => notifiable_app) }
52
+ subject(:n) { create(:notification, :app => notifiable_app)}
53
+ let!(:status) { create(:notification_status, :notification => n, :device_token => token) }
54
+
55
+ before(:each) { put :opened, {:notification_id => n.id, :device_token_id => token.id} }
56
+
57
+ it { expect(Notifiable::NotificationStatus.count).to eq 1 }
58
+ it { expect(Notifiable::NotificationStatus.first.opened?).to eq true }
59
+ it { expect(Notifiable::Notification.first.opened_count).to eq 1 }
60
+ end
55
61
 
56
- Notifiable::NotificationStatus.count.should == 1
57
- saved_status = Notifiable::NotificationStatus.first
58
- saved_status.status.should == 0
59
62
 
60
- Notifiable::Notification.count.should == 1
61
- saved_notification = Notifiable::Notification.first
62
- saved_notification.opened_count.should == 0
63
63
  end
64
64
 
65
65
  end