Mercurial > p > roundup > code
comparison test/rest_common.py @ 6314:a2fbd3592322
pyjwt 2.00 changed return type of jwt.encode from byte to str
Need to change tests to only do b2s conversion if using version before
2.0.0. Note 2.0.0 drops support for python 2. Also it is not
installed for the python 3.4 ci test by pip install.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Fri, 01 Jan 2021 22:16:45 -0500 |
| parents | 6ef7b66774b4 |
| children | 323661f7c89c |
comparison
equal
deleted
inserted
replaced
| 6313:e3edb0b44d94 | 6314:a2fbd3592322 |
|---|---|
| 66 | 66 |
| 67 backend = None | 67 backend = None |
| 68 url_pfx = 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/' | 68 url_pfx = 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/' |
| 69 | 69 |
| 70 def setUp(self): | 70 def setUp(self): |
| 71 from packaging import version | |
| 72 | |
| 71 self.dirname = '_test_rest' | 73 self.dirname = '_test_rest' |
| 72 # set up and open a tracker | 74 # set up and open a tracker |
| 73 # Set optimize=True as code under test (Client.main()::determine_user) | 75 # Set optimize=True as code under test (Client.main()::determine_user) |
| 74 # will close and re-open the database on user changes. This wipes | 76 # will close and re-open the database on user changes. This wipes |
| 75 # out additions to the schema needed for testing. | 77 # out additions to the schema needed for testing. |
| 160 'aud': self.db.config.TRACKER_WEB, | 162 'aud': self.db.config.TRACKER_WEB, |
| 161 'roles': [ 'User' ], | 163 'roles': [ 'User' ], |
| 162 'iat': now_ts, | 164 'iat': now_ts, |
| 163 'exp': plus1min_ts, | 165 'exp': plus1min_ts, |
| 164 } | 166 } |
| 167 | |
| 168 # in version 2.0.0 and newer jwt.encode returns string | |
| 169 # not bytestring. So we have to skip b2s conversion | |
| 165 | 170 |
| 171 if version.parse(jwt.__version__) >= version.parse('2.0.0'): | |
| 172 tostr = lambda x: x | |
| 173 else: | |
| 174 tostr = b2s | |
| 175 | |
| 166 self.jwt = {} | 176 self.jwt = {} |
| 167 self.claim = {} | 177 self.claim = {} |
| 168 # generate invalid claim with expired timestamp | 178 # generate invalid claim with expired timestamp |
| 169 self.claim['expired'] = copy(claim) | 179 self.claim['expired'] = copy(claim) |
| 170 self.claim['expired']['exp'] = expired_ts | 180 self.claim['expired']['exp'] = expired_ts |
| 171 self.jwt['expired'] = b2s(jwt.encode(self.claim['expired'], secret, | 181 self.jwt['expired'] = tostr(jwt.encode(self.claim['expired'], secret, |
| 172 algorithm='HS256')) | 182 algorithm='HS256')) |
| 173 | 183 |
| 174 # generate valid claim with user role | 184 # generate valid claim with user role |
| 175 self.claim['user'] = copy(claim) | 185 self.claim['user'] = copy(claim) |
| 176 self.claim['user']['exp'] = plus1min_ts | 186 self.claim['user']['exp'] = plus1min_ts |
| 177 self.jwt['user'] = b2s(jwt.encode(self.claim['user'], secret, | 187 self.jwt['user'] = tostr(jwt.encode(self.claim['user'], secret, |
| 178 algorithm='HS256')) | 188 algorithm='HS256')) |
| 179 # generate invalid claim bad issuer | 189 # generate invalid claim bad issuer |
| 180 self.claim['badiss'] = copy(claim) | 190 self.claim['badiss'] = copy(claim) |
| 181 self.claim['badiss']['iss'] = "http://someissuer/bugs" | 191 self.claim['badiss']['iss'] = "http://someissuer/bugs" |
| 182 self.jwt['badiss'] = b2s(jwt.encode(self.claim['badiss'], secret, | 192 self.jwt['badiss'] = tostr(jwt.encode(self.claim['badiss'], secret, |
| 183 algorithm='HS256')) | 193 algorithm='HS256')) |
| 184 # generate invalid claim bad aud(ience) | 194 # generate invalid claim bad aud(ience) |
| 185 self.claim['badaud'] = copy(claim) | 195 self.claim['badaud'] = copy(claim) |
| 186 self.claim['badaud']['aud'] = "http://someaudience/bugs" | 196 self.claim['badaud']['aud'] = "http://someaudience/bugs" |
| 187 self.jwt['badaud'] = b2s(jwt.encode(self.claim['badaud'], secret, | 197 self.jwt['badaud'] = tostr(jwt.encode(self.claim['badaud'], secret, |
| 188 algorithm='HS256')) | 198 algorithm='HS256')) |
| 189 # generate invalid claim bad sub(ject) | 199 # generate invalid claim bad sub(ject) |
| 190 self.claim['badsub'] = copy(claim) | 200 self.claim['badsub'] = copy(claim) |
| 191 self.claim['badsub']['sub'] = str("99") | 201 self.claim['badsub']['sub'] = str("99") |
| 192 self.jwt['badsub'] = b2s(jwt.encode(self.claim['badsub'], secret, | 202 self.jwt['badsub'] = tostr(jwt.encode(self.claim['badsub'], secret, |
| 193 algorithm='HS256')) | 203 algorithm='HS256')) |
| 194 # generate invalid claim bad roles | 204 # generate invalid claim bad roles |
| 195 self.claim['badroles'] = copy(claim) | 205 self.claim['badroles'] = copy(claim) |
| 196 self.claim['badroles']['roles'] = [ "badrole1", "badrole2" ] | 206 self.claim['badroles']['roles'] = [ "badrole1", "badrole2" ] |
| 197 self.jwt['badroles'] = b2s(jwt.encode(self.claim['badroles'], secret, | 207 self.jwt['badroles'] = tostr(jwt.encode(self.claim['badroles'], secret, |
| 198 algorithm='HS256')) | 208 algorithm='HS256')) |
| 199 # generate valid claim with limited user:email role | 209 # generate valid claim with limited user:email role |
| 200 self.claim['user:email'] = copy(claim) | 210 self.claim['user:email'] = copy(claim) |
| 201 self.claim['user:email']['roles'] = [ "user:email" ] | 211 self.claim['user:email']['roles'] = [ "user:email" ] |
| 202 self.jwt['user:email'] = b2s(jwt.encode(self.claim['user:email'], secret, | 212 self.jwt['user:email'] = tostr(jwt.encode(self.claim['user:email'], secret, |
| 203 algorithm='HS256')) | 213 algorithm='HS256')) |
| 204 | 214 |
| 205 # generate valid claim with limited user:emailnorest role | 215 # generate valid claim with limited user:emailnorest role |
| 206 self.claim['user:emailnorest'] = copy(claim) | 216 self.claim['user:emailnorest'] = copy(claim) |
| 207 self.claim['user:emailnorest']['roles'] = [ "user:emailnorest" ] | 217 self.claim['user:emailnorest']['roles'] = [ "user:emailnorest" ] |
| 208 self.jwt['user:emailnorest'] = b2s(jwt.encode(self.claim['user:emailnorest'], secret, | 218 self.jwt['user:emailnorest'] = tostr(jwt.encode(self.claim['user:emailnorest'], secret, |
| 209 algorithm='HS256')) | 219 algorithm='HS256')) |
| 210 | 220 |
| 211 self.db.tx_Source = 'web' | 221 self.db.tx_Source = 'web' |
| 212 | 222 |
| 213 self.db.issue.addprop(tx_Source=hyperdb.String()) | 223 self.db.issue.addprop(tx_Source=hyperdb.String()) |
