copy_tuner_client 3.0.0 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fc7ea8326b6b40250965bd4e76dc2d8e598047ba7d91ff4827ba46cd6e71a530
4
- data.tar.gz: 6f609f48108914b7d66fc14b27b1c0c88d211d2496a2f4d95605ff9d42873c15
3
+ metadata.gz: 56cadc4d0af47b6c281636ddcb073ff73893286255dd01e5e2dd10cd3ba95d5c
4
+ data.tar.gz: 7d85dbdc9264cf68f1caaa246c3cacd5a3de905bdaa879b546c639f0f39d4f87
5
5
  SHA512:
6
- metadata.gz: d20c645325447d51131bce8cd08f99ebb1456958d65e79d1da7e6236902439c182c529c017d67cedd47cbb67c459105ad3d56dcc21d3d84934867b1edabdd5fb
7
- data.tar.gz: 6abdb7682094ce5d247039b68bc1b64a81f27c554f4f3dfe31cbae4d158fb4c75900fe5bac33570dfbc9e9bf55e37c5b528607e9e27816a37e96d07f12f43973
6
+ metadata.gz: 0416246174f3dc6672e59f7ac513466066b2625568ea1b2b7017f02e5390bd1b9aff382c6a0334cfe17190699efcdc8b7366fbd2e09806723afba3a585057532
7
+ data.tar.gz: f0c1f258bcbb60b024b13a9cc0024bc872d17bd3d5e116bda7d55e3dabed5dc6294a13671a786acf8332a11781caf131053d877bd7a1c0f7da6c7dd6e0e59400
@@ -7,6 +7,31 @@ module CopyTunerClient
7
7
  # 「この描画の出力が middleware を通るか」で判定するため、middleware が env にこの印を立てる。
8
8
  ENV_KEY = 'copy_tuner.copyray_injectable'.freeze
9
9
 
10
+ # NOTE: HTML 以外の経路(メール本文・render :json・CSV/PDF など)ではマーカーが文字列として
11
+ # 出力に混入するため注入しない。判定には controller.request.format を使い、@current_template.format /
12
+ # lookup_context.formats のような ActionView の内部実装には依存させない(Rails バージョン間で壊れうるため)。
13
+ def self.injectable?(controller)
14
+ return false if CopyTunerClient.configuration.disable_copyray_comment_injection
15
+ return false if controller.nil?
16
+
17
+ # NOTE: mailer は request を持たず request.format で判定できない。かつメール本文への
18
+ # マーカー混入は実害が大きいため、controller の型で明示除外する。
19
+ return false if defined?(ActionMailer::Base) && controller.is_a?(ActionMailer::Base)
20
+
21
+ rewritable_request?(controller.request)
22
+ end
23
+
24
+ def self.rewritable_request?(request)
25
+ # NOTE: format が html でも出力が CopyrayMiddleware を通らない経路(ApplicationController.renderer や
26
+ # render_to_string)ではマーカーが除去されずに残るため、middleware が立てた印がある場合に限り注入する。
27
+ return false unless request&.env&.[](ENV_KEY)
28
+
29
+ # NOTE: middleware を通っても render json: などは rewritable? に弾かれ書き換えられないため、
30
+ # format が html でない経路には注入しない。
31
+ request.format&.html? || false
32
+ end
33
+ private_class_method :rewritable_request?
34
+
10
35
  # This:
11
36
  # message
12
37
  # Becomes:
@@ -84,7 +84,7 @@ module CopyTunerClient
84
84
  end
85
85
 
86
86
  def rewritable?(status, headers)
87
- # NOTE: マーカーの注入側(copyray_injectable?)はステータスを見ないので、除去側もステータスで絞らない
87
+ # NOTE: マーカーの注入側(Copyray.injectable?)はステータスを見ないので、除去側もステータスで絞らない
88
88
  # (絞るとリダイレクトやエラーページ(429 など)でマーカーが画面に露出する)。
89
89
  # ボディを持たないステータスは書き換えで Content-Length が付くと Rack の仕様違反になるので除外する。
90
90
  !Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.key?(status) &&
@@ -23,13 +23,9 @@ module CopyTunerClient
23
23
  # 使われる。HTML 以外の経路(メール本文・render :json・CSV/PDF など)ではコメントが文字列として
24
24
  # 出力に混入してしまうため、それらの経路には注入しない。default 引数による初期値登録は維持する
