indexmap 0.6.0 → 0.7.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.
@@ -28,167 +28,145 @@ class IndexmapPingerGoogleTest < Minitest::Test
28
28
  end
29
29
 
30
30
  def test_pings_google_for_each_sitemap_file
31
- Dir.mktmpdir do |dir|
32
- public_path = Pathname(dir)
33
- public_path.join("sitemap.xml").write("<sitemapindex/>")
34
-
35
- configuration = Indexmap::Configuration.new
36
- configuration.base_url = "https://www.example.com"
37
- configuration.public_path = public_path
38
- configuration.google.credentials = "{\"type\":\"service_account\"}"
39
-
40
- service = FakeWebmastersService.new(site_urls: ["sc-domain:example.com"])
41
- builder_calls = []
42
- credentials_builder = lambda do |credentials:, scope:|
43
- builder_calls << [credentials, scope]
44
- :fake_authorizer
45
- end
46
-
47
- result = Indexmap::Pinger::Google.new(
48
- configuration: configuration,
49
- service: service,
50
- credentials_builder: credentials_builder
51
- ).ping
52
-
53
- assert_equal [["{\"type\":\"service_account\"}", "https://www.googleapis.com/auth/webmasters"]], builder_calls
54
- assert_equal :fake_authorizer, service.authorization
55
- assert_equal ["sc-domain:example.com", "https://www.example.com/sitemap.xml"], service.submitted
56
- assert_equal :submitted, result[:status]
57
- assert_equal 1, result[:sitemap_count]
58
- assert_equal 0, result[:url_count]
59
- assert_equal 1, service.list_sites_calls
31
+ configuration = configuration_with(storage: storage_with("sitemap.xml" => "<sitemapindex/>"))
32
+ configuration.google.credentials = "{\"type\":\"service_account\"}"
33
+
34
+ service = FakeWebmastersService.new(site_urls: ["sc-domain:example.com"])
35
+ builder_calls = []
36
+ credentials_builder = lambda do |credentials:, scope:|
37
+ builder_calls << [credentials, scope]
38
+ :fake_authorizer
60
39
  end
40
+
41
+ result = Indexmap::Pinger::Google.new(
42
+ configuration: configuration,
43
+ service: service,
44
+ credentials_builder: credentials_builder
45
+ ).ping
46
+
47
+ assert_equal [["{\"type\":\"service_account\"}", "https://www.googleapis.com/auth/webmasters"]], builder_calls
48
+ assert_equal :fake_authorizer, service.authorization
49
+ assert_equal ["sc-domain:example.com", "https://www.example.com/sitemap.xml"], service.submitted
50
+ assert_equal :submitted, result[:status]
51
+ assert_equal 1, result[:sitemap_count]
52
+ assert_equal 0, result[:url_count]
53
+ assert_equal 1, service.list_sites_calls
61
54
  end
62
55
 
63
56
  def test_reports_unique_url_count_from_submitted_sitemaps
