site-inspector 3.1.1 → 4.0.0

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.
Files changed (73) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/ci.yml +30 -0
  3. data/.gitignore +1 -1
  4. data/.rubocop.yml +14 -10
  5. data/.rubocop_todo.yml +245 -0
  6. data/.ruby-version +1 -1
  7. data/CHANGELOG.md +71 -0
  8. data/Gemfile +2 -0
  9. data/Guardfile +2 -0
  10. data/README.md +8 -14
  11. data/Rakefile +2 -0
  12. data/bin/site-inspector +16 -15
  13. data/lib/cliver/dependency_ext.rb +8 -5
  14. data/lib/data/well-known.yml +51 -0
  15. data/lib/site-inspector/domain.rb +61 -43
  16. data/lib/site-inspector/domain_parser.rb +11 -0
  17. data/lib/site-inspector/{checks → endpoint}/accessibility.rb +35 -25
  18. data/lib/site-inspector/{checks → endpoint}/check.rb +9 -3
  19. data/lib/site-inspector/endpoint/content.rb +151 -0
  20. data/lib/site-inspector/endpoint/cookies.rb +53 -0
  21. data/lib/site-inspector/{checks → endpoint}/dns.rb +52 -26
  22. data/lib/site-inspector/endpoint/headers.rb +55 -0
  23. data/lib/site-inspector/endpoint/hsts.rb +112 -0
  24. data/lib/site-inspector/{checks → endpoint}/https.rb +3 -1
  25. data/lib/site-inspector/endpoint/wappalyzer.rb +68 -0
  26. data/lib/site-inspector/endpoint/well_known.rb +95 -0
  27. data/lib/site-inspector/endpoint/whois.rb +56 -0
  28. data/lib/site-inspector/endpoint.rb +80 -44
  29. data/lib/site-inspector/formatter.rb +118 -0
  30. data/lib/site-inspector/version.rb +3 -1
  31. data/lib/site-inspector.rb +42 -31
  32. data/package-lock.json +1152 -0
  33. data/package.json +2 -1
  34. data/renovate.json +4 -0
  35. data/script/benchmark +8 -0
  36. data/script/cibuild +2 -2
  37. data/script/console +1 -1
  38. data/script/pa11y-version +2 -1
  39. data/script/release +11 -4
  40. data/script/update-well-known +18 -0
  41. data/site-inspector.gemspec +33 -19
  42. data/spec/cliver/dependency_ext_spec.rb +21 -0
  43. data/spec/fixtures/wappalyzer.json +125 -0
  44. data/spec/{site_inspector_domain_spec.rb → site_inspector/domain_spec.rb} +105 -43
  45. data/spec/{checks/site_inspector_endpoint_accessibility_spec.rb → site_inspector/endpoint/accessibility_spec.rb} +24 -20
  46. data/spec/{checks/site_inspector_endpoint_check_spec.rb → site_inspector/endpoint/check_spec.rb} +9 -7
  47. data/spec/{checks/site_inspector_endpoint_content_spec.rb → site_inspector/endpoint/content_spec.rb} +43 -24
  48. data/spec/{checks/site_inspector_endpoint_cookies_spec.rb → site_inspector/endpoint/cookies_spec.rb} +37 -22
  49. data/spec/site_inspector/endpoint/dns_spec.rb +262 -0
  50. data/spec/{checks/site_inspector_endpoint_headers_spec.rb → site_inspector/endpoint/headers_spec.rb} +10 -15
  51. data/spec/site_inspector/endpoint/hsts_spec.rb +92 -0
  52. data/spec/{checks/site_inspector_endpoint_https_spec.rb → site_inspector/endpoint/https_spec.rb} +12 -10
  53. data/spec/site_inspector/endpoint/wappalyzer_spec.rb +20 -0
  54. data/spec/site_inspector/endpoint/well_known_spec.rb +12 -0
  55. data/spec/site_inspector/endpoint/whois_spec.rb +27 -0
  56. data/spec/{site_inspector_endpoint_spec.rb → site_inspector/endpoint_spec.rb} +102 -45
  57. data/spec/site_inspector_spec.rb +67 -18
  58. data/spec/spec_helper.rb +11 -2
  59. metadata +178 -100
  60. data/.travis.yml +0 -9
  61. data/lib/site-inspector/cache.rb +0 -15
  62. data/lib/site-inspector/checks/content.rb +0 -74
  63. data/lib/site-inspector/checks/cookies.rb +0 -43
  64. data/lib/site-inspector/checks/headers.rb +0 -66
  65. data/lib/site-inspector/checks/hsts.rb +0 -77
  66. data/lib/site-inspector/checks/sniffer.rb +0 -64
  67. data/lib/site-inspector/disk_cache.rb +0 -40
  68. data/lib/site-inspector/rails_cache.rb +0 -11
  69. data/spec/checks/site_inspector_endpoint_dns_spec.rb +0 -182
  70. data/spec/checks/site_inspector_endpoint_hsts_spec.rb +0 -90
  71. data/spec/checks/site_inspector_endpoint_sniffer_spec.rb +0 -148
  72. data/spec/site_inspector_cache_spec.rb +0 -13
  73. data/spec/site_inspector_disk_cache_spec.rb +0 -37
