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 setUp(self): self.test_author = Author(username="******", email="*****@*****.**", password="******", first_name="testing", last_name="testing", is_active=0) self.test_author.uid = "testserver/author/" + self.test_author.id.hex self.test_author.save() self.retrieve_author_profile_url = reverse( 'retrieve_author_profile', args=[self.test_author.id.hex]) # sample test data: http://service/author/de305d54-75b4-431b-adb2-eb6b9e546013/friends/127.0.0.1:5454/author/ae345d54-75b4-431b-adb2-fb6b9e547891 self.client = Client()
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 setUp(self): self.fixture_authors = [ # Authors Author(username="******", email="*****@*****.**", password="******", first_name="A", last_name="A"), Author(username="******", email="*****@*****.**", password="******", first_name="B", last_name="B"), Author(username="******", email="*****@*****.**", password="******", first_name="C", last_name="C"), # Super User Author(username="******", email="*****@*****.**", password="******", first_name="admin", last_name="."), ] for author in self.fixture_authors: author.is_active = 1 author.save() self.fixture_posts_public = [ Post(title='A post 1 public', content="A1 content", author=self.fixture_authors[0]), Post(title='A post 2 public', content="A2 content", author=self.fixture_authors[0]), Post(title='A post 3 public', content="A3 content", author=self.fixture_authors[0]), Post(title='B post 1 public', content="B1 content", author=self.fixture_authors[1]), Post(title='B post 2 public', content="B2 content", author=self.fixture_authors[1]), Post(title='B post 3 public', content="B3 content", author=self.fixture_authors[1]), Post(title='C post 1 public', content="C1 content", author=self.fixture_authors[2]), Post(title='C post 2 public', content="C2 content", author=self.fixture_authors[2]), Post(title='C post 3 public', content="C3 content", author=self.fixture_authors[2]), ] for post in self.fixture_posts_public: post.size = 0 post.visibility = Post.PUBLIC post.contentType = "text/plain" print(self.fixture_posts_public) for post in self.fixture_posts_public: post.save() self.fixture_posts_other = [ Post(author=self.fixture_authors[0], content="A foaf content", visibility=Post.FOAF, title='A post foaf'), Post(author=self.fixture_authors[0], content="A friend content", visibility=Post.FRIENDS, title='A post friends'), Post(author=self.fixture_authors[0], content="A private content", visibility=Post.PRIVATE, title='A post private'), Post(author=self.fixture_authors[0], content="A server content", visibility=Post.SERVERONLY, title='A post server'), Post(author=self.fixture_authors[1], content="B foaf content", visibility=Post.FOAF, title='B post foaf'), Post(author=self.fixture_authors[1], content="B friend content", visibility=Post.FRIENDS, title='B post friends'), Post(author=self.fixture_authors[1], content="B private content", visibility=Post.PRIVATE, title='B post private'), Post(author=self.fixture_authors[1], content="B server content", visibility=Post.SERVERONLY, title='B post server'), Post(author=self.fixture_authors[2], content="C foaf content", visibility=Post.FOAF, title='C post foaf'), Post(author=self.fixture_authors[2], content="C friend content", visibility=Post.FRIENDS, title='C post friends'), Post(author=self.fixture_authors[2], content="C private content", visibility=Post.PRIVATE, title='C post private'), Post(author=self.fixture_authors[2], content="C server content", visibility=Post.SERVERONLY, title='C post server'), ] for post in self.fixture_posts_other: post.size = 0 post.contentType = 'text/plain' post.save() # Because we are testing the API document, we should NOT use reverse to get the URIs # Instead we should hardcode the URI's such that we ensure we are always spec compliant self.url_public_posts = "/posts" self.url_get_post = lambda post_id: f"/posts/{post_id}" self.url_get_visible_posts = "author/posts" self.client = Client(HTTP_ACCEPT="application/json")
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()