Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(208)

Side by Side Diff: third_party/google-endpoints/test/test_suppliers.py

Issue 2666783008: Add google-endpoints to third_party/. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2016 Google Inc. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import json
16 import unittest
17 import httmock
18 import mock
19
20 from Crypto import PublicKey
21 from jwkest import jwk
22
23 from google.api.auth import suppliers
24
25
26 class KeyUriSupplierTest(unittest.TestCase):
27
28 def test_supply_issuer(self):
29 issuer = "https://issuer.com"
30 jwks_uri = "https://issuer.com/jwks/uri"
31 configs = {issuer: suppliers.IssuerUriConfig(False, jwks_uri)}
32 supplier = suppliers.KeyUriSupplier(configs)
33 self.assertEquals(jwks_uri, supplier.supply(issuer))
34 self.assertIsNone(supplier.supply("random-issuer"))
35
36 def test_openid_discovery(self):
37 jwks_uri = "https://issuer.com/jwks/uri"
38 @httmock.urlmatch(scheme="https", netloc="issuer.com",
39 path="/" + suppliers._OPEN_ID_CONFIG_PATH)
40 def _mock_response(url, request): # pylint: disable=unused-argument
41 response = {"jwks_uri": jwks_uri}
42 return json.dumps(response)
43
44 issuer = "https://issuer.com"
45 configs = {issuer: suppliers.IssuerUriConfig(True, None)}
46 supplier = suppliers.KeyUriSupplier(configs)
47 with httmock.HTTMock(_mock_response):
48 self.assertEquals(jwks_uri, supplier.supply(issuer))
49
50 def test_issuer_without_protocol(self):
51 jwks_uri = "https://issuer.com/jwks/uri"
52 @httmock.urlmatch(scheme="https", netloc="issuer.com",
53 path="/" + suppliers._OPEN_ID_CONFIG_PATH)
54 def _mock_response(url, request): # pylint: disable=unused-argument
55 response = {"jwks_uri": jwks_uri}
56 return json.dumps(response)
57
58 # Specify an issuer without protocol to make sure the "https://" prefix is
59 # added automatically.
60 issuer = "issuer.com"
61 configs = {issuer: suppliers.IssuerUriConfig(True, None)}
62 supplier = suppliers.KeyUriSupplier(configs)
63 with httmock.HTTMock(_mock_response):
64 self.assertEquals(jwks_uri, supplier.supply(issuer))
65
66 def test_openid_discovery_with_bad_json(self):
67 @httmock.urlmatch(scheme="https", netloc="issuer.com")
68 def _mock_response_with_bad_json(url, request): # pylint: disable=unused-ar gument
69 return "bad-json"
70
71 issuer = "https://issuer.com"
72 configs = {issuer: suppliers.IssuerUriConfig(True, None)}
73 supplier = suppliers.KeyUriSupplier(configs)
74 with httmock.HTTMock(_mock_response_with_bad_json):
75 with self.assertRaises(suppliers.UnauthenticatedException):
76 supplier.supply(issuer)
77
78
79 class JwksSupplierTest(unittest.TestCase):
80 _mock_timer = mock.MagicMock()
81
82 def setUp(self):
83 self._key_uri_supplier = mock.MagicMock()
84 self._jwks_uri_supplier = suppliers.JwksSupplier(self._key_uri_supplier)
85
86 def test_supply_with_unknown_issuer(self):
87 self._key_uri_supplier.supply.return_value = None
88 issuer = "unknown-issuer"
89 expected_message = "Cannot find the `jwks_uri` for issuer " + issuer
90 with self.assertRaisesRegexp(suppliers.UnauthenticatedException,
91 expected_message):
92 self._jwks_uri_supplier.supply(issuer)
93
94 def test_supply_with_invalid_json_response(self):
95 scheme = "https"
96 issuer = "issuer.com"
97 self._key_uri_supplier.supply.return_value = scheme + "://" + issuer
98
99 @httmock.urlmatch(scheme=scheme, netloc=issuer)
100 def _mock_response_with_invalid_json(url, response): # pylint: disable=unus ed-argument
101 return "invalid-json"
102
103 with httmock.HTTMock(_mock_response_with_invalid_json):
104 with self.assertRaises(suppliers.UnauthenticatedException):
105 self._jwks_uri_supplier.supply(issuer)
106
107 def test_supply_jwks(self):
108 rsa_key = PublicKey.RSA.generate(2048)
109 jwks = jwk.KEYS()
110 jwks.wrap_add(rsa_key)
111
112 scheme = "https"
113 issuer = "issuer.com"
114 self._key_uri_supplier.supply.return_value = scheme + "://" + issuer
115
116 @httmock.urlmatch(scheme=scheme, netloc=issuer)
117 def _mock_response_with_jwks(url, response): # pylint: disable=unused-argum ent
118 return jwks.dump_jwks()
119
120 with httmock.HTTMock(_mock_response_with_jwks):
121 actual_jwks = self._jwks_uri_supplier.supply(issuer)
122 self.assertEquals(1, len(actual_jwks))
123 actual_key = actual_jwks[0].key
124 self.assertEquals(rsa_key.n, actual_key.n)
125 self.assertEquals(rsa_key.e, actual_key.e)
126
127 def test_supply_jwks_with_x509_certificate(self):
128 rsa_key = PublicKey.RSA.generate(2048)
129 cert = rsa_key.publickey().exportKey("PEM")
130 kid = "rsa-cert"
131
132 scheme = "https"
133 issuer = "issuer.com"
134 self._key_uri_supplier.supply.return_value = scheme + "://" + issuer
135
136 @httmock.urlmatch(scheme=scheme, netloc=issuer)
137 def _mock_response_with_x509_certificates(url, response): # pylint: disable =unused-argument
138 return json.dumps({kid: cert})
139
140 with httmock.HTTMock(_mock_response_with_x509_certificates):
141 actual_jwks = self._jwks_uri_supplier.supply(issuer)
142 self.assertEquals(1, len(actual_jwks))
143 actual_key = actual_jwks[0].key
144
145 self.assertEquals(kid, actual_jwks[0].kid)
146 self.assertEquals(rsa_key.n, actual_key.n)
147 self.assertEquals(rsa_key.e, actual_key.e)
148
149 def test_supply_empty_x509_certificate(self):
150 scheme = "https"
151 issuer = "issuer.com"
152 self._key_uri_supplier.supply.return_value = scheme + "://" + issuer
153
154 @httmock.urlmatch(scheme=scheme, netloc=issuer)
155 def _mock_invalid_response(url, response): # pylint: disable=unused-argumen t
156 return json.dumps({"kid": "invlid-certificate"})
157
158 with httmock.HTTMock(_mock_invalid_response):
159 with self.assertRaises(suppliers.UnauthenticatedException):
160 self._jwks_uri_supplier.supply(issuer)
161
162 @mock.patch("time.time", _mock_timer)
163 def test_supply_cached_jwks(self):
164 rsa_key = PublicKey.RSA.generate(2048)
165 jwks = jwk.KEYS()
166 jwks.wrap_add(rsa_key)
167
168 scheme = "https"
169 issuer = "issuer.com"
170 self._key_uri_supplier.supply.return_value = scheme + "://" + issuer
171
172 @httmock.urlmatch(scheme=scheme, netloc=issuer)
173 def _mock_response_with_jwks(url, response): # pylint: disable=unused-argum ent
174 return jwks.dump_jwks()
175
176 with httmock.HTTMock(_mock_response_with_jwks):
177 JwksSupplierTest._mock_timer.return_value = 10
178 self.assertEqual(1, len(self._jwks_uri_supplier.supply(issuer)))
179
180 # Add an additional key to the JWKS to be returned by the HTTP request.
181 jwks.wrap_add(PublicKey.RSA.generate(2048))
182
183 # Forward the clock by 1 second. The JWKS should remain cached.
184 JwksSupplierTest._mock_timer.return_value += 1
185 self._jwks_uri_supplier.supply(issuer)
186 self.assertEqual(1, len(self._jwks_uri_supplier.supply(issuer)))
187
188 # Forward the clock by 5 minutes. The cache entry should have expired so
189 # the returned JWKS should be the updated one with two keys.
190 JwksSupplierTest._mock_timer.return_value += 5 * 60
191 self._jwks_uri_supplier.supply(issuer)
192 self.assertEqual(2, len(self._jwks_uri_supplier.supply(issuer)))
OLDNEW
« no previous file with comments | « third_party/google-endpoints/test/test_signing.py ('k') | third_party/google-endpoints/test/test_timestamp.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698