def setUp(self): ''' Need to create a family and a gallery ''' self.family = Family() self.family.save() self.gallery = Gallery.objects.create(title="test_gallery", family_id=self.family.id) clear_directory(settings.FACE_RECOG_TRAIN_TEST_DIR) self.test_image = os.path.join( settings.BASE_DIR, 'facial_recognition/tests/test_image_woman.jpg') self.test_image_destination = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ]) self.test_image_s3_key = ''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ]) directory = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id) ]) if not os.path.exists(directory): os.makedirs(directory) #Copy test image to media area shutil.copy2(self.test_image, self.test_image_destination) self.image = Image(gallery=self.gallery, family=self.family, original_image=''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ])) self.image.save() self.image.upload_files_to_s3() self.person = Person(name='Wallace', gender='M', email='*****@*****.**', family_id=self.family.id, language='en') self.person.save() self.tag = Tag.objects.create(image_id=self.image.id, x1=0.279, y1=0.188, x2=0.536, y2=0.381, person_id=self.person.id, face_detected=True)
def setUp(self): ''' Creates credientials as all views require login ''' self.family = Family() self.family.save() self.user = User.objects.create_user( email='*****@*****.**', password='******', name='Adam Lambert', family_id=self.family.id) self.person1 = Person(name='Adam Lambert', gender='M', user_id=self.user.id, email='*****@*****.**', family_id=self.family.id, latitude=39.768, longitude=-86.158, address='Indianapolis, Indiana') self.person1.save() self.person2 = Person(name='Paul Rodgers', gender='M', family_id=self.family.id, latitude=54.574, longitude=-1.235, address='Middlesborough, UK') self.person2.save()
def setUp(self): ''' Creates credientials as all views require login ''' self.family = Family() self.family.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Prince Barin') self.user.save() self.person = Person(name='Prince Barin', gender='M', user_id = self.user.id, email='*****@*****.**', family_id=self.family.id, hierarchy_score=100) self.person.save() #http://flashgordon.wikia.com/wiki/Prince_Alan self.son = Person(name='Prince Alan', gender='M', family_id=self.family.id, hierarchy_score=-1) self.son.save() #http://flashgordon.wikia.com/wiki/King_Vultan self.vultan = Person(name='King Vultan', gender='M', family_id=self.family.id, hierarchy_score = 200) self.vultan.save() self.lura = Person(name='Lura', gender='F', family_id=self.family.id) self.lura.save() self.vultan_junior = Person(name='Vultan Junior', gender='M', family_id=self.family.id) self.vultan_junior.save()
def complete_registration(self, password): ''' Completes the registration of the new sign up ''' # Create a new family, user and person family = Family.objects.create(description=self.email_address) user = User.objects.create_user(email=self.email_address, password=password, name=self.name, family_id=family.id, language=self.language) person = Person(name=self.name, gender=self.gender, family=family, language=self.language, user=user, email=self.email_address, birth_year=self.birth_year) person.address = self.address person.save() # Delete sign up self.delete() return user
def test_combines_multiple_people_near_to_each_other(self): ''' Tests that the service combines two people that are close together ''' person1 = Person( family_id=self.family.id, name="person1", latitude=7.2, longitude=12.3, ) super( Person, person1).save() # original save so does not overwrite the location person1 = Person( family_id=self.family.id, name="person1", latitude=7.3, longitude=12.5, ) super(Person, person1).save( ) # original save so does not overwrite the the location division_size = 5 result = map_service.get_person_location_points( self.family.id, division_size) self.assertEqual(True, '(7.5, 12.5)' in result) self.assertEqual(2, len(result['(7.5, 12.5)']))
def test_create_progeny_relationship(session): parent = Person(first_name="parent", last_name="user", email="*****@*****.**") child = Person(first_name="child", last_name="user", email="*****@*****.**") parent.children.append(child) session.add(parent) session.add(child) session.commit() assert parent in child.parents
def test_delete_progeny_relationship_when_deleting_person(session): """Checking DELETE ON CASCADE functionality""" parent = Person(first_name="parent", last_name="user", email="*****@*****.**") child = Person(first_name="child", last_name="user", email="*****@*****.**") parent.children.append(child) session.add(parent) session.add(child) session.commit() assert parent in child.parents session.delete(parent) assert len(Progeny.query.all()) == 0
def setUp(self): ''' Creates credientials as all views require login ''' self.family = Family() self.family.save() user = User.objects.create_user(email='*****@*****.**', password='******', name='Roger Taylor') user.save() self.person = Person(name='Roger Taylor', gender='M', user_id = user.id, email='*****@*****.**', family_id=self.family.id) self.person.save() self.wife = Person.objects.create(name='wife', gender='F', hierarchy_score=100, family_id=self.family.id) self.wife_to_person = Relation.objects.create(from_person=self.wife, to_person=self.person, relation_type=PARTNERED) self.son = Person.objects.create(name='son', gender='M',hierarchy_score=101, family_id=self.family.id) self.person_to_son = Relation.objects.create(from_person=self.person, to_person=self.son, relation_type=RAISED) self.daughter = Person.objects.create(name='daughter', gender='F',hierarchy_score=101, family_id=self.family.id) self.person_to_daughter = Relation.objects.create(from_person=self.person, to_person=self.daughter, relation_type=RAISED) self.mum = Person.objects.create(name='mum', gender='F', hierarchy_score=99, family_id=self.family.id) self.mum_to_person = Relation.objects.create(from_person=self.mum, to_person=self.person, relation_type=RAISED) self.dad = Person.objects.create(name='dad', gender='M', hierarchy_score=99, family_id=self.family.id) self.dad_to_person = Relation.objects.create(from_person=self.dad, to_person=self.person, relation_type=RAISED) self.grandma = Person.objects.create(name='grandma', gender='F', hierarchy_score=98, family_id=self.family.id) self.grandma_to_mum = Relation.objects.create(from_person=self.grandma, to_person=self.mum, relation_type=RAISED) self.grandson = Person.objects.create(name='grandson', gender='M', hierarchy_score=102, family_id=self.family.id) self.son_to_grandson = Relation.objects.create(from_person=self.son, to_person=self.grandson, relation_type=RAISED)
def setUp(self): ''' Creates credientials as all views require login ''' self.family = Family() self.family.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Adam Lambert', family_id=self.family.id) self.person1 = Person(name='Adam Lambert', gender='M', user_id = self.user.id, email='*****@*****.**' , family_id=self.family.id, latitude = 39.768, longitude = -86.158, address = 'Indianapolis, Indiana') self.person1.save() self.person2 = Person(name='Paul Rodgers', gender='M', family_id=self.family.id, latitude = 54.574, longitude = -1.235, address = 'Middlesborough, UK') self.person2.save()
class TagTestCase(TestCase): # pragma: no cover ''' Tests for the image class ''' def setUp(self): ''' Need to create a family and a gallery and image ''' self.family = Family() self.family.save() self.person = Person(name='Wallace', gender='M', email='*****@*****.**', family_id=self.family.id, language='en') self.person.save() self.gallery = Gallery.objects.create(title="test_gallery", family_id=self.family.id) self.test_image = os.path.join(settings.BASE_DIR, 'gallery/tests/test_image.jpg') self.test_image_destination = ''.join([settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg']) directory = ''.join([settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id)]) if not os.path.exists(directory): os.makedirs(directory) #Copy test image to media area shutil.copy2(self.test_image, self.test_image_destination) self.image = Image(gallery=self.gallery, family=self.family, original_image=''.join(['galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg'])) self.image.save() def test_rotate_tag(self): ''' Tests that we can rotate a tag correctly ''' tag = Tag.objects.create(image_id=self.image.id, x1=0.1, y1=0.2, x2=0.3, y2=0.4, person_id=self.person.id) tag.rotate(90) #print(tag.x1) self.assertTrue(abs(0.2 - tag.x1) < 0.0001) #print(tag.y1) self.assertTrue(abs(0.7 - tag.y1) < 0.0001) #print(tag.x2) self.assertTrue(abs(0.4 - tag.x2) < 0.0001) #print(tag.y2) self.assertTrue(abs(0.9 - tag.y2) < 0.0001)
def delete(self, person_id): person = Person.find_by_id(person_id) if not person: response = {} return make_response(response=response, code=404) person.live = False person.save_to_db() response = {'result': 'ok'} return make_response(response=response, code=204)
def test_create_person(session): new_user = Person(first_name="test", last_name="user", email="*****@*****.**") session.add(new_user) session.commit() assert new_user.id is not None
def setUp(self): ''' Need to create a family and a gallery and image ''' self.family = Family() self.family.save() self.person = Person(name='Wallace', gender='M', email='*****@*****.**', family_id=self.family.id, language='en') self.person.save() self.gallery = Gallery.objects.create(title="test_gallery", family_id=self.family.id) self.test_image = os.path.join(settings.BASE_DIR, 'gallery/tests/test_image.jpg') self.test_image_destination = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ]) directory = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id) ]) if not os.path.exists(directory): os.makedirs(directory) #Copy test image to media area shutil.copy2(self.test_image, self.test_image_destination) self.image = Image(gallery=self.gallery, family=self.family, original_image=''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ])) self.image.save()
def test_partial_update_locked(self): locked_person = Person(name='Locked Person', gender='O', email='*****@*****.**', family_id=self.family.id, language='en', locked=True) locked_person.save() client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) data = {'fieldName': 'name', 'value': 'troll time!'} url = '/api/person/{0}/'.format(locked_person.id) response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def create_person_obj(first_name='test', last_name='user', email=''): return Person(person_id=str(uuid.uuid4()), firstname=first_name, lastname=last_name, email=str(uuid.uuid4()) if not email else email, birthdate=datetime.datetime.strptime("2019-12-31", "%Y-%m-%d"), street_address="123 Main St.", city="City_name", state="IL", zip="12345", phone="123-123-1234")
def setUp(self): ''' Creates credientials as all views require login ''' self.family = Family() self.family.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Prince Barin') self.user.save() self.person = Person(name='Prince Barin', gender='M', user_id = self.user.id, email='*****@*****.**', family_id=self.family.id, hierarchy_score=100) self.person.save() #http://flashgordon.wikia.com/wiki/Prince_Barin self.son = Person(name='Prince Alan', gender='M', family_id=self.family.id) self.son.save() #http://flashgordon.wikia.com/wiki/King_Vultan self.vultan = Person(name='King Vultan', gender='M', family_id=self.family.id) self.vultan.save() self.lura = Person(name='Lura', gender='F', family_id=self.family.id) self.lura.save()
def post(self): person_json = request.json error = best_match(Draft7Validator(schema).iter_errors(person_json)) if error: response = {'error': error.message} return make_response(response=response, code=400) try: birthdate = datetime.datetime.strptime( person_json.get('birthdate'), "%Y-%m-%d") except: response = {'error': "INVALID_BIRTHDATE"} return make_response(response=response, code=400) if person_json.get("children"): children = Person.query.filter( Person.person_id.in_(person_json.get("children"))).all() person_json['children'] = children if person_json.get("parents"): parents = Person.query.filter( Person.person_id.in_(person_json.get("parents"))).all() person_json['parents'] = parents person = Person(person_id=str(uuid.uuid4()), firstname=person_json.get('firstname'), lastname=person_json.get('lastname'), birthdate=birthdate, email=person_json.get('email'), street_address=person_json.get('street_address'), city=person_json.get('city'), state=person_json.get('state'), zip=person_json.get('zip'), phone=person_json.get('phone'), children=person_json.get('children', []), parents=person_json.get('parents', [])) person.save_to_db() response = {"result": "ok", "data": [person_obj(person)]} return make_response(response=response, code=201)
def test_get_snapped_location_for_a_person(self): ''' Tests that the service snaps the location of a person to a grid ''' person1 = Person( family_id=self.family.id, name="person1", latitude=1.2, longitude=4.3, ) division_size = 1.0 result = map_service.get_snapped_location(person1, division_size) self.assertEqual(1.5, result[0]) self.assertEqual(4.5, result[1])
def put(self, person_id): person = Person.find_by_id(person_id) print(person) if not person: response = {} return make_response(response=response, code=404) # print(person_id) person_json = request.json error = best_match(Draft7Validator(schema).iter_errors(person_json)) if error: response = {'error': error.message} return make_response(response=response, code=400) # print(person_id) if person_json.get('birthdate'): try: birthdate = datetime.datetime.strptime( person_json.get('birthdate'), "%Y-%m-%d") person_json['birthdate'] = birthdate except: response = {'error': "INVALID_BIRTHDATE"} return make_response(response=response, code=400) # print(person_id) if person_json.get("children"): children = Person.query.filter( Person.person_id.in_(person_json.get("children"))).all() person_json['children'] = children if person_json.get("parents"): parents = Person.query.filter( Person.person_id.in_(person_json.get("parents"))).all() person_json['parents'] = parents print(person_id) person.patch(person_json) print(person_id) person.save_to_db() print(person_id) response = {"result": "ok", "data": [person_obj(person)]} print(response) return make_response(response=response, code=200)
def add_relation_post(request, person_id = 0, person = None): ''' Receives post information for a new relation ''' relation_type = int(request.POST.get("relation_type")) if relation_type not in ( PARTNERED, RAISED, RAISED_BY): raise Http404 #If person does not exist, create a new person existing_person = int(request.POST.get("existing_person")) if not existing_person: new_name = request.POST.get("name").strip() if len(new_name) == 0: raise Http404 language = request.POST.get("language") #http://stackoverflow.com/a/2917399/1245362 if language not in [x[0] for x in settings.LANGUAGES]: raise Http404 gender = request.POST.get("gender") if gender not in (MALE, FEMALE, OTHER): raise Http404 new_person = Person(name=new_name, gender=gender,language=language,family_id=person.family_id) if relation_type == PARTNERED: new_person.hierarchy_score = person.hierarchy_score elif relation_type == RAISED: new_person.hierarchy_score = person.hierarchy_score + 1 elif relation_type == RAISED_BY: new_person.hierarchy_score = person.hierarchy_score - 1 new_person.save() relation_id = new_person.id else: #Existing person relation_id = int(request.POST.get("relation_id")) new_relation = Relation(from_person_id=person.id, to_person_id=relation_id, relation_type=relation_type) new_relation.save() return HttpResponseRedirect('/person={0}/'.format(person_id))
def setUp(self): self.family = Family() self.family.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Ada Lovelace', family = self.family) self.person = Person(name='Ada Lovelace', gender='F', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) self.person.save() self.another_person = Person(name='Another Person', gender='F', email='*****@*****.**', family_id=self.family.id, language='en') self.another_person.save() self.family2 = Family() self.family2.save() self.user2 = User.objects.create_user(email='*****@*****.**', password='******', name='Evil Lovelace', family = self.family2) self.person2 = Person(name='Evil Lovelace', gender='M', email='*****@*****.**', family_id=self.family2.id, language='en', user_id=self.user2.id) self.person2.save() super(PersonApiTestCase, self).setUp()
class TestTreeViews(TestCase): def setUp(self): ''' Creates credientials as all views require login ''' self.family = Family() self.family.save() user = User.objects.create_user(email='*****@*****.**', password='******', name='Roger Taylor') user.save() self.person = Person(name='Roger Taylor', gender='M', user_id = user.id, email='*****@*****.**', family_id=self.family.id) self.person.save() self.wife = Person.objects.create(name='wife', gender='F', hierarchy_score=100, family_id=self.family.id) self.wife_to_person = Relation.objects.create(from_person=self.wife, to_person=self.person, relation_type=PARTNERED) self.son = Person.objects.create(name='son', gender='M',hierarchy_score=101, family_id=self.family.id) self.person_to_son = Relation.objects.create(from_person=self.person, to_person=self.son, relation_type=RAISED) self.daughter = Person.objects.create(name='daughter', gender='F',hierarchy_score=101, family_id=self.family.id) self.person_to_daughter = Relation.objects.create(from_person=self.person, to_person=self.daughter, relation_type=RAISED) self.mum = Person.objects.create(name='mum', gender='F', hierarchy_score=99, family_id=self.family.id) self.mum_to_person = Relation.objects.create(from_person=self.mum, to_person=self.person, relation_type=RAISED) self.dad = Person.objects.create(name='dad', gender='M', hierarchy_score=99, family_id=self.family.id) self.dad_to_person = Relation.objects.create(from_person=self.dad, to_person=self.person, relation_type=RAISED) self.grandma = Person.objects.create(name='grandma', gender='F', hierarchy_score=98, family_id=self.family.id) self.grandma_to_mum = Relation.objects.create(from_person=self.grandma, to_person=self.mum, relation_type=RAISED) self.grandson = Person.objects.create(name='grandson', gender='M', hierarchy_score=102, family_id=self.family.id) self.son_to_grandson = Relation.objects.create(from_person=self.son, to_person=self.grandson, relation_type=RAISED) def test_home_tree_view_loads(self): ''' Tests that the users home screen loads and uses the correct template ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/home/') self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'family_tree/tree.html') def test_person_tree_view_loads(self): ''' Tests that a tree view loads for a given person and uses correct template ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/person={0}/'.format(self.person.id)) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'family_tree/tree.html') def test_shows_error_screen_if_person_not_found_for_user(self): ''' Tests that the error screen loads and uses the correct template when a person is not found ''' user = User.objects.create_user(email='*****@*****.**', password='******', family_id = self.family.id) user.save() self.client.login(email='*****@*****.**', password='******') response = self.client.get('/home/') self.assertEqual(response.status_code, 404) def test_get_css(self): ''' Checks that the css returned from the get_css function is correct ''' related_data = tree_service.get_related_data(self.person) css = _get_css(self.person, related_data, 300) mum_css = "#person%s{left:" % (self.mum.id) self.assertEqual(True, mum_css in css) dad_css = "#person%s{left:" % (self.dad.id) self.assertEqual(True, dad_css in css) def test_other_family_can_not_view_my_family_tree(self): ''' Check people from different families cannot view each others profiles ''' another_family = Family() another_family.save() user = User.objects.create_user(email='*****@*****.**', password='******', family_id = another_family.id) user.save() self.client.login(email='*****@*****.**', password='******') response = self.client.get('/person={0}/'.format(self.person.id)) self.assertEqual(response.status_code, 404) def test_how_am_i_related_view_loads(self): ''' Tests that the users home screen loads and uses the correct template ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/how_am_i_related={0}/'.format(self.grandma.id)) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'family_tree/how_am_i_related.html') self.assertEqual(True,b'grandma' in response.content) self.assertEqual(True,b'mum' in response.content) self.assertEqual(True,b'Roger Taylor' in response.content) self.assertEqual(True,b'Raised' in response.content) def test_whole_tree_view_loads(self): ''' Tests that the whole tree view loads ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/whole_tree/') self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'family_tree/whole_tree.html') def test_descendants_view_loads(self): ''' Tests that the descendants view loads ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/descendants={0}/'.format( self.grandma.id)) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'family_tree/whole_tree.html') def test_ancestors_view_loads(self): ''' Tests that the descendants view loads ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/ancestors={0}/'.format( self.grandson.id)) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'family_tree/whole_tree.html')
class TestMapViews(TestCase): def setUp(self): ''' Creates credientials as all views require login ''' self.family = Family() self.family.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Adam Lambert', family_id=self.family.id) self.person1 = Person(name='Adam Lambert', gender='M', user_id = self.user.id, email='*****@*****.**' , family_id=self.family.id, latitude = 39.768, longitude = -86.158, address = 'Indianapolis, Indiana') self.person1.save() self.person2 = Person(name='Paul Rodgers', gender='M', family_id=self.family.id, latitude = 54.574, longitude = -1.235, address = 'Middlesborough, UK') self.person2.save() def test_map_view_loads(self): ''' Tests that the users home screen loads and uses the correct template ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/map/') self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'family_tree/open_street_map.html') self.assertTrue(b'Paul Rodgers' in response.content) self.assertTrue(b'54.574' in response.content) self.assertTrue(b'-1.235' in response.content) self.assertTrue(b'Adam Lambert' in response.content) self.assertTrue(b'39.768' in response.content) self.assertTrue(b'-86.158' in response.content) def test_map_with_person_args_view_loads(self): ''' Tests that the users home screen loads and uses the correct template ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/map={0}/'.format(self.person2.id)) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'family_tree/open_street_map.html') self.assertTrue(b'Paul Rodgers' in response.content) self.assertTrue(b'54.574' in response.content) self.assertTrue(b'-1.235' in response.content) self.assertTrue(b'Adam Lambert' in response.content) self.assertTrue(b'39.768' in response.content) self.assertTrue(b'-86.158' in response.content) def test_map_view_loads_in_polish(self): ''' This is to check a bug where coordinates were formatted to have commas instead of decimal points when language set to Polish ''' #Create a Polish user user = User.objects.create_user(email='*****@*****.**', password='******', name='Szajka', family_id=self.family.id, language='pl') self.client.login(email='*****@*****.**', password='******') Person.objects.create(name='Szajka', gender='M', user_id = user.id, email='*****@*****.**', family_id=self.family.id, language='pl') from django.utils import translation translation.activate('pl') response = self.client.get('/map={0}/'.format(self.person2.id)) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'family_tree/open_street_map.html') self.assertTrue(b'Paul Rodgers' in response.content) self.assertTrue(b'54.574' in response.content) self.assertTrue(b'-1.235' in response.content) self.assertTrue(b'Adam Lambert' in response.content) self.assertTrue(b'39.768' in response.content) self.assertTrue(b'-86.158' in response.content)
class PersonDeletedUpdateFaceModelTest(TestCase): # pragma: no cover def setUp(self): ''' Need to create a family and a gallery ''' self.family = Family() self.family.save() self.gallery = Gallery.objects.create(title="test_gallery", family_id=self.family.id) clear_directory(settings.FACE_RECOG_TRAIN_TEST_DIR) self.test_image = os.path.join( settings.BASE_DIR, 'facial_recognition/tests/test_image_woman.jpg') self.test_image_destination = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ]) self.test_image_s3_key = ''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ]) directory = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id) ]) if not os.path.exists(directory): os.makedirs(directory) #Copy test image to media area shutil.copy2(self.test_image, self.test_image_destination) self.image = Image(gallery=self.gallery, family=self.family, original_image=''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ])) self.image.save() self.image.upload_files_to_s3() self.person = Person(name='Wallace', gender='M', email='*****@*****.**', family_id=self.family.id, language='en') self.person.save() self.tag = Tag.objects.create(image_id=self.image.id, x1=0.279, y1=0.188, x2=0.536, y2=0.381, person_id=self.person.id, face_detected=True) # Upload new image self.test_image2 = os.path.join( settings.BASE_DIR, 'facial_recognition/tests/test_image_woman_and_baby.jpg') self.test_image2_image_destination = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image_woman_and_baby.jpg' ]) # Copy to test area shutil.copy2(self.test_image2, self.test_image2_image_destination) self.image2 = Image(gallery=self.gallery, family=self.family, original_image=''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image_woman_and_baby.jpg' ])) self.image2.save() self.image2.upload_files_to_s3() self.person2 = Person(name='Gromit', gender='M', email='*****@*****.**', family_id=self.family.id, language='en') self.person2.save() self.tag2 = Tag.objects.create(image_id=self.image2.id, x1=0.312, y1=0.239, x2=0.732, y2=0.811, person_id=self.person2.id, face_detected=True) # Create a trained model process_family(self.family.id) def test_update_family_model(self): data = {'family_id': self.family.id, 'person_id': self.person.id} update_family_model(self.family.id, [data]) face_model = FaceModel.objects.filter(family_id=self.family.id).first() X = pickle.loads(face_model.fit_data_faces) self.assertEqual(1, len(X)) def test_person_deleted_update_face_model(self): person_deleted_update_face_model_id = Queue.objects.get( name='person_deleted_update_face_model').id data = {'family_id': self.family.id, 'person_id': self.person.id} message_encoded = json.dumps(data) message = Message.objects.create( queue_id=person_deleted_update_face_model_id, string_data=message_encoded) person_deleted_update_face_model([message]) face_model = FaceModel.objects.filter(family_id=self.family.id).first() X = pickle.loads(face_model.fit_data_faces) self.assertEqual(1, len(X))
def setUp(self): ''' Creates credientials as all views require login ''' self.family = Family() self.family.save() user = User.objects.create_user(email='*****@*****.**', password='******', name='Roger Taylor') user.save() self.person = Person(name='Roger Taylor', gender='M', user_id=user.id, email='*****@*****.**', family_id=self.family.id) self.person.save() self.wife = Person.objects.create(name='wife', gender='F', hierarchy_score=100, family_id=self.family.id) self.wife_to_person = Relation.objects.create(from_person=self.wife, to_person=self.person, relation_type=PARTNERED) self.son = Person.objects.create(name='son', gender='M', hierarchy_score=101, family_id=self.family.id) self.person_to_son = Relation.objects.create(from_person=self.person, to_person=self.son, relation_type=RAISED) self.daughter = Person.objects.create(name='daughter', gender='F', hierarchy_score=101, family_id=self.family.id) self.person_to_daughter = Relation.objects.create( from_person=self.person, to_person=self.daughter, relation_type=RAISED) self.mum = Person.objects.create(name='mum', gender='F', hierarchy_score=99, family_id=self.family.id) self.mum_to_person = Relation.objects.create(from_person=self.mum, to_person=self.person, relation_type=RAISED) self.dad = Person.objects.create(name='dad', gender='M', hierarchy_score=99, family_id=self.family.id) self.dad_to_person = Relation.objects.create(from_person=self.dad, to_person=self.person, relation_type=RAISED) self.grandma = Person.objects.create(name='grandma', gender='F', hierarchy_score=98, family_id=self.family.id) self.grandma_to_mum = Relation.objects.create(from_person=self.grandma, to_person=self.mum, relation_type=RAISED) self.grandson = Person.objects.create(name='grandson', gender='M', hierarchy_score=102, family_id=self.family.id) self.son_to_grandson = Relation.objects.create(from_person=self.son, to_person=self.grandson, relation_type=RAISED)
class TestTreeViews(TestCase): # pragma: no cover def setUp(self): ''' Creates credientials as all views require login ''' self.family = Family() self.family.save() user = User.objects.create_user(email='*****@*****.**', password='******', name='Roger Taylor') user.save() self.person = Person(name='Roger Taylor', gender='M', user_id=user.id, email='*****@*****.**', family_id=self.family.id) self.person.save() self.wife = Person.objects.create(name='wife', gender='F', hierarchy_score=100, family_id=self.family.id) self.wife_to_person = Relation.objects.create(from_person=self.wife, to_person=self.person, relation_type=PARTNERED) self.son = Person.objects.create(name='son', gender='M', hierarchy_score=101, family_id=self.family.id) self.person_to_son = Relation.objects.create(from_person=self.person, to_person=self.son, relation_type=RAISED) self.daughter = Person.objects.create(name='daughter', gender='F', hierarchy_score=101, family_id=self.family.id) self.person_to_daughter = Relation.objects.create( from_person=self.person, to_person=self.daughter, relation_type=RAISED) self.mum = Person.objects.create(name='mum', gender='F', hierarchy_score=99, family_id=self.family.id) self.mum_to_person = Relation.objects.create(from_person=self.mum, to_person=self.person, relation_type=RAISED) self.dad = Person.objects.create(name='dad', gender='M', hierarchy_score=99, family_id=self.family.id) self.dad_to_person = Relation.objects.create(from_person=self.dad, to_person=self.person, relation_type=RAISED) self.grandma = Person.objects.create(name='grandma', gender='F', hierarchy_score=98, family_id=self.family.id) self.grandma_to_mum = Relation.objects.create(from_person=self.grandma, to_person=self.mum, relation_type=RAISED) self.grandson = Person.objects.create(name='grandson', gender='M', hierarchy_score=102, family_id=self.family.id) self.son_to_grandson = Relation.objects.create(from_person=self.son, to_person=self.grandson, relation_type=RAISED) def test_home_tree_view_loads(self): ''' Tests that the users home screen loads and uses the correct template ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/home/') self.assertNotEqual(response.status_code, 404) def test_person_tree_view_loads(self): ''' Tests that a tree view loads for a given person and uses correct template ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/person={0}/'.format(self.person.id)) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'family_tree/tree_app.html') def test_get_css(self): ''' Checks that the css returned from the get_css function is correct ''' related_data = tree_service.get_related_data(self.person) css = _get_css(self.person, related_data, 300) mum_css = "#person%s{left:" % (self.mum.id) self.assertEqual(True, mum_css in css) dad_css = "#person%s{left:" % (self.dad.id) self.assertEqual(True, dad_css in css) def test_other_family_can_not_view_my_family_tree(self): ''' Check people from different families cannot view each others profiles ''' another_family = Family() another_family.save() user = User.objects.create_user(email='*****@*****.**', password='******', family_id=another_family.id) user.save() self.client.login(email='*****@*****.**', password='******') response = self.client.get('/person={0}/'.format(self.person.id)) self.assertEqual(response.status_code, 404) def test_how_am_i_related_view_loads(self): ''' Tests that the users home screen loads and uses the correct template ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/how_am_i_related={0}/'.format( self.grandma.id)) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'family_tree/how_am_i_related.html') self.assertEqual(True, b'grandma' in response.content) self.assertEqual(True, b'mum' in response.content) self.assertEqual(True, b'Roger Taylor' in response.content) self.assertEqual(True, b'Raised' in response.content) def test_whole_tree_view_loads(self): ''' Tests that the whole tree view loads ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/tree/all/') self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'family_tree/whole_tree.html') def test_descendants_view_loads(self): ''' Tests that the descendants view loads ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/descendants={0}/'.format(self.grandma.id)) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'family_tree/whole_tree.html') def test_ancestors_view_loads(self): ''' Tests that the descendants view loads ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/ancestors={0}/'.format(self.grandson.id)) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'family_tree/whole_tree.html') def test_tree_data_loads(self): ''' Tests that the tree data loads ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/tree/data/') self.assertEqual(response.status_code, 200) def test_tree_app_loads(self): ''' Tests that the tree app loads loads ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/tree/{0}/'.format(self.person.id)) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'family_tree/tree_app.html')
def add_relation_post(request, person_id=0, person=None): ''' Receives post information for a new relation ''' relation_type = int(request.POST.get("relation_type")) if relation_type not in (PARTNERED, RAISED, RAISED_BY): raise Http404 #If person does not exist, create a new person existing_person = int(request.POST.get("existing_person")) if not existing_person: new_name = request.POST.get("name").strip() if len(new_name) == 0: raise Http404 language = request.POST.get("language") #http://stackoverflow.com/a/2917399/1245362 if language not in [x[0] for x in settings.LANGUAGES]: raise Http404 gender = request.POST.get("gender") if gender not in (MALE, FEMALE, OTHER): raise Http404 birth_year, birth_year_valid = intTryParse( request.POST.get("birth_year")) if not birth_year_valid: birth_year = 0 new_person = Person(name=new_name, gender=gender, language=language, family_id=person.family_id, birth_year=birth_year) address = request.POST.get("address") if address: new_person.address = address if relation_type == PARTNERED: new_person.hierarchy_score = person.hierarchy_score elif relation_type == RAISED: new_person.hierarchy_score = person.hierarchy_score + 1 elif relation_type == RAISED_BY: new_person.hierarchy_score = person.hierarchy_score - 1 new_person.save() relation_id = new_person.id else: #Existing person relation_id = int(request.POST.get("relation_id")) new_relation = Relation(from_person_id=person.id, to_person_id=relation_id, relation_type=relation_type) new_relation.save() reevaluate_hierarchy_scores_of_orphans(request.user.family_id) return HttpResponseRedirect('/tree/{0}/'.format(person_id))
class TestRelationViews(TestCase): def setUp(self): ''' Creates credientials as all views require login ''' self.family = Family() self.family.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Prince Barin') self.user.save() self.person = Person(name='Prince Barin', gender='M', user_id = self.user.id, email='*****@*****.**', family_id=self.family.id, hierarchy_score=100) self.person.save() #http://flashgordon.wikia.com/wiki/Prince_Barin self.son = Person(name='Prince Alan', gender='M', family_id=self.family.id) self.son.save() #http://flashgordon.wikia.com/wiki/King_Vultan self.vultan = Person(name='King Vultan', gender='M', family_id=self.family.id) self.vultan.save() self.lura = Person(name='Lura', gender='F', family_id=self.family.id) self.lura.save() def test_add_relation_view_loads(self): ''' Tests that the add_relation_view loads and uses the correct template ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/add_relation={0}/'.format(self.person.id)) self.assertEqual(200, response.status_code) self.assertTemplateUsed(response, 'family_tree/add_relation.html') def test_add_relation_post_rejects_invalid_relation(self): ''' Tests that the add relation api rejects incorrect data ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post('/add_relation_post={0}/'.format(self.person.id),{'existing_person': '0', 'relation_type': '4',}) self.assertEqual(404, response.status_code) def test_add_relation_post_rejects_invalid_name(self): ''' Tests that the add relation api rejects incorrect data ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post('/add_relation_post={0}/'.format(self.person.id),{'existing_person': '0', 'relation_type': '1', 'name': ''}) self.assertEqual(404, response.status_code) def test_add_relation_post_rejects_invalid_language(self): ''' Tests that the add relation api rejects incorrect data ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post('/add_relation_post={0}/'.format(self.person.id),{'existing_person': '0', 'relation_type': '1', 'name': 'Princess Aura', 'language': 'Klingon'}) self.assertEqual(404, response.status_code) def test_add_relation_post_rejects_invalid_gender(self): ''' Tests that the add relation api rejects incorrect data ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post('/add_relation_post={0}/'.format(self.person.id),{'existing_person': '0', 'relation_type': '1', 'name': 'Princess Aura', 'language': 'en', 'gender': 'W'}) self.assertEqual(404, response.status_code) def test_add_relation_creates_person_and_relation(self): ''' Test that the add relation api correctly creates the right records ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post('/add_relation_post={0}/'.format(self.person.id),{'existing_person': '0', 'relation_type': '1', 'name': 'Princess Aura', 'language': 'en', 'gender': 'F'}) self.assertEqual(302, response.status_code) aura = Person.objects.get(name = 'Princess Aura') self.assertEqual('en', aura.language) self.assertEqual('F', aura.gender) self.assertEqual(self.family.id, aura.family_id) self.assertEqual(100, aura.hierarchy_score) relation =Relation.objects.get(from_person_id = aura.id, to_person_id = self.person.id) self.assertEqual(1, relation.relation_type) def test_add_parent_creates_person_and_relation_and_sets_correct_hierarchy(self): ''' Test that the add relation api correctly creates the right records ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post('/add_relation_post={0}/'.format(self.person.id),{'existing_person': '0', 'relation_type': '3', 'name': 'King Barin', 'language': 'en', 'gender': 'M'}) self.assertEqual(302, response.status_code) king = Person.objects.get(name = 'King Barin') self.assertEqual('en', king.language) self.assertEqual('M', king.gender) self.assertEqual(self.family.id, king.family_id) self.assertEqual(99, king.hierarchy_score) relation =Relation.objects.get(from_person_id = king.id, to_person_id = self.person.id) self.assertEqual(2, relation.relation_type) def test_add_relation_to_existing_people(self): ''' Tests that a relation can be added between two existing people ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post('/add_relation_post={0}/'.format(self.person.id),{'existing_person': '1', 'relation_type': '2', 'relation_id': str(self.son.id)}) self.assertEqual(302, response.status_code) relation =Relation.objects.get(from_person_id = self.person.id, to_person_id = self.son.id) self.assertEqual(2, relation.relation_type) def test_break_relation_view_loads(self): ''' Tests that the break_relation_view loads and uses the correct template ''' relation = Relation(from_person_id=self.vultan.id, to_person_id=self.lura.id,relation_type=PARTNERED) relation.save() self.client.login(email='*****@*****.**', password='******') response = self.client.get('/break_relation={0}/'.format(self.vultan.id)) self.assertEqual(200, response.status_code) self.assertTemplateUsed(response, 'family_tree/break_relation.html') self.assertEquals(True, b'Lura' in response.content) def test_break_relation_post_deletes_relation(self): ''' Tests that the relation is deleted from the post view ''' relation = Relation(from_person_id=self.vultan.id, to_person_id=self.lura.id,relation_type=PARTNERED) relation.save() self.client.login(email='*****@*****.**', password='******') response = self.client.post('/break_relation_post={0}/'.format(self.vultan.id),{'relation_id': relation.id}) self.assertEqual(302, response.status_code) self.assertEqual(0, Relation.objects.filter(from_person_id=self.vultan.id, to_person_id=self.lura.id).count())
def _create_person(first="Test", last="User"): p = Person(first_name=first, last_name=last, email=str(uuid.uuid4())) session.add(p) session.commit() return p
class PersonApiTestCase(TestCase): ''' Tests for the person API ''' def setUp(self): self.family = Family() self.family.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Ada Lovelace', family = self.family) self.person = Person(name='Ada Lovelace', gender='F', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) self.person.save() self.another_person = Person(name='Another Person', gender='F', email='*****@*****.**', family_id=self.family.id, language='en') self.another_person.save() self.family2 = Family() self.family2.save() self.user2 = User.objects.create_user(email='*****@*****.**', password='******', name='Evil Lovelace', family = self.family2) self.person2 = Person(name='Evil Lovelace', gender='M', email='*****@*****.**', family_id=self.family2.id, language='en', user_id=self.user2.id) self.person2.save() super(PersonApiTestCase, self).setUp() def test_list_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') response = client.get('/api/person/', format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_list(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') # Check this works with JWT token auth_details = { 'email': '*****@*****.**', 'password': '******' } auth_response = client.post('/api/auth/obtain_token/', auth_details, format='json') token = json.loads(auth_response.content)["access"] client.credentials(HTTP_AUTHORIZATION='Bearer ' + token) response = client.get('/api/person/', format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(b'Ada Lovelace' in response.content) def test_list_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') # Check this works with JWT token auth_details = { 'email': '*****@*****.**', 'password': '******' } auth_response = client.post('/api/auth/obtain_token/', auth_details, format='json') token = json.loads(auth_response.content)["access"] client.credentials(HTTP_AUTHORIZATION='Bearer ' + token) response = client.get('/api/person/', format='json') self.assertFalse(b'Ada Lovelace' in response.content) def test_list_search_match(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/person/?search=ada' response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(b'Ada Lovelace' in response.content) def test_list_search_no_match(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/person/?search=asfasfasfa' response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertFalse(b'Ada Lovelace' in response.content) def test_retrieve_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/person/{0}/'.format(self.person.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_retrieve(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/person/{0}/'.format(self.person.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(b'Ada Lovelace' in response.content) def test_retrieve_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user2) url = '/api/person/{0}/'.format(self.person.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) json.loads(response.content) def test_partial_update(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) data = { 'fieldName': 'name', 'value': 'Ada Lovelace II' } url = '/api/person/{0}/'.format(self.person.id) response = client.patch(url, data, format='json') self.person = Person.objects.get(id=self.person.id) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(b'Ada Lovelace II' in response.content) self.assertEqual('Ada Lovelace II', self.person.name) def test_partial_update_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user2) data = { 'fieldName': 'name', 'value': 'Ada Lovelace II' } url = '/api/person/{0}/'.format(self.person.id) response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) json.loads(response.content) def test_partial_update_locked(self): locked_person = Person(name='Locked Person', gender='O', email='*****@*****.**', family_id=self.family.id, language='en', locked=True) locked_person.save() client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) data = { 'fieldName': 'name', 'value': 'troll time!' } url = '/api/person/{0}/'.format(locked_person.id) response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_partial_update_field_not_whitelisted(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) data = { 'fieldName': 'creation_date', 'value': '2001-01-01' } url = '/api/person/{0}/'.format(self.person.id) response = client.patch(url, data, format='json') self.person = Person.objects.get(id=self.person.id) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_partial_update_email_for_other_user(self): user = User.objects.create_user(email='*****@*****.**', password='******', name='Ada Horrible Cousin', family = self.family) client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=user) data = { 'fieldName': 'email', 'value': '*****@*****.**' } url = '/api/person/{0}/'.format(self.person.id) response = client.patch(url, data, format='json') self.person = Person.objects.get(id=self.person.id) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_partial_update_locked_for_other_user(self): user = User.objects.create_user(email='*****@*****.**', password='******', name='Ada Horrible Cousin', family = self.family) client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=user) data = { 'fieldName': 'locked', 'value': True } url = '/api/person/{0}/'.format(self.person.id) response = client.patch(url, data, format='json') self.person = Person.objects.get(id=self.person.id) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_create_person(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) data = { 'from_person_id': self.person.id, 'relation_type': 1, 'name': 'wife', 'gender': 'F', 'birth_year': 1920, } url = '/api/person/' response = client.post(url, data, format='json') new_person = Person.objects.get(name='wife') new_relation = Relation.objects.get(from_person_id=self.person.id, to_person_id=new_person.id) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(b'wife' in response.content) self.assertEqual(1, new_relation.relation_type) def test_create_person_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') data = { 'from_person_id': self.person.id, 'relation_type': 1, 'name': 'wife', 'gender': 'F', 'birth_year': 1920, } url = '/api/person/' response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_create_person_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user2) data = { 'from_person_id': self.person.id, 'relation_type': 1, 'name': 'wife', 'gender': 'F', 'birth_year': 1920, } url = '/api/person/' response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) json.loads(response.content) def test_create_person_invalid_from_person(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) data = { 'relation_type': 1, 'name': 'wife', 'gender': 'F', 'birth_year': 1920, } url = '/api/person/' response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content) def test_create_person_invalid_relation_type(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) data = { 'from_person_id': self.person.id, 'relation_type': 11, 'name': 'wife', 'gender': 'F', 'birth_year': 1920, } url = '/api/person/' response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content) def test_create_person_invalid_name(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) data = { 'from_person_id': self.person.id, 'relation_type': 1, 'name': ' ', 'gender': 'F', 'birth_year': 1920, } url = '/api/person/' response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content) def test_create_person_invalid_gender(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) data = { 'from_person_id': self.person.id, 'relation_type': 1, 'name': 'wife', 'gender': 'AA', 'birth_year': 1920, } url = '/api/person/' response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content) def test_destroy_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') response = client.delete('/api/person/{0}/'.format(self.another_person.id), format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_destroy_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user2) response = client.delete('/api/person/{0}/'.format(self.another_person.id), format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) json.loads(response.content) def test_destroy_existing_user(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) response = client.delete('/api/person/{0}/'.format(self.person.id), format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_destroy(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) response = client.delete('/api/person/{0}/'.format(self.another_person.id), format='json') self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(0, Person.objects.filter(id=self.another_person.id).count())
class ProfilePhotoProcessTest(TestCase): # pragma: no cover def setUp(self): ''' Need to create a family and a gallery ''' self.family = Family() self.family.save() self.gallery = Gallery.objects.create(title="test_gallery", family_id=self.family.id) clear_directory(settings.FACE_RECOG_TRAIN_TEST_DIR) self.test_image = os.path.join( settings.BASE_DIR, 'facial_recognition/tests/test_image_woman.jpg') self.test_image_destination = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ]) self.test_image_s3_key = ''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ]) directory = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id) ]) if not os.path.exists(directory): os.makedirs(directory) #Copy test image to media area shutil.copy2(self.test_image, self.test_image_destination) self.image = Image(gallery=self.gallery, family=self.family, original_image=''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ])) self.image.save() self.image.upload_files_to_s3() self.person = Person(name='Wallace', gender='M', email='*****@*****.**', family_id=self.family.id, language='en') self.person.save() self.tag = Tag.objects.create(image_id=self.image.id, x1=0.279, y1=0.188, x2=0.536, y2=0.381, person_id=self.person.id, face_detected=True) # Upload new image self.test_image2 = os.path.join( settings.BASE_DIR, 'facial_recognition/tests/test_image_woman_and_baby.jpg') self.test_image2_destination = settings.MEDIA_ROOT + 'profile_photos/test_image_woman_and_baby.jpg' # Create a trained model process_family(self.family.id) def tearDown(self): self.image.delete_local_image_files() self.image.delete_remote_image_files() try: os.remove(self.test_image_destination) except: pass try: os.remove(self.test_image2_destination) except: pass self.person.remove_local_images() self.person.remove_remote_images() def test_profile_photo_process(self): # Copy to test area shutil.copy2(self.test_image2, self.test_image2_destination) # Add profile photo self.person.set_profile_image_crop_rotate_resize( self.test_image2_destination, 372, 406, 878, 1378, 0, True) self.person.save() profile_photo_process_id = Queue.objects.get( name='profile_photo_process').id message = Message.objects.create(queue_id=profile_photo_process_id, integer_data=self.person.id) profile_photo_process([message]) face_model = FaceModel.objects.filter(family_id=self.family.id).first() X = pickle.loads(face_model.fit_data_faces) message = Message.objects.get(pk=message.id) self.assertEqual(1, len(X)) self.assertEqual(False, message.error)
class TestMapViews(TestCase): # pragma: no cover def setUp(self): ''' Creates credientials as all views require login ''' self.family = Family() self.family.save() self.user = User.objects.create_user( email='*****@*****.**', password='******', name='Adam Lambert', family_id=self.family.id) self.person1 = Person(name='Adam Lambert', gender='M', user_id=self.user.id, email='*****@*****.**', family_id=self.family.id, latitude=39.768, longitude=-86.158, address='Indianapolis, Indiana') self.person1.save() self.person2 = Person(name='Paul Rodgers', gender='M', family_id=self.family.id, latitude=54.574, longitude=-1.235, address='Middlesborough, UK') self.person2.save() def test_map_points_data_returns(self): ''' Tests that map points data can be polled ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/map_points/10.1/') self.assertEqual(response.status_code, 200) self.assertTrue(b'Paul Rodgers' in response.content) self.assertTrue(b'Adam Lambert' in response.content) def test_map_points_returns(self): ''' Test situation that caused 404 in dev ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/map_points/10.2/') self.assertEqual(response.status_code, 200) def test_map_view_loads(self): ''' Tests that the users home screen loads and uses the correct template ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/map/') self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'maps/map.html') def test_map_view_loads_person_args(self): ''' Tests that the users home screen loads and uses the correct template ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/map={0}/'.format(self.person2.id)) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'maps/map.html') def test_map_view_loads_in_polish(self): ''' This is to check a bug where coordinates were formatted to have commas instead of decimal points when language set to Polish ''' #Create a Polish user user = User.objects.create_user(email='*****@*****.**', password='******', name='Szajka', family_id=self.family.id, language='pl') self.client.login(email='*****@*****.**', password='******') person = Person.objects.create(name='Szajka', gender='M', user_id=user.id, email='*****@*****.**', family_id=self.family.id, language='pl') person.longitude = 1.1 person.latitude = 1.2 super(Person, person).save() from django.utils import translation translation.activate('pl') response = self.client.get('/map/') self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'maps/map.html') self.assertTrue(b'1.1' in response.content) self.assertTrue(b'1.2' in response.content)
def create(self, request): ''' Creates a new person record and links it to another person needs from_person_id, relation_type, name, gender, birth_year and address ''' queryset = Person.objects.filter(family_id=request.user.family_id) from_person_id, from_person_id_valid = intTryParse( request.data.get("from_person_id")) if not from_person_id_valid: return HttpResponse(status=400, content="Invalid from_person_id") from_person = get_object_or_404(queryset, pk=from_person_id) relation_type, relation_type_valid = intTryParse( request.data.get("relation_type")) if not relation_type_valid or relation_type not in (PARTNERED, RAISED, RAISED_BY): return HttpResponse(status=400, content="Invalid relation_type") name = request.data.get("name") if not name or len(name.strip()) == 0: return HttpResponse(status=400, content="Invalid name") gender = request.data.get("gender") if gender not in (MALE, FEMALE, OTHER, NON_BINARY, PREFER_NOT_TO_SAY): return HttpResponse(status=400, content="Invalid gender") birth_year, birth_year_valid = intTryParse( request.POST.get("birth_year")) if not birth_year_valid: birth_year = 0 with reversion.create_revision(): new_person = Person(name=name.strip(), gender=gender, family_id=from_person.family_id, birth_year=birth_year) address = request.data.get("address") if address: new_person.address = address # Hierarchy scores will eventually be deprecated if relation_type == PARTNERED: new_person.hierarchy_score = from_person.hierarchy_score elif relation_type == RAISED: new_person.hierarchy_score = from_person.hierarchy_score + 1 elif relation_type == RAISED_BY: new_person.hierarchy_score = from_person.hierarchy_score - 1 new_person.save() # Store some meta-information. reversion.set_user(request.user) reversion.set_comment('Create ' + request.META.get('HTTP_X_REAL_IP')) relation = create_relation(request.user, from_person, new_person, relation_type) relation_serializer = RelationSerializer(relation) person_serializer = PersonSerializer(new_person) return Response({ 'person': person_serializer.data, 'relation': relation_serializer.data })
def get(self, person_id): print(person_id) if person_id: person = Person.find_by_id(person_id) if person: relation_type = "" if "siblings" in request.url: relation_type, persons = "siblings", person.siblings elif "grandparents" in request.url: relation_type, persons = "grandparents", person.grandparents elif "parents" in request.url: relation_type, persons = "parents", person.parents elif "children" in request.url: relation_type, persons = "children", person.children elif "cousins" in request.url: relation_type, persons = "cousins", person.cousins if relation_type: response = { "result": "ok", "links": [{ "href": "/persons/%s/%s" % (person_id, relation_type), "rel": "self" }], "data": persons_obj_list(persons, noparents=True, nochildren=True), } else: response = {"result": "ok", "data": [person_obj(person)]} return make_response(response=response, code=200) else: response = {} return make_response(response=response, code=404) else: person_href = "/persons/?page=%s" persons = Person.get_all_live() if "firstname" in request.args: firstname = request.args.get('firstname') persons = persons.filter_by(firstname=firstname) person_href += "&firstname=" + firstname if "state" in request.args: state = request.args.get('state') persons = persons.filter_by(state=state) person_href += "&state=" + state # pagination page = int(request.args.get('page', 1)) persons = persons.paginate(page=page, per_page=self.PERSONS_PER_PAGE) response = { "result": "ok", "links": [{ "href": person_href % page, "rel": "self" }], "data": persons_obj(persons) } if persons.has_prev: response["links"].append({ "href": person_href % persons.prev_num, "rel": "previous" }) if persons.has_next: response["links"].append({ "href": person_href % persons.next_num, "rel": "next" }) return make_response(response=response, code=200)
class TrainTestCase(TestCase): # pragma: no cover def setUp(self): ''' Need to create a family and a gallery ''' self.family = Family() self.family.save() self.gallery = Gallery.objects.create(title="test_gallery", family_id=self.family.id) clear_directory(settings.FACE_RECOG_TRAIN_TEST_DIR) self.test_image = os.path.join( settings.BASE_DIR, 'facial_recognition/tests/test_image_woman.jpg') self.test_image_destination = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ]) self.test_image_s3_key = ''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ]) directory = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id) ]) if not os.path.exists(directory): os.makedirs(directory) #Copy test image to media area shutil.copy2(self.test_image, self.test_image_destination) self.image = Image(gallery=self.gallery, family=self.family, original_image=''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ])) self.image.save() self.image.upload_files_to_s3() self.person = Person(name='Wallace', gender='M', email='*****@*****.**', family_id=self.family.id, language='en') self.person.save() self.tag = Tag.objects.create(image_id=self.image.id, x1=0.279, y1=0.188, x2=0.536, y2=0.381, person_id=self.person.id, face_detected=True) def test_get_file_for_tag(self): dir_name = settings.FACE_RECOG_TRAIN_TEST_DIR file = get_file_for_tag(self.tag, self.image, dir_name) self.assertIsNotNone(file) #Clear up self.image.delete_local_image_files() self.image.delete_remote_image_files() def test_process_file(self): X = [] y = [] file = self.test_image_destination process_file(file, X, y, self.person.id) self.assertEqual(1, len(X)) self.assertEqual(1, len(y)) #Clear up self.image.delete_local_image_files() self.image.delete_remote_image_files() def test_process_person(self): path = settings.MEDIA_ROOT + 'profile_photos/large_test_image1.jpg' shutil.copy2(self.test_image, path) self.person.set_profile_image_crop_rotate_resize( path, 1, 1, 1200, 1700, 0, True) self.person.save() X = [] y = [] process_person(self.person, X, y) self.person.remove_local_images() self.person.remove_remote_images() self.assertEqual(2, len(X)) self.assertEqual(2, len(y)) #Clear up self.image.delete_local_image_files() self.image.delete_remote_image_files() def test_process_family(self): path = settings.MEDIA_ROOT + 'profile_photos/large_test_image1.jpg' shutil.copy2(self.test_image, path) self.person.set_profile_image_crop_rotate_resize( path, 1, 1, 1200, 1700, 0, True) self.person.save() process_family(self.family.id) self.person.remove_local_images() self.person.remove_remote_images() face_model = FaceModel.objects.get(family_id=self.family.id) self.assertTrue(self.person.id in face_model.fit_data_person_ids) self.assertIsNotNone(face_model.trained_knn_model) #Clear up self.image.delete_local_image_files() self.image.delete_remote_image_files()
class ResizeTagsTestCase(TestCase): # pragma: no cover def setUp(self): ''' Need to create a family and a gallery ''' self.family = Family() self.family.save() self.gallery = Gallery.objects.create(title="test_gallery", family_id=self.family.id) self.test_image = os.path.join( settings.BASE_DIR, 'facial_recognition/tests/test_image_woman.jpg') self.test_image_destination = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ]) self.test_image_s3_key = ''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ]) directory = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id) ]) if not os.path.exists(directory): os.makedirs(directory) #Copy test image to media area shutil.copy2(self.test_image, self.test_image_destination) self.image = Image(gallery=self.gallery, family=self.family, original_image=''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ])) self.image.save() self.image.upload_files_to_s3() self.person = Person(name='Wallace', gender='M', email='*****@*****.**', family_id=self.family.id, language='en') self.person.save() self.tag = Tag.objects.create(image_id=self.image.id, x1=0.3, y1=0.2, x2=0.5, y2=0.4, person_id=self.person.id) def test_tag_resizes(self): # Create a message to resize tag resize_tag_queue_id = Queue.objects.get(name='resize_tag').id message = Message.objects.create(queue_id=resize_tag_queue_id, integer_data=self.tag.id) resize_tags([message]) resized_tag = Tag.objects.get(pk=self.tag.id) self.assertTrue(abs(0.279 - resized_tag.x1) < 0.001) self.assertTrue(abs(0.188 - resized_tag.y1) < 0.001) self.assertTrue(abs(0.536 - resized_tag.x2) < 0.001) self.assertTrue(abs(0.381 - resized_tag.y2) < 0.001) #Clear up self.image.delete_local_image_files() self.image.delete_remote_image_files()
class ImageFaceDetectTest(TestCase): # pragma: no cover def setUp(self): ''' Need to create a family and a gallery ''' self.family = Family() self.family.save() self.gallery = Gallery.objects.create(title="test_gallery", family_id=self.family.id) clear_directory(settings.FACE_RECOG_TRAIN_TEST_DIR) self.test_image = os.path.join( settings.BASE_DIR, 'facial_recognition/tests/test_image_woman.jpg') self.test_image_destination = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ]) self.test_image_s3_key = ''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ]) directory = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id) ]) if not os.path.exists(directory): os.makedirs(directory) #Copy test image to media area shutil.copy2(self.test_image, self.test_image_destination) self.image = Image(gallery=self.gallery, family=self.family, original_image=''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ])) self.image.save() self.image.upload_files_to_s3() self.person = Person(name='Wallace', gender='M', email='*****@*****.**', family_id=self.family.id, language='en') self.person.save() self.tag = Tag.objects.create(image_id=self.image.id, x1=0.279, y1=0.188, x2=0.536, y2=0.381, person_id=self.person.id, face_detected=True) # Create a trained model process_family(self.family.id) def tearDown(self): try: self.image.delete_local_image_files() self.image.delete_remote_image_files() except: pass try: os.remove(self.test_image_destination) except: pass def test_image_face_detect(self): # Upload new image new_test_image = os.path.join( settings.BASE_DIR, 'facial_recognition/tests/test_image_woman_and_baby.jpg') new_test_image_destination = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image_woman_and_baby.jpg' ]) # Copy to test area shutil.copy2(new_test_image, new_test_image_destination) new_image = Image(gallery=self.gallery, family=self.family, original_image=''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image_woman_and_baby.jpg' ])) new_image.save() new_image.upload_files_to_s3() # Create a message to resize tag image_face_detect_queue_id = Queue.objects.get( name='image_face_detect').id message = Message.objects.create(queue_id=image_face_detect_queue_id, integer_data=new_image.id) image_face_detect([message]) suggested_tags = SuggestedTag.objects.filter(image_id=new_image.id) self.assertEqual(2, suggested_tags.count()) self.assertEqual(self.person.id, suggested_tags[0].person_id) new_image.delete_local_image_files() new_image.delete_remote_image_files()