25
25
  # 必要があるため、このガードは初期値登録(上の I18n.t 呼び出し)より後に置く。
26
- return source unless copyray_injectable?
26
+ return source unless CopyTunerClient::Copyray.injectable?(controller)
27
27
 
28
- if CopyTunerClient.configuration.disable_copyray_comment_injection
29
- source
30
- else
31
- CopyTunerClient::Copyray.augment_template(source, copyray_scope_key(key, options))
32
- end
28
+ CopyTunerClient::Copyray.augment_template(source, copyray_scope_key(key, options))
33
29
  end
34
30
 
35
31
  def copyray_scope_key(key, options)
@@ -40,33 +36,6 @@ module CopyTunerClient
40
36
  I18n.normalize_keys(nil, key, options[:scope], separator).compact.join(separator)
41
37
  end
42
38
  private :copyray_scope_key
43
-
44
- # NOTE: HTML 以外の経路(メール本文・render :json・CSV/PDF など)ではマーカーが文字列として
45
- # 出力に混入するため注入しない。判定には controller.request.format を使い、@current_template.format /
46
- # lookup_context.formats のような ActionView の内部実装には依存させない(Rails バージョン間で壊れうるため)。
47
- def copyray_injectable?
48
- current_controller = controller
49
- return false if current_controller.nil?
50
-
51
- # NOTE: mailer は request を持たず request.format で判定できない。かつメール本文への
52
- # マーカー混入は実害が大きいため、controller の型で明示除外する。
53
- return false if defined?(ActionMailer::Base) && current_controller.is_a?(ActionMailer::Base)
54
-
55
- request = current_controller.request
56
- return false unless passes_copyray_middleware?(request)
57
-
58
- # NOTE: middleware を通っても render json: などは rewritable? に弾かれ書き換えられないため、
59
- # format が html でない経路には注入しない。
60
- request.format&.html? || false
61
- end
62
- private :copyray_injectable?
63
-
64
- # NOTE: format が html でも出力が CopyrayMiddleware を通らない経路(ApplicationController.renderer や
65
- # render_to_string)ではマーカーが除去されずに残るため、middleware が立てた印がある場合に限り注入する。
66
- def passes_copyray_middleware?(request)
67
- request&.env&.[](CopyTunerClient::Copyray::ENV_KEY)
68
- end
69
- private :passes_copyray_middleware?
70
39
  end
71
40
  private_constant :CopyrayCommentInjection
72
41
 
@@ -14,7 +14,9 @@ if defined?(SimpleForm)
14
14
  def label_translation_with_copyray_comment
15
15
  source = label_translation_without_copyray_comment
16
16
 
17
- if !CopyTunerClient.configuration.disable_copyray_comment_injection && object.class.respond_to?(:lookup_ancestors)
17
+ # NOTE: translate と同じ Copyray.injectable? で注入可否を決める。jQuery で画面を差し替える JS 応答などは
18
+ # CopyrayMiddleware の書き換え対象外で、注入するとマーカーが除去されずに画面に出るため。
19
+ if object.class.respond_to?(:lookup_ancestors) && CopyTunerClient::Copyray.injectable?(template.controller)
18
20
  attributes_scope = "#{object.class.i18n_scope}.attributes"
19
21
  defaults =
20
22
  object.class.lookup_ancestors.map do |klass|
@@ -1,6 +1,6 @@
1
1
  module CopyTunerClient
2
2
  # Client version
3
- VERSION = '3.0.0'.freeze
3
+ VERSION = '3.0.1'.freeze
4
4
 
5
5
  # API version being used to communicate with the server
6
6
  API_VERSION = '2.0'.freeze
@@ -2,6 +2,68 @@ require 'spec_helper'
2
2
  require 'copy_tuner_client/copyray'
3
3
 
4
4
  describe CopyTunerClient::Copyray do
