Skip to content

Commit

Permalink
Added unittests around new to_csv()
Browse files Browse the repository at this point in the history
  • Loading branch information
IanLee1521 committed Oct 21, 2017
1 parent e852e81 commit bf31668
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import os
import sys
import tempfile
import unittest

from pshtt.models import Domain, Endpoint
from pshtt import pshtt as _pshtt
from pshtt.cli import to_csv


class FakeSuffixList(object):
def get_public_suffix(self, hostname, *args, **kwargs):
return hostname


# Artificially setup the the preload and suffix lists
# This should be irrelevant after #126 is decided upon / merged
_pshtt.suffix_list = FakeSuffixList()
_pshtt.preload_list = []
_pshtt.preload_pending = []


class TestToCSV(unittest.TestCase):
@classmethod
def setUpClass(cls):
base_domain = 'example.com'

domain = Domain(base_domain)
domain.http = Endpoint("http", "root", base_domain)
domain.httpwww = Endpoint("http", "www", base_domain)
domain.https = Endpoint("https", "root", base_domain)
domain.httpswww = Endpoint("https", "www", base_domain)

cls.results = _pshtt.result_for(domain)
cls.temp_filename = os.path.join(tempfile.gettempdir(), 'results.csv')

@unittest.skipIf(sys.version_info[0] < 3, 'Python 3 test only')
def test_no_results(self):
to_csv([], self.temp_filename)

with open(self.temp_filename) as fh:
content = fh.read()

expected = 'Domain,Base Domain,Canonical URL,Live,Redirect,Redirect To,Valid HTTPS,Defaults to HTTPS,Downgrades HTTPS,Strictly Forces HTTPS,HTTPS Bad Chain,HTTPS Bad Hostname,HTTPS Expired Cert,HTTPS Self Signed Cert,HSTS,HSTS Header,HSTS Max Age,HSTS Entire Domain,HSTS Preload Ready,HSTS Preload Pending,HSTS Preloaded,Base Domain HSTS Preloaded,Domain Supports HTTPS,Domain Enforces HTTPS,Domain Uses Strong HSTS,Unknown Error\n'

self.assertEqual(content, expected)

@unittest.skipIf(sys.version_info[0] < 3, 'Python 3 test only')
def test_single_result(self):
to_csv([self.results], self.temp_filename)

with open(self.temp_filename) as fh:
content = fh.read()

expected = ''
expected += 'Domain,Base Domain,Canonical URL,Live,Redirect,Redirect To,Valid HTTPS,Defaults to HTTPS,Downgrades HTTPS,Strictly Forces HTTPS,HTTPS Bad Chain,HTTPS Bad Hostname,HTTPS Expired Cert,HTTPS Self Signed Cert,HSTS,HSTS Header,HSTS Max Age,HSTS Entire Domain,HSTS Preload Ready,HSTS Preload Pending,HSTS Preloaded,Base Domain HSTS Preloaded,Domain Supports HTTPS,Domain Enforces HTTPS,Domain Uses Strong HSTS,Unknown Error\n'
expected += 'example.com,example.com,http://example.com,False,False,,False,False,False,False,False,False,False,False,False,,,False,False,False,False,False,False,False,False,False\n'

self.assertEqual(content, expected)

0 comments on commit bf31668

Please sign in to comment.