def testEditUserFromRegistration(self): """ Edit a user via registration XML """ # really this should be in the "update" test but all the infrastructure # for dealing with the xml payload is here. original_user, created = CommCareUser.create_or_update_from_xform(self.xform) self.assertTrue(created) self.assertEqual("test_reg", original_user.username.split("@")[0]) original_django_user = original_user.get_django_user() original_count = User.objects.count() xform = self.xform xform.form['username'] = '******' xform.form['password'] = "******" self.xform.form['registering_phone_id'] = 'phone_edit' xform.form['user_data'] = {'data': [{'@key': 'user_type', '#text': 'boss'}]} updated_user, created = CommCareUser.create_or_update_from_xform(xform) self.assertFalse(created) # make sure they got different usernames self.assertEqual("a_new_username", updated_user.username.split("@")[0]) self.assertEqual("phone_edit", updated_user.device_id) self.assertEqual("boss", updated_user.user_data["user_type"]) # make sure we didn't create a new django user and updated # the old one correctly updated_django_user = updated_user.get_django_user() self.assertEqual(original_count, User.objects.count()) self.assertEqual(original_django_user.pk, updated_django_user.pk) self.assertNotEqual(original_django_user.username, updated_django_user.username) self.assertNotEqual(original_django_user.password, updated_django_user.password)
def testCreateDuplicateUsersFromRegistration(self): """ use case: chw on phone registers a username/password/domain triple somewhere another chw somewhere else somehow registers the same username/password/domain triple outcome: 2 distinct users on hq with the same info, but the usernames should be updated appropriately to not be duplicates. """ first_user, created = CommCareUser.create_or_update_from_xform(self.xform) # switch uuid so that we don't violate unique key constraints on django use creation xform = self.xform xform.form['uuid'] = 'AVNSDNVLDSFDESFSNSIDNFLDKN' second_user, created = CommCareUser.create_or_update_from_xform(xform) # make sure they got different usernames self.assertEqual("test_reg", first_user.username.split("@")[0]) self.assertEqual("test_reg2", second_user.username.split("@")[0])
def testCreateDuplicateUsersFromRegistration(self): """ use case: chw on phone registers a username/password/domain triple somewhere another chw somewhere else somehow registers the same username/password/domain triple outcome: 2 distinct users on hq with the same info, but the usernames should be updated appropriately to not be duplicates. """ first_user, created = CommCareUser.create_or_update_from_xform( self.xform) # switch uuid so that we don't violate unique key constraints on django use creation xform = self.xform xform.form['uuid'] = 'AVNSDNVLDSFDESFSNSIDNFLDKN' second_user, created = CommCareUser.create_or_update_from_xform(xform) # make sure they got different usernames self.assertEqual("test_reg", first_user.username.split("@")[0]) self.assertEqual("test_reg2", second_user.username.split("@")[0])
def _runCreateUserFromRegistrationTest(self): """ test creating of couch user from a registration xmlns. this is more of an integration test than a unit test. """ couch_user, created = CommCareUser.create_or_update_from_xform( self.xform) self.assertEqual(couch_user.user_id, self.uuid) # czue: removed lxml reference #uuid = ET.fromstring(xml).findtext(".//{http://openrosa.org/user/registration}uuid") couch_user = CommCareUser.get_by_user_id(self.xform.form['uuid']) self.assertNotEqual(couch_user, None) self.assertEqual(couch_user.username, format_username(self.username, self.domain)) self.assertEqual(couch_user.domain, self.domain) self.assertEqual(couch_user.user_id, self.uuid) date = datetime.date(datetime.strptime(self.date_string, '%Y-%m-%d')) self.assertEqual(couch_user.created_on, force_to_datetime(date)) self.assertEqual(couch_user.device_ids[0], self.registering_device_id) django_user = couch_user.get_django_user() self.assertEqual(couch_user.user_id, CouchUser.from_django_user(django_user).user_id)
def create_user_from_commcare_registration(sender, xform, **kwargs): """ # this comes in as xml that looks like: # <n0:registration xmlns:n0="openrosa.org/user-registration"> # <username>user</username> # <password>pw</password> # <uuid>MTBZJTDO3SCT2ONXAQ88WM0CH</uuid> # <date>2008-01-07</date> # <registering_phone_id>NRPHIOUSVEA215AJL8FFKGTVR</registering_phone_id> # <user_data> ... some custom stuff </user_data> """ if xform.xmlns not in VALID_USER_REGISTRATION_XMLNSES: return False try: CommCareUser.create_or_update_from_xform(xform) except Exception, e: logging.exception(e) raise
def testEditUserFromRegistration(self): """ Edit a user via registration XML """ # really this should be in the "update" test but all the infrastructure # for dealing with the xml payload is here. original_user, created = CommCareUser.create_or_update_from_xform( self.xform) self.assertTrue(created) self.assertEqual("test_reg", original_user.username.split("@")[0]) original_django_user = original_user.get_django_user() original_count = User.objects.count() xform = self.xform xform.form['username'] = '******' xform.form['password'] = "******" self.xform.form['registering_phone_id'] = 'phone_edit' xform.form['user_data'] = { 'data': [{ '@key': 'user_type', '#text': 'boss' }] } updated_user, created = CommCareUser.create_or_update_from_xform(xform) self.assertFalse(created) # make sure they got different usernames self.assertEqual("a_new_username", updated_user.username.split("@")[0]) self.assertEqual("phone_edit", updated_user.device_id) self.assertEqual("boss", updated_user.user_data["user_type"]) # make sure we didn't create a new django user and updated # the old one correctly updated_django_user = updated_user.get_django_user() self.assertEqual(original_count, User.objects.count()) self.assertEqual(original_django_user.pk, updated_django_user.pk) self.assertNotEqual(original_django_user.username, updated_django_user.username) self.assertNotEqual(original_django_user.password, updated_django_user.password)
def testEditUserFromRegistrationWithConflicts(self): original_user, created = CommCareUser.create_or_update_from_xform(self.xform) self.assertEqual("test_reg", original_user.username.split("@")[0]) xform = self.xform xform.form['uuid'] = 'AVNSDNVLDSFDESFSNSIDNFLDKN' xform.form['username'] = '******' second_user, created = CommCareUser.create_or_update_from_xform(xform) # try to set it to a conflict xform.form['username'] = '******' updated_user, created = CommCareUser.create_or_update_from_xform(xform) # make sure they got different usernames self.assertEqual(second_user.get_id, updated_user.get_id) self.assertEqual("test_reg", original_user.username.split("@")[0]) self.assertEqual("test_reg2", updated_user.username.split("@")[0]) # since we changed it we should be able to back to the original id xform.form['username'] = '******' updated_user, created = CommCareUser.create_or_update_from_xform(xform) self.assertEqual(second_user.get_id, updated_user.get_id) self.assertEqual("new_user", updated_user.username.split("@")[0])
def testEditUserFromRegistrationWithConflicts(self): original_user, created = CommCareUser.create_or_update_from_xform( self.xform) self.assertEqual("test_reg", original_user.username.split("@")[0]) xform = self.xform xform.form['uuid'] = 'AVNSDNVLDSFDESFSNSIDNFLDKN' xform.form['username'] = '******' second_user, created = CommCareUser.create_or_update_from_xform(xform) # try to set it to a conflict xform.form['username'] = '******' updated_user, created = CommCareUser.create_or_update_from_xform(xform) # make sure they got different usernames self.assertEqual(second_user.get_id, updated_user.get_id) self.assertEqual("test_reg", original_user.username.split("@")[0]) self.assertEqual("test_reg2", updated_user.username.split("@")[0]) # since we changed it we should be able to back to the original id xform.form['username'] = '******' updated_user, created = CommCareUser.create_or_update_from_xform(xform) self.assertEqual(second_user.get_id, updated_user.get_id) self.assertEqual("new_user", updated_user.username.split("@")[0])
def _runCreateUserFromRegistrationTest(self): """ test creating of couch user from a registration xmlns. this is more of an integration test than a unit test. """ couch_user, created = CommCareUser.create_or_update_from_xform(self.xform) self.assertEqual(couch_user.user_id, self.uuid) # czue: removed lxml reference # uuid = ET.fromstring(xml).findtext(".//{http://openrosa.org/user/registration}uuid") couch_user = CommCareUser.get_by_user_id(self.xform.form['uuid']) self.assertNotEqual(couch_user, None) self.assertEqual(couch_user.username, format_username(self.username, self.domain)) self.assertEqual(couch_user.domain, self.domain) self.assertEqual(couch_user.user_id, self.uuid) date = iso_string_to_date(self.date_string) self.assertEqual(couch_user.created_on, force_to_datetime(date)) self.assertEqual(couch_user.device_ids[0], self.registering_device_id) django_user = couch_user.get_django_user() self.assertEqual(couch_user.user_id, CouchUser.from_django_user(django_user).user_id)
def create_user_from_commcare_registration(sender, xform, **kwargs): """ # this comes in as xml that looks like: # <n0:registration xmlns:n0="openrosa.org/user-registration"> # <username>user</username> # <password>pw</password> # <uuid>MTBZJTDO3SCT2ONXAQ88WM0CH</uuid> # <date>2008-01-07</date> # <registering_phone_id>NRPHIOUSVEA215AJL8FFKGTVR</registering_phone_id> # <user_data> ... some custom stuff </user_data> """ if xform.xmlns not in VALID_USER_REGISTRATION_XMLNSES: return False try: couch_user, created = CommCareUser.create_or_update_from_xform(xform) except Exception, e: #import traceback, sys #exc_type, exc_value, exc_traceback = sys.exc_info() #traceback.print_tb(exc_traceback, limit=1, file=sys.stdout) logging.exception(e) raise