Skip to content

Commit

Permalink
Make plugin only action to be explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Lei committed Nov 10, 2016
1 parent 2196850 commit 177320c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
9 changes: 5 additions & 4 deletions skygear/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,17 @@ def _request_url(self, action_name):
endpoint = endpoint[:-1] if endpoint[-1] == '/' else endpoint
return endpoint + '/' + action_name.replace(':', '/')

def _payload(self, action_name, params):
def _payload(self, action_name, params, plugin_request):
payload = params.copy() if isinstance(params, dict) else {}
payload['action'] = action_name
payload['_from_plugin'] = True
if self.access_token:
payload['access_token'] = self.access_token
if self.api_key:
payload['api_key'] = self.api_key
if self.user_id:
payload['_user_id'] = self.user_id
if plugin_request:
payload['_from_plugin'] = True
return payload

@classmethod
Expand All @@ -85,9 +86,9 @@ def set_default_endpoint(cls, endpoint):
def set_default_apikey(cls, api_key):
cls.api_key = api_key

def send_action(self, action_name, params):
def send_action(self, action_name, params, plugin_request=False):
resp = send_action(self._request_url(action_name),
self._payload(action_name, params))
self._payload(action_name, params, plugin_request))
if 'error' in resp:
raise error.SkygearException.from_dict(resp['error'])

Expand Down
22 changes: 13 additions & 9 deletions skygear/tests/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,37 +49,41 @@ def test_send_date(self, mock_requests):
class TestContainer():
def test_payload_include_action(self):
c = SkygearContainer(endpoint='endpoint', access_token='access-token')
payload = c._payload('action:work', {'data': 'hello world'})
assert payload['_from_plugin']
payload = c._payload('action:work', {'data': 'hello world'}, False)
assert payload['action'] == 'action:work'
assert payload['data'] == 'hello world'

def test_payload_include_api_key(self):
c = SkygearContainer(endpoint='endpoint', api_key='api-key')
payload = c._payload('action:work', {})
assert payload['_from_plugin']
payload = c._payload('action:work', {}, False)
assert payload['api_key'] == 'api-key'

def test_payload_include_user_id(self):
c = SkygearContainer(endpoint='endpoint', api_key='api-key',
user_id='user-id')
payload = c._payload('action:work', {})
payload = c._payload('action:work', {}, False)
assert payload['_user_id'] == 'user-id'
assert payload['_from_plugin']

def test_payload_include_access_token(self):
c = SkygearContainer(endpoint='endpoint', access_token='access-token')
payload = c._payload('action:work', {})
assert payload['_from_plugin']
payload = c._payload('action:work', {}, False)
assert payload['access_token'] == 'access-token'

def test_payload_include_all_credentials(self):
c = SkygearContainer(endpoint='endpoint',
access_token='access-token',
api_key='api-key',
user_id='user-id')
payload = c._payload('action:work', {})
payload = c._payload('action:work', {}, False)
assert payload['access_token'] == 'access-token'
assert payload['api_key'] == 'api-key'
assert payload['_user_id'] == 'user-id'

def test_plugin_request_payload(self):
c = SkygearContainer(endpoint='endpoint',
access_token='access-token',
api_key='master-key')
payload = c._payload('action:work', {}, True)
assert payload['access_token'] == 'access-token'
assert payload['api_key'] == 'master-key'
assert payload['_from_plugin']

0 comments on commit 177320c

Please sign in to comment.