def test_delete(self): author = Author(username="******", email="*****@*****.**", password="******", first_name="test", last_name="test", is_active=1) author.uid = "testserver/author/" + str(author.id) author.save() self.client = Client(HTTP_ACCEPT="application/json") self.client.force_login(author) profile = reverse("update_author_profile", args=[author.id]) response = self.client.post(profile, { 'first_name': 'test', 'last_name': 'test', 'email': '*****@*****.**', 'bio': '', 'github': '', 'display_name': '', 'delete': True }, content_type="application/json") self.assertEquals(response.status_code, 200) self.assertEquals( Author.objects.filter(username='******').exists(), False)
def test_view_friend_candidate(self): # create two test authors author = Author(username="******", email="*****@*****.**", password="******", first_name="testing", last_name="testing", is_active=1) author.uid = "testserver/author/" + str(author.id) author.save() author2 = Author(username="******", email="*****@*****.**", password="******", first_name="testing", last_name="testing", is_active=1) author2.uid = "testserver/author/" + str(author2.id) author2.save() view_friend_candidate_url = reverse( 'view_list_of_available_authors_to_befriend', args=[author.id]) response = self.client.get(view_friend_candidate_url) json_response = response.json() self.assertTrue(response.status_code == 200) self.assertTrue( json_response["available_authors_to_befriend"][0] == author2.uid) # friend author with author2 new_friend1 = Friend(author_id=author.uid, friend_id=author2.uid) new_friend1.save() response = self.client.get(view_friend_candidate_url) json_response = response.json() self.assertTrue(response.status_code == 200) self.assertTrue( len(json_response["available_authors_to_befriend"]) == 0) # clean up test data Friend.objects.filter(author_id=author.uid).filter( friend_id=author2.uid).delete() Author.objects.filter(username="******").delete() Author.objects.filter(username="******").delete()
def test_fail_invalid_author_id_posts_visible_to_auth_user(self): author1 = Author(username="******", email="*****@*****.**", password="******", first_name="test", last_name="test", host="testserver", is_active=1) author1.uid = "testserver/author/" + str(author1.id) author1.save() self.client = Client(HTTP_ACCEPT="application/json") self.client.force_login(author1) view_post_of_author = reverse( 'retrieve_posts_of_author_id_visible_to_current_auth_user', args=[1]) response = self.client.get(view_post_of_author) self.assertEqual(404, response.status_code)
def test_author_id_posts_visible_to_auth_user(self): author1 = Author(username="******", email="*****@*****.**", password="******", first_name="test", last_name="test", host="testserver", is_active=1) author1.uid = "testserver/author/" + str(author1.id) author1.save() author2 = Author(username="******", email="*****@*****.**", password="******", first_name="post", last_name="post", host="testserver", is_active=1) author2.uid = "testserver/author/" + str(author2.id) author2.save() author3 = Author(username="******", email="post_friend@post_friend.com", password="******", first_name="post_friend", last_name="post_friend", host="testserver", is_active=1) author3.uid = "testserver/author/" + str(author3.id) author3.save() friendship = [ # author1 and author2 are friends Friend(author_id=author1.uid, friend_id=author2.uid), Friend(author_id=author2.uid, friend_id=author1.uid), # author2 and author3 are friends Friend(author_id=author2.uid, friend_id=author3.uid), Friend(author_id=author3.uid, friend_id=author2.uid), ] for friends in friendship: friends.save() # posts created by author2 author2_friends = Post(author=author2, content="friends post created by author2", visibility=Post.FRIENDS, title='friends post') author2_friends.size = 1 author2_friends.contentType = 'text/plain' author2_friends.save() author2_public = Post(author=author2, content="public post created by author2", visibility=Post.PUBLIC, title='public post') author2_public.size = 1 author2_public.contentType = 'text/plain' author2_public.save() author2_private = Post( author=author2, content="private post created by author2 not visible to test", visibility=Post.PRIVATE, title='private post') author2_private.visibleTo.set([author1.uid]) author2_private.size = 1 author2_private.contentType = 'text/plain' author2_private.save() # post created by author3 author3_foaf = Post(author=author3, content="fofa post created by author3", visibility=Post.FOAF, title='foaf post') author3_foaf.size = 1 author3_foaf.contentType = 'text/plain' author3_foaf.save() author3_private = Post(author=author3, content="private post created by author3", visibility=Post.PRIVATE, title='privage post') author3_private.size = 1 author3_private.contentType = 'text/plain' author3_private.save() self.client = Client(HTTP_ACCEPT="application/json") self.client.force_login(author1) viewable_post_for_author3 = [author3_foaf] viewable_post_for_author2 = [ author2_private, author2_public, author2_friends ] post_id_author3 = [] post_id_author2 = [] for post in viewable_post_for_author3: post_id_author3.append(str(post.id)) for post in viewable_post_for_author2: post_id_author2.append(str(post.id)) view_post_of_author2 = reverse( 'retrieve_posts_of_author_id_visible_to_current_auth_user', args=[author2.id]) view_post_of_author3 = reverse( 'retrieve_posts_of_author_id_visible_to_current_auth_user', args=[author3.id]) response = self.client.get(view_post_of_author2) self.assertEqual(200, response.status_code) # getting json json_data = response.json() self.assertEquals(json_data['query'], 'posts') self.assertEquals(json_data['count'], len(viewable_post_for_author2)) self.assertEquals(json_data['size'], 10) returned_post_id = [] for each_post in json_data['posts']: returned_post_id.append(each_post['id']) self.assertEqual(post_id_author2, returned_post_id) response = self.client.get(view_post_of_author3) self.assertEqual(200, response.status_code) # getting json json_data = response.json() self.assertEquals(json_data['query'], 'posts') self.assertEquals(json_data['count'], len(viewable_post_for_author3)) self.assertEquals(json_data['size'], 10) returned_post_id = [] for each_post in json_data['posts']: returned_post_id.append(each_post['id']) self.assertEqual(post_id_author3, returned_post_id) Post.objects.filter(author=author1).delete() Post.objects.filter(author=author2).delete() Post.objects.filter(author=author3).delete() Author.objects.filter(username="******").delete() Author.objects.filter(username="******").delete() Author.objects.filter(username="******").delete()