|
| 1 | +# PyBites Code Challenge 28 - The Art of Refactoring: Improve Your Code |
| 2 | + |
| 3 | +For this challenge I decided to refactor a Flask route handler because of this [issue](https://github.com/realpython/flask-jwt-auth/issues/9) that was added to the [Flask JWT Auth](https://github.com/realpython/flask-jwt-auth) project. Since this project is the example app built for the [Token-Based Authentication With Flask](https://realpython.com/blog/python/token-based-authentication-with-flask/) blog post, I had to update the blog post as well. |
| 4 | + |
| 5 | +## Issue |
| 6 | + |
| 7 | +Review the actual submitted [issue](https://github.com/realpython/flask-jwt-auth/issues/9) from GitHub for full details. |
| 8 | + |
| 9 | +Essentially, the following code only handles situations where the `Authorization` header has a space between `Bearer` and the actual token: |
| 10 | + |
| 11 | +``` |
| 12 | +Bearer TOKEN_VALUE |
| 13 | +``` |
| 14 | + |
| 15 | +Code: |
| 16 | + |
| 17 | +```python |
| 18 | +def get(self): |
| 19 | + # get the auth token |
| 20 | + auth_header = request.headers.get('Authorization') |
| 21 | + if auth_header: |
| 22 | + auth_token = auth_header.split(" ")[1] |
| 23 | + else: |
| 24 | + auth_token = '' |
| 25 | + if auth_token: |
| 26 | + resp = User.decode_auth_token(auth_token) |
| 27 | +``` |
| 28 | + |
| 29 | +Even though the correct format for authorization is `Bearer TOKEN_VALUE`, it's best to handle situations where an end-user does not correctly format the auth header. |
| 30 | + |
| 31 | +Test coverage before refactor: |
| 32 | + |
| 33 | +```sh |
| 34 | +Name Stmts Miss Branch BrPart Cover |
| 35 | +---------------------------------------------------------------- |
| 36 | +project/__init__.py 0 0 0 0 100% |
| 37 | +project/server/__init__.py 11 0 0 0 100% |
| 38 | +project/server/auth/views.py 84 16 18 5 79% |
| 39 | +project/server/models.py 47 5 6 1 89% |
| 40 | +---------------------------------------------------------------- |
| 41 | +TOTAL 142 21 24 6 84% |
| 42 | +``` |
| 43 | + |
| 44 | +## Refactor |
| 45 | + |
| 46 | +You can view the full refactor [here](https://github.com/realpython/flask-jwt-auth/pull/10). |
| 47 | + |
| 48 | +Test: |
| 49 | + |
| 50 | +```python |
| 51 | +def test_user_status_malformed_bearer_token(self): |
| 52 | + """ Test for user status with malformed bearer token""" |
| 53 | + with self.client: |
| 54 | + resp_register = register_user( self, '[email protected]', '123456') |
| 55 | + response = self.client.get( |
| 56 | + '/auth/status', |
| 57 | + headers=dict( |
| 58 | + Authorization='Bearer' + json.loads( |
| 59 | + resp_register.data.decode() |
| 60 | + )['auth_token'] |
| 61 | + ) |
| 62 | + ) |
| 63 | + data = json.loads(response.data.decode()) |
| 64 | + self.assertTrue(data['status'] == 'fail') |
| 65 | + self.assertTrue(data['message'] == 'Bearer token malformed.') |
| 66 | + self.assertEqual(response.status_code, 401) |
| 67 | +``` |
| 68 | + |
| 69 | +Code: |
| 70 | + |
| 71 | +```python |
| 72 | +def get(self): |
| 73 | + # get the auth token |
| 74 | + auth_header = request.headers.get('Authorization') |
| 75 | + if auth_header: |
| 76 | + try: |
| 77 | + auth_token = auth_header.split(" ")[1] |
| 78 | + except IndexError: |
| 79 | + responseObject = { |
| 80 | + 'status': 'fail', |
| 81 | + 'message': 'Bearer token malformed.' |
| 82 | + } |
| 83 | + return make_response(jsonify(responseObject)), 401 |
| 84 | + else: |
| 85 | + auth_token = '' |
| 86 | + if auth_token: |
| 87 | + resp = User.decode_auth_token(auth_token) |
| 88 | +``` |
| 89 | + |
| 90 | +Test coverage after refactor: |
| 91 | + |
| 92 | +```sh |
| 93 | +Name Stmts Miss Branch BrPart Cover |
| 94 | +---------------------------------------------------------------- |
| 95 | +project/__init__.py 0 0 0 0 100% |
| 96 | +project/server/__init__.py 11 0 0 0 100% |
| 97 | +project/server/auth/views.py 88 16 18 5 80% |
| 98 | +project/server/models.py 47 5 6 1 89% |
| 99 | +---------------------------------------------------------------- |
| 100 | +TOTAL 146 21 24 6 84% |
| 101 | +``` |
0 commit comments