Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yugangw-msft committed Oct 17, 2018
1 parent 9b58f3c commit f2e3568
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 40 deletions.
22 changes: 15 additions & 7 deletions tests/test_self_signed_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@
class TestSelfSignedJwt(unittest.TestCase):
testNowDate = cp['nowDate']
testJwtId = cp['jwtId']
expectedJwt = cp['expectedJwt']
expectedJwtWithThumbprint = cp['expectedJwtWithThumbprint']
expectedJwtWithPublicCert = cp['expectedJwtWithPublicCert']

unexpectedJwt = 'unexpectedJwt'
testAuthority = Authority('https://login.windows.net/naturalcauses.com', False)
testClientId = 'd6835713-b745-48d1-bb62-7a8248477d35'
testCert = cp['cert']
testPublicCert=cp['publicCert']

def _create_jwt(self, cert, thumbprint, encodeError = None):
def _create_jwt(self, cert, thumbprint, public_certificate = None, encodeError = None):
ssjwt = SelfSignedJwt(cp['callContext'], self.testAuthority, self.testClientId)

self_signed_jwt._get_date_now = mock.MagicMock(return_value = self.testNowDate)
Expand All @@ -67,19 +70,24 @@ def _create_jwt(self, cert, thumbprint, encodeError = None):
if encodeError:
self_signed_jwt._encode_jwt = mock.MagicMock(return_value = self.unexpectedJwt)
else:
self_signed_jwt._encode_jwt = mock.MagicMock(return_value = self.expectedJwt)
expected = self.expectedJwtWithPublicCert if public_certificate else self.expectedJwtWithThumbprint
self_signed_jwt._encode_jwt = mock.MagicMock(return_value = expected)

jwt = ssjwt.create(cert, thumbprint, public_certificate=None)
jwt = ssjwt.create(cert, thumbprint, public_certificate=public_certificate)
return jwt

def _create_jwt_and_match_expected_err(self, testCert, thumbprint, encodeError = None):
with self.assertRaises(Exception):
self._create_jwt(testCert, thumbprint, encodeError)
self._create_jwt(testCert, thumbprint, encodeError = encodeError)

def _create_jwt_and_match_expected_jwt(self, cert, thumbprint):
jwt = self._create_jwt(cert, thumbprint)
self.assertTrue(jwt, 'No JWT generated')
self.assertTrue(jwt == self.expectedJwt, 'Generated JWT does not match expected:{}'.format(jwt))
self.assertTrue(jwt == self.expectedJwtWithThumbprint, 'Generated JWT does not match expected:{}'.format(jwt))

def test_jwt_hash_with_public_cert(self):
jwt = self._create_jwt(self.testCert, cp['certHash'], public_certificate = self.testPublicCert)
self.assertTrue(jwt == self.expectedJwtWithPublicCert, 'Generated JWT does not match expected:{}'.format(jwt))

def test_create_jwt_hash_colons(self):
self._create_jwt_and_match_expected_jwt(self.testCert, cp['certHash'])
Expand All @@ -93,7 +101,7 @@ def test_create_jwt_hash_straight_hex(self):
self._create_jwt_and_match_expected_jwt(self.testCert, thumbprint)

def test_create_jwt_invalid_cert(self):
self._create_jwt_and_match_expected_err('foobar', cp['certHash'], True)
self._create_jwt_and_match_expected_err('foobar', cp['certHash'], encodeError = True)

def test_create_jwt_invalid_thumbprint_1(self):
self._create_jwt_and_match_expected_err(self.testCert, 'zzzz')
Expand Down
83 changes: 50 additions & 33 deletions tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,41 +176,58 @@
# This is a dummy RSA private cert used for testing purpose.It does not represent valid credential.
# privatePem variable is a fake certificate in the form of a string.
def get_self_signed_cert():
private_pem = ("-----BEGIN RSA PRIVATE KEY-----"
"MIIEpAIBAAKCAQEAoMGZTZi0vU/ICYVgV4vcTwzvZCNXdJ9EgGBBFu1E0/j4FF0Y"
"Fd2sP7IwmWVZLlWJ5VbwAtdMiRdrogX/QnWPfsNfsPzDdRRJD+Erh9tmBzJm08h7"
"1RggS1/VehZ9WNdTDlQM3P+zNg0IG274VIr+ZSBzIbYxV6ecPdRU/EsZ5Wa5SCwG"
"Fu1qPJW8KY8yvse9PHdFiHjrmcZSKTbBCp/2grdBrk/N1jwtH6Yj100l7G69HPE/"
"4kXYRX9f/LjpzF77VMCj7UJtmb1yR3fRHpppbm7GkqvJFM2Kg3UG5fsp8nQBDRc+"
"R3kjm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AJtkAER41AfvTQwHhHxoSDsfoU4DXAvuIvPouSncwOgdKj"
"XEGz2wKBgQDQmB/u4oGaPRf5DdasiAcqofYDEoo/OcpdRPeW2t5z7WDZcjeN4ajR"
"GDoQssBpy1fpsPnghksMhYZL2l9xiSInkFw87ax5EYBS43Mt5HfJPgwpEnA5yV3W"
"WGt3TBp7BgYOKhIID6803lBYfDmtQzdD+xMjlJKSQ9wfZYCuXrYwSg=="
"-----END RSA PRIVATE KEY-----")

