Skip to content

Commit f990176

Browse files
AnatoliiSrszalski
authored andcommitted
Fix/release 0 1 1 fixes (#21)
* Django plugin: Changes assert to self.assert* in tests * Django plugin: Removes unused attr 'request' in context processors * Django plugin: Adds a separate login_button.html template * Django plugin: Switching to reverse url resolver for redirects * Django plugin: Includes html templates to binary distribution
1 parent 0b127c7 commit f990176

File tree

11 files changed

+80
-49
lines changed

11 files changed

+80
-49
lines changed

plugins/django_yoti/README.md

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@ INSTALLED_APPS = [
1717
]
1818
```
1919

20-
* In order to use context tags inside your template (`{{ yoti_site_verification }}`, `{{ yoti_login_button_*}}`),
21-
you should include `django_yoti`'s context processors into your templates configuration like this:
20+
* Django Yoti plugin provides the following template context vars:
21+
- `yoti_site_verification`
22+
- `yoti_application_id`
23+
- `yoti_login_button_*`
24+
25+
* If you're using django template backend that supports context processors
26+
like default DTL (Django Template Language) and want to use context tags
27+
inside your template (e.g. `{{ yoti_login_button_*}}`), then you should
28+
include `django_yoti`'s context processors into your templates
29+
configuration like this:
2230
```python
2331
# your_django_project/settings.py
2432

@@ -36,6 +44,13 @@ TEMPLATES = [
3644
},
3745
]
3846
```
47+
* Otherwise you'll have to pass yoti context to your templates manually:
48+
```python
49+
from django_yoti import yoti_context
50+
51+
def some_view(request):
52+
return render(request, 'index.html', yoti_context)
53+
```
3954

4055
* And then use the following settings to configure the plugin:
4156

@@ -104,7 +119,7 @@ YOTI = {
104119
```
105120
* **`YOTI_LOGIN_VIEW`**<br>
106121
If *not* authenticated user is trying to access a view with
107-
`@yoti_authenticated` decorator, he/she will be redirected to this view.
122+
`@yoti_authenticated` decorator, he/she will be redirected to this view.<br>
108123
Example: `login`<br>
109124
In this case you should have something like this in your project's `urls.py` file:
110125
```python
@@ -160,11 +175,7 @@ you can use a `{{ yoti_site_verification }}` tag inside 'head' tag of that page.
160175
{{ yoti_login_button_md }}
161176
{{ yoti_login_button_lg }}
162177
```
163-
- or with a default one `{{ yoti_login_button }}`, in case you're using DTL
164-
as a template language)
165-
- or with `{{ yoti_login_button(size='small', text='Log In with Yoti')`, in
166-
case you're using Jinja2 as your template language<br>
167-
Available button sizes: `small`, `medium`, `large`
178+
- or with a default one `{{ yoti_login_button }}`<br>
168179

169180
By clicking this button, user will be redirected to the Yoti Authentication page.
170181