5
+ # NOTE: request.format で描画フォーマットを判定するため、format を差し替えられる
6
+ # 最小のフェイク request / controller を用意する。mailer 判定は controller の型で行う。
7
+ let(:format_class) do
8
+ Struct.new(:type) do
9
+ def html?
10
+ type == :html
11
+ end
12
+ end
13
+ end
14
+ let(:request_class) { Struct.new(:format, :env) }
15
+ let(:controller_class) { Struct.new(:request) }
16
+
17
+ # NOTE: CopyrayMiddleware を通るリクエストを再現するため、フラグ付きの env を持つ request を作る。
18
+ def injectable_request(format = :html)
19
+ request_class.new(format_class.new(format), { described_class::ENV_KEY => true })
20
+ end
21
+
22
+ describe '.injectable?' do
23
+ it 'request.format が html のとき true を返す' do
24
+ expect(described_class.injectable?(controller_class.new(injectable_request))).to be true
25
+ end
26
+
27
+ %i[json text csv pdf].each do |format|
28
+ it "request.format が :#{format} のとき false を返す" do
29
+ expect(described_class.injectable?(controller_class.new(injectable_request(format)))).to be false
30
+ end
31
+ end
32
+
33
+ it 'disable_copyray_comment_injection が有効なとき false を返す' do
34
+ CopyTunerClient.configuration.disable_copyray_comment_injection = true
35
+ expect(described_class.injectable?(controller_class.new(injectable_request))).to be false
36
+ end
37
+
38
+ it 'format が html でも env にフラグが無ければ false を返す' do
39
+ controller = controller_class.new(request_class.new(format_class.new(:html), {}))
40
+ expect(described_class.injectable?(controller)).to be false
41
+ end
42
+
43
+ it 'request.env が nil でも落ちずに false を返す' do
44
+ controller = controller_class.new(request_class.new(format_class.new(:html), nil))
45
+ expect(described_class.injectable?(controller)).to be false
46
+ end
47
+
48
+ it 'mailer から描画されたとき false を返す' do
49
+ stub_const('ActionMailer::Base', Class.new)
50
+ expect(described_class.injectable?(ActionMailer::Base.new)).to be false
51
+ end
52
+
53
+ it 'controller が nil のとき false を返す' do
54
+ expect(described_class.injectable?(nil)).to be false
55
+ end
56
+
57
+ it 'controller が request を持たないとき false を返す' do
58
+ expect(described_class.injectable?(controller_class.new(nil))).to be false
59
+ end
60
+
61
+ it 'ActionMailer が未ロードでも落ちない' do
62
+ hide_const('ActionMailer::Base') if defined?(ActionMailer::Base)
63
+ expect { described_class.injectable?(controller_class.new(injectable_request)) }.not_to raise_error
64
+ end
65
+ end
66
+
5
67
  describe '.augment_template' do
6
68
  subject(:augmented) { described_class.augment_template(source, key) }
7
69
 
@@ -82,51 +82,18 @@ describe CopyTunerClient::HelperExtension do
82
82
  expect(view.translate('views.foo', name: 'World')).to eq 'Hello, World'
83
83
  end
84
84
 
85
- context 'injection guard by rendering context' do
86
- it 'injects the marker when request.format is :html' do
85
+ # NOTE: 注入可否の判定そのものは Copyray.injectable? の spec で検証済みなので、
86
+ # ここでは translate がその結果に従うことだけを見る。
87
+ context '注入ガード' do
88
+ it 'Copyray.injectable? が true のときマーカーを注入する' do
89
+ allow(CopyTunerClient::Copyray).to receive(:injectable?).with(view.controller).and_return(true)
87
90
  expect(view.translate('some.key', name: 'World')).to eq '⟦CT:some.key⟧Hello, World'
88
91
  end
89
92
 
90
- %i[json text csv pdf].each do |format|
91
- it "does not inject the marker when request.format is :#{format}" do
92
- view.controller = controller_class.new(injectable_request(format))
93
- expect(view.translate('some.key', name: 'World')).to eq 'Hello, World'
94
- end
95
- end
96
-
97
- it 'format が html でも env にフラグが無ければ注入しない' do
98
- view.controller = controller_class.new(request_class.new(format_class.new(:html), {}))
99
- expect(view.translate('some.key', name: 'World')).to eq 'Hello, World'
100
- end
101
-
102
- it 'request.env が nil でも落ちずに注入しない' do
103
- view.controller = controller_class.new(request_class.new(format_class.new(:html), nil))
104
- expect(view.translate('some.key', name: 'World')).to eq 'Hello, World'
105
- end
106
- end
107
-
108
- context 'injection guard by controller' do
109
- it 'does not inject the marker when rendered by a mailer' do
110
- stub_const('ActionMailer::Base', Class.new)
111
- view.controller = ActionMailer::Base.new
93
+ it 'Copyray.injectable? が false のときマーカーを注入しない' do
94
+ allow(CopyTunerClient::Copyray).to receive(:injectable?).with(view.controller).and_return(false)
112
95
  expect(view.translate('some.key', name: 'World')).to eq 'Hello, World'
