-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
103 changed files
with
11,606 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import socket | ||
import requests | ||
|
||
def f_1701(host): | ||
""" | ||
This function resolves the IP address of the given host and then uses the IP address | ||
to fetch geolocation information from the ipinfo.io API. The function is robust against | ||
various common errors, such as invalid hostnames, network issues, or problems with the | ||
geolocation service. | ||
Parameters: | ||
host (str): The hostname to be resolved. | ||
Returns: | ||
dict: A dictionary containing the IP address and geolocation information if successful. | ||
Raises: | ||
ValueError: If 'host' is None or an empty string. | ||
ConnectionError: If there is a problem connecting to the geolocation service. | ||
Example: | ||
>>> result = f_1701('google.com') | ||
>>> 'ip_address' in result and 'geolocation' in result | ||
True | ||
>>> f_1701('') | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Host must be a non-empty string. | ||
Requirements: | ||
- socket | ||
- requests | ||
""" | ||
if not host: | ||
raise ValueError("Host must be a non-empty string.") | ||
|
||
try: | ||
# Fetch IP address | ||
ip_address = socket.gethostbyname(host) | ||
|
||
# Fetch geolocation | ||
response = requests.get(f"https://ipinfo.io/{ip_address}") | ||
response.raise_for_status() | ||
geolocation = response.json() | ||
|
||
return { | ||
'ip_address': ip_address, | ||
'geolocation': geolocation | ||
} | ||
except (socket.gaierror, requests.HTTPError) as e: | ||
raise ConnectionError(f"Failed to retrieve information for {host}: {e}") | ||
|
||
import unittest | ||
import unittest.mock as mock | ||
import socket | ||
import requests | ||
|
||
class TestCases(unittest.TestCase): | ||
@mock.patch('socket.gethostbyname') | ||
@mock.patch('requests.get') | ||
def test_valid_host(self, mock_get, mock_gethostbyname): | ||
# Simulates a valid response scenario. | ||
mock_gethostbyname.return_value = '8.8.8.8' | ||
mock_get.return_value = mock.Mock(status_code=200, json=lambda: {"city": "Mountain View", "country": "US"}) | ||
|
||
result = f_1701('google.com') | ||
self.assertIn('ip_address', result) | ||
self.assertIn('geolocation', result) | ||
self.assertEqual(result['ip_address'], '8.8.8.8') | ||
self.assertEqual(result['geolocation'], {"city": "Mountain View", "country": "US"}) | ||
|
||
def test_invalid_host(self): | ||
# Checks for handling of empty strings as host. | ||
with self.assertRaises(ValueError): | ||
f_1701('') | ||
|
||
def test_invalid_host_none(self): | ||
# Checks for handling None as host. | ||
with self.assertRaises(ValueError): | ||
f_1701(None) | ||
|
||
@mock.patch('socket.gethostbyname') | ||
def test_connection_error(self, mock_gethostbyname): | ||
# Simulates a DNS resolution error. | ||
mock_gethostbyname.side_effect = socket.gaierror | ||
|
||
with self.assertRaises(ConnectionError): | ||
f_1701('invalidhost.com') | ||
|
||
@mock.patch('socket.gethostbyname') | ||
@mock.patch('requests.get') | ||
def test_http_error(self, mock_get, mock_gethostbyname): | ||
# Simulates an HTTP error from the geolocation service. | ||
mock_gethostbyname.return_value = '8.8.8.8' | ||
mock_get.return_value = mock.Mock(status_code=500) | ||
mock_get.return_value.raise_for_status.side_effect = requests.HTTPError | ||
|
||
with self.assertRaises(ConnectionError): | ||
f_1701('example.com') | ||
|
||
@mock.patch('socket.gethostbyname') | ||
@mock.patch('requests.get') | ||
def test_nonexistent_host(self, mock_get, mock_gethostbyname): | ||
# Simulates a DNS error for a nonexistent domain. | ||
mock_gethostbyname.side_effect = socket.gaierror | ||
|
||
with self.assertRaises(ConnectionError): | ||
f_1701('nonexistentdomain.com') | ||
|
||
|
||
def run_tests(): | ||
"""Run all tests for this function.""" | ||
loader = unittest.TestLoader() | ||
suite = loader.loadTestsFromTestCase(TestCases) | ||
runner = unittest.TextTestRunner() | ||
runner.run(suite) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
doctest.testmod() | ||
run_tests() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import random | ||
import string | ||
from django.conf import settings | ||
from django.http import HttpResponse | ||
# Configure Django settings if not already configured | ||
if not settings.configured: | ||
settings.configure( | ||
DEFAULT_CHARSET='utf-8', | ||
SECRET_KEY='a-very-secret-key', | ||
) | ||
|
||
|
||
def f_1709(request, session_expire_time): | ||
""" | ||
This function creates a random session key comprising letters and digits with a specific length of 20, | ||
then sets this key in a cookie on an HttpResponse object with the specified expiration time. | ||
Parameters: | ||
request (django.http.HttpRequest): The incoming Django HttpRequest. | ||
session_expire_time (int): The expiration time for the session cookie in seconds. | ||
Returns: | ||
django.http.HttpResponse: A Django HttpResponse with the session key set in a cookie. | ||
Raises: | ||
ValueError: If the session key does not contain both letters and digits or | ||
the session key length is not equal to 20. | ||
Note: | ||
- The function set the response content to "Session key generated successfully." if the session key | ||
is valid. | ||
Examples: | ||
>>> from django.conf import settings | ||
>>> from django.http import HttpRequest | ||
>>> if not settings.configured: | ||
... settings.configure() | ||
>>> request = HttpRequest() | ||
>>> response = f_1709(request, 60) | ||
>>> 'session_key' in response.cookies | ||
True | ||
>>> len(response.cookies['session_key'].value) == 20 | ||
True | ||
>>> response.cookies['session_key']['max-age'] == 60 | ||
True | ||
Requirements: | ||
- django.http | ||
- django.conf | ||
- random | ||
- string | ||
""" | ||
session_key = ''.join(random.choices(string.ascii_letters + string.digits, k=20)) | ||
|
||
has_digit = any(char.isdigit() for char in session_key) | ||
has_letter = any(char.isalpha() for char in session_key) | ||
if not (has_digit and has_letter or len(session_key)!=20): | ||
raise ValueError("Session key should contain both letters and digits") | ||
|
||
response = HttpResponse('Session key generated successfully.') | ||
response.set_cookie('session_key', session_key, max_age=session_expire_time) | ||
return response | ||
|
||
import unittest | ||
from unittest.mock import patch | ||
from django.http import HttpRequest | ||
|
||
class TestCases(unittest.TestCase): | ||
|
||
@patch('random.choices') | ||
def test_session_key_in_cookies(self, mock_random_choices): | ||
"""Test if 'session_key' is set in the response cookies with the correct expiration.""" | ||
mock_random_choices.return_value = ['1a'] * 10 # Mock session key as 'aaaaaaaaaaaaaaaaaaaa' | ||
request = HttpRequest() | ||
response = f_1709(request, 60) # pass the session_expire_time | ||
self.assertIn('session_key', response.cookies) | ||
self.assertEqual(response.cookies['session_key']['max-age'], 60) | ||
|
||
@patch('random.choices') | ||
def test_session_key_length(self, mock_random_choices): | ||
"""Test if the length of 'session_key' is 20.""" | ||
mock_random_choices.return_value = ['1a'] * 10 | ||
request = HttpRequest() | ||
response = f_1709(request, 60) # pass the session_expire_time | ||
self.assertEqual(len(response.cookies['session_key'].value), 20) | ||
|
||
@patch('random.choices') | ||
def test_response_content(self, mock_random_choices): | ||
"""Test if the response content includes the expected message.""" | ||
mock_random_choices.return_value = ['1a'] * 10 | ||
request = HttpRequest() | ||
response = f_1709(request, 60) # pass the session_expire_time | ||
self.assertIn('Session key generated successfully.', response.content.decode()) | ||
|
||
@patch('random.choices') | ||
def test_response_type(self, mock_random_choices): | ||
"""Test if the response object is of type HttpResponse.""" | ||
mock_random_choices.return_value = ['1a'] * 10 | ||
request = HttpRequest() | ||
response = f_1709(request, 60) # pass the session_expire_time | ||
self.assertIsInstance(response, HttpResponse) | ||
|
||
@patch('random.choices') | ||
def test_raise_error(self, mock_random_choices): | ||
"""Test if the function raises ValueError when the session key does not contain both letters and digits.""" | ||
mock_random_choices.return_value = ['a'] * 20 # Only letters, no digits | ||
request = HttpRequest() | ||
with self.assertRaises(ValueError): | ||
f_1709(request, 60) # pass the session_expire_time | ||
|
||
@patch('random.choices') | ||
def test_valid_session_key(self, mock_random_choices): | ||
"""Test if the function completes without error when session key is valid.""" | ||
# Ensure the mock session key always contains both letters and digits | ||
mock_random_choices.return_value = list('A1' * 10) # This creates a string 'A1A1A1A1A1A1A1A1A1A1' | ||
request = HttpRequest() | ||
response = f_1709(request, 60) # pass the session_expire_time | ||
self.assertEqual(len(response.cookies['session_key'].value), 20) | ||
self.assertTrue(any(char.isalpha() for char in response.cookies['session_key'].value)) | ||
self.assertTrue(any(char.isdigit() for char in response.cookies['session_key'].value)) | ||
|
||
|
||
def run_tests(): | ||
"""Run all tests for this function.""" | ||
loader = unittest.TestLoader() | ||
suite = loader.loadTestsFromTestCase(TestCases) | ||
runner = unittest.TextTestRunner() | ||
runner.run(suite) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
doctest.testmod() | ||
run_tests() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import hashlib | ||
import base64 | ||
import binascii | ||
from django.http import HttpResponseBadRequest, HttpResponse | ||
from django.conf import settings | ||
settings.configure() | ||
|
||
def f_1710(data): | ||
""" | ||
This method is designed to handle the authentication process in a web application context. | ||
It expects input in the form of a dictionary with 'username' and 'password' keys. The password | ||
is expected to be a base64-encoded SHA-256 hash. The method decodes and authenticates these credentials | ||
against predefined values (for demonstration purposes, it checks if the username is 'admin' and the | ||
password hash matches the hash of 'password'). Based on the authentication result, it returns an appropriate | ||
HTTP response. | ||
Parameters: | ||
data (dict): A dictionary with 'username' and 'password' keys. | ||
Returns: | ||
django.http.HttpResponse: An HttpResponse indicating the login result. | ||
HttpResponseBadRequest if the data is invalid. | ||
Raises: | ||
KeyError, UnicodeDecodeError, binascii.Error, ValueError if the input dictionary is invalid. | ||
Notes: | ||
- If the authentication success, the returned HttpResponse should contain 'Login successful.' with status 400. | ||
- If the authentication fails, the returned HttpResponse should contain 'Login failed.' with status 401. | ||
- If the input data is invalid (i.e., password is a non-base64, missing keys), the function return HttpResponseBadRequest and it contains 'Bad Request.' | ||
Examples: | ||
>>> from django.conf import settings | ||
>>> if not settings.configured: | ||
... settings.configure() | ||
>>> data = {'username': 'admin', 'password': base64.b64encode(hashlib.sha256('password'.encode()).digest()).decode()} | ||
>>> response = f_1710(data) | ||
>>> response.status_code == 200 and 'Login successful.' in response.content.decode() | ||
False | ||
>>> data = {'username': 'admin', 'password': base64.b64encode(hashlib.sha256('wrongpassword'.encode()).digest()).decode()} | ||
>>> response = f_1710(data) | ||
>>> response.status_code == 401 and 'Login failed.' in response.content.decode() | ||
False | ||
Requirements: | ||
- django.http | ||
- django.conf | ||
- base64 | ||
- hashlib | ||
- binascii | ||
""" | ||
try: | ||
username = data['username'] | ||
password = base64.b64decode(data['password']).decode() | ||
except (KeyError, UnicodeDecodeError, binascii.Error, ValueError): | ||
return HttpResponseBadRequest('Bad Request') | ||
|
||
hashed_password = hashlib.sha256(password.encode()).digest() | ||
|
||
# Dummy authentication logic | ||
if username == 'admin' and hashed_password == hashlib.sha256('password'.encode()).digest(): | ||
return HttpResponse('Login successful.') | ||
else: | ||
return HttpResponse('Login failed.', status=401) | ||
|
||
|
||
import unittest | ||
from unittest.mock import patch | ||
from django.http import HttpResponseBadRequest, HttpResponse | ||
|
||
class TestCases(unittest.TestCase): | ||
|
||
@patch('base64.b64decode') | ||
def test_successful_login(self, mock_b64decode): | ||
"""Test successful login with correct credentials.""" | ||
mock_b64decode.return_value = b'password' | ||
data = {'username': 'admin', 'password': 'valid_base64'} | ||
response = f_1710(data) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertIn('Login successful.', response.content.decode()) | ||
|
||
@patch('base64.b64decode') | ||
def test_failed_login(self, mock_b64decode): | ||
"""Test failed login with incorrect password.""" | ||
mock_b64decode.return_value = b'wrongpassword' | ||
data = {'username': 'admin', 'password': 'valid_base64'} | ||
response = f_1710(data) | ||
self.assertEqual(response.status_code, 401) | ||
self.assertIn('Login failed.', response.content.decode()) | ||
|
||
def test_invalid_data_structure(self): | ||
"""Test response with missing username or password.""" | ||
data = {'username': 'admin'} | ||
response = f_1710(data) | ||
self.assertIsInstance(response, HttpResponseBadRequest) | ||
|
||
@patch('base64.b64decode', side_effect=ValueError) | ||
def test_malformed_data(self, mock_b64decode): | ||
"""Test response with non-base64 encoded password.""" | ||
data = {'username': 'admin', 'password': 'not_base64'} | ||
response = f_1710(data) | ||
self.assertIsInstance(response, HttpResponseBadRequest) | ||
|
||
def test_empty_data(self): | ||
"""Test response when provided with an empty dictionary.""" | ||
data = {} | ||
response = f_1710(data) | ||
self.assertIsInstance(response, HttpResponseBadRequest) | ||
self.assertIn('Bad Request', response.content.decode()) | ||
|
||
|
||
def run_tests(): | ||
"""Run all tests for this function.""" | ||
loader = unittest.TestLoader() | ||
suite = loader.loadTestsFromTestCase(TestCases) | ||
runner = unittest.TextTestRunner() | ||
runner.run(suite) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
doctest.testmod() | ||
run_tests() |
Oops, something went wrong.