Skip to content
This repository has been archived by the owner on Nov 10, 2021. It is now read-only.

Commit

Permalink
unit tests for irws person post. EDS-110
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffFranklin committed May 21, 2015
1 parent 70104ff commit e2011a4
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 3 deletions.
3 changes: 2 additions & 1 deletion restclients/dao_implementation/irws.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def putURL(self, url, headers, body):
response = MockHTTP()
logger.debug('made it to putURL')

response.status = 200
response.headers = {"Content-Type": 'application/json'}
if url in File._cache:
cache = json.loads(File._cache[url])
Expand All @@ -52,8 +51,10 @@ def putURL(self, url, headers, body):
cache[type][0][attr] = request[type][0][attr]
File._cache[url] = json.dumps(cache)
response.data = File._cache[url]
response.status = 200
else:
response.data = body
response.status = 404
return response

def postURL(self, url, headers, body):
Expand Down
2 changes: 1 addition & 1 deletion restclients/irws.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def post_hepps_person_by_netid(self, netid, data):
{'Accept': 'application/json'},
json.dumps(hepps_person))
if response.status != 200:
raise DataFailureException(url, response.status, response.data)
raise DataFailureException(post_url, response.status, response.data)

return response.status

Expand Down
101 changes: 100 additions & 1 deletion restclients/test/irws.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,45 @@
from django.test import TestCase, override_settings
from django.conf import settings
from restclients.irws import IRWS
from restclients.exceptions import InvalidIRWSName
from restclients.exceptions import InvalidIRWSName, DataFailureException, InvalidNetID
from restclients.exceptions import IRWSPersonNotFound, InvalidIRWSPerson
from restclients.dao_implementation.irws import File

logger = logging.getLogger(__name__)


@override_settings(RESTCLIENTS_IRWS_DAO_CLASS='restclients.dao_implementation.irws.File',
RESTCLIENTS_IRWS_SERVICE_NAME='registry-dev')
class IRWSTest(TestCase):
def setUp(self):
File._cache = {}

def test_get_name_by_netid(self):
irws = IRWS()
name = irws.get_name_by_netid('javerage')
self.assertEquals(name.display_cname, 'JAMES AVERAGE STUDENT')

def test_put_name_by_netid(self):
irws = IRWS()
# prime the cache first
irws.get_name_by_netid('javerage')

response = irws.put_name_by_netid(
'javerage',
self._json_name_from_tuple(('J', '', 'Student')))
self.assertEquals(200, response)
name = irws.get_name_by_netid('javerage')
self.assertEquals('J', name.display_fname)
self.assertEquals('', name.display_mname)
self.assertEquals('Student', name.display_lname)

def test_put_name_by_netid_no_user(self):
irws = IRWS()
self.assertRaises(DataFailureException,
irws.put_name_by_netid,
'nonuser',
self._json_name_from_tuple(('J', '', 'Student')))

def test_valid_name_part_good(self):
irws = IRWS()
self.assertTrue(irws.valid_name_part('james'))
Expand Down Expand Up @@ -119,6 +144,80 @@ def test_valid_irws_name_too_long(self):
irws.valid_irws_name_from_json,
bad_name)

def test_get_identity_by_netid(self):
irws = IRWS()
identity = irws.get_identity_by_netid('javerage')
self.assertNotEqual(0, len(identity.identifiers.keys()))

def test_get_identity_by_netid_bad_netid(self):
irws = IRWS()
self.assertRaises(InvalidNetID,
irws.get_identity_by_netid,
'lkfajdslkjf#@$$@')

def test_get_identity_by_netid_nonexistent(self):
irws = IRWS()
self.assertRaises(DataFailureException,
irws.get_identity_by_netid,
'nonuser')

def test_get_hepps_person_by_netid(self):
irws = IRWS()
# idtest56 is set up to be a hepps person
hepps_person = irws.get_hepps_person_by_netid('idtest56')
self.assertEqual('N', hepps_person.wp_publish)
self.assertEqual('28098ACBAC71425D9B2912757E4EF3AE', hepps_person.regid)

def test_get_hepps_person_by_netid_not_hepps(self):
irws = IRWS()
# idtest55 is set up to NOT be a hepps person
self.assertRaises(IRWSPersonNotFound,
irws.get_hepps_person_by_netid,
'idtest55')

def test_post_hepps_person_by_netid(self):
irws = IRWS()
netid = 'idtest56'
# prime the cache
irws.get_hepps_person_by_netid(netid)
response = irws.post_hepps_person_by_netid(
netid,
'{"wp_publish": "E"}')
self.assertEqual(200, response)
identity = irws.get_hepps_person_by_netid(netid)
self.assertEqual('E', identity.wp_publish)

def test_post_hepps_person_by_netid_not_hepps(self):
irws = IRWS()
self.assertRaises(IRWSPersonNotFound,
irws.post_hepps_person_by_netid,
'idtest55',
'{"wp_publish": "E"}')

def test_post_hepps_person_by_netid_data_failure(self):
self.assertRaises(DataFailureException,
IRWS().post_hepps_person_by_netid,
'idtest56',
'{"wp_publish": "E"}')

def test_valid_hepps_person_from_json(self):
person = IRWS().valid_hepps_person_from_json('{"wp_publish": "Y"}')
expected = json.dumps({'person': [{'wp_publish': 'Y'}]})
self.assertEqual(expected, json.dumps(person))
# test all the possible values
person = IRWS().valid_hepps_person_from_json('{"wp_publish": "N"}')
person = IRWS().valid_hepps_person_from_json('{"wp_publish": "E"}')

def test_valid_hepps_person_from_json_bad_payload(self):
self.assertRaises(InvalidIRWSPerson,
IRWS().valid_hepps_person_from_json,
'{"wp_publish": "J"}')

def test_valid_hepps_person_from_json_bad_json(self):
self.assertRaises(InvalidIRWSPerson,
IRWS().valid_hepps_person_from_json,
'{"wp_publish":')

def _json_name_from_tuple(self, x):
return json.dumps({'display_fname': x[0],
'display_mname': x[1],
Expand Down

0 comments on commit e2011a4

Please sign in to comment.