def test_get_pagespeed(self):
     website = WebsiteAvailability(os.environ.get("TEST_DOMAIN"))
     strategy = "strategy_unspecified"
     page_performance = website.get_pagespeed(strategy)
     self.assertIsInstance(page_performance, tuple)
     self.assertTrue(len(page_performance)==4)
     self.assertTrue(type(page_performance[0]) is float and type(page_performance[1]) and type(page_performance[2]) and type(page_performance[3]) is str)
    def test_signals_ip_api(self):
        error = False
        website = WebsiteAvailability(os.environ.get("TEST_DOMAIN"))
        try:
            blacklist_score = website.check_blacklisting()
        except:
            error = True

        self.assertFalse(error)
        self.assertIsInstance(blacklist_score, int)
    def test_google_page_insights_api(self):
        error = False
        website = WebsiteAvailability(os.environ.get("TEST_DOMAIN"))
        strategy = "strategy_unspecified"

        try:
            page_performance = website.get_pagespeed(strategy)
        except:
            error = True

        self.assertFalse(error)
        self.assertIsInstance(page_performance[0], float)
class TestWebsiteAvailabilityFunctions(unittest.TestCase):
    
    website = WebsiteAvailability(os.environ.get("TEST_DOMAIN"))
    
    def test_get_ip_address(self):
        website = WebsiteAvailability(os.environ.get("TEST_DOMAIN"))
        ip_address = website.get_ip_address()
        self.assertIsInstance(ip_address, str)
        self.assertEqual(ip_address, os.environ.get("TEST_IP_ADDRESS"))
        
    def test_get_http_status_code(self):
        website = WebsiteAvailability(os.environ.get("TEST_DOMAIN"))
        http_status_code = website.get_http_status_code()
        self.assertIsInstance(http_status_code, tuple)
        self.assertTrue(len(http_status_code)==2)
        self.assertTrue(type(http_status_code[0]) is int and type(http_status_code[1]) is str)
        
    def test_get_server_and_content_type(self):
        website = WebsiteAvailability(os.environ.get("TEST_DOMAIN"))
        server_and_content_type = website.get_server_and_content_type()
        self.assertIsInstance(server_and_content_type, tuple)
        self.assertTrue(len(server_and_content_type)==2)
        self.assertTrue(type(server_and_content_type[0]) and type(server_and_content_type[1]) is str)
        
    def test_check_whois_status(self):
        website = WebsiteAvailability(os.environ.get("TEST_DOMAIN"))
        whois_status = website.check_whois_status()
        self.assertIsInstance(whois_status, tuple)
        self.assertTrue(len(whois_status)==2)
    
    def test_get_pagespeed(self):
        website = WebsiteAvailability(os.environ.get("TEST_DOMAIN"))
        strategy = "strategy_unspecified"
        page_performance = website.get_pagespeed(strategy)
        self.assertIsInstance(page_performance, tuple)
        self.assertTrue(len(page_performance)==4)
        self.assertTrue(type(page_performance[0]) is float and type(page_performance[1]) and type(page_performance[2]) and type(page_performance[3]) is str)
        
    def test_is_registered(self):
        website = WebsiteAvailability(os.environ.get("TEST_DOMAIN"))
        domain_name_is_registered = website.is_registered()
        self.assertIsInstance(domain_name_is_registered, bool)
        
    def test_ssl_expiry_datetime(self):
        website = WebsiteAvailability(os.environ.get("TEST_DOMAIN"))
        ssl_expiry = website.ssl_expiry_datetime()
        self.assertIsInstance(ssl_expiry, datetime.datetime)
    
    def test_health_check(self):
        website = WebsiteAvailability(os.environ.get("TEST_DOMAIN"))
        website_health_check = website.health_check()
        self.assertIsInstance(website_health_check, tuple)
        self.assertTrue(len(website_health_check)==3)
        self.assertTrue(type(website_health_check[0]) is float and type(website_health_check[1]) is int and type(website_health_check[2] is int) )

    def test_check_blacklisting(self):
        website = WebsiteAvailability(os.environ.get("TEST_DOMAIN"))
        blacklist_score = website.check_blacklisting()
        self.assertIsInstance(blacklist_score, int)
