Skip to content

Commit 6644de2

Browse files
committed
Added unit tests for some views
1 parent 9eb8e25 commit 6644de2

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

mep_django/linkedin/.tests.py.swp

12 KB
Binary file not shown.

mep_django/linkedin/tests.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
11
from django.test import TestCase
2+
#from django.test import Client
3+
4+
class TestHomeView(TestCase):
5+
'''Unit test for the home view'''
6+
def setUp(self):
7+
pass
8+
9+
def test_status_200(self):
10+
'''Return status 200 upon GET request'''
11+
response = self.client.get('/')
12+
self.assertEqual(response.status_code, 200)
13+
14+
class TestNewsView(TestCase):
15+
'''Unit test for the news view'''
16+
17+
def setUp(self):
18+
self.MY_URL = '/news/'
19+
20+
def test_status_403(self):
21+
'''Return status 403 upon GET request when user is not logged in'''
22+
response = self.client.get(self.MY_URL)
23+
self.assertEqual(response.status_code, 403)
24+
25+
def test_status_200(self):
26+
'''Return status 200 upon GET request when user IS logged in'''
27+
self.client.login(username='user', password='pass')
28+
response = self.client.get(self.MY_URL)
29+
self.assertEqual(response.status_code, 200)
230

3-
# Create your tests here.

mep_django/linkedin/views.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from social.apps.django_app.default.models import UserSocialAuth
44
from linkedin import linkedin
55
#from django.http import HttpResponse
6-
6+
from django.core.exceptions import PermissionDenied
77

88
def logout(request):
99
"""logs the user out, then redirects to the home page"""
@@ -14,7 +14,6 @@ def home(request):
1414
"""displays the home page"""
1515
return render(request, 'home.html', {})
1616

17-
1817
def about(request):
1918
"""displays the about page"""
2019
return render(request, 'about.html', {})
@@ -23,7 +22,6 @@ def contact(request):
2322
"""displays the contact page"""
2423
return render(request, 'contact.html', {})
2524

26-
2725
def temp(request):
2826
"""this is used for the popup window which houses the linkedin login page"""
2927
return render(request, 'temp.html', {})
@@ -59,9 +57,13 @@ def get_access_tokens(user):
5957
def news(request):
6058
"""Diplays the LinkedIn content, this is the critical view of the app"""
6159

62-
if not request.user.is_authenticated():
63-
return redirect('home')
60+
#if not request.user.is_authenticated():
61+
#return redirect('home')
6462

63+
# handle unauthorized access with a 403
64+
if not request.user.is_authenticated() or not request.user.is_active:
65+
raise PermissionDenied
66+
6567
API_KEY = '75l485e9k29snc'
6668
API_SECRET = 'iw7fONMpJZcY5HOb'
6769
USER_KEY, USER_SECRET = get_access_tokens(request.user)
@@ -114,4 +116,4 @@ def news(request):
114116
#raw_input()
115117
'''
116118
return render(request, 'news.html', {'post_list':group_posts['values'], 'update_list': update_list})
117-
119+

mep_django/settings.py

+3
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@
4444
'django.contrib.staticfiles',
4545
'social.apps.django_app.default', # added by chad
4646
'mep_django.linkedin', # added by chad
47+
'django_nose',
4748
)
4849

50+
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
51+
4952
MIDDLEWARE_CLASSES = (
5053
'django.contrib.sessions.middleware.SessionMiddleware',
5154
'django.middleware.common.CommonMiddleware',

0 commit comments

Comments
 (0)