64
- Dir.mktmpdir do |dir|
65
- public_path = Pathname(dir)
66
- public_path.join("sitemap.xml").write(<<~XML)
57
+ storage = storage_with(
58
+ "sitemap.xml" => <<~XML,
67
59
  <?xml version="1.0" encoding="UTF-8"?>
68
60
  <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
69
61
  <sitemap><loc>https://www.example.com/sitemap-pages.xml</loc></sitemap>
70
62
  <sitemap><loc>https://www.example.com/sitemap-posts.xml</loc></sitemap>
71
63
  </sitemapindex>
72
64
  XML
73
- public_path.join("sitemap-pages.xml").write(<<~XML)
65
+ "sitemap-pages.xml" => <<~XML,
74
66
  <?xml version="1.0" encoding="UTF-8"?>
75
67
  <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
76
68
  <url><loc>https://www.example.com/</loc></url>
77
69
  <url><loc>https://www.example.com/about</loc></url>
78
70
  </urlset>
79
71
  XML
80
- public_path.join("sitemap-posts.xml").write(<<~XML)
72
+ "sitemap-posts.xml" => <<~XML
81
73
  <?xml version="1.0" encoding="UTF-8"?>
82
74
  <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
83
75
  <url><loc>https://www.example.com/about</loc></url>
84
76
  <url><loc>https://www.example.com/blog</loc></url>
85
77
  </urlset>
86
78
  XML
87
-
88
- configuration = Indexmap::Configuration.new
89
- configuration.base_url = "https://www.example.com"
90
- configuration.public_path = public_path
91
- configuration.google.credentials = "{\"type\":\"service_account\"}"
92
-
93
- service = FakeWebmastersService.new(site_urls: ["sc-domain:example.com"])
94
- result = Indexmap::Pinger::Google.new(
95
- configuration: configuration,
96
- service: service,
97
- credentials_builder: ->(**) { :fake_authorizer }
98
- ).ping
99
-
100
- assert_equal :submitted, result[:status]
101
- assert_equal 3, result[:sitemap_count]
102
- assert_equal 3, result[:url_count]
103
- assert_equal [
104
- ["sc-domain:example.com", "https://www.example.com/sitemap-pages.xml"],
105
- ["sc-domain:example.com", "https://www.example.com/sitemap-posts.xml"],
106
- ["sc-domain:example.com", "https://www.example.com/sitemap.xml"]
107
- ], service.submissions
108
- end
79
+ )
80
+ configuration = configuration_with(storage: storage)
81
+ configuration.google.credentials = "{\"type\":\"service_account\"}"
82
+
83
+ service = FakeWebmastersService.new(site_urls: ["sc-domain:example.com"])
84
+ result = Indexmap::Pinger::Google.new(
85
+ configuration: configuration,
86
+ service: service,
87
+ credentials_builder: ->(**) { :fake_authorizer }
88
+ ).ping
89
+
90
+ assert_equal :submitted, result[:status]
91
+ assert_equal 3, result[:sitemap_count]
92
+ assert_equal 3, result[:url_count]
93
+ assert_equal [
94
+ ["sc-domain:example.com", "https://www.example.com/sitemap-pages.xml"],
95
+ ["sc-domain:example.com", "https://www.example.com/sitemap-posts.xml"],
96
+ ["sc-domain:example.com", "https://www.example.com/sitemap.xml"]
97
+ ], service.submissions
109
98
  end
110
99
 
111
100
  def test_skips_google_ping_when_credentials_are_missing
112
- Dir.mktmpdir do |dir|
113
- public_path = Pathname(dir)
114
- public_path.join("sitemap.xml").write("<sitemapindex/>")
115
-
116
- configuration = Indexmap::Configuration.new
117
- configuration.base_url = "https://www.example.com"
118
- configuration.public_path = public_path
101
+ configuration = configuration_with(storage: storage_with("sitemap.xml" => "<sitemapindex/>"))
102
+ service = FakeWebmastersService.new(site_urls: ["sc-domain:example.com"])
119
103
 
120
- service = FakeWebmastersService.new(site_urls: ["sc-domain:example.com"])
104
+ result = Indexmap::Pinger::Google.new(configuration: configuration, service: service).ping
121
105
 
122
- result = Indexmap::Pinger::Google.new(configuration: configuration, service: service).ping
123
-
124
- assert_nil service.submitted
125
- assert_equal({status: :skipped, reason: :missing_credentials}, result)
126
- end
106
+ assert_nil service.submitted
107
+ assert_equal({status: :skipped, reason: :missing_credentials}, result)
127
108
  end
128
109
 
129
110
  def test_reports_missing_sitemap_files
130
- Dir.mktmpdir do |dir|
131
- configuration = Indexmap::Configuration.new
132
- configuration.base_url = "https://www.example.com"
133
- configuration.public_path = Pathname(dir)
134
- configuration.google.credentials = "{\"type\":\"service_account\"}"
135
-
136
- service = FakeWebmastersService.new(site_urls: ["sc-domain:example.com"])
137
- result = Indexmap::Pinger::Google.new(
138
- configuration: configuration,
139
- service: service,
140
- credentials_builder: ->(**) { :fake_authorizer }
141
- ).ping
142
-
143
- assert_equal({status: :skipped, reason: :no_sitemaps}, result)
144
- assert_nil service.submitted
145
- end
111
+ configuration = configuration_with
112
+ configuration.google.credentials = "{\"type\":\"service_account\"}"
113
+
114
+ service = FakeWebmastersService.new(site_urls: ["sc-domain:example.com"])
115
+ result = Indexmap::Pinger::Google.new(
116
+ configuration: configuration,
117
+ service: service,
118
+ credentials_builder: ->(**) { :fake_authorizer }
119
+ ).ping
120
+
121
+ assert_equal({status: :skipped, reason: :no_sitemaps}, result)
122
+ assert_nil service.submitted
146
123
  end
