api_test.rb
7.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
require_relative '../../../../test/api/test_helper'
class PushNotificationApiTest < ActiveSupport::TestCase
def setup
create_and_activate_user
login_api
environment = Environment.default
environment.enable_plugin(PushNotificationPlugin)
end
should 'list all my device tokens' do
logged_user = @user
token1 = PushNotificationPlugin::DeviceToken.create!(:token => "firsttoken", device_name: "my device", :user => logged_user)
token2 = PushNotificationPlugin::DeviceToken.create!(:token => "secondtoken", device_name: "my device", :user => logged_user)
get "/api/v1/push_notification_plugin/device_tokens?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equivalent [token1.token, token2.token], json
end
should 'not list other people device tokens' do
user = User.create!(:login => 'outro', :email => 'outro@example.com', :password => 'outro', :password_confirmation => 'outro', :environment => Environment.default)
user.activate
PushNotificationPlugin::DeviceToken.create!(:token => "firsttoken", device_name: "my device", :user => user)
get "/api/v1/push_notification_plugin/device_tokens?#{params.merge(:target_id => user.id).to_query}"
assert_equal 401, last_response.status
end
should 'admin see other user\'s device tokens' do
logged_user = @user
Environment.default.add_admin(logged_user.person)
logged_user.reload
user = User.create!(:login => 'outro', :email => 'outro@example.com', :password => 'outro', :password_confirmation => 'outro', :environment => Environment.default)
user.activate
token1 = PushNotificationPlugin::DeviceToken.create!(:token => "firsttoken", device_name: "my device", :user => user)
get "/api/v1/push_notification_plugin/device_tokens?#{params.merge(:target_id => user.id).to_query}"
json = JSON.parse(last_response.body)
assert_equivalent [token1.token], json
end
#------------------------------------------------------------------------------------------------------
should 'add my device token' do
params.merge!(:device_name => "my_device", :token => "token1")
post "/api/v1/push_notification_plugin/device_tokens?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equivalent ["token1"], json["user"]["device_tokens"]
end
should 'not add device tokens for other people' do
user = User.create!(:login => 'outro', :email => 'outro@example.com', :password => 'outro', :password_confirmation => 'outro', :environment => Environment.default)
user.activate
params.merge!(:device_name => "my_device", :token => "tokenX", :target_id => user.id)
post "/api/v1/push_notification_plugin/device_tokens?#{params.to_query}"
assert_equal 401, last_response.status
end
should 'admin add device tokens for other users' do
logged_user = @user
Environment.default.add_admin(logged_user.person)
logged_user.reload
user = User.create!(:login => 'outro', :email => 'outro@example.com', :password => 'outro', :password_confirmation => 'outro', :environment => Environment.default)
user.activate
params.merge!(:device_name => "my_device", :token=> "tokenY", :target_id => user.id)
post "/api/v1/push_notification_plugin/device_tokens?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equivalent ["tokenY"], json["user"]["device_tokens"]
end
#------------------------------------------------------------------------------------------------------
should 'delete my device tokens' do
logged_user = @user
PushNotificationPlugin::DeviceToken.create!(:token => "firsttoken", device_name: "my device", :user => logged_user)
PushNotificationPlugin::DeviceToken.create!(:token => "secondtoken", device_name: "my device", :user => logged_user)
params.merge!(:token => "secondtoken")
delete "/api/v1/push_notification_plugin/device_tokens?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equivalent ["firsttoken"], json["user"]["device_tokens"]
end
should 'not delete device tokens for other people' do
user = User.create!(:login => 'outro', :email => 'outro@example.com', :password => 'outro', :password_confirmation => 'outro', :environment => Environment.default)
user.activate
PushNotificationPlugin::DeviceToken.create!(:token => "secondtoken", device_name: "my device", :user => user)
user.reload
params.merge!(:token => "secondtoken", :target_id => user.id)
delete "/api/v1/push_notification_plugin/device_tokens?#{params.to_query}"
assert_equal 401, last_response.status
assert_equivalent user.device_token_list, ["secondtoken"]
end
should 'admin delete device tokens for other users' do
logged_user = @user
Environment.default.add_admin(logged_user.person)
logged_user.reload
user = User.create!(:login => 'outro', :email => 'outro@example.com', :password => 'outro', :password_confirmation => 'outro', :environment => Environment.default)
user.activate
PushNotificationPlugin::DeviceToken.create!(:token => "firsttoken", device_name: "my device", :user => user)
PushNotificationPlugin::DeviceToken.create!(:token => "secondtoken", device_name: "my device", :user => user)
user.reload
params.merge!(:token=> "secondtoken", :target_id => user.id)
delete "/api/v1/push_notification_plugin/device_tokens?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equivalent ["firsttoken"], json["user"]["device_tokens"]
end
#--------------------------------------------------------------------------------------------------------------------------------------------
should 'list all notifications disabled by default for new users' do
get "/api/v1/push_notification_plugin/notification_settings?#{params.to_query}"
json = JSON.parse(last_response.body)
json["user"]["notification_settings"].each_pair do |notification, status|
refute status
end
end
should 'list device tokens notification options' do
logged_user = @user
logged_user.notification_settings.activate_notification "new_comment"
logged_user.save!
get "/api/v1/push_notification_plugin/notification_settings?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal true, json["user"]["notification_settings"]["new_comment"]
assert_equal false, json["user"]["notification_settings"]["add_friend"]
end
should 'get possible notifications' do
get "/api/v1/push_notification_plugin/possible_notifications?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equivalent PushNotificationPlugin::NotificationSettings::NOTIFICATIONS.keys, json["possible_notifications"]
end
should 'change device tokens notification options' do
logged_user = @user
params.merge!("new_comment"=> "true")
post "/api/v1/push_notification_plugin/notification_settings?#{params.to_query}"
logged_user.reload
json = JSON.parse(last_response.body)
assert_equal true, json["user"]["notification_settings"]["new_comment"]
assert_equal true, logged_user.notification_settings.hash_flags["new_comment"]
end
should 'get active notifications list' do
logged_user = @user
logged_user.notification_settings.activate_notification "new_comment"
logged_user.save!
get "/api/v1/push_notification_plugin/active_notifications?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equivalent ["new_comment"], json
end
should 'get inactive notifications list' do
logged_user = @user
logged_user.notification_settings.activate_notification "new_comment"
logged_user.save
get "/api/v1/push_notification_plugin/inactive_notifications?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equivalent (PushNotificationPlugin::NotificationSettings::NOTIFICATIONS.keys-["new_comment"]), json
end
end