def testGetNotesWithinBounds(self): boundsString = '0.000000,0.000000,1.5,1.5' testUser = User.objects.create(username='******', email='*****@*****.**', pwHash=auth.encryptPass('james', 'password'), lastLogin=dt.now()) testNote = Note.objects.create(title='test note', user=testUser, lat='1.000000', lng='1.000000', private=True, content='this is a note') testNote = Note.objects.create(title='test note 2', user=testUser, lat='2.000000', lng='2.000000', private=True, content='this is a note') notes = Note.objects.filter(user__username__iexact='james2') self.assertEqual(getHandlers.notesWithinBounds(notes, boundsString).count(), 1)
def deleteAccount(bundle): #get posted data passw = bundle.data.get('passw') user = User.objects.get(username__iexact=bundle.request.GET.get('user')) #check hash of password matches the one stored, and delete accordingly if user.pwHash == auth.encryptPass(passw, user.username): user.delete() else: raise Http404('Invalid Username or Password') return bundle
def updateAccount(bundle): #get posted data passw = bundle.data.get('passw') user = User.objects.get(username__iexact=bundle.request.GET.get('user')) #check hash of supplied password against the one stored #update supplied data accordingly if user.pwHash == auth.encryptPass(passw, user.username): if bundle.data.get('email') != None: if User.objects.filter(email__iexact=bundle.data.get('email')).count() != 0: raise Http404('Email aleady in use') else: user.email = bundle.data.get('email') if bundle.data.get('newpassw') != None: user.pwHash = auth.encryptPass(bundle.data.get('newpassw'), user.username) else: raise Http404('Invalid Username or Password') #save changes user.save() #update bundle object bundle.obj = user return bundle
def testNotesWithinBounds(self): testUser = User.objects.create(username='******', email='*****@*****.**', pwHash=auth.encryptPass('james', 'password'), lastLogin=dt.now()) testNote = Note.objects.create(title='test note', user=testUser, lat='1.000000', lng='1.000000', private=True, content='this is a note') testNote = Note.objects.create(title='test note 2', user=testUser, lat='2.000000', lng='2.000000', private=True, content='this is a note') notes = Note.objects.filter(user__username__iexact='james2') coords = { 'swLat': Decimal('0.0'), 'swLng': Decimal('0.0'), 'neLat': Decimal('1.5'), 'neLng': Decimal('1.5'), } self.assertEqual(geography.notesWithinBounds(notes, coords).count(), 1)
def testGetRoutesWithinBounds(self): boundsString = '0.000000,0.000000,1.5,1.5' testUser = User.objects.create(username='******', email='*****@*****.**', pwHash=auth.encryptPass('james', 'password'), lastLogin=dt.now()) testRoute = Route.objects.create(user=testUser, name='route 1', private=True, mapThumbnail='thumb') PathPoint.objects.create(route=testRoute, orderNum=0, lat=Decimal('2.000000'), lng=Decimal('2.000000')) testRouteTwo = Route.objects.create(user=testUser, name='route 2', private=True, mapThumbnail='thumb') PathPoint.objects.create(route=testRouteTwo, orderNum=0, lat=Decimal('1.000000'), lng=Decimal('1.000000')) testRouteThree = Route.objects.create(user=testUser, name='route 3', private=True, mapThumbnail='thumb') PathPoint.objects.create(route=testRouteThree, orderNum=0, lat=Decimal('-1.000000'), lng=Decimal('1.000000')) routes = Route.objects.filter(user__username__iexact='james2') self.assertEqual(getHandlers.routesWithinBounds(routes, boundsString).count(), 1)
def testAddPathpoints(self): testUser = User.objects.create(username='******', email='*****@*****.**', pwHash=auth.encryptPass('james', 'password'), lastLogin=dt.now()) testRoute = Route.objects.create(user=testUser, name='route 1', private=True, mapThumbnail='thumb') pathpoints = [ { 'lat': Decimal('1.5'), 'lng': Decimal('1.0'), }, { 'lat': Decimal('2'), 'lng': Decimal('1.0'), }, ] postHandlers.addPathPoints(pathpoints, testRoute) self.assertTrue(testRoute.pathpoints.all().count(), 2)
def resetPassword(bundle): #get posted data passw = bundle.data.get('newpassw') code = bundle.data.get('code') try: #check if code matches against user userObj = AuthLinkCode.objects.get(code=code).user except Exception: raise Http404('Invalid reset code') #use supplied new password to generate new pw hash userObj.pwHash = auth.encryptPass(passw, userObj.username) #save changes userObj.save() #update bundle object bundle.obj = userObj return bundle
def testRoutesWithBounds(self): testUser = User.objects.create(username='******', email='*****@*****.**', pwHash=auth.encryptPass('james', 'password'), lastLogin=dt.now()) testRoute = Route.objects.create(user=testUser, name='route 1', private=True, mapThumbnail='thumb') PathPoint.objects.create(route=testRoute, orderNum=0, lat=Decimal('2.000000'), lng=Decimal('2.000000')) testRouteTwo = Route.objects.create(user=testUser, name='route 2', private=True, mapThumbnail='thumb') PathPoint.objects.create(route=testRouteTwo, orderNum=0, lat=Decimal('1.000000'), lng=Decimal('1.000000')) routes = Route.objects.filter(user__username__iexact='james2') coords = { 'swLat': Decimal('0.0'), 'swLng': Decimal('0.0'), 'neLat': Decimal('1.5'), 'neLng': Decimal('1.5'), } self.assertEqual(geography.routesWithinBounds(routes, coords).count(), 1)
def testFilterRouteKeywords(self): testUser = User.objects.create(username='******', email='*****@*****.**', pwHash=auth.encryptPass('james', 'password'), lastLogin=dt.now()) testRouteOne = Route.objects.create(user=testUser, name='route 1', private=True, mapThumbnail='thumb') testRouteTwo = Route.objects.create(user=testUser, name='route 2', private=True, mapThumbnail='thumb') newWordOne = Keyword.objects.create(keyword='testWORD') newWordTwo = Keyword.objects.create(keyword='test123') HasKeyword.objects.create(keyword=newWordOne, route=testRouteOne) HasKeyword.objects.create(keyword=newWordTwo, route=testRouteTwo) routes = Route.objects.filter(user__username__iexact='james2') testBothWords = getHandlers.filterRouteKeywords(routes, 'testWORD,test123') testOneWord = getHandlers.filterRouteKeywords(routes, 'testWORD,UnusedKeyword') testUnusedWord = getHandlers.filterRouteKeywords(routes, 'UnusedKeyword') self.assertEqual(testBothWords.count(), 2) self.assertEqual(testOneWord.count(), 1) self.assertEqual(testUnusedWord.count(), 0)
def handleRegister(bundle): #get info from posted data username = bundle.data.get('user') email = bundle.data.get('email') #generate password hash pwHash = auth.encryptPass(bundle.data.get('passw'), username) #check for pre-existing username/email if User.objects.filter(username__iexact=username).count() != 0: raise Http404('Username aleady in use') if User.objects.filter(email__iexact=email).count() != 0: raise Http404('Email aleady in use') #make account try: newUser = User.objects.create(username=username, email=email, pwHash=pwHash, lastLogin=dt.now()) bundle.obj = newUser except Exception: raise Http404('An error occurred when creating the account') return bundle
def testGetNotesWithinBounds(self): boundsString = '0.000000,0.000000,1.5,1.5' testUser = User.objects.create(username='******', email='*****@*****.**', pwHash=auth.encryptPass( 'james', 'password'), lastLogin=dt.now()) testNote = Note.objects.create(title='test note', user=testUser, lat='1.000000', lng='1.000000', private=True, content='this is a note') testNote = Note.objects.create(title='test note 2', user=testUser, lat='2.000000', lng='2.000000', private=True, content='this is a note') notes = Note.objects.filter(user__username__iexact='james2') self.assertEqual( getHandlers.notesWithinBounds(notes, boundsString).count(), 1)
def testAddPathpoints(self): testUser = User.objects.create(username='******', email='*****@*****.**', pwHash=auth.encryptPass( 'james', 'password'), lastLogin=dt.now()) testRoute = Route.objects.create(user=testUser, name='route 1', private=True, mapThumbnail='thumb') pathpoints = [ { 'lat': Decimal('1.5'), 'lng': Decimal('1.0'), }, { 'lat': Decimal('2'), 'lng': Decimal('1.0'), }, ] postHandlers.addPathPoints(pathpoints, testRoute) self.assertTrue(testRoute.pathpoints.all().count(), 2)
def testAddKeywords(self): testUser = User.objects.create(username='******', email='*****@*****.**', pwHash=auth.encryptPass('james', 'password'), lastLogin=dt.now()) testRoute = Route.objects.create(user=testUser, name='route 1', private=True, mapThumbnail='thumb') postHandlers.addKeywords(['hot', 'dog'], testRoute) self.assertEqual(testRoute.keywords.all().count(), 2)
def testEncryptPass(self): passw = 'hello123' username = '******' h = hashlib.sha1() h.update('adgi43g3g' + passw + '4352fmv' + username.lower()) self.assertEqual(auth.encryptPass(passw, username), h.hexdigest())
def testEncryptPass(self): passw= 'hello123' username='******' h = hashlib.sha1() h.update('adgi43g3g' + passw + '4352fmv' + username.lower()) self.assertEqual(auth.encryptPass(passw, username), h.hexdigest())
def testValidKey(self): testUser = User.objects.create(username='******', email='*****@*****.**', pwHash=auth.encryptPass('james', 'password'), lastLogin=dt.now()) testKeyStr = 'abcdef' testKey = ApiKeys.objects.create(user=testUser, key='abcdef') self.assertTrue(auth.validKey('james', 'abcdef'))