From af9b7ddeb637278a7705964ba98c8e6a2e7307f4 Mon Sep 17 00:00:00 2001 From: maxbr Date: Mon, 17 Oct 2016 14:56:11 +0200 Subject: Add unit tests JIRA: RELENG-12 This commit increases test statement coverage to 84%. It also fixes the bugs that emerged while testing. Change-Id: I696091f1a07f8b7647872c7cb15f4368a4690619 Signed-off-by: maxbr --- tools/pharos-dashboard/src/account/jira_util.py | 3 +-- tools/pharos-dashboard/src/account/models.py | 5 +---- .../src/account/tests/test_general.py | 10 ++++++++++ tools/pharos-dashboard/src/account/views.py | 22 ++++++++++++++++++---- 4 files changed, 30 insertions(+), 10 deletions(-) (limited to 'tools/pharos-dashboard/src/account') diff --git a/tools/pharos-dashboard/src/account/jira_util.py b/tools/pharos-dashboard/src/account/jira_util.py index c333f8c4..fdb87f77 100644 --- a/tools/pharos-dashboard/src/account/jira_util.py +++ b/tools/pharos-dashboard/src/account/jira_util.py @@ -12,11 +12,10 @@ import base64 import os import oauth2 as oauth +from django.conf import settings from jira import JIRA from tlslite.utils import keyfactory -from django.conf import settings - class SignatureMethod_RSA_SHA1(oauth.SignatureMethod): name = 'RSA-SHA1' diff --git a/tools/pharos-dashboard/src/account/models.py b/tools/pharos-dashboard/src/account/models.py index d87ee183..c2e99028 100644 --- a/tools/pharos-dashboard/src/account/models.py +++ b/tools/pharos-dashboard/src/account/models.py @@ -8,12 +8,9 @@ ############################################################################## -from django.db import models - from django.contrib.auth.models import User -from rest_framework.authtoken.models import Token +from django.db import models -from dashboard.models import Resource def upload_to(object, filename): return object.user.username + '/' + filename diff --git a/tools/pharos-dashboard/src/account/tests/test_general.py b/tools/pharos-dashboard/src/account/tests/test_general.py index 72e7ea11..e8f483b5 100644 --- a/tools/pharos-dashboard/src/account/tests/test_general.py +++ b/tools/pharos-dashboard/src/account/tests/test_general.py @@ -48,3 +48,13 @@ class AccountMiddlewareTestCase(TestCase): self.user1profile.save() self.client.get(url) self.assertEqual(timezone.get_current_timezone_name(), 'Etc/Greenwich') + + # if there is no profile for a user, it should be created + user2 = User.objects.create(username='user2') + user2.set_password('user2') + user2.save() + self.client.login(username='user2', password='user2') + self.client.get(url) + self.assertTrue(user2.userprofile) + + diff --git a/tools/pharos-dashboard/src/account/views.py b/tools/pharos-dashboard/src/account/views.py index ac973f53..17fbdc3a 100644 --- a/tools/pharos-dashboard/src/account/views.py +++ b/tools/pharos-dashboard/src/account/views.py @@ -21,7 +21,6 @@ from django.contrib.auth.models import User from django.urls import reverse from django.utils.decorators import method_decorator from django.views.generic import RedirectView, TemplateView, UpdateView - from jira import JIRA from rest_framework.authtoken.models import Token @@ -58,9 +57,16 @@ class JiraLoginView(RedirectView): client.set_signature_method(SignatureMethod_RSA_SHA1()) # Step 1. Get a request token from Jira. - resp, content = client.request(settings.OAUTH_REQUEST_TOKEN_URL, "POST") + try: + resp, content = client.request(settings.OAUTH_REQUEST_TOKEN_URL, "POST") + except Exception as e: + messages.add_message(self.request, messages.ERROR, + 'Error: Connection to Jira failed. Please contact an Administrator') + return '/' if resp['status'] != '200': - raise Exception("Invalid response %s: %s" % (resp['status'], content)) + messages.add_message(self.request, messages.ERROR, + 'Error: Connection to Jira failed. Please contact an Administrator') + return '/' # Step 2. Store the request token in a session for later use. self.request.session['request_token'] = dict(urllib.parse.parse_qsl(content.decode())) @@ -87,8 +93,15 @@ class JiraAuthenticatedView(RedirectView): client.set_signature_method(SignatureMethod_RSA_SHA1()) # Step 2. Request the authorized access token from Jira. - resp, content = client.request(settings.OAUTH_ACCESS_TOKEN_URL, "POST") + try: + resp, content = client.request(settings.OAUTH_ACCESS_TOKEN_URL, "POST") + except Exception as e: + messages.add_message(self.request, messages.ERROR, + 'Error: Connection to Jira failed. Please contact an Administrator') + return '/' if resp['status'] != '200': + messages.add_message(self.request, messages.ERROR, + 'Error: Connection to Jira failed. Please contact an Administrator') return '/' access_token = dict(urllib.parse.parse_qsl(content.decode())) @@ -128,6 +141,7 @@ class JiraAuthenticatedView(RedirectView): # redirect user to settings page to complete profile return url + @method_decorator(login_required, name='dispatch') class UserListView(TemplateView): template_name = "account/user_list.html" -- cgit 1.2.3-korg