Exemplo n.º 5
0
    def choose_options(self):
        # Returns multiple data types depending on the option chosen.
        website = WebsiteAvailability(self.website_address)

        website_hash_test = CheckHashAndPorts(self.website_address)

        if self.individual_website_response == "15":
            print("*"*32)
            print(Fore.GREEN + "Exited the program successfully")
            print("*"*32)
            sys.exit()

        elif self.individual_website_response == "2":
            try:
                ip_address = website.get_ip_address()
                return Fore.GREEN + f"The IP address of this website is {ip_address}"
            except Exception:
                return "Unable to get IP address"

        elif self.individual_website_response == "3":
            try:
                http_status_code = website.get_http_status_code()
                return Fore.GREEN + f"The HTTP status code is {http_status_code[0]}: {http_status_code[1]}"
            except Exception:
                return "Unable to get HTTP status code"

        elif self.individual_website_response == "4":
            print("*"*50)
            print("Page performance is the overall performance score.")
            print("First Meaningful Paint measures when the primary content is visible.")
            print("Speed Index shows how quickly the page is populated.")
            print("Time to interactive is time it takes to become fully interactive.")
            print("*"*50)
            try: 
                strategy = "strategy_unspecified"
                page_performance = website.get_pagespeed(strategy)
                return Fore.GREEN + f"Your page performance is {page_performance[0]}, First Meaningful Paint: {page_performance[1]}, Speed Index: {page_performance[2]},  Time To Interactive: {page_performance[3]}"
            except Exception:
                return "Unable to get data"

        elif self.individual_website_response == "5":
            try:
                whois_status = website.check_whois_status()
                return Fore.GREEN + f"Expiration date: {whois_status[0]}, Registrar: {whois_status[1]}"
            except Exception:
                return "Unable to get Expiration date or registrar"

        elif self.individual_website_response == "6":
            try:
                server_and_content_type = website.get_server_and_content_type()
                return Fore.GREEN + f"Server: {server_and_content_type[0]}, Content type: {server_and_content_type[1]}"
            except Exception:
                return "Unable to get server and content type"

        elif self.individual_website_response == "7":
            try:
                ssl_expiry = website.ssl_expiry_datetime()
                return Fore.GREEN + f"Expiration date of SSL certificate: {ssl_expiry}"
            except Exception:
                return "Unable to get expiration dat of SSL"

        elif self.individual_website_response == "8":
            try:
                domain_name_is_registered = website.is_registered()
                if domain_name_is_registered is True:
                    return (Fore.GREEN + "Domain name is registered")
                elif domain_name_is_registered is False:
                    return Fore.RED + "Domain name is not registered"
            except Exception:
                return "Cannot find if domain is registered"

        elif self.individual_website_response == "9":
            try:
                website_hash = website_hash_test.check_hash()
                return Fore.GREEN + f"{website_hash}"
            except Exception:
                return "Cannot get MD5 sum"

        elif self.individual_website_response == "10":
            try:
                port_scan = website_hash_test.nmap_port_scanning()
                return Fore.GREEN + f"{port_scan}"
            except Exception:
                return "Unable to do port scan"

        elif self.individual_website_response == "11" or self.individual_website_response == "1":
            try:
                nmap_ping = website_hash_test.nmap_ping_scanning()
                return Fore.GREEN + f"The website is {nmap_ping}"
            except Exception:
                return "Unable to ping website"

        elif self.individual_website_response == "12":
            try:
                website_scrape = ScrapeWebsite(self.website_address)
                json_metadata = website_scrape.return_page_metadata()
                all_data = website_scrape.all_metadata()
                print(Fore.GREEN + f"Title: {all_data[-1]['title']}")
                print(Fore.GREEN + f"Sitename: {all_data[-1]['sitename']}")
                print(Fore.GREEN + f"Description: {all_data[-1]['description']}")
                print(Fore.GREEN + f"Image: {all_data[-1]['image']}")
                print(Fore.GREEN + f"Favicon: {all_data[-1]['favicon']}")
                return Fore.GREEN + "Saved metadata to metadata.json"
            except Exception:
                return "Unable to retrieve metadata"

        elif self.individual_website_response == "13":
            try:
                print("Performing health check. Please be patient.")
                website_health_check = website.health_check()
                url = "https://api.sendgrid.com/v3/mail/send"
                headers = {"Authorization": f"{os.environ.get('SENDGRID_API')}",
                           "Content-Type": "application/json"}
                email_content = f"Your page performance is: {website_health_check[0]}, HTTP Status: {website_health_check[1]}, Blacklisting score is: {website_health_check[2]}"
                payload = {"personalizations":
                           [{"to": [{"email": f"{os.environ.get('TEST_EMAIL')}"}]}],
                           "from": {"email": f"{os.environ.get('TEST_EMAIL')}"},
                           "subject": "Health Check Status Report",
                           "content": [{"type": "text/plain", "value": f"{email_content}"}]}
                message = requests.post(url, data=json.dumps(payload), headers=headers)
                return Fore.GREEN + f"Your page performance is: {website_health_check[0]}, HTTP Status: {website_health_check[1]}, Blacklisting score is: {website_health_check[2]}"
            except Exception:
                return "Unable to perform health check"

        elif self.individual_website_response == "14":
            try:
                blacklist_score = website.check_blacklisting()
                return Fore.GREEN + f"Your confidence score is {blacklist_score}"
            except Exception:
                return "Unable to get blacklist score"

        else:
            return Fore.RED + "That wasn't an option, try again."
 def test_get_ip_address(self):
     website = WebsiteAvailability(os.environ.get("TEST_DOMAIN"))
     ip_address = website.get_ip_address()
     self.assertIsInstance(ip_address, str)
     self.assertEqual(ip_address, os.environ.get("TEST_IP_ADDRESS"))
 def test_check_blacklisting(self):
     website = WebsiteAvailability(os.environ.get("TEST_DOMAIN"))
     blacklist_score = website.check_blacklisting()
     self.assertIsInstance(blacklist_score, int)
 def test_health_check(self):
     website = WebsiteAvailability(os.environ.get("TEST_DOMAIN"))
     website_health_check = website.health_check()
     self.assertIsInstance(website_health_check, tuple)
     self.assertTrue(len(website_health_check)==3)
     self.assertTrue(type(website_health_check[0]) is float and type(website_health_check[1]) is int and type(website_health_check[2] is int) )
 def test_ssl_expiry_datetime(self):
     website = WebsiteAvailability(os.environ.get("TEST_DOMAIN"))
     ssl_expiry = website.ssl_expiry_datetime()
     self.assertIsInstance(ssl_expiry, datetime.datetime)
 def test_is_registered(self):
     website = WebsiteAvailability(os.environ.get("TEST_DOMAIN"))
     domain_name_is_registered = website.is_registered()
     self.assertIsInstance(domain_name_is_registered, bool)
 def test_check_whois_status(self):
     website = WebsiteAvailability(os.environ.get("TEST_DOMAIN"))
     whois_status = website.check_whois_status()
     self.assertIsInstance(whois_status, tuple)
     self.assertTrue(len(whois_status)==2)
 def test_get_server_and_content_type(self):
     website = WebsiteAvailability(os.environ.get("TEST_DOMAIN"))
     server_and_content_type = website.get_server_and_content_type()
     self.assertIsInstance(server_and_content_type, tuple)
     self.assertTrue(len(server_and_content_type)==2)
     self.assertTrue(type(server_and_content_type[0]) and type(server_and_content_type[1]) is str)
 def test_get_http_status_code(self):
     website = WebsiteAvailability(os.environ.get("TEST_DOMAIN"))
     http_status_code = website.get_http_status_code()
     self.assertIsInstance(http_status_code, tuple)
     self.assertTrue(len(http_status_code)==2)
     self.assertTrue(type(http_status_code[0]) is int and type(http_status_code[1]) is str)