def testClientSpecificPrecompute(self): for email in self.testUsers: currUser = User.fromEmail(email) self.assertEqual(currUser.getProfile().get("testfield1"), None) self.assertEqual(currUser.getProfile().get("testfield2"), None) self.assertEqual(default.getCarbonFootprint(currUser), None) fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey=False) tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) user = User.fromEmail(fakeEmail) self.assertEqual(user.getFirstStudy(), 'testclient') self.pr.precomputeResults() self.assertEqual(user.getProfile()['testfield1'], 'value1') self.assertEqual(user.getProfile()['testfield2'], 'value2') for email in self.testUsers: if email != fakeEmail: currUser = User.fromEmail(email) carbonFootprint = default.getCarbonFootprint(currUser) self.assertEqual(len(carbonFootprint), 12)
def testClientSpecificPrecompute(self): for email in self.testUsers: currUser = User.fromEmail(email) self.assertEqual(currUser.getProfile().get("testfield1"), None) self.assertEqual(currUser.getProfile().get("testfield2"), None) self.assertEqual(data.getCarbonFootprint(currUser), None) fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) user = User.fromEmail(fakeEmail) self.assertEqual(user.getFirstStudy(), 'testclient') self.pr.precomputeResults() self.assertEqual(user.getProfile()['testfield1'], 'value1') self.assertEqual(user.getProfile()['testfield2'], 'value2') for email in self.testUsers: if email != fakeEmail: currUser = User.fromEmail(email) carbonFootprint = data.getCarbonFootprint(currUser) self.assertEqual(len(carbonFootprint), 12)
def testPreRegisterNewUser(self): client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 1) self.assertEqual(resultReg, 0)
def javascriptCallback(clientName, method): from clients.choice import choice client = Client(clientName) client_key = request.query.client_key client.callJavascriptCallback(client_key, method, request.params) return {'status': 'ok'}
def testQueryUnclassifiedSectionsLowConfidence(self): from dao.user import User fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) user = User.fromEmail(fakeEmail) self.assertEqual(user.getFirstStudy(), 'testclient') queriedUnclassifiedSections = tripManager.queryUnclassifiedSections(User.fromEmail(fakeEmail).uuid) self.assertEqual(queriedUnclassifiedSections.count(), 2) # Set the auto_confirmed values for the trips for section in queriedUnclassifiedSections: print section['_id'] self.SectionsColl.update({'_id': section['_id']}, {'test_auto_confirmed': {'mode': section['mode'], 'prob': 0.95}}) # Now, set the update timestamp to two weeks ago so that we will start filtering tests.common.updateUserCreateTime(user.uuid) queriedUnclassifiedSections = tripManager.queryUnclassifiedSections(User.fromEmail(fakeEmail).uuid) self.assertEqual(queriedUnclassifiedSections.count(), 0)
def testQueryUnclassifiedSectionsLowConfidence(self): from dao.user import User fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) user = User.fromEmail(fakeEmail) self.assertEqual(user.getFirstStudy(), 'testclient') queriedUnclassifiedSections = tripManager.queryUnclassifiedSections(User.fromEmail(fakeEmail).uuid) self.assertEqual(queriedUnclassifiedSections.count(), 2) # Set the auto_confirmed values for the trips for section in queriedUnclassifiedSections: print section['_id'] self.SectionsColl.update({'_id': section['_id']}, {'test_auto_confirmed': {'mode': section['mode'], 'prob': 0.95}}) # Now, set the update timestamp to two weeks ago so that we will start filtering tests.common.updateUserCreateTime(user.uuid) queriedUnclassifiedSections = tripManager.queryUnclassifiedSections(User.fromEmail(fakeEmail).uuid) self.assertEqual(queriedUnclassifiedSections.count(), 0)
def testSavePredictionsStepWithClient(self): from dao.user import User fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) user = User.fromEmail(fakeEmail) self.assertEqual(user.getFirstStudy(), 'testclient') self.testPredictedProb() self.pipeline.savePredictionsStep() # Confirm that the predictions are saved correctly test_id_1_sec = self.SectionsColl.find_one({'_id': 'test_id_1'}) self.assertIsNotNone(test_id_1_sec['predicted_mode']) self.assertEquals(test_id_1_sec['predicted_mode'], {'walking': 1}) self.assertEquals(test_id_1_sec['test_auto_confirmed'], {'mode': 1, 'prob': 1.0}) test_id_2_sec = self.SectionsColl.find_one({'_id': 'test_id_2'}) self.assertIsNotNone(test_id_2_sec['predicted_mode']) self.assertEquals(test_id_2_sec['predicted_mode'], {'bus': 1}) self.assertEquals(test_id_2_sec['test_auto_confirmed'], {'mode': 5, 'prob': 1.0}) # Let's make sure that we didn't accidentally mess up other fields self.assertIsNotNone(test_id_1_sec['distance']) self.assertIsNotNone(test_id_2_sec['trip_id'])
def getUUID(request, inHeader=False): retUUID = None if skipAuth: if 'User' in request.headers or 'user' in request.json: # skipAuth = true, so the email will be sent in plaintext userEmail = __getToken__(request, inHeader) retUUID = __getUUIDFromEmail__(userEmail) logging.debug( "skipAuth = %s, returning UUID directly from email %s" % (skipAuth, retUUID)) else: # Return a random user to make it easy to experiment without having to specify a user # TODO: Remove this if it is not actually used from get_database import get_uuid_db user_uuid = get_uuid_db().find_one()['uuid'] retUUID = user_uuid logging.debug("skipAuth = %s, returning arbitrary UUID %s" % (skipAuth, retUUID)) if Client("choice").getClientKey() is None: Client("choice").update(createKey=True) else: userToken = __getToken__(request, inHeader) retUUID = getUUIDFromToken(userToken) request.params.user_uuid = retUUID return retUUID
def testUpdateClient(self): client = Client("testclient") self.updateWithTestSettings(client, "tests/dao/testclient/testclient_settings_update.json") self.assertEqual(client.startDatetime, datetime(2015, 01, 13)) self.assertEqual(client.endDatetime, datetime(2015, 04, 25)) self.assertEqual(client.getDates()[0], datetime(2015, 01, 13)) self.assertEqual(client.getDates()[1], datetime(2015, 04, 25))
def post(self: tornado.web.RequestHandler): client = Client(name=str(self.get_argument('FirstName')), surname=str(self.get_argument('LastName'))) User.create_user( User(login=str(self.get_argument('Login')), password=str(self.get_argument('Password')), client=Client.create_client(client))) self.redirect('/login')
def testGetSettingsCustomUser(self): client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 1) self.assertEqual(resultReg, 0) user = User.register('*****@*****.**') self.assertRegexpMatches(user.getSettings()['result_url'], ".*/test/test/test")
def testRegisterStudyUser(self): client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 1) self.assertEqual(resultReg, 0) user = User.register('*****@*****.**') self.assertEquals(user.getStudy(), ['testclient'])
def testCountForStudy(self): client = Client("testclient") client.update(createKey=False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 1) self.assertEqual(resultReg, 0) user = User.register('*****@*****.**') self.assertEquals(userclient.countForStudy('testclient'), 1)
def testGetSettingsCustomUser(self): client = Client("testclient") client.update(createKey=False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 1) self.assertEqual(resultReg, 0) user = User.register('*****@*****.**') self.assertRegexpMatches(user.getSettings()['result_url'], ".*/test/test/test")
def testGetFirstStudy(self): user = User.register('*****@*****.**') self.assertTrue(User.isRegistered('*****@*****.**')) client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) user = User.fromEmail('*****@*****.**') self.assertEqual(user.getFirstStudy(), 'testclient')
def getResult(user_uuid): # This is in here, as opposed to the top level as recommended by the PEP # because then we don't have to worry about loading bottle in the unit tests from bottle import template import base64 from dao.user import User from dao.client import Client user = User.fromUUID(user_uuid) renderedTemplate = template( "clients/choice/result_template.html", variables=json.dumps({ 'curr_view': getCurrView(user_uuid), 'uuid': str(user_uuid), 'client_key': Client("choice").getClientKey() }), gameResult=base64.b64encode(gamified.getResult(user_uuid)), leaderboardResult=base64.b64encode(leaderboard.getResult(user_uuid)), dataResult=base64.b64encode(data.getResult(user_uuid)), commonTripsResult=base64.b64encode(commontrips.getResult(user_uuid)), recommendationResult=base64.b64encode( recommendation.getResult(user_uuid))) return renderedTemplate
def savePredictionsStep(self): from dao.user import User from dao.client import Client uniqueModes = sorted(set(self.cleanedResultVector)) for i in range(self.predictedProb.shape[0]): currSectionId = self.sectionIds[i] currProb = self.convertPredictedProbToMap(self.modeList, uniqueModes, self.predictedProb[i]) logging.debug("Updating probability for section with id = %s" % currSectionId) self.Sections.update({'_id': currSectionId}, {"$set": { "predicted_mode": currProb }}) currUser = User.fromUUID(self.sectionUserIds[i]) clientSpecificUpdate = Client( currUser.getFirstStudy()).clientSpecificSetters( currUser.uuid, currSectionId, currProb) if clientSpecificUpdate != None: self.Sections.update({'_id': currSectionId}, clientSpecificUpdate)
def testConfirmationModeQueryCorrectedManualAndAuto(self): from dao.client import Client (user, dummySection, dummyPredModeMap) = self.setupClientTest() clientSetQuery = Client(user.getFirstStudy()).clientSpecificSetters(user.uuid, dummySection, dummyPredModeMap) # Apply the change get_section_db().update({'_id': dummySection['_id']}, clientSetQuery) retrievedSection = get_section_db().find_one({'_id': dummySection['_id']}) self.assertEqual(retrievedSection['test_auto_confirmed']['mode'], 1) get_section_db().update({'_id': dummySection['_id']}, {'$set': {'confirmed_mode': 4}}) get_section_db().update({'_id': dummySection['_id']}, {'$set': {'corrected_mode': 9}}) retrieveByQuery = get_section_db().find(common.getConfirmationModeQuery(1)) for entry in retrieveByQuery: print entry self.assertEqual(retrieveByQuery.count(), 0) retrieveByQuery = get_section_db().find(common.getConfirmationModeQuery(4)) for entry in retrieveByQuery: print entry self.assertEqual(retrieveByQuery.count(), 0) retrieveByQuery = get_section_db().find(common.getConfirmationModeQuery(9)) for entry in retrieveByQuery: print entry self.assertEqual(retrieveByQuery.count(), 1)
def testGetFirstStudy(self): user = User.register('*****@*****.**') self.assertTrue(User.isRegistered('*****@*****.**')) client = Client("testclient") client.update(createKey=False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) user = User.fromEmail('*****@*****.**') self.assertEqual(user.getFirstStudy(), 'testclient')
def additional_variables_prepare(self, variables: dict): clients = Client.get_all_clients() client_dict = dict() state_dict_payed = dict() state_dict_unpayed = dict() for client in clients: client_dict[client.id] = dict() client_dict[client.id]['client_name'] = client.name client_dict[client.id]['client_surname'] = client.surname states = RoomState.get_all_room_states_for_client(client.id) for state in states: if state.payment: state_dict_payed[state.id] = dict() state_dict_payed[state.id]['client_id'] = client.id state_dict_payed[state.id]['room_name'] = Room.get_room(state.room).name state_dict_payed[state.id]['reserved_from'] = state.reserved_from state_dict_payed[state.id]['reserved_to'] = state.reserved_to if state.payment: state_dict_payed[state.id]['state'] = 'PAYED' else: state_dict_payed[state.id]['state'] = 'RESERVED' else: state_dict_unpayed[state.id] = dict() state_dict_unpayed[state.id]['client_id'] = client.id state_dict_unpayed[state.id]['room_name'] = Room.get_room(state.room).name state_dict_unpayed[state.id]['reserved_from'] = state.reserved_from state_dict_unpayed[state.id]['reserved_to'] = state.reserved_to if state.payment: state_dict_unpayed[state.id]['state'] = 'PAYED' else: state_dict_unpayed[state.id]['state'] = 'RESERVED' variables['client_dict'] = client_dict variables['state_dict_payed_dict'] = state_dict_payed variables['state_dict_unpayed_dict'] = state_dict_unpayed
def testInitClient(self): emptyClient = Client("testclient") self.assertEqual(emptyClient.clientName, "testclient") self.assertEqual(emptyClient.settings_filename, "clients/testclient/settings.json") self.assertEqual(emptyClient.clientJSON, None) self.assertEqual(emptyClient.startDatetime, None) self.assertEqual(emptyClient.endDatetime, None)
def testPendingClientRegsDelete(self): fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) studyList = Client.getPendingClientRegs(fakeEmail) self.assertEqual(studyList, ["testclient"]) Client.deletePendingClientRegs(fakeEmail) afterDelList = Client.getPendingClientRegs(fakeEmail) self.assertEqual(afterDelList, [])
def getSettings(self): from dao.client import Client # Combine profile settings and study settings. # We currently don't have any profile settings retSettings = self.defaultSettings studyList = self.getStudy() if len(studyList) > 0: logging.debug( "To return user settings, combining %s data from %s" % (Client(studyList[0]).getSettings(), studyList[0])) retSettings = User.mergeDicts(retSettings, Client(studyList[0]).getSettings()) logging.debug("After merge retSettings = %s" % retSettings) else: logging.debug("To return user settings, using defaults") return retSettings
def getUserClient(user_uuid): study = User.fromUUID(user_uuid).getFirstStudy() if study != None: client = Client(study) return client else: # User is not part of any study, so no additional filtering is needed return None
def testClientSpecificSettersWithOverride(self): from dao.user import User fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) studyList = Client.getPendingClientRegs(fakeEmail) self.assertEqual(studyList, ["testclient"]) user = User.register("*****@*****.**") self.assertEqual(user.getFirstStudy(), 'testclient') dummyPredModeMap = {'walking': 1.0} dummySection = common.createDummySection(startTime = datetime.now() - timedelta(seconds = 60 * 60), endTime = datetime.now(), startLoc = [-122, 34], endLoc = [-122, 35], predictedMode = dummyPredModeMap) clientSetQuery = client.clientSpecificSetters(user.uuid, dummySection['_id'], dummyPredModeMap) self.assertEqual(clientSetQuery, {'$set': {'test_auto_confirmed': {'mode': 1, 'prob': 1.0}}}) # Apply the change get_section_db().update({'_id': dummySection['_id']}, clientSetQuery) retrievedSection = get_section_db().find_one({'_id': dummySection['_id']}) self.assertEqual(retrievedSection['test_auto_confirmed']['mode'], 1)
def testRegisterExistingUser(self): user = User.register('*****@*****.**') self.assertEquals(user.getStudy(), []) client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) user = User.fromEmail("*****@*****.**") self.assertEquals(user.getStudy(), ['testclient']) # Here's the key difference, now register again user = User.register('*****@*****.**') self.assertEquals(user.getStudy(), ['testclient'])
def testGetUnclassifiedSectionsFiltered(self): """ Tests that queryUnclassifiedSections never returns a section with section['retained'] == False. A section is only returned if section['retained'] == True and all other query conditions are met """ # Clear previous Stage_Sections data and load new data # specific to filtering self.SectionsColl.remove() load_database_json.loadTable(self.serverName, "Stage_Sections", "tests/data/testFilterFile") tests.common.updateSections(self) # Extra updates to Sections necessary for testing filtering for section in self.SectionsColl.find(): section['section_start_point'] = "filler start point" section['section_end_point'] = "filler end point" self.SectionsColl.save(section) from dao.user import User fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey=False) tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) user = User.fromEmail(fakeEmail) self.assertEqual(user.getFirstStudy(), 'testclient') unclassifiedSections = tripManager.getUnclassifiedSections( User.fromEmail(fakeEmail).uuid)['sections'] # Check that of the valid sections in the testFilterFile (2/3), only one of them is returned by the query self.assertEqual(len(unclassifiedSections), 1) # Check that the second entry in the testFilterFile is the only section # that is loaded into the database self.assertEqual('20140401T095742-0700', unclassifiedSections[0]['trip_id'])
def testRegisterExistingUser(self): user = User.register('*****@*****.**') self.assertEquals(user.getStudy(), []) client = Client("testclient") client.update(createKey=False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) user = User.fromEmail("*****@*****.**") self.assertEquals(user.getStudy(), ['testclient']) # Here's the key difference, now register again user = User.register('*****@*****.**') self.assertEquals(user.getStudy(), ['testclient'])
def registeredForStudy(): userEmail = request.query.email client = request.query.client client_key = request.query.client_key logging.debug("request = %s" % (request)) logging.debug("userEmail = %s, client = %s, client_key = %s" % (userEmail, client, client_key)) # try: newSignupCount = Client(client).preRegister(client_key, userEmail) # except Exception as e: # abort(e.code, e.msg) return {'email': userEmail, 'client': client, 'signup_count': newSignupCount }
def setupClientTest(self): # At this point, the more important test is to execute the query and see # how well it works from dao.user import User from dao.client import Client import tests.common from get_database import get_section_db fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) studyList = Client.getPendingClientRegs(fakeEmail) self.assertEqual(studyList, ["testclient"]) user = User.register("*****@*****.**") self.assertEqual(user.getFirstStudy(), 'testclient') dummyPredModeMap = {'walking': 1.0} dummySection = tests.common.createDummySection( startTime = datetime.now() - timedelta(seconds = 60 * 60), endTime = datetime.now(), startLoc = [-122, 34], endLoc = [-122, 35], predictedMode = dummyPredModeMap) return (user, dummySection, dummyPredModeMap)
def setupUserAndClient(self): # At this point, the more important test is to execute the query and see # how well it works from dao.user import User from dao.client import Client import tests.common from datetime import datetime, timedelta from get_database import get_section_db fakeEmail = "*****@*****.**" client = Client("gamified") client.update(createKey = False) tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) studyList = Client.getPendingClientRegs(fakeEmail) self.assertEqual(studyList, ["gamified"]) user = User.register("*****@*****.**") self.assertEqual(user.getFirstStudy(), 'gamified') self.user = user
def testGetUnclassifiedSectionsFiltered(self): """ Tests that queryUnclassifiedSections never returns a section with section['retained'] == False. A section is only returned if section['retained'] == True and all other query conditions are met """ # Clear previous Stage_Sections data and load new data # specific to filtering self.SectionsColl.remove() load_database_json.loadTable(self.serverName, "Stage_Sections", "tests/data/testFilterFile") tests.common.updateSections(self) # Extra updates to Sections necessary for testing filtering for section in self.SectionsColl.find(): section['section_start_point'] = "filler start point" section['section_end_point'] = "filler end point" self.SectionsColl.save(section) from dao.user import User fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) tests.common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) user = User.fromEmail(fakeEmail) self.assertEqual(user.getFirstStudy(), 'testclient') unclassifiedSections = tripManager.getUnclassifiedSections(User.fromEmail(fakeEmail).uuid)['sections'] # Check that of the valid sections in the testFilterFile (2/3), only one of them is returned by the query self.assertEqual(len(unclassifiedSections), 1) # Check that the second entry in the testFilterFile is the only section # that is loaded into the database self.assertEqual('20140401T095742-0700',unclassifiedSections[0]['trip_id'])
def testPreRegisterFail(self): client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 1) self.assertEqual(resultReg, 0) pendingRegs = Client.getPendingClientRegs("*****@*****.**") self.assertEqual(pendingRegs, ["testclient"])
def testClientSpecificSettersNoOverride(self): from dao.user import User fakeEmail = "*****@*****.**" user = User.register("*****@*****.**") self.assertEqual(user.getFirstStudy(), None) dummyPredModeMap = {'walking': 1.0} dummySection = common.createDummySection(startTime = datetime.now() - timedelta(seconds = 60 * 60), endTime = datetime.now(), startLoc = [-122, 34], endLoc = [-122, 35], predictedMode = dummyPredModeMap) clientSetQuery = Client(user.getFirstStudy()).clientSpecificSetters(user.uuid, dummySection, dummyPredModeMap) self.assertEqual(clientSetQuery, None)
def __handle_cookie(self: tornado.web.RequestHandler): user = User.get_user(login=self.get_argument('inputLogin'), password=self.get_argument('inputPassword')) if user is None: self.redirect('/login') return False else: self.set_secure_cookie('user', Client.get_client(user.client).name, expires_days=0.4) self.set_secure_cookie('id', str(user.id), expires_days=0.4) self.set_secure_cookie('client_id', str(user.client), expires_days=0.4) self.set_secure_cookie('access_lvl', str(user.permission_level), expires_days=0.4) return True
def testGetSectionFilter(self): from dao.user import User fakeEmail = "*****@*****.**" client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", fakeEmail) studyList = Client.getPendingClientRegs(fakeEmail) self.assertEqual(studyList, ["testclient"]) user = User.register("*****@*****.**") self.assertEqual(user.getFirstStudy(), 'testclient') self.assertEqual(client.getSectionFilter(user.uuid), []) # Now, set the update timestamp to two weeks ago common.updateUserCreateTime(user.uuid) self.assertEqual(client.getSectionFilter(user.uuid), [{'test_auto_confirmed.prob': {'$lt': 0.9}}])
def create_sample_data(): if get_engine().dialect.has_table(get_engine(), 'user'): return conn = get_engine() create_tables(conn) #add some client session.begin() for client in [ Client(name=('example_name_%s' % x), surname=('example_surname_%s' % x)) for x in range(1, 4) ]: session.add(client) session.commit() #add some user session.begin() for user in [ User(login=('user_%s' % x), password=('user_%s' % x), client=x, permission_level=x) for x in range(1, 4) ]: session.add(user) session.commit() #add some rooms session.begin() for room in [Room(name=('room_%s' % x)) for x in range(1, 4)]: session.add(room) session.commit() #add some reservations session.begin() for roomstate in [ RoomState(reserved_from=datetime.date(2016, 11, 15), reserved_to=datetime.date(2016, 11, 17 + x), room=x, client=3) for x in range(1, 4) ]: session.add(roomstate) session.commit()
def testPreRegisterExistingUser(self): from dao.user import User user = User.register("*****@*****.**") client = Client("testclient") client.update(createKey = False) common.makeValid(client) (resultPre, resultReg) = client.preRegister("this_is_the_super_secret_id", "*****@*****.**") self.assertEqual(resultPre, 0) self.assertEqual(resultReg, 1) self.assertEqual(user.getStudy(), ['testclient']) pendingRegs = Client.getPendingClientRegs("*****@*****.**") self.assertEqual(pendingRegs, [])
def register(userEmail): import uuid from datetime import datetime from dao.client import Client # We are accessing three databases here: # - The list of pending registrations (people who have filled out demographic # information but not installed the app) # - The mapping from the userEmail to the user UUID # - The mapping from the UUID to other profile information about the user # The first two are indexed by the user email. We will use the same field # name in both to indicate that it is a shared key. This also allows us to # have a simple query that we can reuse. userEmailQuery = {'user_email': userEmail} # First, we construct the email -> uuid mapping and store it in the appropriate database. # At this point, we don't know or care whether the user is part of a study # We also store a create timestamp just because that's always a good idea # What happens if the user calls register() again? Do we want to generate a new UUID? # Do we want to update the create timestamp? # For now, let's assume that the answer to both of those questions is yes, # because that allows us to use upsert :) # A bonus fix is that if something is messed up in the DB, calling create again will fix it. # This is the UUID that will be stored in the trip database # in order to do some fig leaf of anonymity # If we have an existing entry, should we change the UUID or not? If we # change the UUID, then there will be a break in the trip history. Let's # change for now since it makes the math easier. anonUUID = uuid.uuid3(uuid.NAMESPACE_URL, "mailto:%s" % userEmail.encode("UTF-8")) emailUUIDObject = {'user_email': userEmail, 'uuid': anonUUID, 'update_ts': datetime.now()} writeResultMap = get_uuid_db().update(userEmailQuery, emailUUIDObject, upsert=True) # Note, if we did want the create_ts to not be overwritten, we can use the # writeResult to decide how to deal with the values # Now, we look to see if the user is part of a study. We can either store # this information in the profile database, or the mapping, or both. For now, # let us store this in the profile database since it is sufficient for it to # be associated with the UUID, we anticipate using it for customization, and # we assume that other customization stuff will be stored in the profile. # We could also assume that we will create the profile if we created the map # and update if we updated. But that has some reliability issues. For # example, what if creating the map succeeded but creating the profile # failed? Subsequently calling the method again to try and fix the profile # will continue to fail because we will be trying to update. # Much better to deal with it separately by doing a separate upsert # Second decision: what do we do if the user is not part of a study? Create a # profile anyway with an empty list, or defer the creation of the profile? # # Decision: create profile with empty list for two reasons: # a) for most of the functions, we want to use the profile data. We should # only use the email -> uuid map in the API layer to get the UUID, and use # the UUID elsewhere. So we need to have profiles for non-study participants # as well. # b) it will also make the scripts to update the profile in the background # easier to write. They won't have to query the email -> UUID database and # create the profile if it doesn't exist - they can just work off the profile # database. # TODO: Write a script that periodically goes through and identifies maps # that don't have an associated profile and fix them study_list = Client.getPendingClientRegs(userEmail) writeResultProfile = User.createProfile(anonUUID, datetime.now(), study_list) if 'err' not in writeResultProfile: # update was successful! # Either upserted or updatedExisting will be true # We can now cleanup the entry from the pending database # Note that we could also move this to a separate cleanup script because # eventual consistency is good enough for us # If there is a profile entry for a particular signup, then delete it Client.deletePendingClientRegs(userEmail) return User.fromUUID(anonUUID)