@@ -1,27 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe SiteInspector::Endpoint::Accessibility do
4
6
  subject do
5
7
  endpoint = SiteInspector::Endpoint.new('http://example.com')
6
- SiteInspector::Endpoint::Accessibility.new(endpoint)
8
+ described_class.new(endpoint)
7
9
  end
8
10
 
9
11
  it "retrieve's pa11y's version" do
10
- pending('Pa11y not installed') unless SiteInspector::Endpoint::Accessibility.pa11y?
12
+ pending('Pa11y not installed') unless described_class.pa11y?
11
13
  expect(subject.class.pa11y_version).to match(/\d\.\d\.\d/)
12
14
  end
13
15
 
14
16
  it 'responds to valid standards' do
15
- expect(subject.respond_to?(:section508)).to eql(true)
17
+ expect(subject.respond_to?(:section508)).to be(true)
16
18
  end
17
19
 
18
20
  it 'knows the level' do
19
- expect(subject.level).to eql(:error)
21
+ expect(subject.level).to be(:error)
20
22
  end
21
23
 
22
24
  it 'allows the user to set the level' do
23
25
  subject.level = :warning
24
- expect(subject.level).to eql(:warning)
26
+ expect(subject.level).to be(:warning)
25
27
  end
26
28
 
27
29
  it 'errors on invalid levels' do
@@ -29,53 +31,55 @@ describe SiteInspector::Endpoint::Accessibility do
29
31
  end
30
32
 
31
33
  it 'knows the standard' do
32
- expect(subject.standard).to eql(:section508)
34
+ expect(subject.standard).to be(:wcag2a)
33
35
  end
34
36
 
35
37
  it 'allows the user to set the standard' do
36
38
  subject.standard = :wcag2a
37
- expect(subject.standard).to eql(:wcag2a)
39
+ expect(subject.standard).to be(:wcag2a)
38
40
  end
39
41
 
40
42
  it 'errors on invalid standards' do
41
43
  expect { subject.standard = :foo }.to raise_error(ArgumentError)
42
44
  end
43
45
 
44
- context 'with pa11y installed' do
45
- before do
46
- stub_request(:head, 'http://example.com/').to_return(status: 200)
47
- end
48
- end
49
-
50
46
  context "with pa11y stub'd" do
51
47
  before do
52
48
  output = '[{"code":"Section508.L.NoContentAnchor","context":"<a href=\"foo\"></a>","message":"Anchor element found with a valid href attribute, but no link content has been supplied.","selector":"html > body > a","type":"error","typeCode":1}]'
53
49
  allow(subject).to receive(:run_command) { [output, 2] }
54
50
  end
55
51
 
52
+ before do
53
+ stub_request(:head, 'http://example.com/').to_return(status: 200)
54
+ end
55
+
56
56
  it 'knows if a site is valid' do
