about summary refs log tree commit diff
path: root/spec/helpers/admin/action_log_helper_spec.rb
blob: d7af6b939e6ae3d1d6b837b42685033635d4c5de (plain) (blame)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Admin::ActionLogsHelper, type: :helper do
  klass = Class.new do
    include ActionView::Helpers
    include Admin::ActionLogsHelper
  end

  let(:hoge) { klass.new }

  describe '#log_target' do
    after do
      hoge.log_target(log)
    end

    context 'log.target' do
      let(:log) { double(target: true) }

      it 'calls linkable_log_target' do
        expect(hoge).to receive(:linkable_log_target).with(log.target)
      end
    end

    context '!log.target' do
      let(:log) { double(target: false, target_type: :type, recorded_changes: :change) }

      it 'calls log_target_from_history' do
        expect(hoge).to receive(:log_target_from_history).with(log.target_type, log.recorded_changes)
      end
    end
  end

  describe '#relevant_log_changes' do
    let(:log) { double(target_type: target_type, action: log_action, recorded_changes: recorded_changes) }
    let(:recorded_changes) { double }

    after do
      hoge.relevant_log_changes(log)
    end

    context "log.target_type == 'CustomEmoji' && [:enable, :disable, :destroy].include?(log.action)" do
      let(:target_type) { 'CustomEmoji' }
      let(:log_action)  { :enable }

      it "calls log.recorded_changes.slice('domain')" do
        expect(recorded_changes).to receive(:slice).with('domain')
      end
    end

    context "log.target_type == 'CustomEmoji' && log.action == :update" do
      let(:target_type) { 'CustomEmoji' }
      let(:log_action)  { :update }

      it "calls log.recorded_changes.slice('domain', 'visible_in_picker')" do
        expect(recorded_changes).to receive(:slice).with('domain', 'visible_in_picker')
      end
    end

    context "log.target_type == 'User' && [:promote, :demote].include?(log.action)" do
      let(:target_type) { 'User' }
      let(:log_action)  { :promote }

      it "calls log.recorded_changes.slice('moderator', 'admin')" do
        expect(recorded_changes).to receive(:slice).with('moderator', 'admin')
      end
    end

    context "log.target_type == 'User' && [:change_email].include?(log.action)" do
      let(:target_type) { 'User' }
      let(:log_action)  { :change_email }

      it "calls log.recorded_changes.slice('email', 'unconfirmed_email')" do
        expect(recorded_changes).to receive(:slice).with('email', 'unconfirmed_email')
      end
    end

    context "log.target_type == 'DomainBlock'" do
      let(:target_type) { 'DomainBlock' }
      let(:log_action)  { nil }

      it "calls log.recorded_changes.slice('severity', 'reject_media')" do
        expect(recorded_changes).to receive(:slice).with('severity', 'reject_media')
      end
    end

    context "log.target_type == 'Status' && log.action == :update" do
      let(:target_type) { 'Status' }
      let(:log_action)  { :update }

      it "log.recorded_changes.slice('sensitive')" do
        expect(recorded_changes).to receive(:slice).with('sensitive')
      end
    end
  end

  describe '#log_extra_attributes' do
    after do
      hoge.log_extra_attributes(hoge: 'hoge')
    end

    it "calls content_tag(:span, key, class: 'diff-key')" do
      allow(hoge).to receive(:log_change).with(anything)
      expect(hoge).to receive(:content_tag).with(:span, :hoge, class: 'diff-key')
    end

    it 'calls safe_join twice' do
      expect(hoge).to receive(:safe_join).with(
        ['<span class="diff-key">hoge</span>',
         '=',
         '<span class="diff-neutral">hoge</span>']
      )

      expect(hoge).to receive(:safe_join).with([nil], ' ')
    end
  end

  describe '#log_change' do
    after do
      hoge.log_change(val)
    end

    context '!val.is_a?(Array)' do
      let(:val) { 'hoge' }

      it "calls content_tag(:span, val, class: 'diff-neutral')" do
        expect(hoge).to receive(:content_tag).with(:span, val, class: 'diff-neutral')
      end
    end

    context 'val.is_a?(Array)' do
      let(:val) { %w(foo bar) }

      it 'calls #content_tag twice and #safe_join' do
        expect(hoge).to receive(:content_tag).with(:span, 'foo', class: 'diff-old')
        expect(hoge).to receive(:content_tag).with(:span, 'bar', class: 'diff-new')
        expect(hoge).to receive(:safe_join).with([nil, nil], '→')
      end
    end
  end

  describe '#icon_for_log' do
    subject   { hoge.icon_for_log(log) }

    context "log.target_type == 'Account'" do
      let(:log) { double(target_type: 'Account') }

      it 'returns "user"' do
        expect(subject).to be 'user'
      end
    end

    context "log.target_type == 'User'" do
      let(:log) { double(target_type: 'User') }

      it 'returns "user"' do
        expect(subject).to be 'user'
      end
    end

    context "log.target_type == 'CustomEmoji'" do
      let(:log) { double(target_type: 'CustomEmoji') }

      it 'returns "file"' do
        expect(subject).to be 'file'
      end
    end

    context "log.target_type == 'Report'" do
      let(:log) { double(target_type: 'Report') }

      it 'returns "flag"' do
        expect(subject).to be 'flag'
      end
    end

    context "log.target_type == 'DomainBlock'" do
      let(:log) { double(target_type: 'DomainBlock') }

      it 'returns "lock"' do
        expect(subject).to be 'lock'
      end
    end

    context "log.target_type == 'EmailDomainBlock'" do
      let(:log) { double(target_type: 'EmailDomainBlock') }

      it 'returns "envelope"' do
        expect(subject).to be 'envelope'
      end
    end

    context "log.target_type == 'Status'" do
      let(:log) { double(target_type: 'Status') }

      it 'returns "pencil"' do
        expect(subject).to be 'pencil'
      end
    end
  end

  describe '#class_for_log_icon' do
    subject   { hoge.class_for_log_icon(log) }

    %i(enable unsuspend unsilence confirm promote resolve).each do |action|
      context "log.action == #{action}" do
        let(:log) { double(action: action) }

        it 'returns "positive"' do
          expect(subject).to be 'positive'
        end
      end
    end

    context 'log.action == :create' do
      context 'opposite_verbs?(log)' do
        let(:log) { double(action: :create, target_type: 'DomainBlock') }

        it 'returns "negative"' do
          expect(subject).to be 'negative'
        end
      end

      context '!opposite_verbs?(log)' do
        let(:log) { double(action: :create, target_type: '') }

        it 'returns "positive"' do
          expect(subject).to be 'positive'
        end
      end
    end

    %i(update reset_password disable_2fa memorialize change_email).each do |action|
      context "log.action == #{action}" do
        let(:log) { double(action: action) }

        it 'returns "neutral"' do
          expect(subject).to be 'neutral'
        end
      end
    end

    %i(demote silence disable suspend remove_avatar remove_header reopen).each do |action|
      context "log.action == #{action}" do
        let(:log) { double(action: action) }

        it 'returns "negative"' do
          expect(subject).to be 'negative'
        end
      end
    end

    context 'log.action == :destroy' do
      context 'opposite_verbs?(log)' do
        let(:log) { double(action: :destroy, target_type: 'DomainBlock') }

        it 'returns "positive"' do
          expect(subject).to be 'positive'
        end
      end

      context '!opposite_verbs?(log)' do
        let(:log) { double(action: :destroy, target_type: '') }

        it 'returns "negative"' do
          expect(subject).to be 'negative'
        end
      end
    end
  end
end