test_azure_auth_util

python 5 days, 22 hours ago
import unittest from unittest.mock import patch, MagicMock, mock_open import jwt import time from pathlib import Path from azure_auth_util import ( Client, is_azure_token_valid, azure_auth, client_certificate_method, client_thumbprint_method, _make_client_assertion, _use_client_assertion, fetch ) class TestAzureAuthUtil(unittest.TestCase): def setUp(self): self.client = Client( tenant_id="tenant123", client_id="client123", scopes=["scope1", "scope2"] ) self.certificate_key_path = "mock_key_path.pem" self.certificate_path = "mock_cert_path.pem" @patch("jwt.decode") def test_is_azure_token_valid_valid_token(self, mock_decode): mock_decode.return_value = {"exp": time.time() + 1000} self.assertTrue(is_azure_token_valid("mock_token")) @patch("jwt.decode") def test_is_azure_token_valid_expired_token(self, mock_decode): mock_decode.return_value = {"exp": time.time() - 1000} self.assertFalse(is_azure_token_valid("mock_token")) @patch("jwt.decode", side_effect=Exception("Invalid token")) def test_is_azure_token_valid_invalid_token(self, mock_decode): self.assertFalse(is_azure_token_valid("mock_token")) @patch("azure_auth_util.client_certificate_method") def test_azure_auth(self, mock_client_cert_method): mock_client_cert_method.return_value = "mock_token" result = azure_auth(self.client, self.certificate_key_path, self.certificate_path) self.assertEqual(result, "mock_token") @patch("pathlib.Path.read_bytes", return_value=b"mock_cert_data") @patch("cryptography.x509.load_pem_x509_certificate") @patch("hashlib.sha1") @patch("azure_auth_util.client_thumbprint_method") def test_client_certificate_method(self, mock_thumbprint_method, mock_sha1, mock_load_cert, mock_read_bytes): mock_sha1.return_value = "mock_sha1" mock_thumbprint_method.return_value = "mock_token" result = client_certificate_method(self.client, Path(self.certificate_key_path), Path(self.certificate_path)) self.assertEqual(result, "mock_token") @patch("azure_auth_util._make_client_assertion") @patch("azure_auth_util._use_client_assertion") def test_client_thumbprint_method(self, mock_use_assertion, mock_make_assertion): mock_make_assertion.return_value = "mock_assertion" mock_use_assertion.return_value = "mock_token" result = client_thumbprint_method(self.client, Path(self.certificate_key_path), "mock_thumbprint") self.assertEqual(result, "mock_token") @patch("pathlib.Path.read_bytes", return_value=b"mock_key_data") @patch("uuid.uuid4", return_value="mock_uuid") @patch("datetime.datetime.now") def test_make_client_assertion(self, mock_now, mock_uuid, mock_read_bytes): mock_now.return_value.timestamp.return_value = 1234567890 mock_thumb_print = "mock_thumb_print" with patch("jwt.jwk.jwk_from_pem", return_value="mock_key"): with patch("jwt.jwt.JWT.encode", return_value="mock_jwt_token"): result = _make_client_assertion(Path(self.certificate_key_path), self.client, mock_thumb_print) self.assertEqual(result, "mock_jwt_token") @patch("urllib.request.urlopen") def test_use_client_assertion(self, mock_urlopen): mock_response = MagicMock() mock_response.read.return_value = json.dumps({"access_token": "mock_token"}).encode("utf-8") mock_urlopen.return_value = mock_response result = _use_client_assertion(self.client, "mock_assertion") self.assertEqual(result, "mock_token") @patch("urllib.request.urlopen") def test_fetch_success(self, mock_urlopen): mock_response = MagicMock() mock_response.read.return_value = "mock_response".encode("utf-8") mock_urlopen.return_value = mock_response req = MagicMock() result = fetch(req) self.assertEqual(result, "mock_response") @patch("urllib.request.urlopen", side_effect=urllib.error.HTTPError( url=None, code=400, msg="Bad Request", hdrs=None, fp=None )) def test_fetch_http_error(self, mock_urlopen): req = MagicMock() with self.assertRaises(Exception) as context: fetch(req) self.assertIn("http error: 400", str(context.exception)) if __name__ == "__main__": unittest.main()
15
Posted By
Python Script to create AWS beanstalk
#!/usr/bin/python
  
import boto
python aws beanstalk
sandeep sandeep
List all files and folders using python os mo
import os

def list_files_folders(path):
python python-os
kishore_kumar
Get current environment variables in python
import os
env = os.environ

python python-os
kishore_kumar
Get os details using python os
import os
print os.uname()
# Don't use os.system('uname -a'), its j
python python-os
kishore_kumar
Get stats ( lines, words, char count ) of fil
def file_stats(path):
    f = open(path, 'r')
    lines = f.readlines()
python
kishore_kumar
Use map function in python
def get_double(num):
    return num * 2

python
kishore_kumar
Python sample codes for beginners
print "Welcome to python"
python
gaya38 gaya38
Python program for even number checking
a=input("Enter a value:")
if (a%2==0):
    print "The given number is even numb
python
gaya38 gaya38
Python program for prime number check
a=input("Enter a value:")
k=0
b=(a/2)+1
python
gaya38 gaya38
Pass command line arguments in python
import sys
x=len(sys.argv)
a=[]
python
gaya38 gaya38
Python program for the largest number in an a
a = [1,43,98,5]#Dummy data
for l in range(len(a)-1):
        if (a[l]>a[l+1]):
python
gaya38 gaya38
print list of even numbers within a range
n=100
a=[10,20,30,40,50]
b=[60,70,80,90]
python
gaya38 gaya38
generate fibonacci series in python
n=input("Enter the constraint to print n
m=input("Enter the maximum value to prin
a=0
python
gaya38 gaya38
Generate Random number within the range in py
import random
print random.uniform(10,500)
python
gaya38 gaya38
Shuffle list elements in python
import random;
z = [1,90,4,2]
z = random.shuffle(z)
python
gaya38 gaya38
use python requests to get contents of url (
import requests

req = requests.get("https://httpbin.org/
python python-requests
kishore_kumar
how to iterate or get values in python dictio
sample_dict = { "number": 1, "fruits": [

for key in sample_dict:
python
kishore_kumar
create matrix and multiply using numpy in pyt
import numpy as np

matrix = [[1,2,3], [4,5,6], [7,8,9]]
python numpy
kishore_kumar
generate random numbers matrix with numpy pyt
import numpy as np

random_arr = np.random.randint(1,50,9)
python numpy
kishore_kumar
Find min , max and mean for numpy arrays
import numpy as np

random_arr = np.random.randint(1,50,9)
python numpy
kishore_kumar