@@ -185,6 +196,23 @@ def profile_view(request):
185196
user_profile = request.yoti_user_profile
186197
return render(request, 'profile.html', user_profile)
187198
```
199+
<br>
200+
201+
To make `yoti_authenticated` decorator work with django class based
202+
views as well as with classic method based views, you can use it while
203+
declaring your project's urls:
204+
```python
205+
# your_django_project/urls.py
206+
from django_yoti import yoti_authenticated
207+
208+
urlpatterns = [
209+
...
210+
url(r'^profile/', yoti_authenticated(views.profile_view), name='yoti_profile'),
211+
# or
212+
url(r'^profile/', yoti_authenticated(views.ProfileView.as_view()), name='yoti_profile'),
213+
...
214+
]
215+
```
188216

189217
4. All *not authenticated* users trying to access endpoint with this decorator,
190218
will be redirected to an endpoint, provided by the `YOTI_LOGIN_VIEW` setting.

plugins/django_yoti/django_yoti/context_processors.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
from .settings import YOTI_VERIFICATION_KEY, YOTI_APPLICATION_ID
55

66

7-
def yoti_context(request):
8-
context = login_button_context(request)
9-
context.update(site_verification_context(request))
10-
context.update(application_context(request))
7+
def yoti_context(request=None):
8+
context = login_button_context()
9+
context.update(site_verification_context())
10+
context.update(application_context())
1111
return context
1212

1313

14-
def login_button_context(request):
14+
def login_button_context():
1515
return {
1616
'yoti_login_button': get_login_button_html,
1717
'yoti_login_button_sm': get_login_button_html('small'),
@@ -20,12 +20,12 @@ def login_button_context(request):
2020
}
2121

2222

23-
def site_verification_context(request):
23+
def site_verification_context():
2424
raw_text = '<meta name="yoti-site-verification" content="{0}">'.format(
2525
YOTI_VERIFICATION_KEY
2626
)
2727
return {'yoti_site_verification': format_html(raw_text)}
2828

2929

30-
def application_context(request):
30+
def application_context():
3131
return {'yoti_application_id': YOTI_APPLICATION_ID}

plugins/django_yoti/django_yoti/decorators.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from functools import wraps
22
from django.shortcuts import redirect
3+
from django.urls import reverse
4+
35
from .settings import YOTI_LOGIN_VIEW
46

57

@@ -8,7 +10,7 @@ def yoti_authenticated(view_func):
810
def _decorated(request, *args, **kwargs):
911
activity_details = request.session.get('activity_details')
1012
if not activity_details or activity_details.get('outcome') != 'SUCCESS':
11-
return redirect(YOTI_LOGIN_VIEW)
13+
return redirect(reverse(YOTI_LOGIN_VIEW))
1214

1315
yoti_user_id = activity_details.get('user_id')
1416
yoti_user_profile = activity_details.get('user_profile')
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
from django.utils.html import format_html
1+
from django.template.loader import render_to_string
2+
23
from .settings import YOTI_APPLICATION_ID, YOTI_LOGIN_BUTTON_LABEL
34

45
LOGIN_BUTTON_SIZES = ('small', 'medium', 'large')
56

67

78
def get_login_button_html(size=None, text=YOTI_LOGIN_BUTTON_LABEL):
8-
if size and size not in LOGIN_BUTTON_SIZES:
9-
size = None
10-
data_size = 'data-size="{0}"'.format(size) if size else ''
11-
raw_text = '<span data-yoti-application-id="{0}" {1}>' \
12-
'{2}</span>'.format(YOTI_APPLICATION_ID, data_size, text)
13-
return format_html(raw_text)
9+
size = size if size in LOGIN_BUTTON_SIZES else None
10+
return render_to_string('login_button.html', {
11+
'app_id': YOTI_APPLICATION_ID,
12+
'size': size,
13+
'text': text,
14+
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<span data-yoti-application-id="{{ app_id }}" {% if size %}data-size="{{ size }}"{% endif %}>{{ text }}</span>

plugins/django_yoti/django_yoti/tests/test_context_processors.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ def test_yoti_context(self):
2020
keys = ('yoti_application_id', 'yoti_site_verification',
2121
'yoti_login_button', 'yoti_login_button_sm',
2222
'yoti_login_button_md', 'yoti_login_button_lg')
23-
assert set(self.context.keys()) == set(keys)
23+
self.assertEqual(set(self.context.keys()), set(keys))
2424

2525
def test_application_id(self):
2626
app_id = self.context.get('yoti_application_id')
2727
expected_app_id = self.defaults.get('YOTI_APPLICATION_ID')
28-
assert app_id == expected_app_id
28+
self.assertEqual(app_id, expected_app_id)
2929

3030
def test_verification_key(self):
3131
context_tag = self.context.get('yoti_site_verification')
@@ -36,21 +36,21 @@ def test_verification_key(self):
3636

3737
def test_predefined_login_buttons(self):
3838
context = self.context
39-
assert 'data-size="small"' in context.get('yoti_login_button_sm')
40-
assert 'data-size="medium"' in context.get('yoti_login_button_md')
41-
assert 'data-size="large"' in context.get('yoti_login_button_lg')
39+
self.assertIn('data-size="small"', context.get('yoti_login_button_sm'))
40+
self.assertIn('data-size="medium"', context.get('yoti_login_button_md'))
41+
self.assertIn('data-size="large"', context.get('yoti_login_button_lg'))
4242

4343
def test_login_button_func(self):
4444
app_id = self.defaults.get('YOTI_APPLICATION_ID')
4545
button_label = self.defaults.get('YOTI_LOGIN_BUTTON_LABEL')
4646
login_button_func = self.context.get('yoti_login_button')
47-
assert hasattr(login_button_func, '__call__')
47+
self.assertTrue(hasattr(login_button_func, '__call__'))
4848
button_html = login_button_func(text=button_label)
4949
expected = '<span data-yoti-application-id="{0}" >' \
5050
'{1}</span>'.format(app_id, button_label)
51-
assert button_html == expected
51+
self.assertEqual(button_html, expected)
5252

5353
def test_context_login_button_func_with_different_sizes(self):
5454
for size in ('small', 'medium', 'large'):
5555
button_html = get_login_button_html(size)
56-
assert 'data-size="{0}"'.format(size) in button_html
56+
self.assertIn('data-size="{0}"'.format(size), button_html)

plugins/django_yoti/django_yoti/tests/test_views.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import re
2-
31
from django.test import TestCase, Client, RequestFactory
42
from django.http.response import HttpResponse, HttpResponseRedirectBase
53
from django.contrib.sessions.middleware import SessionMiddleware
@@ -17,21 +15,21 @@ def setUp(self):
1715

1816
def test_auth_view(self):
1917
response = self.client.get('/auth/')
20-
assert isinstance(response, HttpResponse)
21-
assert response.status_code == 200
22-
assert 'meta name="yoti-site-verification"' in str(response.content)
18+
self.assertIsInstance(response, HttpResponse)
19+
self.assertEqual(response.status_code, 200)
20+
self.assertIn('meta name="yoti-site-verification"', str(response.content))
2321

2422
def test_login_view(self):
2523
response = self.client.get('/login/')
26-
assert isinstance(response, HttpResponse)
27-
assert response.status_code == 200
24+
self.assertIsInstance(response, HttpResponse)
25+
self.assertEqual(response.status_code, 200)
2826

2927
def test_profile_not_logged_in(self):
3028
request = self.factory.get('/profile')
3129
self._update_session(request)
3230
response = profile(request)
33-
assert isinstance(response, HttpResponseRedirectBase)
34-
assert response.url == '/login/'
31+
self.assertIsInstance(response, HttpResponseRedirectBase)
32+
self.assertEqual(response.url, '/login/')
3533

3634
def test_profile_outcome_is_failure(self):
3735
receipt = {'remember_me_id': 'some_id',
@@ -42,8 +40,8 @@ def test_profile_outcome_is_failure(self):
4240
self._update_session(request, activity_details=dict(activity_details))
4341
response = profile(request)
4442

45-
assert isinstance(response, HttpResponseRedirectBase)
46-
assert response.url == '/login/'
43+
self.assertIsInstance(response, HttpResponseRedirectBase)
44+
self.assertEqual(response.url, '/login/')
4745

4846
def test_profile_outcome_is_success(self):
4947
user_id = 'some_id'
@@ -57,10 +55,10 @@ def test_profile_outcome_is_success(self):
5755
self._update_session(request, activity_details=dict(activity_details))
5856
response = profile(request)
5957

60-
assert isinstance(response, HttpResponse)
61-
assert response.status_code == 200
62-
assert getattr(request, 'yoti_user_id', None) == user_id
63-
assert getattr(request, 'yoti_user_profile', None) == user_profile
58+
self.assertIsInstance(response, HttpResponse)
59+
self.assertEqual(response.status_code, 200)
60+
self.assertEqual(getattr(request, 'yoti_user_id', None), user_id)
61+
self.assertEqual(getattr(request, 'yoti_user_profile', None), user_profile)
6462

6563
@staticmethod
6664
def _update_session(request, **params):

plugins/django_yoti/django_yoti/views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from yoti import Client
22
from django.shortcuts import render, redirect
3+
from django.urls import reverse
34

45
from .decorators import yoti_authenticated
56
from .settings import (
@@ -21,7 +22,7 @@ def auth(request):
2122
client = Client(YOTI_CLIENT_SDK_ID, YOTI_KEY_FILE_PATH)
2223
activity_details = client.get_activity_details(token)
2324
request.session['activity_details'] = dict(activity_details)
24-
return redirect(YOTI_REDIRECT_TO)
25+
return redirect(reverse(YOTI_REDIRECT_TO))
2526

2627

2728
@yoti_authenticated

plugins/django_yoti/example/yoti_example/settings.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,5 @@
122122
STATIC_URL = '/static/'
123123

124124
YOTI = {
125-
'YOTI_REDIRECT_TO': 'profile',
126125
'YOTI_LOGIN_VIEW': 'login',
127126
}

plugins/django_yoti/example/yoti_example/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
urlpatterns = [
2121
url(r'^yoti/', include('django_yoti.urls')),
2222
url(r'^login/', views.login, name='login'),
23-
url(r'^profile/', views.profile, name='profile'),
23+
url(r'^profile/', views.profile, name='yoti_profile'),
2424
url(r'^admin/', admin.site.urls),
2525
]

0 commit comments

Comments
 (0)