113
96
  end
114
-
115
- it 'does not inject the marker when controller is nil' do
116
- view.controller = nil
117
- expect(view.translate('some.key', name: 'World')).to eq 'Hello, World'
118
- end
119
-
120
- it 'does not inject the marker when the controller has no request' do
121
- view.controller = controller_class.new(nil)
122
- expect(view.translate('some.key', name: 'World')).to eq 'Hello, World'
123
- end
124
-
125
- it 'does not raise when ActionMailer is not loaded' do
126
- hide_const('ActionMailer::Base') if defined?(ActionMailer::Base)
127
- view.controller = controller_class.new(injectable_request)
128
- expect { view.translate('some.key', name: 'World') }.not_to raise_error
129
- end
130
97
  end
131
98
 
132
99
  # NOTE: マーカー注入を抑止する非 HTML 経路でも、default 引数による初期値登録(I18n.t 呼び出し)は
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+ require 'active_model'
3
+ require 'copy_tuner_client/copyray'
4
+
5
+ # NOTE: simple_form は開発依存に含めていないため、ラベル生成部品(SimpleForm::Components::Labels)を
6
+ # 最小のフェイクで用意し、そこへ拡張を読み込ませて注入ガードだけを検証する。
7
+ describe 'SimpleForm::Components::Labels の Copyray 拡張' do # rubocop:disable RSpec/DescribeClass
8
+ let(:labels_module) do
9
+ Module.new do
10
+ def label_translation
11
+ '体温'
12
+ end
13
+ end
14
+ end
15
+
16
+ let(:model_class) do
17
+ Class.new do
18
+ include ActiveModel::Model
19
+
20
+ def self.name
21
+ 'BodyTemperatureRecord'
22
+ end
23
+ end
24
+ end
25
+
26
+ let(:input_class) do
27
+ labels = labels_module
28
+ Struct.new(:object, :template) do
29
+ include labels
30
+
31
+ def reflection_or_attribute_name
32
+ :value
33
+ end
34
+ end
35
+ end
36
+
37
+ let(:controller) { Object.new }
38
+ let(:template) { Struct.new(:controller).new(controller) }
39
+
40
+ # NOTE: 注入可否の判定そのものは Copyray.injectable? の spec で検証済みなので、
41
+ # ここではその結果に従うことだけを見る。
42
+ def label_for
43
+ input_class.new(model_class.new, template).__send__(:label_translation)
44
+ end
45
+
46
+ before do
47
+ stub_const('SimpleForm::Components::Labels', labels_module)
48
+ allow(CopyTunerClient.configuration).to receive(:enable_middleware?).and_return(true)
49
+ load File.join(PROJECT_ROOT, 'lib', 'copy_tuner_client', 'simple_form_extention.rb')
50
+ end
51
+
52
+ it 'Copyray.injectable? が true のときはラベルにマーカーを注入する' do
53
+ allow(CopyTunerClient::Copyray).to receive(:injectable?).with(controller).and_return(true)
54
+ expect(label_for).to eq '⟦CT:activemodel.attributes.body_temperature_record.value⟧体温'
55
+ end
56
+
57
+ it 'Copyray.injectable? が false のとき(JS 応答など)はラベルにマーカーを注入しない' do
58
+ allow(CopyTunerClient::Copyray).to receive(:injectable?).with(controller).and_return(false)
59
+ expect(label_for).to eq '体温'
60
+ end
61
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: copy_tuner_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - SonicGarden
@@ -260,6 +260,7 @@ files:
260
260
  - spec/copy_tuner_client/process_guard_spec.rb
261
261
  - spec/copy_tuner_client/queue_with_timeout_spec.rb
262
262
  - spec/copy_tuner_client/request_sync_spec.rb
263
+ - spec/copy_tuner_client/simple_form_extention_spec.rb
263
264
  - spec/copy_tuner_client/translation_log_spec.rb
264
265
  - spec/copy_tuner_client/warden_integration_spec.rb
265
266
  - spec/copy_tuner_client_spec.rb