You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 lines
7.4 KiB

  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Admin::ActionLogsHelper, type: :helper do
  4. klass = Class.new do
  5. include ActionView::Helpers
  6. include Admin::ActionLogsHelper
  7. end
  8. let(:hoge) { klass.new }
  9. describe '#log_target' do
  10. after do
  11. hoge.log_target(log)
  12. end
  13. context 'log.target' do
  14. let(:log) { double(target: true) }
  15. it 'calls linkable_log_target' do
  16. expect(hoge).to receive(:linkable_log_target).with(log.target)
  17. end
  18. end
  19. context '!log.target' do
  20. let(:log) { double(target: false, target_type: :type, recorded_changes: :change) }
  21. it 'calls log_target_from_history' do
  22. expect(hoge).to receive(:log_target_from_history).with(log.target_type, log.recorded_changes)
  23. end
  24. end
  25. end
  26. describe '#relevant_log_changes' do
  27. let(:log) { double(target_type: target_type, action: log_action, recorded_changes: recorded_changes) }
  28. let(:recorded_changes) { double }
  29. after do
  30. hoge.relevant_log_changes(log)
  31. end
  32. context "log.target_type == 'CustomEmoji' && [:enable, :disable, :destroy].include?(log.action)" do
  33. let(:target_type) { 'CustomEmoji' }
  34. let(:log_action) { :enable }
  35. it "calls log.recorded_changes.slice('domain')" do
  36. expect(recorded_changes).to receive(:slice).with('domain')
  37. end
  38. end
  39. context "log.target_type == 'CustomEmoji' && log.action == :update" do
  40. let(:target_type) { 'CustomEmoji' }
  41. let(:log_action) { :update }
  42. it "calls log.recorded_changes.slice('domain', 'visible_in_picker')" do
  43. expect(recorded_changes).to receive(:slice).with('domain', 'visible_in_picker')
  44. end
  45. end
  46. context "log.target_type == 'User' && [:promote, :demote].include?(log.action)" do
  47. let(:target_type) { 'User' }
  48. let(:log_action) { :promote }
  49. it "calls log.recorded_changes.slice('moderator', 'admin')" do
  50. expect(recorded_changes).to receive(:slice).with('moderator', 'admin')
  51. end
  52. end
  53. context "log.target_type == 'User' && [:change_email].include?(log.action)" do
  54. let(:target_type) { 'User' }
  55. let(:log_action) { :change_email }
  56. it "calls log.recorded_changes.slice('email', 'unconfirmed_email')" do
  57. expect(recorded_changes).to receive(:slice).with('email', 'unconfirmed_email')
  58. end
  59. end
  60. context "log.target_type == 'DomainBlock'" do
  61. let(:target_type) { 'DomainBlock' }
  62. let(:log_action) { nil }
  63. it "calls log.recorded_changes.slice('severity', 'reject_media')" do
  64. expect(recorded_changes).to receive(:slice).with('severity', 'reject_media')
  65. end
  66. end
  67. context "log.target_type == 'Status' && log.action == :update" do
  68. let(:target_type) { 'Status' }
  69. let(:log_action) { :update }
  70. it "log.recorded_changes.slice('sensitive')" do
  71. expect(recorded_changes).to receive(:slice).with('sensitive')
  72. end
  73. end
  74. end
  75. describe '#log_extra_attributes' do
  76. after do
  77. hoge.log_extra_attributes(hoge: 'hoge')
  78. end
  79. it "calls content_tag(:span, key, class: 'diff-key')" do
  80. allow(hoge).to receive(:log_change).with(anything)
  81. expect(hoge).to receive(:content_tag).with(:span, :hoge, class: 'diff-key')
  82. end
  83. it 'calls safe_join twice' do
  84. expect(hoge).to receive(:safe_join).with(
  85. ['<span class="diff-key">hoge</span>',
  86. '=',
  87. '<span class="diff-neutral">hoge</span>']
  88. )
  89. expect(hoge).to receive(:safe_join).with([nil], ' ')
  90. end
  91. end
  92. describe '#log_change' do
  93. after do
  94. hoge.log_change(val)
  95. end
  96. context '!val.is_a?(Array)' do
  97. let(:val) { 'hoge' }
  98. it "calls content_tag(:span, val, class: 'diff-neutral')" do
  99. expect(hoge).to receive(:content_tag).with(:span, val, class: 'diff-neutral')
  100. end
  101. end
  102. context 'val.is_a?(Array)' do
  103. let(:val) { %w(foo bar) }
  104. it 'calls #content_tag twice and #safe_join' do
  105. expect(hoge).to receive(:content_tag).with(:span, 'foo', class: 'diff-old')
  106. expect(hoge).to receive(:content_tag).with(:span, 'bar', class: 'diff-new')
  107. expect(hoge).to receive(:safe_join).with([nil, nil], '→')
  108. end
  109. end
  110. end
  111. describe '#icon_for_log' do
  112. subject { hoge.icon_for_log(log) }
  113. context "log.target_type == 'Account'" do
  114. let(:log) { double(target_type: 'Account') }
  115. it 'returns "user"' do
  116. expect(subject).to be 'user'
  117. end
  118. end
  119. context "log.target_type == 'User'" do
  120. let(:log) { double(target_type: 'User') }
  121. it 'returns "user"' do
  122. expect(subject).to be 'user'
  123. end
  124. end
  125. context "log.target_type == 'CustomEmoji'" do
  126. let(:log) { double(target_type: 'CustomEmoji') }
  127. it 'returns "file"' do
  128. expect(subject).to be 'file'
  129. end
  130. end
  131. context "log.target_type == 'Report'" do
  132. let(:log) { double(target_type: 'Report') }
  133. it 'returns "flag"' do
  134. expect(subject).to be 'flag'
  135. end
  136. end
  137. context "log.target_type == 'DomainBlock'" do
  138. let(:log) { double(target_type: 'DomainBlock') }
  139. it 'returns "lock"' do
  140. expect(subject).to be 'lock'
  141. end
  142. end
  143. context "log.target_type == 'EmailDomainBlock'" do
  144. let(:log) { double(target_type: 'EmailDomainBlock') }
  145. it 'returns "envelope"' do
  146. expect(subject).to be 'envelope'
  147. end
  148. end
  149. context "log.target_type == 'Status'" do
  150. let(:log) { double(target_type: 'Status') }
  151. it 'returns "pencil"' do
  152. expect(subject).to be 'pencil'
  153. end
  154. end
  155. end
  156. describe '#class_for_log_icon' do
  157. subject { hoge.class_for_log_icon(log) }
  158. %i(enable unsuspend unsilence confirm promote resolve).each do |action|
  159. context "log.action == #{action}" do
  160. let(:log) { double(action: action) }
  161. it 'returns "positive"' do
  162. expect(subject).to be 'positive'
  163. end
  164. end
  165. end
  166. context 'log.action == :create' do
  167. context 'opposite_verbs?(log)' do
  168. let(:log) { double(action: :create, target_type: 'DomainBlock') }
  169. it 'returns "negative"' do
  170. expect(subject).to be 'negative'
  171. end
  172. end
  173. context '!opposite_verbs?(log)' do
  174. let(:log) { double(action: :create, target_type: '') }
  175. it 'returns "positive"' do
  176. expect(subject).to be 'positive'
  177. end
  178. end
  179. end
  180. %i(update reset_password disable_2fa memorialize change_email).each do |action|
  181. context "log.action == #{action}" do
  182. let(:log) { double(action: action) }
  183. it 'returns "neutral"' do
  184. expect(subject).to be 'neutral'
  185. end
  186. end
  187. end
  188. %i(demote silence disable suspend remove_avatar remove_header reopen).each do |action|
  189. context "log.action == #{action}" do
  190. let(:log) { double(action: action) }
  191. it 'returns "negative"' do
  192. expect(subject).to be 'negative'
  193. end
  194. end
  195. end
  196. context 'log.action == :destroy' do
  197. context 'opposite_verbs?(log)' do
  198. let(:log) { double(action: :destroy, target_type: 'DomainBlock') }
  199. it 'returns "positive"' do
  200. expect(subject).to be 'positive'
  201. end
  202. end
  203. context '!opposite_verbs?(log)' do
  204. let(:log) { double(action: :destroy, target_type: '') }
  205. it 'returns "negative"' do
  206. expect(subject).to be 'negative'
  207. end
  208. end
  209. end
  210. end
  211. end