def test_last_observation_when_no_observation_exists(self): site = Website('https://example.com', 'example') site.clean() observation = site.last_observation() self.assertEqual(observation, None)
def test_initialize(self): uri = 'https://example.com' expected_text = 'example' site = Website(uri, expected_text) self.assertEqual(uri, site.uri) self.assertEqual(expected_text, site.expected_text)
def test_subdomain(self): uris = { 'http://my.ex.com': 'my', 'https://by.the.com': 'by', 'https://where.com': '' } for uri, subdomain in uris.items(): site = Website(uri, '') self.assertEqual(site._subdomain, subdomain)
def test_message_status_was_up_now_down(self, MockFetcher): site = Website('https://example.com', 'this phrase not found') site.fetcher = MockFetcher() site.fetcher.fetch.return_value = (502, 'Nginx Error') site.new_observation('up') site.evaluate() self.assertEqual(site.message, 'https://example.com is down.')
def test_message_when_no_observation_exists(self, MockFetcher): site = Website('https://example.com', 'example') site.fetcher = MockFetcher() site.fetcher.fetch.return_value = (200, 'An example') site.clean() site.evaluate() self.assertEqual(site.message, 'https://example.com is up.')
def test_message_status_same_as_last_time(self, MockFetcher): site = Website('https://example.com', 'example') site.new_observation('up') site.fetcher = MockFetcher() site.fetcher.fetch.return_value = (200, 'This is an example') # This line would make a network call, but uses our mock instead site.evaluate() self.assertFalse(site.message, 'No Message expected')
def test_message_status_was_down_now_up(self, MockFetcher): site = Website('https://example.com', 'example') site.fetcher = MockFetcher() site.fetcher.fetch.return_value = (200, 'An example') site.new_observation('down') site.evaluate() variations = [ 'https://example.com is up. Last down duration: 0 seconds.', 'https://example.com is up. Last down duration: 1 seconds.' ] self.assertTrue(site.message in variations, 'Expected duration is either 0 or 1 second')
def load_config(self, data=None): if not data: with open(self.CONFIG_FILE, 'r') as f: text = f.read() data = yaml.load(text) self.courier = Courier(data['webhook_url']) sites = [] for entry in data['sites']: pieces = re.split(self.WHITESPACE_REGEX, entry, maxsplit=1) uri = pieces[0] if len(pieces) > 1: expected_text = pieces[1] else: expected_text = '' site = Website(uri, expected_text) sites.append(site) self.sites = sites
def test_current_status_with_malformed_uri(self): site = Website('h://malformed.com', 'example') status = site.current_status() self.assertEqual(status, 'down')
def test_current_status(self): site = Website('https://example.com', 'example') status = site.current_status()
def test_last_observation_happy_path(self): site = Website('https://example.com', 'example') site.new_observation('up') last = site.last_observation() self.assertEqual(last.status, 'up')
def test_observation_file(self): site = Website('https://example.com', 'example') self.assertEqual(site.observation_file, 'observations/https:||example.com')
def test_domain(self): uris = {'http://ex.com': 'ex', 'https://by.the.com': 'the'} for uri, domain in uris.items(): site = Website(uri, '') self.assertEqual(site._domain, domain)