def test_can_delete_account_and_profile(self): ''' Test that a user can delete their account and their profile ''' family = Family() family.save() user = User.objects.create_user(email='*****@*****.**', password='******', name='Andy Lau') Person.objects.create(name='Andy Lau', gender='M', family_id=family.id, user_id=user.id) self.client.login(email='*****@*****.**', password='******') self.client.post('/accounts/delete/', { 'delete_profile': '1', }) self.assertEqual(0, Person.objects.filter(name='Lau Fok Wing').count()) self.assertEqual( 0, User.objects.filter(email='*****@*****.**').count())
def test_create_news_letter_events_first_time(self): ''' Tests that the correct news events are created ''' #Flush database FamilyNewsLetterEvents.objects.all().delete() from family_tree.models import Person, Family family = Family() family.save() Person.objects.all().delete() p1 = Person.objects.create(name='Connor Macleod', gender='M', family_id=family.id) p1.save() p2 = Person.objects.create(name='The Kurgan', gender='M', family_id=family.id) p2.save() FamilyNewsLetterEvents.objects.create_news_letter_events() self.assertEqual(2, FamilyNewsLetterEvents.objects.count())
def test_create_single_language_emails(self): ''' Tests that we create emails in batches for sections of families in their who share a first language ''' from family_tree.models import Family family = Family() family.save() from custom_user.models import User User.objects.all().delete() user = User.objects.create_user(email='*****@*****.**', password='******', name='Fabio Testi', language='pl', family_id=family.id) user.is_confirmed = True user.save() user2 = User.objects.create_user(email='*****@*****.**', password='******', name='Ron Nummi', language='pl', family_id=family.id) user2.is_confirmed = True user2.save() Email.objects.all().delete() Email.objects._create_single_language_emails( 'pl', family.id, "subject", "content's", "content's html") #use ' to check injection errors self.assertEqual(2, Email.objects.all().count())
def test_create_emails(self): ''' Tests that we create emails in batches for sections of families in their who share a first language ''' from family_tree.models import Family family = Family() family.save() from custom_user.models import User User.objects.all().delete() user = User.objects.create_user(email='*****@*****.**', password='******', name='Fabio Testi', language='pl', family_id= family.id) user.is_confirmed = True user.save() user2 = User.objects.create_user(email='*****@*****.**', password='******', name='Ron Nummi', language='pl', family_id= family.id) user2.is_confirmed = True user2.save() FamilyNewsLetterEvents.objects.all().delete() n1 = FamilyNewsLetterEvents(family_id=family.id, person_id=1, person_name='Fabio Testi', new_member=True ) n1.save() n2 = FamilyNewsLetterEvents(family_id=family.id, person_id=1, person_name='Ron Nummii', new_member=False ) n2.save() Email.objects.all().delete() Email.objects._create_emails() self.assertEqual(2,Email.objects.all().count())
def test_get_distinct_families_and_languages(self): ''' Tests that we get the languages and families involved in the newsletters ''' from family_tree.models import Family family = Family() family.save() from custom_user.models import User user = User.objects.create_user(email='*****@*****.**', password='******', name='Fabio Testi', language='en', family_id=family.id) user.save() user2 = User.objects.create_user(email='*****@*****.**', password='******', name='Ron Nummi', language='pl', family_id=family.id) user2.save() FamilyNewsLetterEvents.objects.all().delete() n1 = FamilyNewsLetterEvents(family_id=family.id, person_id=1, person_name='Mustapha Ibrahim', new_member=True) n1.save() rows = Email.objects._get_distinct_families_and_languages() self.assertEqual(2, len(rows))
def test_can_delete_account_without_deleting_profile(self): ''' Test that a user can delete their account without deleting their profile ''' family = Family() family.save() user = User.objects.create_user(email='*****@*****.**', password='******', name='Lau Fok Wing') Person.objects.create(name='Lau Fok Wing', gender='M', family_id=family.id, user_id=user.id) self.client.login(email='*****@*****.**', password='******') self.client.post('/accounts/delete/', { 'delete_profile': '0', }) self.assertEqual(1, Person.objects.filter(name='Lau Fok Wing').count()) self.assertEqual(None, Person.objects.get(name='Lau Fok Wing').user_id) self.assertEqual( 0, User.objects.filter(email='*****@*****.**').count())
class FacialRecognitionTestCase(TestCase): # pragma: no cover def setUp(self): self.family = Family() self.family.save() def test_create_model(self): array1 = [(1, 2), ('no', 'yes'), 'blah'] array2 = [1, 2] obj1 = { 'stuff': 'yes', 'notstuff': 'no', } FaceModel.objects.create(family=self.family, fit_data_faces=pickle.dumps(array1), fit_data_person_ids=pickle.dumps(array2), n_neighbors=3, trained_knn_model=pickle.dumps(obj1)) result = FaceModel.objects.get(family=self.family) self.assertEqual(array1, pickle.loads(result.fit_data_faces)) self.assertEqual(array2, pickle.loads(result.fit_data_person_ids)) self.assertEqual(obj1, pickle.loads(result.trained_knn_model))
class TestGallery(TestCase): # pragma: no cover ''' Test class for the gallery object ''' def setUp(self): ''' Creates credientials as all views require login ''' self.family = Family() self.family.save() self.user = User.objects.create_user( email='arif_mardin:@queenonline.com', password='******', name='Arif Mardin:', family_id=self.family.id) self.gallery = Gallery.objects.create(family_id=self.family.id, title="gallery") 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.images = [] for i in range(0, 20): self.images.append( Image(gallery=self.gallery, family=self.family, original_image=self.test_image_destination, thumbnail=self.test_image_destination, large_thumbnail=self.test_image_destination)) def test_delete_all_images(self): ''' Tests we can delete all images associated with gallery ''' self.gallery.delete_all_images() self.assertEqual( 0, Image.objects.filter(gallery_id=self.gallery.id).count())
class MapServiceTestCase(TestCase): # pragma: no cover ''' This defines all the tests for the map service ''' def setUp(self): self.family = Family() self.family.save() 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 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)']))
class TestCustomUserViews(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='Bruce Lee' ) self.person = Person.objects.create(name='Bruce Lee', gender='M', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) self.client = HttpClient(HTTP_X_REAL_IP='127.0.0.1')
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)
class ImageTestCase(TestCase): ''' Tests for the image class ''' 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, '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) def test_create_thumbnail(self): ''' Tests that we can create a thumbnail ''' #Copy test image to media area shutil.copy2(self.test_image, self.test_image_destination) image = Image(gallery=self.gallery, family=self.family, original_image=''.join(['galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg'])) thumbnail = settings.MEDIA_ROOT + image._create_thumbnail((500,500)) PIL.Image.open(thumbnail) #Clear up mess afterwards os.remove(self.test_image_destination) os.remove(thumbnail) def test_make_thumbnails(self): ''' Tests themake thumbnails routine ''' #Copy test image to media area shutil.copy2(self.test_image, self.test_image_destination) image = Image(gallery=self.gallery, family=self.family, original_image=self.test_image_destination) image.make_thumbnails() PIL.Image.open(settings.MEDIA_ROOT+ str(image.thumbnail)) PIL.Image.open(settings.MEDIA_ROOT + str(image.large_thumbnail)) PIL.Image.open(settings.MEDIA_ROOT +str(self.gallery.thumbnail))
def test_can_delete_account_and_profile(self): ''' Test that a user can delete their account and their profile ''' family = Family() family.save() user = User.objects.create_user(email='*****@*****.**', password='******', name='Andy Lau' ) Person.objects.create(name='Andy Lau', gender='M', family_id=family.id, user_id = user.id) self.client.login(email='*****@*****.**', password='******') self.client.post('/accounts/delete/',{'delete_profile': '1',}) self.assertEqual(0, Person.objects.filter(name='Lau Fok Wing').count()) self.assertEqual(0, User.objects.filter(email='*****@*****.**').count())
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_can_delete_account_without_deleting_profile(self): ''' Test that a user can delete their account without deleting their profile ''' family = Family() family.save() user = User.objects.create_user(email='*****@*****.**', password='******', name='Lau Fok Wing' ) Person.objects.create(name='Lau Fok Wing', gender='M', family_id=family.id, user_id = user.id) self.client.login(email='*****@*****.**', password='******') self.client.post('/accounts/delete/',{'delete_profile': '0',}) self.assertEqual(1, Person.objects.filter(name='Lau Fok Wing').count()) self.assertEqual(None, Person.objects.get(name='Lau Fok Wing').user_id) self.assertEqual(0, User.objects.filter(email='*****@*****.**').count())
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 test_create_emails(self): ''' Tests that we create emails in batches for sections of families in their who share a first language ''' from family_tree.models import Family family = Family() family.save() from custom_user.models import User User.objects.all().delete() user = User.objects.create_user(email='*****@*****.**', password='******', name='Fabio Testi', language='pl', family_id=family.id) user.is_confirmed = True user.save() user2 = User.objects.create_user(email='*****@*****.**', password='******', name='Ron Nummi', language='pl', family_id=family.id) user2.is_confirmed = True user2.save() FamilyNewsLetterEvents.objects.all().delete() n1 = FamilyNewsLetterEvents(family_id=family.id, person_id=1, person_name='Fabio Testi', new_member=True) n1.save() n2 = FamilyNewsLetterEvents(family_id=family.id, person_id=1, person_name='Ron Nummii', new_member=False) n2.save() Email.objects.all().delete() Email.objects._create_emails() self.assertEqual(2, Email.objects.all().count())
def test_create_news_letter_events_first_time(self): ''' Tests that the correct news events are created ''' #Flush database FamilyNewsLetterEvents.objects.all().delete() from family_tree.models import Person, Family family = Family() family.save() Person.objects.all().delete() p1 = Person.objects.create(name='Connor Macleod', gender='M',family_id=family.id) p1.save() p2 = Person.objects.create(name='The Kurgan', gender='M',family_id=family.id) p2.save() FamilyNewsLetterEvents.objects.create_news_letter_events() self.assertEqual(2,FamilyNewsLetterEvents.objects.count())
def test_get_distinct_families_and_languages(self): ''' Tests that we get the languages and families involved in the newsletters ''' from family_tree.models import Family family = Family() family.save() from custom_user.models import User user = User.objects.create_user(email='*****@*****.**', password='******', name='Fabio Testi', language='en', family_id= family.id) user.save() user2 = User.objects.create_user(email='*****@*****.**', password='******', name='Ron Nummi', language='pl', family_id= family.id) user2.save() FamilyNewsLetterEvents.objects.all().delete() n1 = FamilyNewsLetterEvents(family_id=family.id, person_id=1, person_name='Mustapha Ibrahim', new_member=True ) n1.save() rows = Email.objects._get_distinct_families_and_languages() self.assertEqual(2,len(rows))
def test_create_single_language_emails(self): ''' Tests that we create emails in batches for sections of families in their who share a first language ''' from family_tree.models import Family family = Family() family.save() from custom_user.models import User User.objects.all().delete() user = User.objects.create_user(email='*****@*****.**', password='******', name='Fabio Testi', language='pl', family_id= family.id) user.is_confirmed = True user.save() user2 = User.objects.create_user(email='*****@*****.**', password='******', name='Ron Nummi', language='pl', family_id= family.id) user2.is_confirmed = True user2.save() Email.objects.all().delete() Email.objects._create_single_language_emails('pl', family.id, "subject", "content's","content's html") #use ' to check injection errors self.assertEqual(2,Email.objects.all().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 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()
class TestProfileViews(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='John Deacon', family_id=self.family.id) self.user.is_confirmed = True self.user.save() self.person = Person.objects.create(name='John Deacon', gender='M', user_id=self.user.id, email='*****@*****.**', family_id=self.family.id) self.person.save() self.user2 = User.objects.create_user( email='*****@*****.**', password='******', name='Freddie Mercury', family_id=self.family.id) self.user2.save() self.person2 = Person.objects.create(name='Freddie Mercury', gender='M', user_id=self.user2.id, locked=True, email='*****@*****.**', family_id=self.family.id) self.person2.save() self.another_family = Family() self.another_family.save() self.another_user = User.objects.create_user( email='*****@*****.**', password="******", name='Prince Vultan', family_id=self.another_family.id) self.another_user.save() self.confirmed_user = User.objects.create_user( email='*****@*****.**', password='******', name='General Kala', family_id=self.family.id) self.confirmed_user.is_confirmed = True self.confirmed_user.save() self.confirmed_person = Person.objects.create( name='General Kala', gender='F', user_id=self.confirmed_user.id, locked=False, email='*****@*****.**', family_id=self.family.id) self.confirmed_person.save() def test_genders_returns_response(self): ''' Tests that list of available genders returns ''' response = self.client.get('/genders/') self.assertEqual(200, response.status_code) self.assertEqual(True, 'Male' in str(response.content)) def test_home_profile_loads(self): ''' Tests that the users home screen loads and uses the correct template ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/profile={0}/'.format(self.person.id)) self.assertEqual(200, response.status_code) self.assertTemplateUsed(response, 'family_tree/profile.html') def test_home_profile_not_visible_for_other_family(self): ''' Test that people in different families cannot see profile ''' self.client.login(email='*****@*****.**', password="******") response = self.client.get('/profile={0}/'.format(self.person.id)) self.assertEqual(404, response.status_code) def test_edit_profile_loads(self): ''' Tests that the edit profile view loads and uses the correct template ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/edit_profile={0}/'.format(self.person.id)) self.assertEqual(200, response.status_code) self.assertTemplateUsed(response, 'family_tree/edit_profile.html') def test_edit_profile_not_visible_for_other_family(self): ''' Test that people in different families cannot see profile ''' self.client.login(email='*****@*****.**', password="******") response = self.client.get('/edit_profile={0}/'.format(self.person.id)) self.assertEqual(404, response.status_code) def test_update_person_denies_get_requests(self): ''' Tests that get requests are not allowed ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/update_person={0}/'.format( self.person2.id)) self.assertEqual(405, response.status_code) self.assertEqual(b"Only POST requests allowed", response.content) def test_update_person_denied_with_invalid_person_id(self): ''' Tests that an invalid response is sent when trying to change a person that does not exist ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post('/update_person=204/', { 'pk': 204, 'name': 'name', 'value': 'new name' }) self.assertEqual(404, response.status_code) def test_update_person_denied_with_locked_profile(self): ''' Tests that an invalid response is sent when trying to change a persons profile that is locked ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post( '/update_person={0}/'.format(self.person2.id), { 'pk': self.person2.id, 'name': 'name', 'value': 'new name' }) self.assertEqual(405, response.status_code) self.assertEqual(b"Access denied to locked profile", response.content) def test_cannot_update_family_id_through_api(self): ''' Test cannot update non-whitelisted properties through api ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post( '/update_person={0}/'.format(self.person2.id), { 'pk': self.person2.id, 'name': 'family_id', 'value': self.family.id }) self.assertEqual(405, response.status_code) self.assertEqual(b"Access denied to locked profile", response.content) def test_update_person_can_update_name(self): ''' Tests that a field can be updated through api ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post( '/update_person={0}/'.format(self.person.id), { 'pk': self.person.id, 'name': 'name', 'value': 'Brian Harold May' }) self.assertEqual(200, response.status_code) self.person = Person.objects.get(id=self.person.id) self.assertEqual("Brian Harold May", self.person.name) def test_update_email_saves_to_lowercase(self): ''' Tests that email field can be updated through api and is always saved as lower case ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post( '/update_person={0}/'.format(self.person.id), { 'pk': self.person.id, 'name': 'email', 'value': '*****@*****.**' }) self.assertEqual(200, response.status_code) self.person = Person.objects.get(id=self.person.id) self.assertEqual("*****@*****.**", self.person.email) def test_another_family_cannot_update_person_name(self): ''' Tests that a field can be updated through api ''' self.client.login(email='*****@*****.**', password="******") response = self.client.post( '/update_person={0}/'.format(self.person.id), { 'pk': self.person.id, 'name': 'name', 'value': 'Brian Harold May' }) self.assertEqual(404, response.status_code) def test_update_person_another_user_can_update_nonlocked_profile(self): ''' Tests that a person field can be updated through api by a user who is not that person ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post( '/update_person={0}/'.format(self.person.id), { 'pk': self.person.id, 'name': 'name', 'value': 'John Richard Deacon' }) self.assertEqual(200, response.status_code) self.person = Person.objects.get(id=self.person.id) self.assertEqual("John Richard Deacon", self.person.name) def test_update_person_can_update_boolean(self): ''' Tests that a boolean field can be updated through api ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post( '/update_person={0}/'.format(self.person.id), { 'pk': self.person.id, 'name': 'locked', 'value': '1' }) self.assertEqual(200, response.status_code) self.person = Person.objects.get(id=self.person.id) self.assertEqual(True, self.person.locked) response = self.client.post( '/update_person={0}/'.format(self.person.id), { 'pk': self.person.id, 'name': 'locked', 'value': '' }) self.assertEqual(200, response.status_code) self.person = Person.objects.get(id=self.person.id) self.assertEqual(False, self.person.locked) def test_update_person_cannot_update_email_with_confirmed_user(self): ''' Tests that an invalid response is sent when trying to change a persons profile that is attached to a confirmed user ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post( '/update_person={0}/'.format(self.confirmed_person.id), { 'pk': self.confirmed_person.id, 'name': 'email', 'value': '*****@*****.**' }) self.assertEqual(405, response.status_code) self.assertEqual(b"Access denied to change confirmed user settings", response.content) def test_update_biography_denied_with_invalid_person_id(self): ''' Tests that an invalid response is sent when trying to change a person that does not exist ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post('/update_biography=999/', {'biography': 'new content'}) self.assertEqual(404, response.status_code) def test_update_biography_denied_with_locked_profile(self): ''' Tests that an invalid response is sent when trying to change the biography of a locked profile ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post( '/update_biography={0}/'.format(self.person2.id), {'biography': 'new content'}) self.assertEqual(405, response.status_code) self.assertEqual(b"Access denied to locked profile", response.content) def test_update_biography_can_update_existing_biography(self): ''' Tests that biography content can be updated through api when a biography already exists ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post( '/update_biography={0}/'.format(self.person.id), {'biography': 'new content'}) self.assertEqual(200, response.status_code) self.person = Person.objects.get(id=self.person.id) self.assertEqual('new content', self.person.biography) def test_another_family_cannot_update_biography(self): ''' Tests that a field cannot be updated by another family through api ''' self.client.login(email='*****@*****.**', password="******") response = self.client.post( '/update_biography={0}/'.format(self.person.id), {'biography': 'new content'}) self.assertEqual(404, response.status_code) def test_update_biography_can_create_new_biography(self): ''' Tests that biography content can be updated through api when a biography already exists ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post( '/update_biography={0}/'.format(self.person2.id), {'biography': 'new content'}) self.assertEqual(200, response.status_code) self.person2 = Person.objects.get(id=self.person2.id) self.assertEqual('new content', self.person2.biography) def test_cannot_delete_profile_of_a_confirmed_user(self): ''' Make sure that you cannot delete the profile of someone who is a user ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/delete={0}/'.format(self.person.id)) self.assertEqual(405, response.status_code) #Check it exists person = Person.objects.get(id=self.person.id) self.assertEqual(self.person.id, person.id) def test_cannot_delete_profile(self): ''' Make sure that you cannot delete the profile of someone who is a user ''' person = Person.objects.create(name='David Tennant', gender='M', family_id=self.family.id) person.save() self.client.login(email='*****@*****.**', password='******') response = self.client.get('/delete={0}/'.format(person.id)) self.assertEqual(302, response.status_code) #Check it has been deleted self.assertEqual(0, Person.objects.filter(id=person.id).count())
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 TestImageUploadViews(TestCase): def setUp(self): ''' Set up a family, user and profile to test with ''' self.family = Family() self.family.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Fairy Fellar') self.user.save() self.person = Person.objects.create(name='Fairy Fellar', gender='M', user_id=self.user.id, email='*****@*****.**', family_id=self.family.id) self.person.save() self.another_family = Family() self.another_family.save() self.another_user = User.objects.create_user(email='*****@*****.**', password="******", name='Dale Arden', family_id=self.another_family.id) self.another_user.save() def test_upload_image_view_loads(self): ''' tests that the view loads if someone navigates to it ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/edit_profile_photo={0}/'.format(self.person.id)) self.assertEqual(200, response.status_code) self.assertTemplateUsed(response, 'family_tree/image_upload.html') def test_image_upload_view_does_not_load_for_another_family(self): ''' tests that the view does not load if a person from a different family is trying to access it ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/edit_profile_photo={0}/'.format(self.person.id)) self.assertEqual(404, response.status_code) def test_image_upload_receives_file(self): ''' test that we can upload a file ''' self.client.login(email='*****@*****.**', password='******') with open(os.path.join(BASE_DIR, 'tests/test_image_upload.png'), 'rb') as fp: response = self.client.post('/image_upload={0}/'.format(self.person.id),{'picture': fp}) self.assertEqual(200, response.status_code) #Check file has been uploaded and remove it data = json.loads(response.content.decode('utf-8')) filename = settings.MEDIA_ROOT + 'profile_photos/' + data['filename'] os.remove(filename) #Reload object self.person = Person.objects.get(pk=self.person.id) self.assertEqual('profile_photos/' + data['filename'],self.person.photo) def test_image_upload_cannot_receive_file_from_another_family(self): ''' test that we can can't upload file to another family ''' self.client.login(email='*****@*****.**', password='******') with open(os.path.join(BASE_DIR, 'tests/test_image_upload.png'), 'rb') as fp: response = self.client.post('/image_upload={0}/'.format(self.person.id),{'picture': fp}) self.assertEqual(404, response.status_code) def test_image_upload_does_not_process_image_file(self): ''' test that an invalid file is not processed ''' num_files = len([item for item in os.listdir( settings.MEDIA_ROOT + 'profile_photos/')]) self.client.login(email='*****@*****.**', password='******') with open(os.path.join(BASE_DIR, 'tests/test_not_a_real_picture.png'), 'rb') as fp: response = self.client.post('/image_upload={0}/'.format(self.person.id),{'picture': fp}) self.assertEqual(200, response.status_code) data = json.loads(response.content.decode('utf-8')) self.assertEqual('Invalid image!', data['error']) #Check no new files self.assertEqual(num_files, len([item for item in os.listdir( settings.MEDIA_ROOT + 'profile_photos/')])) def test_image_resize_view_loads(self): ''' test that we can upload a file ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/image_resize={0}/'.format(self.person.id)) self.assertEqual(200, response.status_code) self.assertTemplateUsed(response, 'family_tree/image_resize.html') def test_image_resize_view_does_not_load_for_another_family(self): ''' tests that the view does not load if a person from a different family is trying to access it ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/image_resize={0}/'.format(self.person.id)) self.assertEqual(404, response.status_code) def test_image_crop_can_be_posted_to(self): ''' Tests that the image_crop view can posted to ''' #Copy test image to media area shutil.copy2(os.path.join(settings.BASE_DIR, 'family_tree/tests/large_test_image.jpg'), settings.MEDIA_ROOT + 'profile_photos/large_test_image.jpg') self.person.photo = 'profile_photos/large_test_image.jpg' self.person.save() self.client.login(email='*****@*****.**', password='******') response = self.client.post('/image_crop={0}/'.format(self.person.id),{'x': 100, 'y': 200, 'w': 300, 'h': 300, 'display_height' : 550}) #Clear up mess afterwards os.remove(settings.MEDIA_ROOT + 'profile_photos/large_test_image.jpg') self.assertEqual(302, response.status_code) def test_image_crop_cannot_be_posted_to_for_another_family(self): ''' tests that the view does not load if a person from a different family is trying to access it ''' #Copy test image to media area shutil.copy2(os.path.join(settings.BASE_DIR, 'family_tree/tests/large_test_image.jpg'), settings.MEDIA_ROOT + 'profile_photos/large_test_image.jpg') self.person.photo = 'profile_photos/large_test_image.jpg' self.person.save() self.client.login(email='*****@*****.**', password='******') response = self.client.post('/image_crop={0}/'.format(self.person.id),{'x': 100, 'y': 200, 'w': 300, 'h': 300, 'display_height' : 550}) #Clear up mess afterwards os.remove(settings.MEDIA_ROOT + 'profile_photos/large_test_image.jpg') self.assertEqual(404, response.status_code)
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 ImageTestCase(TestCase): # pragma: no cover ''' Tests for the image class ''' 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, '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' ]) 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) def test_create_thumbnail(self): ''' Tests that we can create a thumbnail ''' #Copy test image to media area shutil.copy2(self.test_image, self.test_image_destination) image = Image(gallery=self.gallery, family=self.family, original_image=''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ])) thumbnail, image = image._create_thumbnail((500, 500)) PIL.Image.open(settings.MEDIA_ROOT + thumbnail) #Clear up mess afterwards os.remove(self.test_image_destination) os.remove(settings.MEDIA_ROOT + thumbnail) def test_make_thumbnails_and_delete(self): ''' Tests the make thumbnails routine ''' #Copy test image to media area shutil.copy2(self.test_image, self.test_image_destination) image = Image(gallery=self.gallery, family=self.family, original_image=self.test_image_destination) image.make_thumbnails() PIL.Image.open(settings.MEDIA_ROOT + str(image.thumbnail)) PIL.Image.open(settings.MEDIA_ROOT + str(image.large_thumbnail)) PIL.Image.open(settings.MEDIA_ROOT + str(self.gallery.thumbnail)) #Clear up mess afterwards image.delete_local_image_files() def test_get_exif_data(self): ''' Tests we can extract gps data from an image ''' exif_test_image = os.path.join(settings.BASE_DIR, 'gallery/tests/exif_test.jpg') exif_test_image_destination = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/exif_test.jpg' ]) shutil.copy2(exif_test_image, exif_test_image_destination) image = Image(gallery=self.gallery, family=self.family, original_image=exif_test_image_destination) image._populate_exif_data() self.assertEqual( datetime(2014, 3, 30, 13, 18, 6).replace(tzinfo=utc), image.date_taken) self.assertEqual(True, image.latitude != 0) self.assertEqual(True, image.longitude != 0) #Clear up mess afterwards os.remove(exif_test_image_destination) def test_get_exif_data2(self): ''' Tests we can extract gps data from an image ''' exif_test_image = os.path.join(settings.BASE_DIR, 'gallery/tests/exif_test_2.jpg') exif_test_image_destination = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/exif_test.jpg' ]) shutil.copy2(exif_test_image, exif_test_image_destination) image = Image(gallery=self.gallery, family=self.family, original_image=exif_test_image_destination) image._populate_exif_data() self.assertEqual( datetime(2015, 6, 21, 13, 50, 35).replace(tzinfo=utc), image.date_taken) self.assertEqual(True, image.latitude != 0) self.assertEqual(True, image.longitude != 0) #Clear up mess afterwards os.remove(exif_test_image_destination) def test_save_and_rotate_image(self): ''' Tests that we can save an image and rotate it without error ''' #Copy test image to media area shutil.copy2(self.test_image, self.test_image_destination) image = Image(gallery=self.gallery, family=self.family, original_image=''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ])) image.save() image.upload_files_to_s3() image.rotate(90) #Clear up image.delete_local_image_files() image.delete_remote_image_files()
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 TestImageViews(TestCase): # pragma: no cover ''' Test class for the gallery views ''' 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='White Queen', family_id=self.family.id) self.person = Person.objects.create(name='White Queen', family=self.family) self.person2 = Person.objects.create(name='Black Queen', family=self.family) self.gallery = Gallery.objects.create(family_id=self.family.id, title="gallery") 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=self.test_image_destination, thumbnail=self.test_image_destination, large_thumbnail=self.test_image_destination) self.image.save() self.another_family = Family.objects.create() self.another_user = User.objects.create_user( email='*****@*****.**', password='******', name='Queen Of Hearts', family_id=self.another_family.id) def test_get_tags_on_an_image(self): ''' Tests that the images load for a gallery ''' Tag.objects.create(image=self.image, person=self.person, x1=1, x2=2, y1=3, y2=4) Tag.objects.create(image=self.image, person=self.person2, x1=5, x2=6, y1=7, y2=8) self.client.login(email='*****@*****.**', password='******') response = self.client.get('/image={0}/tags/get/'.format( self.image.id)) self.assertEqual(200, response.status_code) #Check that the response is valid json serializers.json.Deserializer(response.content) self.assertEqual(True, b'3' in response.content) self.assertEqual(True, b'8' in response.content) self.assertEqual(True, b'White Queen' in response.content) self.assertEqual(True, b'Black Queen' in response.content) def test_get_tags_different_family(self): ''' Checks that another family cannot get the tags for a family ''' Tag.objects.create(image=self.image, person=self.person2, x1=5, x2=6, y1=7, y2=8) self.client.login(email='*****@*****.**', password='******') response = self.client.get('/image={0}/tags/get/'.format( self.image.id)) self.assertEqual(404, response.status_code) def test_delete_tag(self): ''' Tests tag deletion api ''' image = Image(gallery=self.gallery, family=self.family, original_image=self.test_image_destination, thumbnail=self.test_image_destination, large_thumbnail=self.test_image_destination) image.save() tag = Tag.objects.create(image=image, person=self.person, x1=1, x2=2, y1=3, y2=4) self.client.login(email='*****@*****.**', password='******') response = self.client.post('/tag={0}/delete/'.format(tag.id)) self.assertEqual(200, response.status_code) self.assertEqual(0, Tag.objects.filter(id=tag.id).count()) def test_delete_tag_fails_for_different_family(self): ''' Tests tag deletion api ''' image = Image(gallery=self.gallery, family=self.family, original_image=self.test_image_destination, thumbnail=self.test_image_destination, large_thumbnail=self.test_image_destination) image.save() tag = Tag.objects.create(image=image, person=self.person, x1=1, x2=2, y1=3, y2=4) self.client.login(email='*****@*****.**', password='******') response = self.client.post('/tag={0}/delete/'.format(tag.id)) self.assertEqual(404, response.status_code) def test_create_tag(self): ''' Tests create tag api ''' image = Image(gallery=self.gallery, family=self.family, original_image=self.test_image_destination, thumbnail=self.test_image_destination, large_thumbnail=self.test_image_destination) image.save() self.client.login(email='*****@*****.**', password='******') response = self.client.post( '/image={0}/tags/create/'.format(self.image.id), { 'person': self.person.id, 'x1': 0.314159, 'y1': 0.1, 'x2': 0.6, 'y2': 0.2, }) self.assertEqual(200, response.status_code) tag = Tag.objects.get(x1=0.314159) self.assertEqual(self.person.id, tag.person_id) self.assertEqual(0.2, tag.y2) def test_create_tag_fails_for_another_family(self): ''' Tests create tag api fails if in wrong family ''' image = Image(gallery=self.gallery, family=self.family, original_image=self.test_image_destination, thumbnail=self.test_image_destination, large_thumbnail=self.test_image_destination) image.save() self.client.login(email='*****@*****.**', password='******') response = self.client.post( '/image={0}/tags/create/'.format(self.image.id), { 'person': self.person.id, 'x1': 0.314159, 'y1': 0.1, 'x2': 0.6, 'y2': 0.2, }) self.assertEqual(404, response.status_code)
class TestTagViews(TestCase): # pragma: no cover ''' Test class for the gallery views ''' 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='White Queen', family_id=self.family.id) self.person = Person.objects.create(name='White Queen', family=self.family) self.person2 = Person.objects.create(name='Black Queen', family=self.family) self.person3 = Person.objects.create(name='As It Began', family=self.family) self.gallery = Gallery.objects.create(family_id=self.family.id, title="gallery") 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=self.test_image_destination, thumbnail=self.test_image_destination, large_thumbnail=self.test_image_destination) self.image.save() self.tag1 = Tag.objects.create(image=self.image, person=self.person, x1=1, x2=2, y1=3, y2=4) self.tag2 = Tag.objects.create(image=self.image, person=self.person2, x1=5, x2=6, y1=7, y2=8) self.another_family = Family.objects.create() self.another_user = User.objects.create_user( email='*****@*****.**', password='******', name='Queen Of Hearts', family_id=self.another_family.id) self.other_family_person = Person.objects.create( name='Queen Adreena', family=self.another_family) self.other_family_gallery = Gallery.objects.create( family_id=self.another_family.id, title="gallery") self.other_family_image = Image( gallery=self.other_family_gallery, family=self.another_family, original_image=self.test_image_destination, thumbnail=self.test_image_destination, large_thumbnail=self.test_image_destination) self.other_family_image.save() 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_list(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/image_tagging/?image_id={0}'.format(self.image.id) response = client.get(url, format='json') # Check it contains both tags self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(b'"person_id":1' in response.content) self.assertTrue(b'"person_id":2' in response.content) json.loads(response.content) def test_list_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/image_tagging/?image_id={0}'.format(self.image.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_list_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.another_user) url = '/api/image_tagging/?image_id={0}'.format(self.image.id) response = client.get(url, format='json') self.assertFalse(b'"person_id":1' in response.content) self.assertFalse(b'"person_id":2' in response.content) json.loads(response.content) def test_list_filter_by_person(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/image_tagging/?image_id={0}&person_id={1}'.format( self.image.id, self.person.id) response = client.get(url, format='json') # Check it contains both tags self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(b'"person_id":1' in response.content) self.assertFalse(b'"person_id":2' in response.content) json.loads(response.content) def test_destroy(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/image_tagging/{0}/'.format(self.tag1.id) response = client.delete(url, format='json') # Check its deleted self.assertEqual(response.status_code, status.HTTP_200_OK) tags = Tag.objects.filter(id=self.tag1.id) self.assertEqual(0, tags.count()) json.loads(response.content) def test_destroy_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/image_tagging/{0}/'.format(self.tag1) response = client.delete(url, 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.another_user) url = '/api/image_tagging/{0}/'.format(self.tag1.id) response = client.delete(url, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) json.loads(response.content) def test_create(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) data = { 'person_id': self.person3.id, 'image_id': self.image.id, 'x1': 0.1, 'x2': 0.2, 'y1': 0.3, 'y2': 0.4, } url = '/api/image_tagging/' response = client.post(url, data, format='json') new_tag = Tag.objects.get(person_id=self.person3.id) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(0.1, new_tag.x1) json.loads(response.content) def test_create_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') data = { 'person_id': self.person3.id, 'image_id': self.image.id, 'x1': 0.1, 'x2': 0.2, 'y1': 0.3, 'y2': 0.4, } url = '/api/image_tagging/' 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.user) data = { 'person_id': self.other_family_person.id, 'image_id': self.image.id, 'x1': 0.1, 'x2': 0.2, 'y1': 0.3, 'y2': 0.4, } url = '/api/image_tagging/' response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) json.loads(response.content) def test_create_image_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) data = { 'person_id': self.person3.id, 'image_id': self.other_family_image.id, 'x1': 0.1, 'x2': 0.2, 'y1': 0.3, 'y2': 0.4, } url = '/api/image_tagging/' response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) json.loads(response.content) def test_create_invalid_x1(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) data = { 'person_id': self.person3.id, 'image_id': self.image.id, 'x1': 'invalid!', 'x2': 0.2, 'y1': 0.3, 'y2': 0.4, } url = '/api/image_tagging/' response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content)
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())
class UserApiTestCase(TestCase): ''' Tests for the user API ''' def setUp(self): self.family = Family() self.family.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='user', family=self.family) self.user2 = User.objects.create_user(email='*****@*****.**', password='******', name='user2', family=self.family) self.family2 = Family() self.family2.save() self.user3 = User.objects.create_user(email='*****@*****.**', password='******', name='user3', family=self.family2) super(UserApiTestCase, self).setUp() def test_list_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/users/' response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_list(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/users/' response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(b'*****@*****.**' in response.content) self.assertTrue(b'*****@*****.**' in response.content) self.assertFalse(b'*****@*****.**' in response.content) def test_get_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/user_settings/' response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_retrieve(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/user_settings/' response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(b'receive_update_emails' in response.content) def test_update(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) data = { 'language': 'fi', 'receive_update_emails': True, 'receive_photo_update_emails': True, } url = '/api/user_settings/' response = client.patch(url, data, format='json') self.user = User.objects.get(id=self.user.id) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(b'fi' in response.content) self.assertTrue(self.user.receive_update_emails) def test_cannot_update_other_user_values(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) data = { 'language': 'fi', 'receive_update_emails': True, 'receive_photo_update_emails': True, 'is_superuser': True, } url = '/api/user_settings/' response = client.patch(url, data, format='json') self.user = User.objects.get(id=self.user.id) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertFalse(self.user.is_superuser) def test_change_password_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/password_change/' data = {'old_password': '******', 'new_password': '******'} response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_change_password_success(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/password_change/' data = {'old_password': '******', 'new_password': '******'} response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) user = User.objects.get(id=self.user.id) result = user.check_password('user12345') self.assertTrue(result) def test_change_password_missing_parameters(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/password_change/' data = {} response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) def test_change_password_invalid_old_password(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/password_change/' data = {'old_password': '******', 'new_password': '******'} response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) def test_change_password_too_short(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/password_change/' data = {'old_password': '******', 'new_password': '******'} response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) def test_delete_account_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/delete_account/' response = client.post(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_delete_account_no_password(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/delete_account/' data = { 'password': '', } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) def test_delete_account_last_user(self): person3 = Person.objects.create(name='User3', gender='M', email='*****@*****.**', family_id=self.family2.id, language='en', user_id=self.user3.id) client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user3) url = '/api/delete_account/' data = { 'password': '******', } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(0, Person.objects.filter(id=person3.id).count()) self.assertEqual(0, User.objects.filter(id=self.user3.id).count()) def test_delete_account_multi_user_leave_profile(self): person = Person.objects.create(name='User', gender='M', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/delete_account/' data = {'password': '******', 'delete_profile': False} response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(1, Person.objects.filter(id=person.id).count()) self.assertEqual(0, User.objects.filter(id=self.user.id).count()) def test_delete_account_multi_user_delete_profile(self): person = Person.objects.create(name='User', gender='M', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/delete_account/' data = {'password': '******', 'delete_profile': True} response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(0, Person.objects.filter(id=person.id).count()) self.assertEqual(0, User.objects.filter(id=self.user.id).count())
class TestGalleryMapViews(TestCase): # pragma: no cover ''' Test class for the gallery views ''' 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='gromit', family_id=self.family.id) self.gallery = Gallery.objects.create(family_id=self.family.id, title="gallery") 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.images = [] for i in range(0, 3): self.images.append( Image(gallery=self.gallery, family=self.family, title="title{0}".format(i), original_image=self.test_image_destination, thumbnail=self.test_image_destination, large_thumbnail=self.test_image_destination, latitude=i, longitude=i)) self.another_family = Family.objects.create() self.another_user = User.objects.create_user( email='*****@*****.**', password='******', name='shaun', family_id=self.another_family.id) def test_gallery_map_loads(self): ''' Tests the gallery map view loads and uses the correct template ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/gallery={0}/map/'.format(self.gallery.id)) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'gallery/gallery_map.html') def test_gallery_map_does_not_load_for_another_family(self): ''' Tests gallery does not load for another family ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/gallery={0}/map/'.format(self.gallery.id)) self.assertEqual(response.status_code, 404) def test_gallery_map_data_does_not_load_another_family(self): ''' Tests gallery data does not load for another family ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/gallery={0}/map_data/1.0/'.format( self.gallery.id)) self.assertEqual(response.status_code, 404) def test_gallery_map_data_loads(self): ''' Tests gallery data loads ''' for i in self.images: i.save() self.client.login(email='*****@*****.**', password='******') response = self.client.get('/gallery={0}/map_data/1.0/'.format( self.gallery.id)) self.assertEqual(response.status_code, 200) self.assertEqual(True, b'title' in response.content)
class TestImageUploadViews(TestCase): # pragma: no cover def setUp(self): ''' Set up a family, user and profile to test with ''' self.family = Family() self.family.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Fairy Fellar') self.user.save() self.person = Person.objects.create(name='Fairy Fellar', gender='M', user_id=self.user.id, email='*****@*****.**', family_id=self.family.id) self.person.save() self.another_family = Family() self.another_family.save() self.another_user = User.objects.create_user( email='*****@*****.**', password="******", name='Dale Arden', family_id=self.another_family.id) self.another_user.save() def test_upload_image_view_loads(self): ''' tests that the view loads if someone navigates to it ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/edit_profile_photo={0}/'.format( self.person.id)) self.assertEqual(200, response.status_code) self.assertTemplateUsed(response, 'family_tree/image_upload.html') def test_image_upload_view_does_not_load_for_another_family(self): ''' tests that the view does not load if a person from a different family is trying to access it ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/edit_profile_photo={0}/'.format( self.person.id)) self.assertEqual(404, response.status_code) def test_image_upload_receives_file(self): ''' test that we can upload a file ''' self.client.login(email='*****@*****.**', password='******') with open(os.path.join(BASE_DIR, 'tests/test_image_upload.png'), 'rb') as fp: response = self.client.post( '/image_upload={0}/'.format(self.person.id), {'picture': fp}) self.assertEqual(200, response.status_code) #Check file has been uploaded and remove it data = json.loads(response.content.decode('utf-8')) filename = settings.MEDIA_ROOT + 'profile_photos/' + data['filename'] os.remove(filename) #Reload object self.person = Person.objects.get(pk=self.person.id) self.assertEqual('profile_photos/' + data['filename'], self.person.photo) def test_image_upload_cannot_receive_file_from_another_family(self): ''' test that we can can't upload file to another family ''' self.client.login(email='*****@*****.**', password='******') with open(os.path.join(BASE_DIR, 'tests/test_image_upload.png'), 'rb') as fp: response = self.client.post( '/image_upload={0}/'.format(self.person.id), {'picture': fp}) self.assertEqual(404, response.status_code) def test_image_upload_does_not_process_image_file(self): ''' test that an invalid file is not processed ''' num_files = len([ item for item in os.listdir(settings.MEDIA_ROOT + 'profile_photos/') ]) self.client.login(email='*****@*****.**', password='******') with open(os.path.join(BASE_DIR, 'tests/test_not_a_real_picture.png'), 'rb') as fp: response = self.client.post( '/image_upload={0}/'.format(self.person.id), {'picture': fp}) self.assertEqual(200, response.status_code) data = json.loads(response.content.decode('utf-8')) self.assertEqual('Invalid image!', data['error']) #Check no new files self.assertEqual( num_files, len([ item for item in os.listdir(settings.MEDIA_ROOT + 'profile_photos/') ])) def test_image_resize_view_loads(self): ''' test that we can upload a file ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/image_resize={0}/'.format(self.person.id)) self.assertEqual(200, response.status_code) self.assertTemplateUsed(response, 'family_tree/image_resize.html') def test_image_resize_view_does_not_load_for_another_family(self): ''' tests that the view does not load if a person from a different family is trying to access it ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/image_resize={0}/'.format(self.person.id)) self.assertEqual(404, response.status_code) def test_image_crop_can_be_posted_to(self): ''' Tests that the image_crop view can posted to ''' #Copy test image to media area shutil.copy2( os.path.join(settings.BASE_DIR, 'family_tree/tests/large_test_image.jpg'), settings.MEDIA_ROOT + 'profile_photos/large_test_image.jpg') self.person.photo = 'profile_photos/large_test_image.jpg' self.person.save() self.client.login(email='*****@*****.**', password='******') response = self.client.post('/image_crop={0}/'.format(self.person.id), { 'x': 100, 'y': 200, 'w': 300, 'h': 300 }) self.assertEqual(302, response.status_code) def test_image_crop_cannot_be_posted_to_for_another_family(self): ''' tests that the view does not load if a person from a different family is trying to access it ''' #Copy test image to media area shutil.copy2( os.path.join(settings.BASE_DIR, 'family_tree/tests/large_test_image.jpg'), settings.MEDIA_ROOT + 'profile_photos/large_test_image.jpg') self.person.photo = 'profile_photos/large_test_image.jpg' self.person.save() self.client.login(email='*****@*****.**', password='******') response = self.client.post('/image_crop={0}/'.format(self.person.id), { 'x': 100, 'y': 200, 'w': 300, 'h': 300 }) #Clear up mess afterwards os.remove(settings.MEDIA_ROOT + 'profile_photos/large_test_image.jpg') self.assertEqual(404, response.status_code) def test_image_rotate_can_be_posted_to(self): ''' Tests that the image_crop view can posted to ''' #Copy test image to media area shutil.copy2( os.path.join(settings.BASE_DIR, 'family_tree/tests/large_test_image.jpg'), settings.MEDIA_ROOT + 'profile_photos/large_test_image.jpg') self.person.photo = 'profile_photos/large_test_image.jpg' self.person.save() self.client.login(email='*****@*****.**', password='******') response = self.client.post( '/image_rotate={0}/'.format(self.person.id), {'anticlockwise_angle': 90}) #Clear up mess afterwards self.person = Person.objects.get(id=self.person.id) os.remove(settings.MEDIA_ROOT + str(self.person.photo)) self.assertEqual(302, response.status_code) def test_image_rotate_cannot_be_posted_to_for_another_family(self): ''' Tests that the image_crop view can posted to ''' #Copy test image to media area shutil.copy2( os.path.join(settings.BASE_DIR, 'family_tree/tests/large_test_image.jpg'), settings.MEDIA_ROOT + 'profile_photos/large_test_image.jpg') self.person.photo = 'profile_photos/large_test_image.jpg' self.person.save() self.client.login(email='*****@*****.**', password='******') response = self.client.post( '/image_rotate={0}/'.format(self.person.id), {'anticlockwise_angle': 90}) #Clear up mess afterwards self.person = Person.objects.get(id=self.person.id) os.remove(settings.MEDIA_ROOT + str(self.person.photo)) self.assertEqual(404, response.status_code)
class EmailConfirmationTestCase(TestCase): # pragma: no cover ''' Tests for this app ''' def setUp(self): self.family = Family() self.family.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Zandra Rhodes', family_id=self.family.id) self.person = Person.objects.create(name='Tim Staffell', gender='M', family_id=self.family.id, language='en') def test_generate_confirmation_key(self): ''' Tests generate_confirmation_key function creates a confirmation key without errors ''' ec = EmailConfirmation(email_address='*****@*****.**', person_id=self.person.id, user_who_invited_person_id=self.user.id) ec.generate_confirmation_key() self.assertNotEqual(None, ec.confirmation_key) self.assertEqual(True, len(ec.confirmation_key) > 10) def test_create_email_body_html(self): ''' Tests that this generates a valid email body ''' ec = EmailConfirmation(email_address='*****@*****.**', person=self.person, user_who_invited_person_id=self.user.id, confirmation_key='earth') email_body = ec._create_email_body_html() self.assertEqual(True, 'invited by Zandra Rhodes' in email_body) self.assertEqual( True, '<a href="' + settings.DOMAIN + '/accounts/confirmation=earth/">' + settings.DOMAIN + '/accounts/confirmation=earth/</a>' in email_body) self.assertEqual(True, 'Hello Tim Staffell' in email_body) def test_send_email(self): ''' Tests that an email is created and sent ''' ec = EmailConfirmation( email_address='*****@*****.**', person=self.person, user_who_invited_person_id=self.user.id, confirmation_key= 'da5ee09f3823893df60a5ccfdb6bfda8c64c880657d9560a6739cd1dbf67c3de') before = timezone.now() ec.send_email() after = timezone.now() self.assertEqual(True, ec.sent >= before) self.assertEqual(True, ec.sent <= after) def test_remove_expired_email_confirmations(self): ''' Tests that email confirmations that are over a month old are removed ''' before_person = Person.objects.create(name='before', gender='M', family_id=self.family.id, language='en') email_to_be_deleted = EmailConfirmation.objects.create( email_address='*****@*****.**', person=before_person, user_who_invited_person_id=self.user.id, confirmation_key='12345', sent=timezone.now() - timedelta(days=34)) after_person = Person.objects.create(name='after', gender='M', family_id=self.family.id, language='en') email_to_be_kept = EmailConfirmation.objects.create( email_address='*****@*****.**', person=after_person, user_who_invited_person_id=self.user.id, confirmation_key='12345', sent=timezone.now() - timedelta(days=4)) EmailConfirmation.objects.remove_expired_email_confirmations() self.assertEqual( 0, EmailConfirmation.objects.filter( id=email_to_be_deleted.id).count()) self.assertEqual( 1, EmailConfirmation.objects.filter(id=email_to_be_kept.id).count()) def test_confirm_invite_view_loads(self): ''' Tests that the view for confirming an invite is loaded for a valid confirmation key ''' person = Person.objects.create(name='Great King Rat', gender='M', family_id=self.family.id, language='en') invite = EmailConfirmation.objects.create( email_address='*****@*****.**', person=person, user_who_invited_person_id=self.user.id, sent=timezone.now()) response = self.client.get('/accounts/confirmation={0}/'.format( invite.confirmation_key), HTTP_X_REAL_IP='127.0.0.1') self.assertEqual(200, response.status_code) self.assertTemplateUsed(response, 'email_confirmation/confirm_invite.html') self.assertEqual(True, b'Great King Rat' in response.content) self.assertEqual(True, b'Zandra Rhodes' in response.content) def test_expired_invite_view_loads(self): ''' Tests that the view for an expired invite loads ''' response = self.client.get('/accounts/invalid_expired/') self.assertEqual(200, response.status_code) self.assertTemplateUsed(response, 'email_confirmation/invalid_expired.html') def test_confirm_invite_view_does_not_load_with_invalid_confirmation_code( self): ''' Tests that the view for confirming an invite shows invalid/expired page ''' response = self.client.get( '/accounts/confirmation=not_a_real_confirmation_code/', HTTP_X_REAL_IP='127.0.0.1') self.assertEqual(200, response.status_code) self.assertTemplateUsed(response, 'email_confirmation/invalid_expired.html') def test_confirm_invite_view_post_does_all_the_correct_stuff(self): ''' Tests that confirming invite 1. Loads 2. Creates a user correctly 3. Assigns the user to a person 4. Deletes the invite 5. Logs in and displays home page ''' person = Person.objects.create(name='Mike Stone', gender='M', family_id=self.family.id, language='en', email='*****@*****.**') invite = EmailConfirmation.objects.create( email_address='*****@*****.**', person=person, user_who_invited_person_id=self.user.id, sent=timezone.now()) response = self.client.post('/accounts/confirmation={0}/'.format( invite.confirmation_key), {'password': '******'}, HTTP_X_REAL_IP='127.0.0.1') self.assertEqual(False, response.status_code == 404) new_user = User.objects.get(email='*****@*****.**') self.assertEqual('Mike Stone', new_user.name) self.assertEqual(self.family.id, new_user.family_id) self.assertEqual('en', new_user.language) person = Person.objects.get(id=person.id) self.assertEqual(new_user.id, person.user_id) self.assertEqual( 0, EmailConfirmation.objects.filter( email_address='*****@*****.**').count()) #Diverted to home page self.assertRedirects(response, '/', status_code=302, target_status_code=302, msg_prefix='') #check confirmation address no longer valid response = self.client.get('/accounts/confirmation={0}/'.format( invite.confirmation_key), HTTP_X_REAL_IP='127.0.0.1') self.assertEqual(200, response.status_code) self.assertTemplateUsed(response, 'email_confirmation/invalid_expired.html') response = self.client.post('/accounts/confirmation={0}/'.format( invite.confirmation_key), {'password': '******'}, HTTP_X_REAL_IP='127.0.0.1') self.assertEqual(200, response.status_code) self.assertTemplateUsed(response, 'email_confirmation/invalid_expired.html') def test_confirm_invite_view_post_404_with_incorrect_confirmation_key( self): ''' Check we can't be too easily hacked ''' response = self.client.post( '/accounts/confirmation=not_a_real_confirmation_code/', {'password': '******'}, HTTP_X_REAL_IP='127.0.0.1') self.assertEqual(200, response.status_code) self.assertTemplateUsed(response, 'email_confirmation/invalid_expired.html') def test_invite_person_creates_fails_for_existing_user(self): ''' test for invite person post request ''' user = User.objects.create_user(email='*****@*****.**', password='******', name='Existing User') person = Person.objects.create(name='existing_user', gender='M', family_id=self.family.id, language='en', email='*****@*****.**', user_id=user.id) self.client.login(email='*****@*****.**', password='******') response = self.client.post('/accounts/invite_person={0}/'.format( person.id)) self.assertEqual(404, response.status_code) def test_invite_person_fails_if_pending_invite(self): ''' test for invite person post request ''' person = Person.objects.create(name='Norman Sheffield', gender='M', family_id=self.family.id, language='en', email='*****@*****.**') EmailConfirmation.objects.create( email_address='*****@*****.**', person=person, user_who_invited_person_id=self.user.id, sent=timezone.now()) self.client.login(email='*****@*****.**', password='******') response = self.client.post('/accounts/invite_person={0}/'.format( person.id)) self.assertEqual(404, response.status_code) def test_invite_person_successfully_creates_invite(self): ''' test for invite person post request ''' person = Person.objects.create(name='Jim Beach', gender='M', family_id=self.family.id, language='en', email='*****@*****.**') self.client.login(email='*****@*****.**', password='******') response = self.client.post('/accounts/invite_person={0}/'.format( person.id)) self.assertRedirects(response, '/profile={0}/'.format(person.id), status_code=302, target_status_code=200, msg_prefix='') invite = EmailConfirmation.objects.get(person_id=person.id) self.assertEqual('*****@*****.**', invite.email_address) self.assertEqual(self.user.id, invite.user_who_invited_person.id)
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')
class EmailConfirmationTestCase(TestCase): # pragma: no cover ''' Tests for this app ''' def setUp(self): self.family = Family() self.family.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Zandra Rhodes', family_id=self.family.id) self.person = Person.objects.create(name='Tim Staffell', gender='M', family_id=self.family.id, language='en') self.client = HttpClient(HTTP_X_REAL_IP='127.0.0.1') def test_generate_confirmation_key(self): ''' Tests generate_confirmation_key function creates a confirmation key without errors ''' ec = EmailConfirmation(email_address='*****@*****.**', person_id = self.person.id, user_who_invited_person_id=self.user.id) ec.generate_confirmation_key() self.assertNotEqual(None, ec.confirmation_key) self.assertEqual(True, len(ec.confirmation_key) > 10) def test_create_email_body_html(self): ''' Tests that this generates a valid email body ''' ec = EmailConfirmation(email_address='*****@*****.**', person = self.person , user_who_invited_person_id=self.user.id, confirmation_key='earth') email_body = ec._create_email_body_html() link = '<a href="' + settings.DOMAIN + '/#/accounts/invite_confirmation/earth/">' + settings.DOMAIN + '/#/accounts/invite_confirmation/earth/</a>' self.assertEqual(True, 'invited by Zandra Rhodes' in email_body) self.assertEqual(True, link in email_body) self.assertEqual(True, 'Hello Tim Staffell' in email_body) def test_send_email(self): ''' Tests that an email is created and sent ''' ec = EmailConfirmation(email_address='*****@*****.**', person = self.person , user_who_invited_person_id=self.user.id, confirmation_key='da5ee09f3823893df60a5ccfdb6bfda8c64c880657d9560a6739cd1dbf67c3de') before = timezone.now() ec.send_email() after = timezone.now() self.assertEqual(True, ec.sent >= before) self.assertEqual(True, ec.sent <= after) def test_remove_expired_email_confirmations(self): ''' Tests that email confirmations that are over a month old are removed ''' before_person = Person.objects.create(name='before', gender='M', family_id=self.family.id, language='en') email_to_be_deleted = EmailConfirmation.objects.create(email_address='*****@*****.**' , person = before_person , user_who_invited_person_id=self.user.id , confirmation_key='12345' , sent=timezone.now() - timedelta(days=34)) after_person = Person.objects.create(name='after', gender='M', family_id=self.family.id, language='en') email_to_be_kept = EmailConfirmation.objects.create(email_address='*****@*****.**' , person = after_person , user_who_invited_person_id=self.user.id , confirmation_key='12345' , sent=timezone.now() - timedelta(days=4)) EmailConfirmation.objects.remove_expired_email_confirmations() self.assertEqual(0, EmailConfirmation.objects.filter(id=email_to_be_deleted.id).count()) self.assertEqual(1, EmailConfirmation.objects.filter(id=email_to_be_kept.id).count())
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))
class TestProfileViews(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='John Deacon', family_id=self.family.id) self.user.is_confirmed = True self.user.save() self.person = Person.objects.create(name='John Deacon', gender='M', user_id=self.user.id, email='*****@*****.**', family_id=self.family.id) self.person.save() self.biography = Biography(person_id=self.person.id, language='en', content='') self.biography.save() self.user2 = User.objects.create_user(email='*****@*****.**', password='******', name='Freddie Mercury', family_id=self.family.id) self.user2.save() self.person2 = Person.objects.create(name='Freddie Mercury', gender='M', user_id=self.user2.id, locked=True, email='*****@*****.**', family_id=self.family.id) self.person2.save() self.another_family = Family() self.another_family.save() self.another_user = User.objects.create_user(email='*****@*****.**', password="******", name='Prince Vultan', family_id=self.another_family.id) self.another_user.save() self.confirmed_user = User.objects.create_user(email='*****@*****.**', password='******', name='General Kala', family_id=self.family.id) self.confirmed_user.is_confirmed = True self.confirmed_user.save() self.confirmed_person = Person.objects.create(name='General Kala', gender='F', user_id=self.confirmed_user.id, locked=False, email='*****@*****.**', family_id=self.family.id) self.confirmed_person.save() def test_home_profile_loads(self): ''' Tests that the users home screen loads and uses the correct template ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/profile={0}/'.format(self.person.id)) self.assertEqual(200, response.status_code) self.assertTemplateUsed(response, 'family_tree/profile.html') def test_home_profile_not_visible_for_other_family(self): ''' Test that people in different families cannot see profile ''' self.client.login(email='*****@*****.**', password="******") response = self.client.get('/profile={0}/'.format(self.person.id)) self.assertEqual(404, response.status_code) def test_edit_profile_loads(self): ''' Tests that the edit profile view loads and uses the correct template ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/edit_profile={0}/'.format(self.person.id)) self.assertEqual(200, response.status_code) self.assertTemplateUsed(response, 'family_tree/edit_profile.html') def test_edit_profile_not_visible_for_other_family(self): ''' Test that people in different families cannot see profile ''' self.client.login(email='*****@*****.**', password="******") response = self.client.get('/edit_profile={0}/'.format(self.person.id)) self.assertEqual(404, response.status_code) def test_update_person_denies_get_requests(self): ''' Tests that get requests are not allowed ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/update_person={0}/'.format(self.person2.id)) self.assertEqual(405, response.status_code) self.assertEqual(b"Only POST requests allowed", response.content) def test_update_person_denied_with_invalid_person_id(self): ''' Tests that an invalid response is sent when trying to change a person that does not exist ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post('/update_person=204/', {'pk': 204, 'name': 'name', 'value': 'new name'}) self.assertEqual(404, response.status_code) def test_update_person_denied_with_locked_profile(self): ''' Tests that an invalid response is sent when trying to change a persons profile that is locked ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post('/update_person={0}/'.format(self.person2.id), {'pk': self.person2.id, 'name': 'name', 'value': 'new name'}) self.assertEqual(405, response.status_code) self.assertEqual(b"Access denied to locked profile", response.content) def test_cannot_update_family_id_through_api(self): ''' Test cannot update non-whitelisted properties through api ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post('/update_person={0}/'.format(self.person2.id), {'pk': self.person2.id, 'name': 'family_id', 'value': self.family.id}) self.assertEqual(405, response.status_code) self.assertEqual(b"Access denied to locked profile", response.content) def test_update_person_can_update_name(self): ''' Tests that a field can be updated through api ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post('/update_person={0}/'.format(self.person.id), {'pk': self.person.id, 'name': 'name', 'value': 'Brian Harold May'}) self.assertEqual(200, response.status_code) self.person = Person.objects.get(id=self.person.id) self.assertEqual("Brian Harold May", self.person.name) def test_update_email_saves_to_lowercase(self): ''' Tests that email field can be updated through api and is always saved as lower case ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post('/update_person={0}/'.format(self.person.id), {'pk': self.person.id, 'name': 'email', 'value': '*****@*****.**'}) self.assertEqual(200, response.status_code) self.person = Person.objects.get(id=self.person.id) self.assertEqual("*****@*****.**", self.person.email) def test_another_family_cannot_update_person_name(self): ''' Tests that a field can be updated through api ''' self.client.login(email='*****@*****.**', password="******") response = self.client.post('/update_person={0}/'.format(self.person.id), {'pk': self.person.id, 'name': 'name', 'value': 'Brian Harold May'}) self.assertEqual(404, response.status_code) def test_update_person_another_user_can_update_nonlocked_profile(self): ''' Tests that a person field can be updated through api by a user who is not that person ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post('/update_person={0}/'.format(self.person.id), {'pk': self.person.id, 'name': 'name', 'value': 'John Richard Deacon'}) self.assertEqual(200, response.status_code) self.person = Person.objects.get(id=self.person.id) self.assertEqual("John Richard Deacon", self.person.name) def test_update_person_can_update_boolean(self): ''' Tests that a boolean field can be updated through api ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post('/update_person={0}/'.format(self.person.id), {'pk': self.person.id, 'name': 'locked', 'value': '1'}) self.assertEqual(200, response.status_code) self.person = Person.objects.get(id=self.person.id) self.assertEqual(True, self.person.locked) response = self.client.post('/update_person={0}/'.format(self.person.id), {'pk': self.person.id, 'name': 'locked', 'value': ''}) self.assertEqual(200, response.status_code) self.person = Person.objects.get(id=self.person.id) self.assertEqual(False, self.person.locked) def test_update_person_cannot_update_email_with_confirmed_user(self): ''' Tests that an invalid response is sent when trying to change a persons profile that is attached to a confirmed user ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post('/update_person={0}/'.format(self.confirmed_person.id), {'pk': self.confirmed_person.id, 'name': 'email', 'value': '*****@*****.**'}) self.assertEqual(405, response.status_code) self.assertEqual(b"Access denied to change confirmed user settings", response.content) def test_update_biography_denied_with_invalid_person_id(self): ''' Tests that an invalid response is sent when trying to change a person that does not exist ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post('/update_biography=999/ln=en/', {'biography': 'new content'}) self.assertEqual(404, response.status_code) def test_update_biography_denied_with_locked_profile(self): ''' Tests that an invalid response is sent when trying to change the biography of a locked profile ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post('/update_biography={0}/ln=en/'.format(self.person2.id), {'biography': 'new content'}) self.assertEqual(405, response.status_code) self.assertEqual(b"Access denied to locked profile", response.content) def test_update_biography_can_update_existing_biography(self): ''' Tests that biography content can be updated through api when a biography already exists ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post('/update_biography={0}/ln=en/'.format(self.person.id), {'biography': 'new content'}) self.assertEqual(200, response.status_code) self.biography = Biography.objects.get(person_id=self.person.id, language='en') self.assertEqual('new content', self.biography.content) def test_another_family_cannot_update_biography(self): ''' Tests that a field can be updated through api ''' self.client.login(email='*****@*****.**', password="******") response = self.client.post('/update_biography={0}/ln=en/'.format(self.person.id), {'biography': 'new content'}) self.assertEqual(404, response.status_code) def test_update_biography_can_create_new_biography(self): ''' Tests that biography content can be updated through api when a biography already exists ''' self.client.login(email='*****@*****.**', password='******') response = self.client.post('/update_biography={0}/ln=en/'.format(self.person2.id), {'biography': 'new content'}) self.assertEqual(200, response.status_code) biography = Biography.objects.get(person_id=self.person2.id, language='en') self.assertEqual('new content', biography.content) def test_cannot_delete_profile_of_a_confirmed_user(self): ''' Make sure that you cannot delete the profile of someone who is a user ''' self.client.login(email='*****@*****.**', password='******') response = self.client.get('/delete={0}/'.format(self.person.id)) self.assertEqual(405, response.status_code) #Check it exists person = Person.objects.get(id=self.person.id) self.assertEqual(self.person.id, person.id) def test_cannot_delete_profile(self): ''' Make sure that you cannot delete the profile of someone who is a user ''' person = Person.objects.create(name='David Tennant', gender='M', family_id=self.family.id) person.save() self.client.login(email='*****@*****.**', password='******') response = self.client.get('/delete={0}/'.format(person.id)) self.assertEqual(302, response.status_code) #Check it has been deleted self.assertEqual(0, Person.objects.filter(id = person.id).count())
class ProfileImageApiTestCase(TestCase): def setUp(self): ''' Set up a family, user and profile to test with ''' self.family = Family() self.family.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Fairy Fellar', family_id=self.family.id) self.user.save() self.person = Person.objects.create(name='Fairy Fellar', gender='M', user_id=self.user.id, email='*****@*****.**', family_id=self.family.id) self.person.save() self.another_family = Family() self.another_family.save() self.another_user = User.objects.create_user( email='*****@*****.**', password="******", name='Dale Arden', family_id=self.another_family.id) self.another_user.save() self.image_path = os.path.join(BASE_DIR, 'tests/large_test_image.jpg') def test_image_upload_successful(self): ''' test that we can upload a file ''' client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/profile_image/{0}/'.format(self.person.id) with open(self.image_path, 'rb') as fp: data = { 'picture': fp, 'x': 100, 'y': 200, 'w': 300, 'h': 300, 'r': 90, } response = client.put(url, data) self.assertEqual(200, response.status_code) # Clear up remote images person = Person.objects.get(id=self.person.id) json.loads(response.content) person.remove_local_images() person.remove_remote_images() def test_image_upload_without_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/profile_image/{0}/'.format(self.person.id) with open(self.image_path, 'rb') as fp: data = { 'picture': fp, 'x': 100, 'y': 200, 'w': 300, 'h': 300, 'r': 90, } response = client.put(url, data) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_image_upload_other_family(self): ''' test that we can upload a file ''' client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.another_user) url = '/api/profile_image/{0}/'.format(self.person.id) with open(self.image_path, 'rb') as fp: data = { 'picture': fp, 'x': 100, 'y': 200, 'w': 300, 'h': 300, 'r': 90, } response = client.put(url, data) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) json.loads(response.content) def test_image_upload_invalid_parameter(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/profile_image/{0}/'.format(self.person.id) with open(self.image_path, 'rb') as fp: data = { 'picture': fp, 'x': 'blah', 'y': 200, 'w': 300, 'h': 300, 'r': 90, } response = client.put(url, data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content) def test_image_upload_invalid_picture(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/profile_image/{0}/'.format(self.person.id) image_path = os.path.join(BASE_DIR, 'tests/test_not_a_real_picture.png') with open(image_path, 'rb') as fp: data = { 'picture': fp, 'x': 100, 'y': 200, 'w': 300, 'h': 300, 'r': 90, } response = client.put(url, data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content)
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)
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 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')