Skip to content
This repository was archived by the owner on Feb 3, 2020. It is now read-only.

Commit 7df1498

Browse files
authored
Merge pull request #2 from chinoio/consent-tracking
Added support for `public` Applications
2 parents 807beb0 + 48d50a5 commit 7df1498

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

chino/api.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,9 @@ def login(self, username, password):
199199
auth = self.auth
200200
# self.auth = None
201201
url = "auth/token/"
202-
pars = dict(username=username, password=password,
202+
pars = dict(username=username, password=password, client_id=self.auth.client_id, client_secret=self.auth.client_secret,
203203
grant_type='password')
204+
204205
try:
205206
self.auth.set_auth_application()
206207
result = self.apicall('POST', url, form=pars)
@@ -222,6 +223,7 @@ def refresh(self):
222223
url = "auth/token/"
223224
pars = dict(grant_type='refresh_token', client_id=self.auth.client_id, client_secret=self.auth.client_secret,
224225
refresh_token=self.auth.refresh_token)
226+
225227
try:
226228
self.auth.set_auth_null()
227229
result = self.apicall('POST', url, form=pars)
@@ -708,13 +710,14 @@ def __init__(self, customer_id, customer_key=None, bearer_token=None, client_id=
708710
self.client_secret = client_secret
709711
self.refresh_token = refresh_token
710712
self.bearer_exp = bearer_exp
713+
711714
if customer_key:
712715
# if customer_key is set, then set auth as that
713716
self.set_auth_admin()
714717
elif bearer_token:
715718
# if access_token is set, then use it as customer
716719
self.set_auth_user()
717-
elif client_id and client_secret:
720+
elif client_id:
718721
self.set_auth_application()
719722

720723
def set_auth_admin(self):
@@ -727,7 +730,10 @@ def set_auth_null(self):
727730
self.__auth = None
728731

729732
def set_auth_application(self):
730-
self.__auth = HTTPBasicAuth(self.client_id, self.client_secret)
733+
if self.client_secret:
734+
self.__auth = HTTPBasicAuth(self.client_id, self.client_secret)
735+
else:
736+
self.set_auth_null()
731737

732738
def get_auth(self):
733739
return self.__auth
@@ -878,15 +884,17 @@ def list(self, **pars):
878884
url = "auth/applications"
879885
return ListResult(Application, self.apicall('GET', url, params=pars))
880886

881-
def create(self, name, grant_type='password', redirect_url=''):
887+
def create(self, name, grant_type='password', redirect_url='', client_type='confidential'):
882888
"""
883-
Creates a Application
889+
Creates a Application.
890+
Note: optional parameter client_type can be either 'public' or 'confidential'.
891+
"confidential" is the default value.
884892
885893
:param name: (str) the name of the Application
886894
:return: (dict) the Application.
887895
"""
888896
data = dict(name=name, grant_type=grant_type,
889-
redirect_url=redirect_url)
897+
redirect_url=redirect_url, client_type=client_type)
890898
url = "auth/applications"
891899
return Application(**self.apicall('POST', url, data=data)[Application.__str_name__])
892900

test/tests_chino.py

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def setUp(self):
8181
fields = [dict(name='first_name', type='string'), dict(name='last_name', type='string'),
8282
dict(name='email', type='string')]
8383
self.us = self.chino.user_schemas.create('test', fields)
84+
self.app_list = []
8485

8586
def tearDown(self):
8687
# if user has been created we remove it.
@@ -93,6 +94,10 @@ def tearDown(self):
9394
if hasattr(self, 'app'):
9495
self.chino.applications.delete(self.app._id)
9596

97+
# delete also every Application which was created forthis test
98+
for app in self.chino.applications.list().to_dict()['applications']:
99+
self.chino.applications.delete(app['app_id'], force=True)
100+
96101
def test_list(self):
97102
list = self.chino.users.list(self.us._id)
98103
self.assertIsNotNone(list.paging)
@@ -155,7 +160,39 @@ def test_auth(self):
155160
self.assertEqual(ste_2.username, EDIT)
156161

157162
self.chino_user.users.refresh()
158-
# now should be impossible to create the user
163+
# it should be impossible to create the user after login with self.chino_user (no admin access)
164+
self.assertRaises(CallError, self.chino_user.users.create, self.us._id, username='error', password='12345678',
165+
attributes=dict(first_name='john', last_name='doe',
166+
167+
168+
self.chino_user.users.logout()
169+
self.assertRaises(Exception, self.chino_user.users.login, EDIT, '')
170+
171+
self.assertRaises(CallError, self.chino.users.current)
172+
173+
def test_auth_public(self):
174+
# login
175+
NAME = 'test.user.new'
176+
EDIT = NAME + '.edited'
177+
self.app = self.chino.applications.create("test", grant_type='password', client_type='public')
178+
# Init 'public' client
179+
self.chino_user = ChinoAPIClient(customer_id=cfg.customer_id,
180+
url=cfg.url,
181+
client_id=self.app.app_id,
182+
client_secret=None
183+
)
184+
self.assertIsNone(self.chino_user.auth.client_secret)
185+
186+
user = self.chino.users.create(self.us._id, username=EDIT, password='12345678',
187+
attributes=dict(first_name='john', last_name='doe',
188+
189+
190+
self.chino_user.users.login(EDIT, '12345678')
191+
ste_2 = self.chino_user.users.current()
192+
self.assertEqual(ste_2.username, EDIT)
193+
194+
self.chino_user.users.refresh()
195+
# it should be impossible to create the user after login with self.chino_user (no admin access)
159196
self.assertRaises(CallError, self.chino_user.users.create, self.us._id, username='error', password='12345678',
160197
attributes=dict(first_name='john', last_name='doe',
161198
@@ -178,11 +215,20 @@ def tearDown(self):
178215
self.logger.debug("tearing down %s", self.user)
179216

180217
def test_CRUD(self):
181-
app = self.chino.applications.create(name='tessst', grant_type='password')
218+
app = self.chino.applications.create(name='tesssst_confidential', grant_type='password')
219+
app_public = self.chino.applications.create(name='test_public', grant_type='password', client_type='public')
220+
221+
app_public1 = self.chino.applications.detail(app_public._id)
222+
self.assertEqual(app_public._id, app_public1._id)
223+
self.assertEqual(app_public.app_name, app_public1.app_name)
224+
225+
newname = 'test_confidential'
226+
self.chino.applications.update(app._id, name=newname)
182227
app1 = self.chino.applications.detail(app._id)
183-
self.chino.applications.update(app1._id, name='asds')
184-
app2 = self.chino.applications.detail(app1._id)
228+
self.assertEqual(app1.app_name, newname)
229+
185230
apps = self.chino.applications.list()
231+
self.chino.applications.delete(app_public1._id, force=True)
186232
self.chino.applications.delete(app1._id, force=True)
187233

188234

0 commit comments

Comments
 (0)