def test_get_biography(self): ''' Tests get_biography ''' person = Person(name='test_get_biography', gender='M', email='*****@*****.**', family_id=self.family.id) person.save() english = Biography(person_id = person.id, language = 'en', content='biography in english') english.save() polish = Biography(person_id = person.id, language = 'pl', content='biography in polish') polish.save() simplified_chinese = Biography(person_id = person.id, language = 'zh-cn', content='biography in simplified chinese') simplified_chinese.save() #Test when requested language passed in biog = Biography.objects.get_biography(person_id = person.id, requested_language = 'pl') self.assertEqual(polish.id, biog.id) #Test when default language passed in and no requested language biog = Biography.objects.get_biography(person_id = person.id, default_language = 'zh-cn') self.assertEqual(simplified_chinese.id, biog.id) #Test when requested language specified but biography does not exist biog = Biography.objects.get_biography(person_id = person.id, requested_language = 'zh-hk') self.assertEqual(None, biog) #Test when default language exists biog = Biography.objects.get_biography(person_id = person.id, default_language = 'zh-cn') self.assertEqual(simplified_chinese.id, biog.id) #Test english returned when default language does not exist biog = Biography.objects.get_biography(person_id = person.id, default_language = 'zh-hk') self.assertEqual(english.id, biog.id)
def test_update_user_when_email_changed(self): ''' Tests that a user is updated when a person's email is modified ''' user = User.objects.create(email='*****@*****.**', password='******', name='John Wong') person = Person(name='John Wong', gender='M', email='*****@*****.**', family_id=self.family.id, language='pl', user_id=user.id) person.save() person.email = '*****@*****.**' person.create_update_user() self.assertEqual( 1, User.objects.filter(email='*****@*****.**').count()) self.assertEqual( 0, User.objects.filter(email='*****@*****.**').count()) self.assertEqual( person.user_id, User.objects.get(email='*****@*****.**').id) self.assertEqual( person.family_id, User.objects.get(email='*****@*****.**').family_id) self.assertEqual( 'pl', User.objects.get(email='*****@*****.**').language)
def test_get_biography_no_biographies_at_all(self): ''' Tests get_biography when no biographies exist at all ''' person = Person(name='test_get_biography_no_biographies_at_all', gender='M', email='*****@*****.**', family_id=self.family.id) person.save() biog = Biography.objects.get_biography(person_id = person.id, requested_language = 'zh-hk', default_language = 'zh-hk') self.assertEqual(None, biog)
def test_get_biography_no_requested_no_default_no_english(self): ''' Tests get_biography when no default, requested or english language exists, function returns whatever it can ''' person = Person(name='no_requested_no_default_no_english', gender='M', email='*****@*****.**', family_id=self.family.id) person.save() polish = Biography(person_id = person.id, language = 'pl', content='biography in polish') polish.save() biog = Biography.objects.get_biography(person_id = person.id, default_language = 'zh-hk') self.assertEqual(polish.id, biog.id)
def test_partnered_other_to_female_resolves_to_female_to_other(self): ''' Tests when an other gender partners a female, it resolves to females partners an other gender ''' other = Person(name="other", gender="O", family_id=self.family.id) other.save() female = Person(name="female", gender="F", family_id=self.family.id) female.save() relation = Relation(from_person_id = other.id, to_person_id = female.id, relation_type = PARTNERED) relation.normalise() self.assertEqual(female.id, relation.from_person_id) self.assertEqual(other.id, relation.to_person_id)
def test_account_locks_out_on_multiple_invalid_login_attempts(self): self.signal_was_called = False def handler(sender, **kwargs): self.signal_was_called = True user_locked_out.connect(handler) user = User.objects.create_user(email='*****@*****.**', password='******', name='Adele Goldberg', family_id = self.family.id) person = Person(name='Adele Goldberg', gender='F', email='*****@*****.**', family_id=self.family.id, language='en', user_id=user.id) person.save() # 127.0.0.1 is whitelisted client = APIClient(HTTP_X_REAL_IP='127.0.0.2') wrong_auth_details = { 'email': '*****@*****.**', 'password': '******' } for x in range(0, 6): client.post('/api/auth/obtain_token/', wrong_auth_details, format='json') correct_auth_details = { 'email': '*****@*****.**', 'password': '******' } final_response = client.post('/api/auth/obtain_token/', correct_auth_details, format='json') self.assertNotEqual(final_response.status_code, status.HTTP_200_OK) self.assertTrue(self.signal_was_called) # Check ip locked locked_response = client.get('/api/auth/is_locked/', format='json') self.assertEqual(b'true', locked_response.content) user_locked_out.disconnect(handler)
def test_hierachy_score_set_for_adding_child_with_relation_as_param(self): ''' Ensure that hierachy score is set when adding the child of a parent the relation is passed to the function ''' parent = Person(name='parent', gender='M', hierarchy_score = 100, family_id=self.family.id) parent.save() child = Person(name='child', gender='M', family_id=self.family.id) child.save() relation = Relation(from_person_id = parent.id, to_person_id = child.id, relation_type = RAISED) child.set_hierarchy_score(relation) self.assertEqual(101, child.hierarchy_score)
def test_partnered_male_to_female_resolves_to_female_to_male(self): ''' Tests when a male partners a female, it resolves to females partners a male ''' male = Person(name="male", gender="M", family_id=self.family.id) male.save() female = Person(name="female", gender="F", family_id=self.family.id) female.save() relation = Relation(from_person_id = male.id, to_person_id = female.id, relation_type = PARTNERED) relation.normalise() self.assertEqual(female.id, relation.from_person_id) self.assertEqual(male.id, relation.to_person_id)
def test_set_profile_image_crop_rotate_resize(self): ''' Tests that the function correctly sets sets the photo field on a person and converts an image. ''' from django.conf import settings path = settings.MEDIA_ROOT + 'profile_photos/large_test_image.jpg' #Copy test image to media area import shutil import os shutil.copy2( os.path.join(settings.BASE_DIR, 'family_tree/tests/large_test_image.jpg'), path) person = Person(name='陳港生', gender='M', family_id=self.family.id) person.save() person.set_profile_image_crop_rotate_resize(path, 10, 20, 200, 200, 90, test=True) #Check small image is valid small_image = Image.open(settings.MEDIA_ROOT + str(person.small_thumbnail)) small_image.verify() width, height = small_image.size self.assertEqual(80, width) self.assertEqual(80, height) #Check large image is valid large_thumbnail = Image.open(settings.MEDIA_ROOT + str(person.large_thumbnail)) large_thumbnail.verify() width, height = large_thumbnail.size self.assertEqual(200, width) self.assertEqual(200, height) #Clear up mess afterwards if os.path.exists(path): os.remove(path) person.remove_local_images() person.remove_remote_images()
def test_partnered_female_to_male_stays_the_same(self): ''' Tests when a female partners a male, the relationship does not change ''' female = Person(name="female", gender="F", family_id=self.family.id) female.save() male = Person(name="male", gender="M", family_id=self.family.id) male.save() relation = Relation(from_person_id = female.id, to_person_id = male.id, relation_type = PARTNERED) relation.normalise() self.assertEqual(female.id, relation.from_person_id) self.assertEqual(male.id, relation.to_person_id)
def test_create_inverted_relation(self): ''' Tests an inverted relationship is correctly created when using manager function ''' from_person = Person(name="from_person", gender="F", family_id=self.family.id) from_person.save() to_person = Person(name="to_person", gender="F", family_id=self.family.id) to_person.save() relation = Relation(from_person_id = from_person.id, to_person_id = to_person.id, relation_type = RAISED) inverted = Relation.objects._create_inverted_relation(relation) self.assertEqual(from_person.id, inverted.to_person_id) self.assertEqual(to_person.id, inverted.from_person_id) self.assertEqual(RAISED_BY, inverted.relation_type)
def test_raised_by_relation_resolves_to_raised(self): ''' Tests when a relation that a child is raised by parent, it resolves to parent raised child ''' parent = Person(name="parent", gender="F", family_id=self.family.id) parent.save() child = Person(name="child", gender="O", family_id=self.family.id) child.save() relation = Relation(from_person_id = child.id, to_person_id = parent.id, relation_type = RAISED_BY) relation.normalise() self.assertEqual(RAISED, relation.relation_type) self.assertEqual(parent.id, relation.from_person_id) self.assertEqual(child.id, relation.to_person_id)
def test_hierachy_score_set_for_adding_partner(self): ''' Ensure that hierachy score is set when adding a partner ''' husband = Person(name='husband', gender='M', family_id=self.family.id) husband.save() wife = Person(name='wife', gender='F', hierarchy_score = 75, family_id=self.family.id) wife.save() relation = Relation(from_person_id = husband.id, to_person_id = wife.id, relation_type = PARTNERED) relation.save() husband.set_hierarchy_score() self.assertEqual(75, husband.hierarchy_score)
def test_existing_relations_get_replaced(self): ''' Tests that when a relations is added between two people, it replaces any existing relations between them ''' existing1 = Person(name="existing1", gender="F", family_id=self.family.id) existing1.save() existing2 = Person(name="existing2", gender="F", family_id=self.family.id) existing2.save() relation = Relation(from_person_id = existing1.id, to_person_id = existing2.id, relation_type = RAISED) relation.save() new_relation = Relation(from_person_id = existing1.id, to_person_id = existing2.id, relation_type = PARTNERED) new_relation.save() self.assertEqual(1, Relation.objects.filter(from_person_id = existing1.id, to_person_id = existing2.id).count()) self.assertEqual(PARTNERED, Relation.objects.get(from_person_id = existing1.id, to_person_id = existing2.id).relation_type)
def test_hierachy_score_set_for_adding_parent(self): ''' Ensure that hierachy score is set when adding the parent of a child the relation is not passed to the function ''' parent = Person(name='parent', gender='M', family_id=self.family.id) parent.save() child = Person(name='child', gender='M', hierarchy_score = 100, family_id=self.family.id) child.save() relation = Relation(from_person_id = parent.id, to_person_id = child.id, relation_type = RAISED) relation.save() parent.set_hierarchy_score() self.assertEqual(99, parent.hierarchy_score)
def test_update_user_when_email_changed(self): ''' Tests that a user is updated when a person's email is modified ''' user = User.objects.create(email='*****@*****.**', password='******', name='John Wong') person = Person(name='John Wong', gender='M', email='*****@*****.**', family_id=self.family.id, language='pl', user_id=user.id) person.save() person.email = '*****@*****.**' person.create_update_user() self.assertEqual(1, User.objects.filter(email='*****@*****.**').count()) self.assertEqual(0, User.objects.filter(email='*****@*****.**').count()) self.assertEqual(person.user_id, User.objects.get(email='*****@*****.**').id) self.assertEqual(person.family_id, User.objects.get(email='*****@*****.**').family_id) self.assertEqual('pl', User.objects.get(email='*****@*****.**').language)
def test_invalid_login_followed_by_valid_logon(self): user = User.objects.create_user(email='*****@*****.**', password='******', name='Margaret Hamilton', family_id=self.family.id) person = Person(name='Margaret Hamilton', gender='F', email='*****@*****.**', family_id=self.family.id, language='en', user_id=user.id) person.save() # 127.0.0.1 is whitelisted client = APIClient(HTTP_X_REAL_IP='127.0.0.2') wrong_auth_details = { 'email': '*****@*****.**', 'password': '******' } response = client.post('/api/auth/obtain_token/', wrong_auth_details, format='json') self.assertNotEqual(response.status_code, status.HTTP_200_OK) # Check ip is not locked locked_response = client.get('/api/auth/is_locked/', format='json') self.assertEqual(b'false', locked_response.content) correct_auth_details = { 'email': '*****@*****.**', 'password': '******' } response = client.post('/api/auth/obtain_token/', correct_auth_details, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) # Check ip is not locked locked_response = client.get('/api/auth/is_locked/', format='json') self.assertEqual(b'false', locked_response.content)
def test_biography_can_be_written_in_non_latic_characters(self): """ Tests that a biography name can be written in non-latin characters """ person = Person(name='nonlatin', gender='M', email='*****@*****.**', family_id=self.family.id) person.save() #Traditional Chinese traditional_chinese = Biography(person_id = person.id, language = 'zh-hk', content='傳記') traditional_chinese.save() #Simplified Chinese simplified_chinese = Biography(person_id = person.id, language = 'zh-cn', content='传记') simplified_chinese.save() #Polish polish = Biography(person_id = person.id, language = 'pl', content='zabójstwo') polish.save()
def test_person_name_can_be_in_non_latic_characters(self): ''' Tests that a users name can be written in non-latin characters ''' #Traditional Chinese person = Person(name='實驗', gender='M', email='*****@*****.**', family_id=self.family.id) person.save() #Simplified Chinese person = Person(name='实验', gender='M', email='*****@*****.**', family_id=self.family.id) person.save() #Polish person = Person(name='kiełbasa', gender='M', email='*****@*****.**', family_id=self.family.id) person.save()
def test_set_profile_image_crop_rotate_resize_replaces_existing(self): ''' Tests that the function correctly sets sets the photo field on a person and converts an image. ''' from django.conf import settings path = settings.MEDIA_ROOT + 'profile_photos/large_test_image1.jpg' path2 = settings.MEDIA_ROOT + 'profile_photos/large_test_image2.jpg' #Copy test image to media area import shutil import os shutil.copy2( os.path.join(settings.BASE_DIR, 'family_tree/tests/large_test_image.jpg'), path) person = Person(name='陳港生', gender='M', family_id=self.family.id) person.set_profile_image_crop_rotate_resize(path, 10, 20, 200, 200, 90) person.save() photo = settings.MEDIA_URL + str(person.photo) shutil.copy2( os.path.join(settings.BASE_DIR, 'family_tree/tests/large_test_image.jpg'), path2) person.set_profile_image_crop_rotate_resize(path2, 10, 20, 200, 200, 90) person.save() # Check original photo has been removed request = requests.get(photo) self.assertNotEqual(200, request.status_code) if os.path.exists(path): os.remove(path) if os.path.exists(path2): os.remove(path2) #Clear up mess afterwards person.remove_local_images() person.remove_remote_images()
def test_biography_can_be_written_in_non_latic_characters(self): """ Tests that a biography name can be written in non-latin characters """ #Traditional Chinese person = Person(name='nonlatin', gender='M', email='*****@*****.**', family_id=self.family.id, biography='傳記') person.save() #Simplified Chinese person.biography = '传记' person.save() #Polish person.biography = 'zabójstwo' person.save()
class ImageApiTestCase(TestCase): ''' Tests for the Image API ''' def setUp(self): self.family = Family() self.family.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Phil Collins', family=self.family) self.person = Person(name='Phil Collins', gender='M', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) 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() #Tag person 1 in image self.tag = Tag.objects.create(image=self.image, person=self.person, x1=1, x2=2, y1=3, y2=4) self.gallery2 = Gallery.objects.create(title="test_gallery2", family_id=self.family.id) self.test_image2 = os.path.join(settings.BASE_DIR, 'gallery/tests/test_image.jpg') self.test_image2_destination = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery2.id), '/test_image.jpg' ]) directory = ''.join([ settings.MEDIA_ROOT, 'galleries/', str(self.family.id), '/', str(self.gallery2.id) ]) if not os.path.exists(directory): os.makedirs(directory) #Copy test image to media area shutil.copy2(self.test_image2, self.test_image2_destination) self.image2 = Image(gallery=self.gallery2, family=self.family, original_image=''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery2.id), '/test_image.jpg' ])) self.image2.save() self.family2 = Family() self.family2.save() self.user2 = User.objects.create_user( email='*****@*****.**', password='******', name='Phillip Bailey', family=self.family2) self.person2 = Person(name='Phillip Bailey', gender='M', email='*****@*****.**', family_id=self.family2.id, language='en', user_id=self.user2.id) self.person2.save() super(ImageApiTestCase, self).setUp() def tearDown(self): self.image.delete_local_image_files() threading.Thread(target=self.image.delete_remote_image_files).start() self.image2.delete_local_image_files() threading.Thread(target=self.image2.delete_remote_image_files).start() # Delete any updates try: self.image = Image.objects.get(id=self.image.id) self.image.delete_local_image_files() threading.Thread( target=self.image.delete_remote_image_files).start() except: pass try: self.image2 = Image.objects.get(id=self.image2.id) self.image2.delete_local_image_files() threading.Thread( target=self.image2.delete_remote_image_files).start() except: pass try: os.remove(self.test_image_destination) except: pass try: os.remove(self.test_image2_destination) except: pass def test_list_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') response = client.get('/api/image/?page=1', format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_list_page1(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/image/?page=1', format='json') # Check it contains both images self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(str(self.image.thumbnail).encode() in response.content) self.assertTrue( str(self.image2.thumbnail).encode() in response.content) json.loads(response.content) def test_list_page1_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') # Login with other user 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/image/?page=1', format='json') # Check it contains neither images self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertFalse( str(self.image.thumbnail).encode() in response.content) self.assertFalse( str(self.image2.thumbnail).encode() in response.content) json.loads(response.content) def test_list_by_gallery(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/image/?gallery_id={0}'.format(self.gallery2.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertFalse( str(self.image.thumbnail).encode() in response.content) self.assertTrue( str(self.image2.thumbnail).encode() in response.content) json.loads(response.content) def test_list_by_tagged_person(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/image/?person_id={0}'.format(self.person.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(str(self.image.thumbnail).encode() in response.content) self.assertFalse( str(self.image2.thumbnail).encode() in response.content) json.loads(response.content) def test_retrieve_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/image/{0}/'.format(self.image.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) os.remove(self.test_image_destination) os.remove(self.test_image2_destination) def test_retrieve(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/image/{0}/'.format(self.image.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(str(self.image.thumbnail).encode() in response.content) json.loads(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/image/{0}/'.format(self.image.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) json.loads(response.content) def test_create(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/image/' with open(self.test_image, 'rb') as fp: data = { 'picture': fp, 'gallery_id': self.gallery.id, } response = client.post(url, data) # Check image loads image_id = json.loads(response.content)['id'] image = Image.objects.get(id=image_id) image.delete_local_image_files() image.delete_remote_image_files() self.assertEqual(200, response.status_code) self.assertEqual('test_image', image.title) self.assertTrue('Phil Collins', image.uploaded_by.name) json.loads(response.content) def test_create_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/image/' with open(self.test_image, 'rb') as fp: data = { 'picture': fp, 'gallery_id': self.gallery.id, } response = client.post(url, data) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_create_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user2) url = '/api/image/' with open(self.test_image, 'rb') as fp: data = { 'picture': fp, 'gallery_id': self.gallery.id, } response = client.post(url, data) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) 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/{0}/'.format(self.image.id) response = client.delete(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) count = Image.objects.filter(id=self.image.id).count() self.assertEqual(0, count) json.loads(response.content) def test_destroy_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/image/{0}/'.format(self.image.id) 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.user2) url = '/api/image/{0}/'.format(self.image.id) response = client.delete(url, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) json.loads(response.content) def test_partial_update(self): self.image.upload_files_to_s3() client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/image/{0}/'.format(self.image.id) data = { 'title': 'new title', 'description': 'new description', 'anticlockwise_angle': 90, 'latitude': 10, 'longitude': 20, } response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(b'new title' in response.content) self.assertTrue(b'new description' in response.content) self.assertTrue(b'10' in response.content) self.assertTrue(b'20' in response.content) json.loads(response.content) def test_partial_update_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/image/{0}/'.format(self.image.id) data = { 'title': 'new title', 'description': 'new description', 'anticlockwise_angle': 90, 'latitude': 10, 'longitude': 20, } response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_partial_update_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user2) url = '/api/image/{0}/'.format(self.image.id) data = { 'title': 'new title', 'description': 'new description', 'anticlockwise_angle': 90, 'latitude': 10, 'longitude': 20, } 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_invalid_title(self): self.image.upload_files_to_s3() client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/image/{0}/'.format(self.image.id) data = { 'title': ' ', 'description': 'new description', 'anticlockwise_angle': 90, 'latitude': 10, 'longitude': 20, } response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content) def test_partial_update_optional_data_missing(self): self.image.upload_files_to_s3() client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/image/{0}/'.format(self.image.id) data = { 'title': 'new title', 'description': 'new description', } response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(b'new title' in response.content) self.assertTrue(b'new description' in response.content) self.assertTrue(b'"longitude":0.0' in response.content) self.assertTrue(b'"latitude":0.0' in response.content) json.loads(response.content)
class InviteEmailApiTestCase(TestCase): ''' Tests for the Invite Email API ''' def setUp(self): self.family = Family() self.family.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Matt Tong', family=self.family) self.person = Person(name='Matt Tong', gender='M', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) self.person.save() self.new_person = Person(name='Taka Hirose', gender='M', email='*****@*****.**', family_id=self.family.id, language='en') self.new_person.save() self.family2 = Family() self.family2.save() self.user2 = User.objects.create_user(email='*****@*****.**', password='******', name='Herman Li', family=self.family2) self.person2 = Person(name='Herman Li', gender='M', email='*****@*****.**', family_id=self.family2.id, language='en', user_id=self.user2.id) self.person2.save() super(InviteEmailApiTestCase, self).setUp() self.invite = EmailConfirmation( email_address='*****@*****.**', person_id=self.person.id, user_who_invited_person_id=self.user.id, sent=timezone.now()) self.invite.save() def test_retrieve_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/invite_email/{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/invite_email/{0}/'.format(self.person.id) response = client.get(url, format='json') invite = json.loads(response.content) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(self.invite.person_id, invite["person_id"]) json.loads(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/invite_email/{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_create(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/invite_email/' data = { 'person_id': self.new_person.id, } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) invite = json.loads(response.content) self.assertEqual(self.new_person.id, invite["person_id"]) def test_create_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/invite_email/' data = { 'person_id': self.new_person.id, } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_create_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user2) url = '/api/invite_email/' data = { 'person_id': self.new_person.id, } 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_person_id(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/invite_email/' data = { 'person_id': '', } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) def test_create_user_already_exists(self): self.new_person.user = self.user self.new_person.save() client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/invite_email/' data = { 'person_id': self.new_person.id, } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content) def test_create_no_email(self): self.new_person.email = '' self.new_person.save() client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/invite_email/' data = { 'person_id': self.new_person.id, } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content) def test_create_invite_exists_same_email(self): EmailConfirmation.objects.create(email_address=self.new_person.email, person_id=self.new_person.id, user_who_invited_person=self.user) client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/invite_email/' data = { 'person_id': self.new_person.id, } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content) def test_create_invite_exists_different_email(self): EmailConfirmation.objects.create(email_address='*****@*****.**', person_id=self.new_person.id, user_who_invited_person=self.user) client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/invite_email/' data = { 'person_id': self.new_person.id, } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) invite = json.loads(response.content) self.assertEqual(self.new_person.id, invite["person_id"]) json.loads(response.content) def test_confirmation_partial_update(self): invite = EmailConfirmation.objects.create( email_address=self.new_person.email, person_id=self.new_person.id, user_who_invited_person=self.user) client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/invite_email_confirmation/{0}/'.format( invite.confirmation_key) data = { 'password': '******', } response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.new_person = Person.objects.get(pk=self.new_person.id) # Check user assigned newUser = User.objects.get(name=self.new_person.name) self.assertIsNotNone(newUser) json.loads(response.content) def test_confirmation_partial_update_block_ip_after_unsuccessful_attempts( self): invite = EmailConfirmation.objects.create( email_address=self.new_person.email, person_id=self.new_person.id, user_who_invited_person=self.user) client = APIClient(HTTP_X_REAL_IP='127.0.0.2') url = '/api/invite_email_confirmation/{0}/'.format('invalid_key') data = { 'password': '******', } for x in range(0, 6): response = client.patch(url, data, format='json') self.assertNotEqual(response.status_code, status.HTTP_200_OK) # Check ip blocked after multiple failed attempts with correct key url = '/api/invite_email_confirmation/{0}/'.format( invite.confirmation_key) response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_confirmation_partial_update_password_too_short(self): invite = EmailConfirmation.objects.create( email_address=self.new_person.email, person_id=self.new_person.id, user_who_invited_person=self.user) client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/invite_email_confirmation/{0}/'.format( invite.confirmation_key) data = { 'password': '******', } response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content) def test_confirmation_partial_update_email_address_mismatch(self): invite = EmailConfirmation.objects.create( email_address='*****@*****.**', person_id=self.new_person.id, user_who_invited_person=self.user) client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/invite_email_confirmation/{0}/'.format( invite.confirmation_key) data = { 'password': '******', } response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content) def test_confirmation_retrieve(self): invite = EmailConfirmation.objects.create( email_address=self.new_person.email, person_id=self.new_person.id, user_who_invited_person=self.user) client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/invite_email_confirmation/{0}/'.format( invite.confirmation_key) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(b'*****@*****.**' in response.content) self.assertTrue(b'Taka Hirose' in response.content) json.loads(response.content) def test_confirmation_retrieve_block_ip_after_unsuccessful_attempt(self): invite = EmailConfirmation.objects.create( email_address=self.new_person.email, person_id=self.new_person.id, user_who_invited_person=self.user) client = APIClient(HTTP_X_REAL_IP='127.0.0.19') url = '/api/invite_email_confirmation/{0}/'.format('not_a_proper_key') for x in range(0, 6): response = client.get(url, format='json') self.assertNotEqual(response.status_code, status.HTTP_200_OK) # Check ip blocked after multiple failed attempts with correct key url = '/api/invite_email_confirmation/{0}/'.format( invite.confirmation_key) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
class JWTAuthTest(TestCase): ''' Tests JWT auth ''' def setUp(self): self.family = Family() self.family.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Grace Hopper', family_id = self.family.id) self.person = Person(name='Grace Hopper', gender='F', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) self.person.save() def test_jwt_auth_and_refresh_token_created_on_correct_auth_details(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') auth_details = { 'email': '*****@*****.**', 'password': '******' } response = client.post('/api/auth/obtain_token/', auth_details, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) access_token = json.loads(response.content)["access"] refresh_token = json.loads(response.content)["refresh"] auth_token = { 'refresh': refresh_token } # Sleep to ensure new token is different time.sleep(1) refresh_response = client.post('/api/auth/refresh_token/', auth_token, format='json') refresh_token = json.loads(refresh_response.content)["access"] self.assertEqual(refresh_response.status_code, status.HTTP_200_OK) self.assertNotEqual(refresh_token, access_token) # Check verify token new_auth_token ={# 'token': refresh_token } verify_new_token_response = client.post('/api/auth/verify_token/', new_auth_token, format='json') self.assertEqual(verify_new_token_response.status_code, status.HTTP_200_OK) # Check ip not locked locked_response = client.get('/api/auth/is_locked/', format='json') self.assertEqual(b'false', locked_response.content) def test_jwt_fails_on_auth_incorrect_password(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') payload = { 'email': '*****@*****.**', 'password': '******' } response = client.post('/api/auth/obtain_token/', payload, format='json') self.assertNotEqual(response.status_code, status.HTTP_200_OK) def test_verify_fails_on_invalid_token(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') invalid_auth_token ={# 'token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImp0aSI6IjM1ODU0ODc3LWQyZjQtNDIxZS04ZDI5LWY3YTgxNTk3NzdhYyIsImlhdCI6MTU1NDM4NzU4NCwiZXhwIjoxNTU0MzkxMTg0fQ.yIr0TMbalatx7alU1TMGIxxaelqquMJfz3m4H7AA9v4' } verify_old_token_response = client.post('/api/auth/verify_token/', invalid_auth_token, format='json') self.assertNotEqual(verify_old_token_response.status_code, status.HTTP_200_OK) def test_account_locks_out_on_multiple_invalid_login_attempts(self): self.signal_was_called = False def handler(sender, **kwargs): self.signal_was_called = True user_locked_out.connect(handler) user = User.objects.create_user(email='*****@*****.**', password='******', name='Adele Goldberg', family_id = self.family.id) person = Person(name='Adele Goldberg', gender='F', email='*****@*****.**', family_id=self.family.id, language='en', user_id=user.id) person.save() # 127.0.0.1 is whitelisted client = APIClient(HTTP_X_REAL_IP='127.0.0.2') wrong_auth_details = { 'email': '*****@*****.**', 'password': '******' } for x in range(0, 6): client.post('/api/auth/obtain_token/', wrong_auth_details, format='json') correct_auth_details = { 'email': '*****@*****.**', 'password': '******' } final_response = client.post('/api/auth/obtain_token/', correct_auth_details, format='json') self.assertNotEqual(final_response.status_code, status.HTTP_200_OK) self.assertTrue(self.signal_was_called) # Check ip locked locked_response = client.get('/api/auth/is_locked/', format='json') self.assertEqual(b'true', locked_response.content) user_locked_out.disconnect(handler) def test_api_docs_loads(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) response = client.get('/api/docs/') self.assertEqual(response.status_code, status.HTTP_200_OK) def test_api_schema_loads(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) response = client.get('/api/schema/') self.assertEqual(response.status_code, status.HTTP_200_OK)
class GalleryApiTestCase(TestCase): ''' Tests for the Gallery API ''' def setUp(self): self.family = Family() self.family.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Phil Collins', family=self.family) self.person = Person(name='Phil Collins', gender='M', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) self.person.save() self.gallery = Gallery.objects.create(title="test_gallery", family_id=self.family.id) self.gallery2 = Gallery.objects.create(title="test_gallery2", family_id=self.family.id) self.family2 = Family() self.family2.save() self.user2 = User.objects.create_user( email='*****@*****.**', password='******', name='Phillip Bailey', family=self.family2) self.person2 = Person(name='Phillip Bailey', gender='M', email='*****@*****.**', family_id=self.family2.id, language='en', user_id=self.user2.id) self.person2.save() 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) def test_list_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') response = client.get('/api/gallery/', format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_list_page1(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/gallery/', format='json') # Check it contains both galleries self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(str(self.gallery.title).encode() in response.content) self.assertTrue(str(self.gallery2.title).encode() in response.content) def test_list_page1_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') # Login with other user 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/gallery/', format='json') # Check it contains neither galleries self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertFalse(str(self.gallery.title).encode() in response.content) self.assertFalse(str(self.gallery2.title).encode() in response.content) def test_retrieve_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/gallery/{0}/'.format(self.gallery.id) 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/gallery/{0}/'.format(self.gallery.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(str(self.gallery.title).encode() 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/gallery/{0}/'.format(self.gallery.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) def test_partial_update(self): 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() image2 = Image(gallery=self.gallery, family=self.family, original_image=''.join([ 'galleries/', str(self.family.id), '/', str(self.gallery.id), '/test_image.jpg' ])) image2.save() client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/gallery/{0}/'.format(self.gallery.id) data = { 'family_id': self.family2.id, # try to switch families 'title': 'new title', 'description': 'new description', 'thumbnail_id': image2.id, } response = client.patch(url, data, format='json') gallery = Gallery.objects.get(id=self.gallery.id) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual('new title', gallery.title) self.assertEqual(self.family.id, gallery.family_id) self.assertTrue(b'new title' in response.content) self.assertTrue(b'new description' in response.content) self.assertTrue( str(image2.thumbnail) in response.content.decode("utf-8")) image.delete_local_image_files() image.delete_remote_image_files() image2.delete_local_image_files() image2.delete_remote_image_files() def test_partial_update_remove_thumbnail(self): 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() client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/gallery/{0}/'.format(self.gallery.id) data = { 'thumbnail_id': '', } response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue('"thumbnail":null' in response.content.decode("utf-8")) def test_partial_update_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/gallery/{0}/'.format(self.gallery.id) data = { 'title': 'new title', 'description': 'new description', 'thumbnail': 'test_image.jpg', } response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_partial_update_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user2) data = { 'title': 'new title', 'description': 'new description', 'thumbnail': 'test_image.jpg', } url = '/api/gallery/{0}/'.format(self.gallery.id) response = client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) def test_create(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/gallery/' data = { 'family_id': self.family2.id, # try to switch families 'title': 'new gallery title', 'description': 'new gallery description', } response = client.post(url, data, format='json') gallery = Gallery.objects.get(title='new gallery title') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual('new gallery title', gallery.title) self.assertEqual(self.family.id, gallery.family_id) self.assertTrue(b'new gallery title' in response.content) def test_create_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/gallery/'.format(self.gallery.id) data = { 'title': 'new title', 'description': 'new description', } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_create_no_title(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/gallery/' data = { 'title': '', 'description': 'new gallery description', } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
class RelationApiTestCase(TestCase): ''' Tests for the relation API ''' def setUp(self): self.family = Family() self.family.save() self.family2 = Family() self.family2.save() self.user = User.objects.create_user(email='*****@*****.**', password='******', name='Edith Clarke', family=self.family) self.person1 = Person(name='Edith Clarke', gender='F', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) self.person1.save() self.person2 = Person(name='someone else', gender='O', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) self.person2.save() self.person3 = Person(name='another person', gender='O', email='*****@*****.**', family_id=self.family.id, language='en', user_id=self.user.id) self.person3.save() self.relation1 = Relation.objects.create(from_person=self.person1, to_person=self.person2, relation_type=PARTNERED) self.relation1.save() self.relation2 = Relation.objects.create(from_person=self.person1, to_person=self.person3, relation_type=RAISED) self.relation2.save() self.otherFamilyUser = User.objects.create_user( email='*****@*****.**', password='******', name='Another Family', family=self.family2) self.otherFamilyPerson = Person(name='another family', gender='O', email='*****@*****.**', family_id=self.family2.id, language='en', user_id=self.user.id) self.otherFamilyPerson.save() self.otherFamilyPerson2 = Person(name='another family 2', gender='O', email='*****@*****.**', family_id=self.family2.id, language='en', user_id=self.user.id) self.otherFamilyPerson2.save() self.relation3 = Relation.objects.create( from_person=self.otherFamilyPerson, to_person=self.otherFamilyPerson2, relation_type=PARTNERED) self.relation3.save() super(RelationApiTestCase, self).setUp() def test_list_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') response = client.get('/api/relation/', 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') client.force_authenticate(user=self.user) response = client.get('/api/relation/', format='json') relations = json.loads(response.content) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(2, len(relations)) self.assertNotEqual(self.relation3.id, relations[0]["id"]) self.assertNotEqual(self.relation3.id, relations[1]["id"]) json.loads(response.content) def test_retrieve_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/relation/{0}/'.format(self.relation1.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/relation/{0}/'.format(self.relation1.id) response = client.get(url, format='json') relation = json.loads(response.content) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(self.relation1.id, relation["id"]) json.loads(response.content) def test_retrieve_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/relation/{0}/'.format(self.relation3.id) response = client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) json.loads(response.content) def test_delete_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/relation/{0}/'.format(self.relation1.id) response = client.delete(url, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_delete_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/relation/{0}/'.format(self.relation3.id) response = client.delete(url, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) json.loads(response.content) def test_delete(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/relation/{0}/'.format(self.relation1.id) response = client.delete(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) count = Relation.objects.filter(pk=self.relation1.id).count() self.assertEqual(0, count) json.loads(response.content) def test_create(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/relation/' data = { 'from_person_id': self.person2.id, 'to_person_id': self.person3.id, 'relation_type': RAISED } response = client.post(url, data, format='json') relation = json.loads(response.content) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertNotEqual(relation, None) json.loads(response.content) def test_create_requires_authentication(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') url = '/api/relation/' data = { 'from_person_id': self.person2.id, 'to_person_id': self.person3.id, 'relation_type': RAISED } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) json.loads(response.content) def test_create_other_family(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.otherFamilyUser) url = '/api/relation/' data = { 'from_person_id': self.person2.id, 'to_person_id': self.person3.id, 'relation_type': RAISED } 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_parameter(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/relation/' data = { 'from_person_id': 'invalid parameter', 'to_person_id': self.person3.id, 'relation_type': RAISED } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content) def test_create_invalid_relation_type(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/relation/' data = { 'from_person_id': 'invalid parameter', 'to_person_id': self.person3.id, 'relation_type': 50000 } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content) def test_create_related_to_self(self): client = APIClient(HTTP_X_REAL_IP='127.0.0.1') client.force_authenticate(user=self.user) url = '/api/relation/' data = { 'from_person_id': self.person3.id, 'to_person_id': self.person3.id, 'relation_type': RAISED } response = client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) json.loads(response.content)