Skip to content

Commit

Permalink
fix: Resolve some bug risks and code quality issues (#497)
Browse files Browse the repository at this point in the history
Changes:
- Fix dangerous default argument in `twilio/base/serialize.py` (PYL-W0102)
- Classmethods should have `cls` as the first arg. in `twilio/jwt/validation/__init__.py` and `twilio/base/page.py` (PYL-C0202)
- Removed unused imports in `tests/unit/http/test_validation_client.py` and `tests/unit/http/test_http_client.py` (PYL-W0611)
- Fix `len()` used as condition in `twilio/request_validator.py` (PYL-C1801)

Also added `.deepsource.toml` configuration file to run continuous static analysis on the repository with DeepSource.
  • Loading branch information
sanketsaurav authored and childish-sambino committed Nov 4, 2019
1 parent af20c9e commit d647789
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 11 deletions.
25 changes: 25 additions & 0 deletions .deepsource.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version = 1

exclude_patterns = [
'examples/**',

# auto-generated files
'twilio/rest/**',
'twilio/twiml/**',
'tests/integration/**',

# compat files
'twilio/compat.py',
]

test_patterns = [
'tests/**'
]

[[analyzers]]
name = "python"
enabled = true

[analyzers.meta]
max_line_length = 100
skip_doc_coverage = ["module", "magic", "class"]
4 changes: 1 addition & 3 deletions tests/unit/http/test_http_client.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# -*- coding: utf-8 -*-
import six

import unittest

from mock import patch, Mock
from requests import Request, Session
from requests import Session

from twilio.http.http_client import TwilioHttpClient
from twilio.http.response import Response
Expand Down
1 change: 0 additions & 1 deletion tests/unit/http/test_validation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import unittest

import mock
from mock import patch, Mock
from requests import Request
from requests import Session
Expand Down
2 changes: 1 addition & 1 deletion twilio/base/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def next(self):
return self.get_instance(next(self._records))

@classmethod
def process_response(self, response):
def process_response(cls, response):
"""
Load a JSON response.
Expand Down
9 changes: 8 additions & 1 deletion twilio/base/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ def prefixed_collapsible_map(m, prefix):
if m == values.unset:
return {}

def flatten_dict(d, result={}, prv_keys=[]):
def flatten_dict(d, result=None, prv_keys=None):

if result is None:
result = {}

if prv_keys is None:
prv_keys = []

for k, v in d.items():
if isinstance(v, dict):
flatten_dict(v, result, prv_keys + [k])
Expand Down
4 changes: 2 additions & 2 deletions twilio/jwt/validation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ def _generate_payload(self):
}

@classmethod
def _sort_and_join(self, values, joiner):
def _sort_and_join(cls, values, joiner):
if isinstance(values, string_types):
return values
return joiner.join(sorted(values))

@classmethod
def _hash(self, input_str):
def _hash(cls, input_str):
if not input_str:
return input_str

Expand Down
6 changes: 3 additions & 3 deletions twilio/request_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def add_port(uri):
"""
if uri.port:
return uri.geturl()

port = 443 if uri.scheme == "https" else 80
new_netloc = uri.netloc + ":" + str(port)
new_uri = uri._replace(netloc=new_netloc)
Expand All @@ -75,7 +75,7 @@ def compute_signature(self, uri, params, utf=PY3):
:returns: The computed signature
"""
s = uri
if len(params) > 0:
if params:
for k, v in sorted(params.items()):
s += k + v

Expand Down Expand Up @@ -117,7 +117,7 @@ def validate(self, uri, params, signature):
valid_body_hash = compare(self.compute_hash(params), query["bodySHA256"][0])
params = {}

# check signature of uri with and without port,
# check signature of uri with and without port,
# since sig generation on back end is inconsistent
valid_signature = compare(self.compute_signature(uri_without_port, params), signature)
valid_signature_with_port = compare(self.compute_signature(uri_with_port, params), signature)
Expand Down

0 comments on commit d647789

Please sign in to comment.