From 54d3dbad12408f409037a9aac5a2a9e337207bf7 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Thu, 30 Aug 2018 14:53:39 +0200 Subject: [PATCH] Updated factory & policies for new notification template --- test/factories/checks.rb | 2 +- .../check_notification_policy_test.rb | 35 +++++++++++++++++++ test/policies/notification_policy_test.rb | 28 ++++++++------- 3 files changed, 51 insertions(+), 14 deletions(-) create mode 100644 test/policies/check_notification_policy_test.rb diff --git a/test/factories/checks.rb b/test/factories/checks.rb index 70055d4..c7e5243 100644 --- a/test/factories/checks.rb +++ b/test/factories/checks.rb @@ -86,7 +86,7 @@ FactoryBot.define do trait :with_notifications do after :create do |check| - create_list :notification, 2, check: check + create_list :check_notification, 2, check: check end end end diff --git a/test/policies/check_notification_policy_test.rb b/test/policies/check_notification_policy_test.rb new file mode 100644 index 0000000..318a2fb --- /dev/null +++ b/test/policies/check_notification_policy_test.rb @@ -0,0 +1,35 @@ +# Copyright (C) 2018 Colin Darie , 2018 Evolix +# License: GNU AGPL-3+ (see full text in LICENSE file) + +require "test_helper" + +class CheckNotificationPolicyTest < ActiveSupport::TestCase + setup do + @owner, @other = create_list(:user, 2) + @check_notification = create(:check_notification, check: build(:check, user: @owner)) + end + + test "permit to check user" do + assert_permit @owner, @check_notification, :destroy + end + + test "disallow to anonymous and other user" do + refute_permit @other, @check_notification, :destroy + refute_permit nil, @check_notification, :destroy + end + + test "scope only to user checks" do + other_notifications = create_list(:check_notification, 2, check: build(:check, user: @other)) + + assert_empty Pundit.policy_scope!(nil, CheckNotification) + assert_equal [@check_notification], Pundit.policy_scope!(@owner, CheckNotification) + assert_equal other_notifications, Pundit.policy_scope!(@other, CheckNotification) + end + + test "disabled actions" do + refute_permit @owner, @check_notification, :update + refute_permit @owner, @check_notification, :edit + refute_permit @owner, @check_notification, :create + refute_permit @owner, @check_notification, :index + end +end diff --git a/test/policies/notification_policy_test.rb b/test/policies/notification_policy_test.rb index 688149e..cf9254c 100644 --- a/test/policies/notification_policy_test.rb +++ b/test/policies/notification_policy_test.rb @@ -6,30 +6,32 @@ require "test_helper" class NotificationPolicyTest < ActiveSupport::TestCase setup do @owner, @other = create_list(:user, 2) - @notification = create(:notification, check: build(:check, user: @owner)) + @notification = create(:notification, user: @owner) end - test "permit to check user" do + test "create" do + assert_permit @other, Notification, :create + assert_permit @other, Notification, :new + end + + test "permit to owner" do + assert_permit @owner, @notification, :edit + assert_permit @owner, @notification, :update assert_permit @owner, @notification, :destroy end test "disallow to anonymous and other user" do - refute_permit @other, @notification, :destroy - refute_permit nil, @notification, :destroy + %i[update edit destroy].each do |action| + refute_permit @other, @notification, action + refute_permit nil, @notification, action + end end - test "scope only to user checks" do - other_notifications = create_list(:notification, 2, check: build(:check, user: @other)) + test "scope only to owners" do + other_notifications = create_list(:notification, 2, user: @other) assert_empty Pundit.policy_scope!(nil, Notification) assert_equal [@notification], Pundit.policy_scope!(@owner, Notification) assert_equal other_notifications, Pundit.policy_scope!(@other, Notification) end - - test "disabled actions" do - refute_permit @owner, @notification, :update - refute_permit @owner, @notification, :edit - refute_permit @owner, @notification, :create - refute_permit @owner, @notification, :index - end end