Skip to content

Commit e8954d9

Browse files
authored
Merge pull request #49 from UWIT-IAM/qna-error-fix
Raise exception on unexpected IRWS QnA responses.
2 parents a0cb5ca + ca7774f commit e8954d9

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

resttools/irws.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""IRWS service interface"""
22
import re
33
import copy
4-
from six.moves.urllib.parse import quote_plus
4+
from six.moves.urllib.parse import quote
55
import json
66
from resttools.dao import IRWS_DAO
77
from resttools.models.irws import UWNetId
@@ -43,7 +43,7 @@ def _get_code_from_error(self, message):
4343

4444
def _clean(self, arg):
4545
if arg is not None:
46-
arg = quote_plus(arg)
46+
arg = quote(arg)
4747
return arg
4848

4949
# v2 - no change
@@ -363,7 +363,7 @@ def get_generic_person(self, uri):
363363
The uris come in from values in irws.Person.identifiers.
364364
Raises DataFailureExeption on error.
365365
"""
366-
uri = quote_plus(uri, '/')
366+
uri = quote(uri, '/')
367367

368368
url = '/%s/v2%s' % (self._service_name, uri)
369369
response = self.dao.getURL(url, {'Accept': 'application/json'})
@@ -470,7 +470,7 @@ def verify_sc_pin(self, netid, pin):
470470
response = self.dao.getURL(url, {"Accept": "application/json"})
471471
if response.status != 200:
472472
# the pin was good. we return OK, but note the error
473-
logging.warn('Delete SC pin failed: %d' % response.status)
473+
logger.error('Delete SC pin failed: %d' % response.status)
474474
return 200
475475

476476
if (response.status == 400 or response.status == 404):
@@ -497,25 +497,20 @@ def get_qna(self, netid):
497497
def get_verify_qna(self, netid, answers):
498498
"""
499499
Verifies that all answers are present and that all are correct.
500-
answers: dict ('ordinal': 'answer')
501-
"""
502-
netid = self._clean(netid)
503-
504-
q_list = self.get_qna(netid)
505-
for q in q_list:
506-
if q.ordinal not in answers:
507-
logging.debug('q %s, no answer' % q.ordinal)
508-
return False
509-
ans = re.sub(r'\W+', '', answers[q.ordinal])
510-
url = "/%s/v2/qna/%s/%s/check?ans=%s" % (self._service_name, q.ordinal, netid, quote_plus(ans))
500+
answers: ordered list of answers
501+
"""
502+
questions = self.get_qna(netid)
503+
if len(questions) != len(answers):
504+
return False
505+
for index, answer in enumerate(answers, start=1):
506+
answer = re.sub(r'\W+', '', answer)
507+
url = "/%s/v2/qna/%s/%s/check?ans=%s" % (self._service_name, index, quote(netid), quote(answer))
511508
response = self.dao.getURL(url, {"Accept": "application/json"})
512-
if response.status == 404:
513-
logging.debug('q %s, wrong answer' % q.ordinal)
509+
if response.status in (400, 404):
510+
logger.debug('qna wrong answer #{}, status = {}'.format(index, response.status))
514511
return False
515512
if response.status != 200:
516-
logging.debug('q %s, error return: %d' % (q.ordinal, response.status))
517-
return False
518-
logging.debug('q %s, correct answer' % q.ordinal)
513+
raise DataFailureException(url, response.status, response.data)
519514
return True
520515

521516
def verify_person_attribute(self, netid, attribute, value):

resttools/test/irws.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,11 @@ def test_get_qna(self):
9494
eq_(qna[2].ordinal, '3')
9595

9696
def test_verify_qna(self):
97-
correct = {'1': 'skyblue', '2': 'mememe', '3': 'begood'}
98-
st = self.irws.get_verify_qna('user1q', correct)
99-
eq_(st, True)
100-
incorrect = {'1': 'skyblue', '2': 'NOTmememe', '3': 'begood'}
101-
st = self.irws.get_verify_qna('user1q', incorrect)
102-
eq_(st, False)
97+
correct = ['skyblue', 'mememe', 'begood']
98+
incorrect = ['skyblue', 'NOTmememe', 'begood']
99+
eq_(self.irws.get_verify_qna('user1q', correct), True)
100+
eq_(self.irws.get_verify_qna('user1q', incorrect), False)
101+
eq_(self.irws.get_verify_qna('user1q', correct[:2]), False)
103102

104103
def test_verify_person_attibute(self):
105104
st = self.irws.verify_person_attribute('javerage', 'birthdate', '2015-10-04')

0 commit comments

Comments
 (0)