147
124
 
148
125
  def test_reports_google_authorization_failure
149
- Dir.mktmpdir do |dir|
150
- public_path = Pathname(dir)
151
- public_path.join("sitemap.xml").write("<sitemapindex/>")
152
-
153
- configuration = Indexmap::Configuration.new
154
- configuration.base_url = "https://www.example.com"
155
- configuration.public_path = public_path
156
- configuration.google.credentials = "{\"type\":\"service_account\"}"
157
-
158
- service = FakeWebmastersService.new(site_urls: ["sc-domain:not-example.org"])
159
- result = Indexmap::Pinger::Google.new(
160
- configuration: configuration,
161
- service: service,
162
- credentials_builder: ->(**) { :fake_authorizer }
163
- ).ping
164
-
165
- assert_equal :failed, result[:status]
166
- assert_equal 1, result[:failures].count
167
- assert_equal :unauthorized, result[:failures].first[:reason]
168
- assert_nil service.submitted
169
- end
126
+ configuration = configuration_with(storage: storage_with("sitemap.xml" => "<sitemapindex/>"))
127
+ configuration.google.credentials = "{\"type\":\"service_account\"}"
128
+
129
+ service = FakeWebmastersService.new(site_urls: ["sc-domain:not-example.org"])
130
+ result = Indexmap::Pinger::Google.new(
131
+ configuration: configuration,
132
+ service: service,
133
+ credentials_builder: ->(**) { :fake_authorizer }
134
+ ).ping
135
+
136
+ assert_equal :failed, result[:status]
137
+ assert_equal 1, result[:failures].count
138
+ assert_equal :unauthorized, result[:failures].first[:reason]
139
+ assert_nil service.submitted
170
140
  end
171
141
 
172
142
  def test_google_authorization_requires_exact_property_match
173
- Dir.mktmpdir do |dir|
174
- public_path = Pathname(dir)
175
- public_path.join("sitemap.xml").write("<sitemapindex/>")
143
+ configuration = configuration_with(storage: storage_with("sitemap.xml" => "<sitemapindex/>"))
144
+ configuration.google.credentials = "{\"type\":\"service_account\"}"
145
+
146
+ service = FakeWebmastersService.new(site_urls: ["sc-domain:myexample.com"])
147
+ result = Indexmap::Pinger::Google.new(
148
+ configuration: configuration,
149
+ service: service,
150
+ credentials_builder: ->(**) { :fake_authorizer }
151
+ ).ping
152
+
153
+ assert_equal :failed, result[:status]
154
+ assert_equal :unauthorized, result[:failures].first[:reason]
155
+ assert_nil service.submitted
156
+ end
157
+
158
+ private
176
159
 
177
- configuration = Indexmap::Configuration.new
160
+ def configuration_with(storage: Indexmap::Storage::Memory.new(public_url: "https://www.example.com"))
161
+ Indexmap::Configuration.new.tap do |configuration|
178
162
  configuration.base_url = "https://www.example.com"
179
- configuration.public_path = public_path
180
- configuration.google.credentials = "{\"type\":\"service_account\"}"
181
-
182
- service = FakeWebmastersService.new(site_urls: ["sc-domain:myexample.com"])
183
- result = Indexmap::Pinger::Google.new(
184
- configuration: configuration,
185
- service: service,
186
- credentials_builder: ->(**) { :fake_authorizer }
187
- ).ping
188
-
189
- assert_equal :failed, result[:status]
190
- assert_equal :unauthorized, result[:failures].first[:reason]
191
- assert_nil service.submitted
163
+ configuration.storage = storage
164
+ end
165
+ end
166
+
167
+ def storage_with(files)
168
+ Indexmap::Storage::Memory.new(public_url: "https://www.example.com").tap do |storage|
169
+ files.each { |filename, body| storage.write(filename, body) }
192
170
  end
193
171
  end
194
172
  end