Skip to content

Commit

Permalink
Fixed oauth_authorization_uri creation and added a test.
Browse files Browse the repository at this point in the history
  • Loading branch information
angelomelonas committed Aug 14, 2020
1 parent 512e57e commit 0efd0f9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
5 changes: 2 additions & 3 deletions bunq/sdk/http/http_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@


class HttpUtil:
# Query constants.
QUERY_FORMAT = "%s=%s"
QUERY_DELIMITER = "&"
QUERY_FORMAT = '{}={}'
QUERY_DELIMITER = '&'

@classmethod
def create_query_string(cls, all_parameter: Dict[str, str]):
Expand Down
13 changes: 9 additions & 4 deletions bunq/sdk/model/core/oauth_authorization_uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

class OauthAuthorizationUri(BunqModel):
# Auth constants.
AUTH_URI_FORMAT_SANDBOX = "https://oauth.sandbox.bunq.com/auth?%s"
AUTH_URI_FORMAT_PRODUCTION = "https://oauth.bunq.com/auth?%s"
AUTH_URI_FORMAT_SANDBOX = "https://oauth.sandbox.bunq.com/auth?{}"
AUTH_URI_FORMAT_PRODUCTION = "https://oauth.bunq.com/auth?{}"

# Field constants
FIELD_RESPONSE_TYPE = "response_type"
Expand All @@ -38,17 +38,22 @@ def create(cls,
state: str = None) -> OauthAuthorizationUri:
all_request_parameter = {
cls.FIELD_REDIRECT_URI: redirect_uri,
cls.FIELD_RESPONSE_TYPE: response_type.value,
cls.FIELD_CLIENT_ID: client.client_id
cls.FIELD_RESPONSE_TYPE: response_type.name.lower()
}

if client.client_id is not None:
all_request_parameter[cls.FIELD_CLIENT_ID] = client.client_id

if state is not None:
all_request_parameter[cls.FIELD_STATE] = state

return OauthAuthorizationUri(
cls.determine_auth_uri_format().format(HttpUtil.create_query_string(all_request_parameter))
)

def get_authorization_uri(self) -> str:
return self._authorization_uri

def is_all_field_none(self) -> bool:
if self._authorization_uri is None:
return True
Expand Down
26 changes: 26 additions & 0 deletions tests/model/core/test_oauth_authorization_uri.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from bunq.sdk.context.bunq_context import BunqContext
from bunq.sdk.model.core.oauth_authorization_uri import OauthAuthorizationUri
from bunq.sdk.model.core.oauth_response_type import OauthResponseType
from bunq.sdk.model.generated.endpoint import OauthClient
from tests.bunq_test import BunqSdkTestCase


class TestOauthAuthorizationUri(BunqSdkTestCase):
_TEST_EXPECT_URI = 'https://oauth.sandbox.bunq.com/auth?redirect_uri=redirecturi&response_type=code&state=state'
_TEST_REDIRECT_URI = 'redirecturi'
_TEST_STATUS = 'status'
_TEST_STATE = 'state'

@classmethod
def setUpClass(cls) -> None:
BunqContext.load_api_context(cls._get_api_context())

def test_oauth_authorization_uri_create(self) -> None:
uri = OauthAuthorizationUri.create(
OauthResponseType(OauthResponseType.CODE),
self._TEST_REDIRECT_URI,
OauthClient(self._TEST_STATUS),
self._TEST_STATE
).get_authorization_uri()

self.assertEqual(self._TEST_EXPECT_URI, uri)

0 comments on commit 0efd0f9

Please sign in to comment.