From 54405308a78e4dadc7871eef2154cd3816227691 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Fri, 22 Apr 2016 16:07:09 -0300 Subject: [PATCH] push_notification: fix api tests --- plugins/push_notification/test/api/api_test.rb | 178 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/push_notification/test/unit/api_test.rb | 178 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 2 files changed, 178 insertions(+), 178 deletions(-) create mode 100644 plugins/push_notification/test/api/api_test.rb delete mode 100644 plugins/push_notification/test/unit/api_test.rb diff --git a/plugins/push_notification/test/api/api_test.rb b/plugins/push_notification/test/api/api_test.rb new file mode 100644 index 0000000..158b6d6 --- /dev/null +++ b/plugins/push_notification/test/api/api_test.rb @@ -0,0 +1,178 @@ +require_relative '../../../../test/api/test_helper' + +class PushNotificationApiTest < ActiveSupport::TestCase + + def setup + 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 diff --git a/plugins/push_notification/test/unit/api_test.rb b/plugins/push_notification/test/unit/api_test.rb deleted file mode 100644 index 5ac4357..0000000 --- a/plugins/push_notification/test/unit/api_test.rb +++ /dev/null @@ -1,178 +0,0 @@ -require_relative '../../../../test/unit/api/test_helper' - -class PushNotificationApiTest < ActiveSupport::TestCase - - def setup - 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 -- libgit2 0.21.2