return private_pem

parameters['certHash'] = 'C1:5D:EA:86:56:AD:DF:67:BE:80:31:D8:5E:BD:DC:5A:D6:C4:36:E1'
private_pem = ("-----BEGIN PRIVATE KEY-----\n"
"MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDLTKKMn3WJiDnc"
"8VuJPDr1kj9VYr9zdobMUSJ9YgRb6Rz05YiUOlSeNiMA6Y7yrpVbSxewIAkVC/gI"
"W/Ywp4FtR9j/SzQ91HIHvmKBrOAorpPDS0ZQ6nfeaBtZ14UIF16H/OvgyMkweBBd"
"7pIbF4i7ty3gdhFzpN2xd+qXTeDVMtxaQOM8RTAVp1RUpuNpPSIQxo9dLyOotcAe"
"Fj8uc0mMa0o6DbdxD26RiBIOgzcsYr5WUCMihmE3h1EIbXtQC5YwFnU9Q8OgupWz"
"i31QE1fbFgWzpjx1/KU9Yb8doWs1jwXSo4UGYecxiOKBrFuj0I6Kyelv9dfiayZF"
"GpwR+FmXAgMBAAECggEABmSxk/SL0LhtAWrBsy4muIRR45CIbswibxh6GjFT68QH"
"+heh1O+Eq7kOHsA5k54z6jwRUaOgRX4r3a9urZcG9fXVeCnYSb19nIq7NFLIdd8P"
"nIuoeXD2NhNWENw7PcbmXSZyEI6f7RtJgHq5M4ro7OZU1gNAhz9/DU61HO8BDBNP"
"9TT9h2Kf5cAHC79lrRs7Cj9yLK/JFFPFSyTEqxo9O6xHQfRv6X25rZeYo9JzGaqP"
"mwTvmheEqW7a75apBEpbzqD0f0jT4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VBAMMCUNMSS1Mb2dp"
"bjAiGA8yMDE4MTAxNzE2MTAxN1oYDzIwMTkxMDE3MTYxMDE5WjAUMRIwEAYDVQQD"
"DAlDTEktTG9naW4wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDLTKKM"
"n3WJiDnc8VuJPDr1kj9VYr9zdobMUSJ9YgRb6Rz05YiUOlSeNiMA6Y7yrpVbSxew"
"IAkVC/gIW/Ywp4FtR9j/SzQ91HIHvmKBrOAorpPDS0ZQ6nfeaBtZ14UIF16H/Ovg"
"yMkweBBd7pIbF4i7ty3gdhFzpN2xd+qXTeDVMtxaQOM8RTAVp1RUpuNpPSIQxo9d"
"LyOotcAeFj8uc0mMa0o6DbdxD26RiBIOgzcsYr5WUCMihmE3h1EIbXtQC5YwFnU9"
"Q8OgupWzi31QE1fbFgWzpjx1/KU9Yb8doWs1jwXSo4UGYecxiOKBrFuj0I6Kyelv"
"9dfiayZFGpwR+FmXAgMBAAEwDQYJKoZIhvcNAQEFBQADggEBADfEqXzcI/fs82T0"
"9B3H3lGWQL1JlcxOxD2TeMPtubDNllhZBT5GaYiw1LWAq+xJZZh+QPNxvZVw5Q/p"
"wgXo32maLNwjuhlDl/5bNNOMsxszRz60C2QQXzIaBxd6T2EUcnMQozu5y/33HT8k"
"k/ipBKbfmLP7Hgvs2xdhjHQcG61a2QP6qxD0UjVpXlgsL8wwc28ZSk1RqhxnHG0s"
"HRrRuwNhqWRe7JCGNkOwUghlemrqSuL3i6iAaeqipBqS0vVFGN8KS12jKYirEV5T"
"YkJ2HRrzSWEWbGhk+LnVis47nYRFzQB/sec/m+rpCpX6Spmiez6Yge2u874Oks/A"
"OGQyeYk=")

return private_pem, public_pem

parameters['certHash'] = 'B8:D3:FC:F1:51:50:63:7F:B0:ED:EE:32:C5:A2:4B:A2:28:D8:93:91'
parameters['nowDate'] = datetime.fromtimestamp(1418433646.179)
parameters['jwtId'] = '09841beb-a2c2-4777-a347-34ef055238a8'
parameters['expected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cqTrqK6HV9iWuiT0rq3LMP2lShwAhKaTYIeAAek5Bw5LwRR2mo9ybreq_02vCDxIQg0C3kBDGMU8GxQ2tAWMYSqnxNfrjgUhARDQYdZTjCyuq1kOb8QrHly29mPT7xdS7Xnc0IF6JZb1PXj0Q'
parameters['cert'], parameters['publicCert'] = get_self_signed_cert()

correlation_id_regex = re.compile("[^\s]+")

Expand Down

0 comments on commit f2e3568

Please sign in to comment.