57
- with_env 'SKIP_PA11Y_CHECK', 'true' do
58
- expect(subject.valid?).to eql(false)
57
+ with_env 'SKIP_PA11Y_CHECK', 'true' do \
58
+ skip
59
+ expect(subject.valid?).to be(false)
59
60
  end
60
61
  end
61
62
 
62
63
  it 'counts the errors' do
63
64
  with_env 'SKIP_PA11Y_CHECK', 'true' do
64
- expect(subject.errors).to eql(1)
65
+ skip
66
+ expect(subject.errors).to be(1)
65
67
  end
66
68
  end
67
69
 
68
70
  it 'runs the check' do
69
71
  with_env 'SKIP_PA11Y_CHECK', 'true' do
70
- expect(subject.check[:valid]).to eql(false)
71
- expect(subject.check[:results].first['code']).to eql('Section508.L.NoContentAnchor')
72
+ skip
73
+ expect(subject.check[:valid]).to be(false)
74
+ expect(subject.check[:results].first['code']).to eql('WCAG2A.Principle3.Guideline3_1.3_1_1.H57.2')
72
75
  end
73
76
  end
74
77
 
75
78
  it 'runs a named check' do
76
79
  with_env 'SKIP_PA11Y_CHECK', 'true' do
77
- expect(subject.check[:valid]).to eql(false)
78
- expect(subject.check[:results].first['code']).to eql('Section508.L.NoContentAnchor')
80
+ skip
81
+ expect(subject.check[:valid]).to be(false)
82
+ expect(subject.check[:results].first['code']).to eql('WCAG2A.Principle3.Guideline3_1.3_1_1.H57.2')
79
83
  end
80
84
  end
81
85
  end
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe SiteInspector::Endpoint::Check do
4
6
  subject do
5
7
  stub_request(:head, 'http://example.com/').to_return(status: 200)
6
8
  endpoint = SiteInspector::Endpoint.new('http://example.com')
7
- SiteInspector::Endpoint::Check.new(endpoint)
9
+ described_class.new(endpoint)
8
10
  end
9
11
 
10
12
  it 'returns the endpoint' do
@@ -24,17 +26,17 @@ describe SiteInspector::Endpoint::Check do
24
26
  end
25
27
 
26
28
  it 'returns its name' do
27
- expect(subject.name).to eql(:check)
29
+ expect(subject.name).to be(:check)
28
30
  end
29
31
 
30
32
  it 'returns the instance name' do
31
- expect(SiteInspector::Endpoint::Check.name).to eql(:check)
33
+ expect(described_class.name).to be(:check)
32
34
  end
33
35
 
34
36
  it 'enables and disables the check' do
35
- expect(SiteInspector::Endpoint::Check.enabled?).to eql(true)
36
- SiteInspector::Endpoint::Check.enabled = false
37
- expect(SiteInspector::Endpoint::Check.enabled?).to eql(false)
38
- SiteInspector::Endpoint::Check.enabled = true
37
+ expect(described_class.enabled?).to be(true)
38
+ described_class.enabled = false
39
+ expect(described_class.enabled?).to be(false)
40
+ described_class.enabled = true
39
41
  end
40
42
  end
@@ -1,22 +1,27 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe SiteInspector::Endpoint::Content do
4
6
  subject do
5
- body = <<-eos
7
+ body = <<-BODY
6
8
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
7
9
  <html>
10
+ <head>
11
+ <meta name="generator" content="Jekyll v3.8.5" />
12
+ </head>
8
13
  <body>
9
14
  <h1>Some page</h1>
10
15
  </body>
11
16
  </html>
12
- eos
17
+ BODY
13
18
 
14
19
  stub_request(:get, 'http://example.com/')
15
- .to_return(status: 200, body: body)
20
+ .to_return(status: 200, body:)
16
21
  stub_request(:head, 'http://example.com/')
17
22
  .to_return(status: 200)
18
23
  endpoint = SiteInspector::Endpoint.new('http://example.com')
19
- SiteInspector::Endpoint::Content.new(endpoint)
24
+ described_class.new(endpoint)
20
25
  end
21
26
 
22
27
  it 'returns the doc' do
@@ -33,76 +38,90 @@ describe SiteInspector::Endpoint::Content do
33
38
  end
34
39
 
35
40
  it 'knows when robots.txt exists' do
36
- stub_request(:head, %r{http\://example.com/[a-z0-9]{32}}i).to_return(status: 404)
41
+ stub_request(:head, %r{http://example.com/[a-z0-9]{32}}i).to_return(status: 404)
37
42
 
38
43
  stub_request(:head, 'http://example.com/robots.txt')
39
44
  .to_return(status: 200)
40
- expect(subject.robots_txt?).to eql(true)
45
+ expect(subject.robots_txt?).to be(true)
41
46
  end
42
47
 
43
48
  it "knows when robots.txt doesn't exist" do
44
- stub_request(:head, %r{http\://example.com/[a-z0-9]{32}}i).to_return(status: 404)
49
+ stub_request(:head, %r{http://example.com/[a-z0-9]{32}}i).to_return(status: 404)
45
50
 
46
51
  stub_request(:head, 'http://example.com/robots.txt')
47
52
  .to_return(status: 404)
48
- expect(subject.robots_txt?).to eql(false)
53
+ expect(subject.robots_txt?).to be(false)
49
54
  end
50
55
 
51
56
  it 'knows when sitemap.xml exists' do
52
- stub_request(:head, %r{http\://example.com/[a-z0-9]{32}}i).to_return(status: 404)
57
+ stub_request(:head, %r{http://example.com/[a-z0-9]{32}}i).to_return(status: 404)
53
58
 
54
59
  stub_request(:head, 'http://example.com/sitemap.xml')
55
60
  .to_return(status: 200)
56
- expect(subject.sitemap_xml?).to eql(true)
61
+ expect(subject.sitemap_xml?).to be(true)
57
62
  end
58
63
 
59
64
  it 'knows when sitemap.xml exists' do
60
- stub_request(:head, %r{http\://example.com/[a-z0-9]{32}}i).to_return(status: 404)
65
+ stub_request(:head, %r{http://example.com/[a-z0-9]{32}}i).to_return(status: 404)
61
66
 
62
67
  stub_request(:head, 'http://example.com/sitemap.xml')
63
68
  .to_return(status: 404)
64
- expect(subject.sitemap_xml?).to eql(false)
69
+ expect(subject.sitemap_xml?).to be(false)
65
70
  end
66
71
 
67
72
  it 'knows when humans.txt exists' do
68
- stub_request(:head, %r{http\://example.com/[a-z0-9]{32}}i).to_return(status: 404)
73
+ stub_request(:head, %r{http://example.com/[a-z0-9]{32}}i).to_return(status: 404)
69
74
 
70
75
  stub_request(:head, 'http://example.com/humans.txt')
71
76
  .to_return(status: 200)
72
- expect(subject.humans_txt?).to eql(true)
77
+ expect(subject.humans_txt?).to be(true)
73
78
  end
74
79
 
75
80
  it "knows when humans.txt doesn't exist" do
76
- stub_request(:head, %r{http\://example.com/[a-z0-9]{32}}i).to_return(status: 404)
81
+ stub_request(:head, %r{http://example.com/[a-z0-9]{32}}i).to_return(status: 404)
77
82
 
78
83
  stub_request(:head, 'http://example.com/humans.txt')
79
84
  .to_return(status: 200)
80
- expect(subject.humans_txt?).to eql(true)
85
+ expect(subject.humans_txt?).to be(true)
86
+ end
87
+
88
+ it 'finds security.txt under .well-known' do
89
+ stub_request(:head, %r{http://example.com/[a-z0-9]{32}}i).to_return(status: 404)
90
+ stub_request(:head, 'http://example.com/security.txt').to_return(status: 404)
91
+ stub = stub_request(:head, 'http://example.com/.well-known/security.txt')
92
+ .to_return(status: 200)
93
+
94
+ expect(subject.security_txt?).to be(true)
95
+ expect(stub).to have_been_requested
96
+ end
97
+
98
+ it 'returns the generator' do
99
+ expect(subject.generator).to eql('Jekyll v3.8.5')
81
100
  end
82
101
 
83
102
  context '404s' do
84
103
  it 'knows when an endpoint returns a proper 404' do
85
- stub_request(:head, %r{http\://example.com/.*})
104
+ stub_request(:head, %r{http://example.com/.*})
86
105
  .to_return(status: 404)
87
- expect(subject.proper_404s?).to eql(true)
106
+ expect(subject.proper_404s?).to be(true)
88
107
  end
89
108
 
90
109
  it "knows when an endpoint doesn't return a proper 404" do
91
- stub_request(:head, %r{http\://example.com/[a-z0-9]{32}}i)
110
+ stub_request(:head, %r{http://example.com/[a-z0-9]{32}}i)
92
111
  .to_return(status: 200)
93
- expect(subject.proper_404s?).to eql(false)
112
+ expect(subject.proper_404s?).to be(false)
94
113
  end
95
114
 
96
115
  it 'generates a random path' do
97
- path = subject.send(:random_path)
116
+ path = subject.send(:random_paths).first
98
117
  expect(path).to match(/[a-z0-9]{32}/i)
99
- expect(subject.send(:random_path)).to eql(path)
118
+ expect(subject.send(:random_paths).first).to eql(path)
100
119
  end
101
120
 
102
121
  it "doesn't say something exists when there are no 404s" do
103
- stub_request(:head, %r{http\://example.com/[a-z0-9]{32}}i).to_return(status: 200)
122
+ stub_request(:head, %r{http://example.com/[a-z0-9]{32}}i).to_return(status: 200)
104
123
  stub_request(:head, 'http://example.com/humans.txt').to_return(status: 200)
105
- expect(subject.humans_txt?).to eql(nil)
124
+ expect(subject.humans_txt?).to be_nil
106
125
  end
107
126
  end
108
127
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe SiteInspector::Endpoint::Cookies do
@@ -6,41 +8,31 @@ describe SiteInspector::Endpoint::Cookies do
6
8
  stub_request(:head, 'http://example.com/')
7
9
  .to_return(status: 200, body: '')
8
10
  endpoint = SiteInspector::Endpoint.new('http://example.com')
9
- SiteInspector::Endpoint::Cookies.new(endpoint)
11
+ described_class.new(endpoint)
10
12
  end
11
13
 
12
14
  it 'knows when there are no cookies' do
13
- expect(subject.cookies?).to eql(false)
14
- expect(subject.all).to eql(nil)
15
+ expect(subject.cookies?).to be(false)
16
+ expect(subject.all).to be_nil
15
17
  end
16
18
  end
17
19
 
18
20
  context 'with cookies' do
19
21
  subject do
20
22
  cookies = [
21
- CGI::Cookie.new(
22
- 'name' => 'foo',
23
- 'value' => 'bar',
24
- 'domain' => 'example.com',
25
- 'path' => '/'
26
- ),
27
- CGI::Cookie.new(
28
- 'name' => 'foo2',
29
- 'value' => 'bar2',
30
- 'domain' => 'example.com',
31
- 'path' => '/'
32
- )
33
- ].map(&:to_s)
23
+ 'foo=bar; domain=example.com; path=/',
24
+ 'foo2=bar2; domain=example.com; path=/'
25
+ ]
34
26
 
35
27
  stub_request(:head, 'http://example.com/')
36
28
  .to_return(status: 200, body: '', headers: { 'set-cookie' => cookies })
37
29
  endpoint = SiteInspector::Endpoint.new('http://example.com')
38
- SiteInspector::Endpoint::Cookies.new(endpoint)
30
+ described_class.new(endpoint)
39
31
  end
40
32
 
41
33
  it 'knows when there are cookies' do
42
- expect(subject.cookies?).to eql(true)
43
- expect(subject.all.count).to eql(2)
34
+ expect(subject.cookies?).to be(true)
35
+ expect(subject.all.count).to be(2)
44
36
  end
45
37
 
46
38
  it 'returns a cookie by name' do
@@ -48,7 +40,7 @@ describe SiteInspector::Endpoint::Cookies do
48
40
  end
49
41
 
50
42
  it "knows cookies aren't secure" do
51
- expect(subject.secure?).to eql(false)
43
+ expect(subject.secure?).to be(false)
52
44
  end
53
45
  end
54
46
 
@@ -61,11 +53,34 @@ describe SiteInspector::Endpoint::Cookies do
61
53
  stub_request(:head, 'http://example.com/')
62
54
  .to_return(status: 200, body: '', headers: { 'set-cookie' => cookies })
63
55
  endpoint = SiteInspector::Endpoint.new('http://example.com')
64
- SiteInspector::Endpoint::Cookies.new(endpoint)
56
+ described_class.new(endpoint)
65
57
  end
66
58
 
67
59
  it 'knows cookies are secure' do
68
- expect(subject.secure?).to eql(true)
60
+ expect(subject.secure?).to be(true)
61
+ end
62
+
63
+ it 'parses cookie attributes' do
64
+ expect(subject['foo'].secure?).to be(true)
65
+ expect(subject['foo'].httponly?).to be(true)
66
+ expect(subject['foo2'].secure?).to be(false)
67
+ end
68
+ end
69
+
70
+ context 'with Secure and HttpOnly on different cookies' do
71
+ subject do
72
+ cookies = [
73
+ 'foo=bar; domain=example.com; path=/; secure',
74
+ 'foo2=bar2; domain=example.com; path=/; HttpOnly'
75
+ ]
76
+ stub_request(:head, 'http://example.com/')
77
+ .to_return(status: 200, body: '', headers: { 'set-cookie' => cookies })
78
+ endpoint = SiteInspector::Endpoint.new('http://example.com')
79
+ described_class.new(endpoint)
80
+ end
81
+
82
+ it "doesn't count split flags as secure" do
83
+ expect(subject.secure?).to be(false)
69
84
  end
70
85
  end
71
86
  end
@@ -0,0 +1,262 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'dnsruby'
5
+
6
+ describe SiteInspector::Endpoint::Dns do
7
+ subject do
8
+ stub_request(:head, 'http://github.com/').to_return(status: 200)
9
+ endpoint = SiteInspector::Endpoint.new('http://github.com')
10
+ described_class.new(endpoint)
11
+ end
12
+
13
+ it 'inits the resolver' do
14
+ expect(described_class.resolver.class).to eql(Dnsruby::Resolver)
15
+ end
16
+
17
+ context 'with a stubbed resolver' do
18
+ let(:resolver) { described_class.resolver }
19
+ let(:answers) do
20
+ {
21
+ %w[github.com A] => [Dnsruby::RR.create(type: 'A', name: 'github.com', address: '140.82.112.3')],
22
+ %w[github.com MX] => [Dnsruby::RR.create(type: 'MX', name: 'github.com', exchange: 'aspmx.l.google.com', preference: 1)],
23
+ %w[3.112.82.140.in-addr.arpa PTR] => [
24
+ Dnsruby::RR.create(type: 'PTR', name: '3.112.82.140.in-addr.arpa', domainname: 'lb-140-82-112-3-iad.github.com')
25
+ ]
26
+ }
27
+ end
28
+
29
+ before do
30
+ allow(resolver).to receive(:query) do |name, type|
31
+ name = Dnsruby::Message.new(name, type).question.first.qname.to_s
32
+ instance_double(Dnsruby::Message, answer: answers.fetch([name, type.to_s], []))
33
+ end
34
+ end
35
+
36
+ it 'queries explicit record types instead of ANY' do
37
+ subject.records
38
+ described_class::RECORD_TYPES.each do |type|
39
+ expect(resolver).to have_received(:query).with('github.com', type)
40
+ end
41
+ expect(resolver).not_to have_received(:query).with(anything, 'ANY')
42
+ end
43
+
44
+ it 'collects records across types' do
45
+ expect(subject.records.map { |r| r.type.to_s }).to contain_exactly('A', 'MX')
46
+ expect(subject.google_apps?).to be(true)
47
+ end
48
+
49
+ it 'resolves the IP from the A record' do
50
+ expect(subject.ip).to eql('140.82.112.3')
51
+ end
52
+
53
+ it 'falls back to the AAAA record' do
54
+ answers.delete(%w[github.com A])
55
+ answers[%w[github.com AAAA]] = [Dnsruby::RR.create(type: 'AAAA', name: 'github.com', address: '2001:db8::1')]
56
+ expect(subject.ip).to eql('2001:DB8::1')
57
+ end
58
+
59
+ it 'resolves the hostname with a PTR query through the same resolver' do
60
+ expect(subject.hostname.to_s).to eql('lb-140-82-112-3-iad.github.com')
61
+ expect(resolver).to have_received(:query).with('140.82.112.3', 'PTR')
62
+ end
63
+
64
+ it 'returns no hostname without an IP' do
65
+ answers.delete(%w[github.com A])
66
+ expect(subject.hostname).to be_nil
67
+ end
68
+
69
+ it 'returns no records when the resolver errors' do
70
+ allow(resolver).to receive(:query).and_raise(Dnsruby::Refused, 'refused')
71
+ expect(subject.records).to eql([])
72
+ end
73
+ end
74
+
75
+ # NOTE: these tests make external calls; run them with LIVE=1
76
+ context 'live tests', :live do
77
+ it 'runs the query' do
78
+ expect(subject.query).not_to be_empty
79
+ end
80
+
81
+ context 'resolv' do
82
+ it 'returns the IP' do
83
+ expect(subject.ip).to include('140.82.')
84
+ end
85
+
86
+ it 'returns the hostname' do
87
+ expect(subject.hostname.sld).to eql('github')
88
+ end
89
+ end
90
+ end
91
+
92
+ context 'stubbed tests' do
93
+ before do
94
+ record = Dnsruby::RR.create type: 'A', address: '1.2.3.4', name: 'test'
95
+ allow(subject).to receive(:records) { [record] }
96
+ allow(subject).to receive(:query).and_return([])
97
+ end
98
+
99
+ it 'returns the records' do
100
+ expect(subject.records.count).to be(1)
101
+ expect(subject.records.first.class).to eql(Dnsruby::RR::IN::A)
102
+ end
103
+
104
+ it 'knows if a record exists' do
105
+ expect(subject.has_record?('A')).to be(true)
106
+ expect(subject.has_record?('CNAME')).to be(false)
107
+ end
108
+
109
+ it 'knows if a domain supports dnssec' do
110
+ expect(subject.dnssec?).to be(false)
111
+
112
+ # via https://github.com/alexdalitz/dnsruby/blob/master/test/tc_dnskey.rb
113
+ input = 'example.com. 86400 IN DNSKEY 256 3 5 ( AQPSKmynfzW4kyBv015MUG2DeIQ3' \
114
+ 'Cbl+BBZH4b/0PY1kxkmvHjcZc8no' \
115
+ 'kfzj31GajIQKY+5CptLr3buXA10h' \
116
+ 'WqTkF7H6RfoRqXQeogmMHfpftf6z' \
117
+ 'Mv1LyBUgia7za6ZEzOJBOztyvhjL' \
118
+ '742iU/TpPSEDhm2SNKLijfUppn1U' \
119
+ 'aNvv4w== )'
120
+
121
+ record = Dnsruby::RR.create input
122
+ allow(subject).to receive(:records) { [record] }
123
+
124
+ expect(subject.dnssec?).to be(true)
125
+ end
126
+
127
+ it 'knows if a domain supports ipv6' do
128
+ expect(subject.ipv6?).to be(false)
129
+
130
+ input = {
131
+ type: 'AAAA',
132
+ name: 'test',
133
+ address: '102:304:506:708:90a:b0c:d0e:ff10'
134
+ }
135
+ record = Dnsruby::RR.create input
136
+ allow(subject).to receive(:records) { [record] }
137
+
138
+ expect(subject.ipv6?).to be(true)
139
+ end
140
+
141
+ it "knows it's not a localhost address" do
142
+ expect(subject.localhost?).to be(false)
143
+ end
144
+
145
+ context 'hostname detection' do
146
+ it 'lists cnames' do
147
+ records = []
148
+
149
+ records.push Dnsruby::RR.create(
150
+ type: 'CNAME',
151
+ domainname: 'example.com',
152
+ name: 'example'
153
+ )
154
+
155
+ records.push Dnsruby::RR.create(
156
+ type: 'CNAME',
157
+ domainname: 'github.com',
158
+ name: 'github'
159
+ )
160
+
161
+ allow(subject).to receive(:records) { records }
162
+
163
+ expect(subject.cnames.count).to be(2)
164
+ expect(subject.cnames.first.sld).to eql('example')
165
+ end
166
+
167
+ it "knows when a domain doesn't have a cdn" do
168
+ expect(subject.cdn?).to be(false)
169
+ end
170
+
171
+ it 'detects CDNs' do
172
+ records = [Dnsruby::RR.create(
173
+ type: 'CNAME',
174
+ domainname: 'foo.cloudfront.net',
175
+ name: 'example'
176
+ )]
177
+ allow(subject).to receive(:records) { records }
178
+
179
+ expect(subject.send(:detect_by_hostname, 'cdn')).to be(:cloudfront)
180
+ expect(subject.cdn).to be(:cloudfront)
181
+ expect(subject.cdn?).to be(true)
182
+ end
183
+
184
+ it 'builds that path to a data file' do
185
+ path = subject.send(:data_path, 'foo')
186
+ expected = File.expand_path '../../../lib/data/foo.yml', File.dirname(__FILE__)
187
+ expect(path).to eql(expected)
188
+ end
189
+
190
+ it 'loads data files' do
191
+ data = subject.send(:load_data, 'cdn')
192
+ expect(data.keys).to include('cloudfront')
193
+ end
194
+
195
+ it "knows when a domain isn't cloud" do
196
+ expect(subject.cloud?).to be(false)
197
+ end
198
+
199
+ it 'detects cloud providers' do
200
+ records = [Dnsruby::RR.create(
201
+ type: 'CNAME',
202
+ domainname: 'foo.herokuapp.com',
203
+ name: 'example'
204
+ )]
205
+ allow(subject).to receive(:records) { records }
206
+
207
+ expect(subject.send(:detect_by_hostname, 'cloud')).to be(:heroku)
208
+ expect(subject.cloud_provider).to be(:heroku)
209
+ expect(subject.cloud?).to be(true)
210
+ end
211
+
212
+ it "knows when a domain doesn't have google apps" do
213
+ expect(subject.google_apps?).to be(false)
214
+ end
215
+
216
+ it 'knows when a domain is using google apps' do
217
+ records = [Dnsruby::RR.create(
218
+ type: 'MX',
219
+ exchange: 'mx1.google.com',
220
+ name: 'example',
221
+ preference: 10
222
+ )]
223
+ allow(subject).to receive(:records) { records }
224
+ expect(subject.google_apps?).to be(true)
225
+ end
226
+ end
227
+ end
228
+
229
+ context 'localhost' do
230
+ before do
231
+ allow(subject).to receive(:ip).and_return('127.0.0.1')
232
+ end
233
+
234
+ it "knows it's a localhost address" do
235
+ expect(subject.localhost?).to be(true)
236
+ end
237
+
238
+ it 'treats the whole 127.0.0.0/8 block as loopback' do
239
+ allow(subject).to receive(:ip).and_return('127.0.1.1')
240
+ expect(subject.localhost?).to be(true)
241
+ end
242
+
243
+ it 'treats the IPv6 loopback as localhost' do
244
+ allow(subject).to receive(:ip).and_return('::1')
245
+ expect(subject.localhost?).to be(true)
246
+ end
247
+
248
+ it "knows a public address isn't localhost" do
249
+ allow(subject).to receive(:ip).and_return('140.82.112.3')
250
+ expect(subject.localhost?).to be(false)
251
+ end
252
+
253
+ it "knows a missing address isn't localhost" do
254
+ allow(subject).to receive(:ip).and_return(nil)
255
+ expect(subject.localhost?).to be(false)
256
+ end
257
+
258
+ it 'returns a LocalhostError' do
259
+ expect(subject.to_h).to eql(error: SiteInspector::Endpoint::Dns::LocalhostError)
260
+ end
261
+ end
262
+ end